| Line | |
|---|
| 1 | /*! |
|---|
| 2 | * @file threading.h |
|---|
| 3 | * @brief Definition of Thread Classes. |
|---|
| 4 | * |
|---|
| 5 | * These are mainly Classes, that are used for wrapping around SDL_thread |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #ifndef _THREADING_H |
|---|
| 9 | #define _THREADING_H |
|---|
| 10 | #include <string> |
|---|
| 11 | #ifdef HAVE_SDL_H |
|---|
| 12 | #include <SDL_thread.h> |
|---|
| 13 | #else |
|---|
| 14 | #include <SDL/SDL_thread.h> |
|---|
| 15 | #endif |
|---|
| 16 | |
|---|
| 17 | namespace OrxThread |
|---|
| 18 | { |
|---|
| 19 | //! A class for Wrapping Threads |
|---|
| 20 | class Thread |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | public: |
|---|
| 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); }; |
|---|
| 30 | |
|---|
| 31 | void start(); |
|---|
| 32 | void terminate(); |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | private: |
|---|
| 36 | SDL_Thread* thread; |
|---|
| 37 | |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | class Mutex |
|---|
| 41 | { |
|---|
| 42 | public: |
|---|
| 43 | Mutex() { this->mutex = SDL_CreateMutex(); }; |
|---|
| 44 | ~Mutex() { SDL_DestroyMutex(this->mutex); } |
|---|
| 45 | |
|---|
| 46 | void lock() { SDL_mutexP(mutex); }; |
|---|
| 47 | void unlock() { SDL_mutexV(mutex); }; |
|---|
| 48 | |
|---|
| 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 | |
|---|
| 66 | #endif /* _THREADING_H */ |
|---|
Note: See
TracBrowser
for help on using the repository browser.