Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

  1. #ifndef _CLOCK_HXX_
  2. #define _CLOCK_HXX_
  3. //+--------------------------------------------------------------------------
  4. //
  5. // Class: CLock
  6. //
  7. // Purpose: Auto-unlocking critical-section services
  8. //
  9. // Interface: Lock - locks the critical section
  10. // Unlock - unlocks the critical section
  11. // Constructor - locks the critical section (unless told
  12. // otherwise)
  13. // Detructor - unlocks the critical section if its locked
  14. //
  15. // Notes: This provides a convenient way to ensure that you're
  16. // unlocking a LOCK, which is useful if your routine
  17. // can be left via several returns and/or via exceptions....
  18. //
  19. //---------------------------------------------------------------------------
  20. class CLock {
  21. public:
  22. CLock (LOCK* val) : m_pSem(val), m_locked(TRUE) { m_pSem->Lock(); }
  23. CLock (LOCK& val) : m_pSem(&val), m_locked(TRUE) { m_pSem->Lock(); }
  24. CLock (LOCK* val, BOOL fLockMe) : m_pSem(val), m_locked(fLockMe) { if (fLockMe) m_pSem->Lock(); }
  25. ~CLock () { if (m_locked) m_pSem->Unlock(); }
  26. void UnLock () { if (m_locked) { m_pSem->Unlock(); m_locked = FALSE; }}
  27. void Lock () { if (!m_locked) { m_pSem->Lock(); m_locked = TRUE; }}
  28. private:
  29. BOOL m_locked;
  30. LOCK* m_pSem;
  31. };
  32. #endif // !_CLOCK_HXX_