thread.h

Go to the documentation of this file.
00001 /* $Id: thread.h 26349 2014-02-16 21:37:05Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #ifndef THREAD_H
00013 #define THREAD_H
00014 
00016 typedef void (*OTTDThreadFunc)(void *);
00017 
00019 class OTTDThreadExitSignal { };
00020 
00024 class ThreadObject {
00025 public:
00029   virtual ~ThreadObject() {};
00030 
00034   virtual bool Exit() = 0;
00035 
00039   virtual void Join() = 0;
00040 
00049   static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL);
00050 };
00051 
00055 class ThreadMutex {
00056 public:
00060   static ThreadMutex *New();
00061 
00065   virtual ~ThreadMutex() {};
00066 
00073   virtual void BeginCritical(bool allow_recursive = false) = 0;
00074 
00081   virtual void EndCritical(bool allow_recursive = false) = 0;
00082 
00089   virtual void WaitForSignal() = 0;
00090 
00094   virtual void SendSignal() = 0;
00095 };
00096 
00100 class ThreadMutexLocker {
00101 public:
00106   ThreadMutexLocker(ThreadMutex *mutex) : mutex(mutex) { mutex->BeginCritical(); }
00107 
00111   ~ThreadMutexLocker() { this->mutex->EndCritical(); }
00112 
00113 private:
00114   ThreadMutexLocker(const ThreadMutexLocker &) { NOT_REACHED(); }
00115   ThreadMutexLocker &operator=(const ThreadMutexLocker &) { NOT_REACHED(); return *this; }
00116   ThreadMutex *mutex;
00117 };
00118 
00123 uint GetCPUCoreCount();
00124 
00125 #endif /* THREAD_H */