00001 00002 #ifndef __GSEMAPHORE_H 00003 #define __GSEMAPHORE_H 00004 00006 class LgiClass GSemaphore 00007 { 00008 OsThreadId _Thread; 00009 OsSemaphore _Sem; 00010 const char *File; 00011 int Line; 00012 #ifdef _DEBUG 00013 bool _DebugSem; 00014 #endif 00015 00016 bool _Lock(); 00017 void _Unlock(); 00018 char *_Name; 00019 00020 protected: 00021 int _Count; 00022 00023 public: 00025 GSemaphore 00026 ( 00028 const char *name = 0 00029 ); 00030 virtual ~GSemaphore(); 00031 00035 bool Lock 00036 ( 00038 const char *file, 00040 int line 00041 ); 00043 bool LockWithTimeout 00044 ( 00046 int Timeout, 00048 const char *file, 00050 int line 00051 ); 00053 void Unlock(); 00054 00055 char *GetName(); 00056 void SetName(const char *s); 00057 00058 #ifdef _DEBUG 00059 void SetDebug(bool b = true) { _DebugSem = b; } 00060 int GetCount() { return _Count; } 00061 #endif 00062 00063 class Auto 00064 { 00065 GSemaphore *Sem; 00066 bool Locked; 00067 00068 public: 00069 Auto(GSemaphore *s, const char *file, int line) 00070 { 00071 LgiAssert(s); 00072 Locked = (Sem = s) ? Sem->Lock(file, line) : 0; 00073 LgiAssert(Locked); 00074 } 00075 00076 Auto(GSemaphore *s, int timeout, const char *file, int line) 00077 { 00078 LgiAssert(s); 00079 Locked = (Sem = s) ? Sem->LockWithTimeout(timeout, file, line) : 0; 00080 } 00081 00082 ~Auto() 00083 { 00084 if (Locked) Sem->Unlock(); 00085 } 00086 00087 bool GetLocked() { return Locked; } 00088 }; 00089 }; 00090 00091 #endif