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.

145 lines
3.0 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1995 - 1999
  3. Module Name:
  4. Locks.hxx
  5. Abstract:
  6. Several small class (CInterlockedInteger, CMutexLock and CSharedLock
  7. which are wrappers for Win32 APIs.
  8. CInterlockedInteger is a simple wrapper for win32 interlockedm integer APis.
  9. CMutexLock is a wrapper designed to automatically constructed around
  10. win32 critical sections. They never forget to release the lock.
  11. CSharedLocks are similar to NT resources, they allow shared (multiple readers)
  12. and exclusive (single writer) access to the resource. They are different
  13. in the following ways:
  14. CSharedLocks don't starve exclusive threads.
  15. Exclusive threads spin (Sleep(0)) while waiting for readers to finish.
  16. Exclusive threads can gain shared access iff the shared access is properly
  17. contained in the exclusive lock. While performing recursive locking the thread
  18. must hold an exlusive lock.
  19. Exclusive threads can recursively take the lock.
  20. Exclusive threads always block if they can't get access.
  21. Author:
  22. Mario Goertzel [MarioGo]
  23. Revision History:
  24. MarioGo 03-14-95 Moved from misc.?xx
  25. --*/
  26. #ifndef __LOCKS_HXX
  27. #define __LOCKS_HXX
  28. class CInterlockedInteger
  29. {
  30. private:
  31. LONG i;
  32. public:
  33. CInterlockedInteger(LONG i = 0) : i(i) {}
  34. LONG operator++(int)
  35. {
  36. return(InterlockedIncrement(&i));
  37. }
  38. LONG operator--(int)
  39. {
  40. return(InterlockedDecrement(&i));
  41. }
  42. operator LONG()
  43. {
  44. return(i);
  45. }
  46. };
  47. class CMutexLock
  48. {
  49. private:
  50. CRITICAL_SECTION *pCurrentLock;
  51. int owned;
  52. public:
  53. CMutexLock(CRITICAL_SECTION *pLock) : owned(0), pCurrentLock(pLock) {
  54. Lock();
  55. }
  56. ~CMutexLock() {
  57. if (owned)
  58. Unlock();
  59. #if DBG
  60. pCurrentLock = 0;
  61. #endif
  62. }
  63. void Lock()
  64. {
  65. ASSERT(!owned);
  66. EnterCriticalSection(pCurrentLock);
  67. owned = 1;
  68. }
  69. void Unlock()
  70. {
  71. ASSERT(owned);
  72. LeaveCriticalSection(pCurrentLock);
  73. owned = 0;
  74. }
  75. };
  76. class CSharedLock
  77. {
  78. private:
  79. CRITICAL_SECTION lock;
  80. HANDLE hevent;
  81. CInterlockedInteger readers;
  82. LONG writers;
  83. DWORD exclusive_owner;
  84. unsigned recursion_count;
  85. public:
  86. CSharedLock(RPC_STATUS &status);
  87. ~CSharedLock();
  88. void LockShared(void);
  89. void UnlockShared(void);
  90. void LockExclusive(void);
  91. void UnlockExclusive(void);
  92. void Unlock(void);
  93. void ConvertToExclusive(void);
  94. BOOL HeldExclusive()
  95. {
  96. return(exclusive_owner == GetCurrentThreadId());
  97. }
  98. BOOL NotHeldExclusiveByAnyone()
  99. {
  100. return(exclusive_owner == 0);
  101. }
  102. };
  103. #endif // __LOCKS_HXX