1 /** 2 * @file 3 * 4 * Internal platform-specific threads 5 */ 6 7 #ifndef THREADS_H 8 #define THREADS_H 9 10 11 #include "debug.h" 12 13 14 namespace audiere { 15 16 typedef void (*AI_ThreadRoutine)(void* opaque); 17 18 // threads 19 bool AI_CreateThread(AI_ThreadRoutine routine, void* opaque, int priority = 0); 20 21 // waiting 22 void AI_Sleep(unsigned milliseconds); 23 24 25 class Mutex { 26 public: 27 Mutex(); 28 ~Mutex(); 29 30 void lock(); 31 void unlock(); 32 33 private: 34 struct Impl; 35 Impl* m_impl; 36 37 friend class CondVar; 38 }; 39 40 41 class CondVar { 42 public: 43 CondVar(); 44 ~CondVar(); 45 46 void wait(Mutex& mutex, float seconds); 47 void notify(); 48 49 private: 50 struct Impl; 51 Impl* m_impl; 52 }; 53 54 55 class ScopedLock { 56 public: ScopedLock(Mutex & mutex)57 ScopedLock(Mutex& mutex): m_mutex(mutex) { 58 m_mutex.lock(); 59 } 60 ScopedLock(Mutex * mutex)61 ScopedLock(Mutex* mutex): m_mutex(*mutex) { 62 m_mutex.lock(); 63 } 64 ~ScopedLock()65 ~ScopedLock() { 66 m_mutex.unlock(); 67 } 68 69 private: 70 Mutex& m_mutex; 71 }; 72 73 74 #define SYNCHRONIZED(on) ScopedLock lock_obj__(on) 75 76 } 77 78 #endif 79