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.

142 lines
3.4 KiB

  1. /*****************************************************************************
  2. * Copyright (c) 1998-2001 Microsoft Corporation, All Rights Reserved
  3. *
  4. * All Rights Reserved
  5. *
  6. * This software is furnished under a license and may be used and copied
  7. * only in accordance with the terms of such license and with the inclusion
  8. * of the above copyright notice. This software or any other copies thereof
  9. * may not be provided or otherwise made available to any other person. No
  10. * title to and ownership of the software is hereby transferred.
  11. *****************************************************************************/
  12. //============================================================================
  13. //
  14. // CCriticalSec.h -- Critical Section Wrapper
  15. //
  16. // Copyright (c) 1998-2001 Microsoft Corporation, All Rights Reserved
  17. //
  18. // Revisions: 6/26/98 a-kevhu Created
  19. //
  20. //============================================================================
  21. #ifndef __CCriticalSec_H__
  22. #define __CCriticalSec_H__
  23. #include <windows.h>
  24. #include <process.h>
  25. #ifndef STATUS_POSSIBLE_DEADLOCK
  26. #define STATUS_POSSIBLE_DEADLOCK (0xC0000194L)
  27. #endif /*STATUS_POSSIBLE_DEADLOCK */
  28. DWORD BreakOnDbgAndRenterLoop(void);
  29. class CCriticalSec : public CRITICAL_SECTION
  30. {
  31. public:
  32. CCriticalSec()
  33. {
  34. #ifdef _WIN32_WINNT
  35. #if _WIN32_WINNT > 0x0400
  36. bool initialized = (InitializeCriticalSectionAndSpinCount(this,0))?true:false;
  37. if (!initialized) throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  38. #else
  39. bool initialized = false;
  40. __try
  41. {
  42. InitializeCriticalSection(this);
  43. initialized = true;
  44. }
  45. __except(GetExceptionCode() == STATUS_NO_MEMORY)
  46. {
  47. }
  48. if (!initialized) throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  49. #endif
  50. #else
  51. bool initialized = false;
  52. __try
  53. {
  54. InitializeCriticalSection(this);
  55. initialized = true;
  56. }
  57. __except(GetExceptionCode() == STATUS_NO_MEMORY)
  58. {
  59. }
  60. if (!initialized) throw CHeap_Exception(CHeap_Exception::E_ALLOCATION_ERROR);
  61. #endif
  62. }
  63. ~CCriticalSec()
  64. {
  65. DeleteCriticalSection(this);
  66. }
  67. void Enter()
  68. {
  69. __try {
  70. EnterCriticalSection(this);
  71. } __except((STATUS_POSSIBLE_DEADLOCK == GetExceptionCode())? BreakOnDbgAndRenterLoop():EXCEPTION_CONTINUE_SEARCH) {
  72. }
  73. }
  74. void Leave()
  75. {
  76. LeaveCriticalSection(this);
  77. }
  78. };
  79. class CInCritSec
  80. {
  81. protected:
  82. CRITICAL_SECTION* m_pcs;
  83. public:
  84. CInCritSec(CRITICAL_SECTION* pcs) : m_pcs(pcs)
  85. {
  86. __try {
  87. EnterCriticalSection(m_pcs);
  88. } __except((STATUS_POSSIBLE_DEADLOCK == GetExceptionCode())? BreakOnDbgAndRenterLoop():EXCEPTION_CONTINUE_SEARCH) {
  89. }
  90. }
  91. inline ~CInCritSec()
  92. {
  93. LeaveCriticalSection(m_pcs);
  94. }
  95. };
  96. class CStaticCritSec : public CRITICAL_SECTION
  97. {
  98. private:
  99. bool initialized_;
  100. static BOOL anyFailed_;
  101. public:
  102. static BOOL anyFailure();
  103. static void SetFailure();
  104. CStaticCritSec();
  105. ~CStaticCritSec();
  106. void Enter();
  107. void Leave();
  108. };
  109. #endif