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.

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