Rev | Line | |
---|
[4838] | 1 | /*! |
---|
[7329] | 2 | * @file threading.h |
---|
| 3 | * @brief Definition of Thread Classes. |
---|
| 4 | * |
---|
| 5 | * These are mainly Classes, that are used for wrapping around SDL_thread |
---|
[3245] | 6 | */ |
---|
[1853] | 7 | |
---|
[7329] | 8 | #ifndef _THREADING_H |
---|
| 9 | #define _THREADING_H |
---|
[1853] | 10 | |
---|
[7329] | 11 | #ifdef HAVE_SDL_H |
---|
| 12 | #include <SDL_thread.h> |
---|
| 13 | #else |
---|
| 14 | #include <SDL/SDL_thread.h> |
---|
| 15 | #endif |
---|
[1853] | 16 | |
---|
[7737] | 17 | namespace OrxThread |
---|
[7331] | 18 | { |
---|
[7737] | 19 | //! A class for Wrapping Threads |
---|
| 20 | class Thread |
---|
| 21 | { |
---|
[3543] | 22 | |
---|
[7737] | 23 | public: |
---|
[7847] | 24 | Thread(int (*fn)(void *), void *data) { this->thread = SDL_CreateThread(fn, data); }; |
---|
| 25 | virtual ~Thread() { SDL_KillThread(this->thread); } |
---|
| 26 | void exit ( int returnCode = 0 ); |
---|
| 27 | bool isFinished () const; |
---|
| 28 | bool isRunning () const; |
---|
| 29 | void wait() { SDL_WaitThread(this->thread, NULL); }; |
---|
[1853] | 30 | |
---|
[7847] | 31 | void start(); |
---|
| 32 | void terminate(); |
---|
[3245] | 33 | |
---|
[7847] | 34 | |
---|
[7737] | 35 | private: |
---|
[7847] | 36 | SDL_Thread* thread; |
---|
[3245] | 37 | |
---|
[7737] | 38 | }; |
---|
[1853] | 39 | |
---|
[7737] | 40 | class Mutex |
---|
| 41 | { |
---|
| 42 | public: |
---|
| 43 | Mutex() { this->mutex = SDL_CreateMutex(); }; |
---|
| 44 | ~Mutex() { SDL_DestroyMutex(this->mutex); } |
---|
[7331] | 45 | |
---|
[7737] | 46 | void lock() { SDL_mutexP(mutex); }; |
---|
| 47 | void unlock() { SDL_mutexV(mutex); }; |
---|
[7331] | 48 | |
---|
[7737] | 49 | SDL_mutex* getMutex() const { return this->mutex; }; |
---|
| 50 | private: |
---|
| 51 | SDL_mutex* mutex; |
---|
| 52 | }; |
---|
| 53 | |
---|
| 54 | //! A Class that locks a Mutex within its scope |
---|
| 55 | class MutexLock |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | //! Locks the Mutex mutex in this Scope. |
---|
| 59 | MutexLock(Mutex* mutex) { SDL_mutexP(mutex->getMutex()); this->mutex = mutex; }; |
---|
| 60 | ~MutexLock() { SDL_mutexV(mutex->getMutex()); }; |
---|
| 61 | private: |
---|
| 62 | Mutex* mutex; //!< The Mutex to lock. |
---|
| 63 | }; |
---|
| 64 | } |
---|
| 65 | |
---|
[7329] | 66 | #endif /* _THREADING_H */ |
---|
Note: See
TracBrowser
for help on using the repository browser.