Eneboo - Documentación para desarrolladores
|
00001 /* -*- C++ -*- */ 00002 00003 #ifndef _RECURSIVELOCK_H_ 00004 #define _RECURSIVELOCK_H_ 00005 00012 namespace HL { 00013 00014 template <class BaseLock> 00015 class RecursiveLockType : public BaseLock { 00016 public: 00017 00018 inline RecursiveLockType (void); 00019 00020 inline void lock (void); 00021 inline void unlock (void); 00022 00023 private: 00024 int tid; 00025 int count; 00026 }; 00027 00028 00029 }; 00030 00031 template <class BaseLock> 00032 HL::RecursiveLockType<BaseLock>::RecursiveLockType (void) 00033 : tid (-1), 00034 count (0) 00035 {} 00036 00037 template <class BaseLock> 00038 void HL::RecursiveLockType<BaseLock>::lock (void) { 00039 int currthread = GetCurrentThreadId(); 00040 if (tid == currthread) { 00041 count++; 00042 } else { 00043 BaseLock::lock(); 00044 tid = currthread; 00045 count++; 00046 } 00047 } 00048 00049 template <class BaseLock> 00050 void HL::RecursiveLockType<BaseLock>::unlock (void) { 00051 int currthread = GetCurrentThreadId(); 00052 if (tid == currthread) { 00053 count--; 00054 if (count == 0) { 00055 tid = -1; 00056 BaseLock::unlock(); 00057 } 00058 } else { 00059 // We tried to unlock it but we didn't lock it! 00060 // This should never happen. 00061 assert (0); 00062 abort(); 00063 } 00064 } 00065 00066 #endif