recursivelock.h

来自「hoard内存管理器」· C头文件 代码 · 共 67 行

H
67
字号
/* -*- C++ -*- */#ifndef _RECURSIVELOCK_H_#define _RECURSIVELOCK_H_/** * @class RecursiveLockType * @brief Implements a recursive lock using some base lock representation. * @param BaseLock The base lock representation. */namespace HL {template <class BaseLock>class RecursiveLockType : public BaseLock {public:  inline RecursiveLockType (void);  inline void lock (void);  inline void unlock (void);private:  int tid;	/// The lock owner's thread id. -1 if unlocked.  int count;	/// The recursion depth of the lock.};};template <class BaseLock>HL::RecursiveLockType<BaseLock>::RecursiveLockType (void)  : tid (-1),    count (0){}template <class BaseLock>void HL::RecursiveLockType<BaseLock>::lock (void) {  int currthread = GetCurrentThreadId();  if (tid == currthread) {    count++;  } else {    BaseLock::lock();    tid = currthread;    count++;  }}template <class BaseLock>void HL::RecursiveLockType<BaseLock>::unlock (void) {  int currthread = GetCurrentThreadId();  if (tid == currthread) {    count--;    if (count == 0) {      tid = -1;      BaseLock::unlock();    }  } else {    // We tried to unlock it but we didn't lock it!    // This should never happen.    assert (0);    abort();  }}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?