00001 00007 #ifndef THREADS_H 00008 #define THREADS_H 00009 00010 00011 #include "debug.h" 00012 00013 00014 namespace audiere { 00015 00016 typedef void (*AI_ThreadRoutine)(void* opaque); 00017 00018 struct AI_CriticalSectionStruct; 00019 typedef AI_CriticalSectionStruct* AI_CriticalSection; 00020 00021 00022 // threads 00023 bool AI_CreateThread(AI_ThreadRoutine routine, void* opaque, int priority = 0); 00024 00025 // waiting 00026 void AI_Sleep(unsigned milliseconds); 00027 00028 // critical section 00029 AI_CriticalSection AI_CreateCriticalSection(); 00030 void AI_DestroyCriticalSection(AI_CriticalSection cs); 00031 void AI_EnterCriticalSection(AI_CriticalSection cs); 00032 void AI_LeaveCriticalSection(AI_CriticalSection cs); 00033 00034 00035 class Synchronized { 00036 public: 00037 Synchronized() { 00038 m_cs = AI_CreateCriticalSection(); 00039 // if it fails... uh oh 00040 } 00041 00042 ~Synchronized() { 00043 AI_DestroyCriticalSection(m_cs); 00044 } 00045 00046 void lock() { 00047 00048 //ADR_LOG("--> Trying to lock"); 00049 AI_EnterCriticalSection(m_cs); 00050 //ADR_LOG("-->:: LOCKED ::"); 00051 } 00052 00053 void unlock() { 00054 //ADR_LOG("<-- Unlocking..."); 00055 AI_LeaveCriticalSection(m_cs); 00056 } 00057 00058 private: 00059 AI_CriticalSection m_cs; 00060 }; 00061 00062 00063 class Lock { 00064 public: 00065 Lock(Synchronized* object) 00066 : m_object(object) { 00067 object->lock(); 00068 } 00069 00070 ~Lock() { 00071 m_object->unlock(); 00072 } 00073 00074 private: 00075 Synchronized* m_object; 00076 }; 00077 00078 00079 #define SYNCHRONIZED(on) Lock lock_obj__(on) 00080 00081 } 00082 00083 #endif
1.2.17