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.

137 lines
3.1 KiB

  1. //@doc
  2. /******************************************************
  3. **
  4. ** @module CRITSEC.H | Header file for CriticalSection class
  5. **
  6. ** Description:
  7. ** Critical Section - Encapsulation of CriticalSection object
  8. **
  9. ** History:
  10. ** Created 03/02/98 Matthew L. Coill (mlc)
  11. **
  12. ** (c) 1986-1998 Microsoft Corporation. All Rights Reserved.
  13. ******************************************************/
  14. #ifndef __CRITSEC_H__
  15. #define __CRITSEC_H__
  16. #include <winbase.h>
  17. #include <winuser.h>
  18. #include <crtdbg.h>
  19. // Assumption macros (I don't like asserts msg boxes)
  20. #ifdef _DEBUG
  21. inline void myassume(BOOL condition, const char* fname, int line)
  22. {
  23. if (!condition) {
  24. char buff[256];
  25. ::wsprintf(buff, "SW_WHEEL.DLL: Assumption Failed in %s on line %d\r\n", fname, line);
  26. _RPT0(_CRT_WARN, buff);
  27. }
  28. }
  29. #define ASSUME(x) myassume(x, __FILE__, __LINE__);
  30. #define ASSUME_NOT_NULL(x) myassume(x != NULL, __FILE__, __LINE__);
  31. #define ASSUME_NOT_REACHED() myassume(FALSE, __FILE__, __LINE__);
  32. #else !_DEBUG
  33. #define ASSUME(x)
  34. #define ASSUME_NOT_NULL(x)
  35. #define ASSUME_NOT_REACHED()
  36. #endif _DEBUG
  37. //
  38. // @class CriticalSection class
  39. //
  40. class CriticalSection
  41. {
  42. public:
  43. CriticalSection() : m_EntryDepth(0)
  44. {
  45. __try
  46. {
  47. ::InitializeCriticalSection(&m_OSCriticalSection);
  48. m_Initialized = TRUE;
  49. }
  50. __except (EXCEPTION_EXECUTE_HANDLER)
  51. {
  52. m_Initialized = FALSE;
  53. }
  54. }
  55. ~CriticalSection() {
  56. ASSUME(m_EntryDepth == 0);
  57. ::DeleteCriticalSection(&m_OSCriticalSection);
  58. }
  59. bool IsInitialized() const
  60. {
  61. if (m_Initialized == TRUE)
  62. {
  63. return true;
  64. }
  65. return false;
  66. }
  67. bool Enter() {
  68. if (m_Initialized == FALSE)
  69. {
  70. return false;
  71. }
  72. m_EntryDepth++;
  73. ::EnterCriticalSection(&m_OSCriticalSection);
  74. return true;
  75. }
  76. bool Leave() {
  77. if (m_Initialized == FALSE)
  78. {
  79. return false;
  80. }
  81. ASSUME(m_EntryDepth > 0);
  82. m_EntryDepth--;
  83. ::LeaveCriticalSection(&m_OSCriticalSection);
  84. return true;
  85. }
  86. /* -- Windows NT Only
  87. BOOL TryEntry() {
  88. if (::TryEnterCriticalSection(&m_OSCriticalSection) != 0) {
  89. m_EntryDepth++;
  90. return TRUE;
  91. }
  92. return FALSE;
  93. }
  94. BOOL WaitEntry(short timeOut, BOOL doSleep) {
  95. // right now timeout is just a loop (since it is not being used anyways)
  96. while(1) {
  97. if (TryEntry()) { return TRUE; }
  98. if (--timeOut > 0) {
  99. if (doSleep) { ::Sleep(0); }
  100. } else {
  101. return FALSE;
  102. }
  103. }
  104. }
  105. -- Windows NT Only */
  106. private:
  107. CriticalSection& operator=(CriticalSection& rhs); // Cannot be copied
  108. CRITICAL_SECTION m_OSCriticalSection;
  109. short m_EntryDepth;
  110. short m_Initialized;
  111. };
  112. extern CriticalSection g_CriticalSection;
  113. //
  114. // @class CriticalLock class
  115. //
  116. // Critical lock is usefor functions with multiple-exit points. Create a stack CriticalLock
  117. // -- object and everything is taken care of for you when it's lifetime ends.
  118. class CriticalLock
  119. {
  120. public:
  121. CriticalLock() { g_CriticalSection.Enter(); }
  122. ~CriticalLock() { g_CriticalSection.Leave(); }
  123. };
  124. #endif __CRITSEC_H__