| 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 |   { | 
|---|
 | 22 |   public: | 
|---|
| [7847] | 23 |     Thread(int (*fn)(void *), void *data) { this->thread = SDL_CreateThread(fn, data); }; | 
|---|
 | 24 |     virtual ~Thread() { SDL_KillThread(this->thread); } | 
|---|
 | 25 |     void exit ( int returnCode = 0 ); | 
|---|
 | 26 |     bool isFinished () const; | 
|---|
 | 27 |     bool isRunning () const; | 
|---|
 | 28 |     void wait() { SDL_WaitThread(this->thread, NULL); }; | 
|---|
| [1853] | 29 |  | 
|---|
| [7847] | 30 |     void start(); | 
|---|
 | 31 |     void terminate(); | 
|---|
| [3245] | 32 |  | 
|---|
| [7847] | 33 |  | 
|---|
| [7737] | 34 |   private: | 
|---|
| [7847] | 35 |     SDL_Thread* thread; | 
|---|
| [3245] | 36 |  | 
|---|
| [7737] | 37 |   }; | 
|---|
| [1853] | 38 |  | 
|---|
| [7737] | 39 |   class Mutex | 
|---|
 | 40 |   { | 
|---|
 | 41 |   public: | 
|---|
 | 42 |     Mutex() {  this->mutex = SDL_CreateMutex(); }; | 
|---|
 | 43 |     ~Mutex() { SDL_DestroyMutex(this->mutex); } | 
|---|
| [7331] | 44 |  | 
|---|
| [7737] | 45 |     void lock() { SDL_mutexP(mutex); }; | 
|---|
 | 46 |     void unlock() { SDL_mutexV(mutex); }; | 
|---|
| [7331] | 47 |  | 
|---|
| [7737] | 48 |     SDL_mutex* getMutex() const { return this->mutex; }; | 
|---|
 | 49 |   private: | 
|---|
 | 50 |     SDL_mutex* mutex; | 
|---|
 | 51 |   }; | 
|---|
 | 52 |  | 
|---|
 | 53 |   //! A Class that locks a Mutex within its scope | 
|---|
 | 54 |   class MutexLock | 
|---|
 | 55 |   { | 
|---|
 | 56 |   public: | 
|---|
 | 57 |     //! Locks the Mutex mutex in this Scope. | 
|---|
 | 58 |     MutexLock(Mutex* mutex) { SDL_mutexP(mutex->getMutex()); this->mutex = mutex; }; | 
|---|
 | 59 |     ~MutexLock() { SDL_mutexV(mutex->getMutex()); }; | 
|---|
 | 60 |   private: | 
|---|
 | 61 |     Mutex* mutex;         //!< The Mutex to lock. | 
|---|
 | 62 |   }; | 
|---|
 | 63 | } | 
|---|
 | 64 |  | 
|---|
| [7329] | 65 | #endif /* _THREADING_H */ | 
|---|
       
      
      Note: See 
TracBrowser
        for help on using the repository browser.