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 |
---|
[9140] | 16 | #include <string> |
---|
[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(); |
---|
[7737] | 33 | private: |
---|
[7847] | 34 | SDL_Thread* thread; |
---|
[3245] | 35 | |
---|
[7737] | 36 | }; |
---|
[1853] | 37 | |
---|
[7737] | 38 | class Mutex |
---|
| 39 | { |
---|
| 40 | public: |
---|
| 41 | Mutex() { this->mutex = SDL_CreateMutex(); }; |
---|
| 42 | ~Mutex() { SDL_DestroyMutex(this->mutex); } |
---|
[7331] | 43 | |
---|
[7737] | 44 | void lock() { SDL_mutexP(mutex); }; |
---|
| 45 | void unlock() { SDL_mutexV(mutex); }; |
---|
[7331] | 46 | |
---|
[7737] | 47 | SDL_mutex* getMutex() const { return this->mutex; }; |
---|
| 48 | private: |
---|
| 49 | SDL_mutex* mutex; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | //! A Class that locks a Mutex within its scope |
---|
| 53 | class MutexLock |
---|
| 54 | { |
---|
| 55 | public: |
---|
| 56 | //! Locks the Mutex mutex in this Scope. |
---|
| 57 | MutexLock(Mutex* mutex) { SDL_mutexP(mutex->getMutex()); this->mutex = mutex; }; |
---|
| 58 | ~MutexLock() { SDL_mutexV(mutex->getMutex()); }; |
---|
| 59 | private: |
---|
| 60 | Mutex* mutex; //!< The Mutex to lock. |
---|
| 61 | }; |
---|
| 62 | } |
---|
| 63 | |
---|
[7329] | 64 | #endif /* _THREADING_H */ |
---|
Note: See
TracBrowser
for help on using the repository browser.