OpenTTD
thread.h
Go to the documentation of this file.
1 /* $Id: thread.h 26349 2014-02-16 21:37:05Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef THREAD_H
13 #define THREAD_H
14 
16 typedef void (*OTTDThreadFunc)(void *);
17 
20 
24 class ThreadObject {
25 public:
29  virtual ~ThreadObject() {};
30 
34  virtual bool Exit() = 0;
35 
39  virtual void Join() = 0;
40 
49  static bool New(OTTDThreadFunc proc, void *param, ThreadObject **thread = NULL);
50 };
51 
55 class ThreadMutex {
56 public:
60  static ThreadMutex *New();
61 
65  virtual ~ThreadMutex() {};
66 
73  virtual void BeginCritical(bool allow_recursive = false) = 0;
74 
81  virtual void EndCritical(bool allow_recursive = false) = 0;
82 
89  virtual void WaitForSignal() = 0;
90 
94  virtual void SendSignal() = 0;
95 };
96 
101 public:
106  ThreadMutexLocker(ThreadMutex *mutex) : mutex(mutex) { mutex->BeginCritical(); }
107 
111  ~ThreadMutexLocker() { this->mutex->EndCritical(); }
112 
113 private:
114  ThreadMutexLocker(const ThreadMutexLocker &) { NOT_REACHED(); }
115  ThreadMutexLocker &operator=(const ThreadMutexLocker &) { NOT_REACHED(); return *this; }
116  ThreadMutex *mutex;
117 };
118 
123 uint GetCPUCoreCount();
124 
125 #endif /* THREAD_H */