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.

182 lines
4.7 KiB

  1. //******************************************************************************
  2. //
  3. // ESSUTILS.H
  4. //
  5. // Copyright (C) 1996-1999 Microsoft Corporation
  6. //
  7. //******************************************************************************
  8. #ifndef __ESS_UTILS__H_
  9. #define __ESS_UTILS__H_
  10. #include <postpone.h>
  11. #include <wbemutil.h>
  12. struct CEssThreadObject
  13. {
  14. BOOL m_bReferencedExternally;
  15. IWbemContext* m_pContext;
  16. CPostponedList m_PostponedList;
  17. CPostponedList m_PostponedEventList;
  18. static IWbemContext* mstatic_pSpecialContext;
  19. protected:
  20. INTERNAL IWbemContext* GetSpecialContext();
  21. public:
  22. CEssThreadObject(IWbemContext* pContext);
  23. ~CEssThreadObject();
  24. BOOL IsReferencedExternally() { return m_bReferencedExternally; }
  25. void SetReferencedExternally();
  26. void static ClearSpecialContext();
  27. };
  28. INTERNAL IWbemContext* GetCurrentEssContext();
  29. INTERNAL CEssThreadObject* GetCurrentEssThreadObject();
  30. void SetCurrentEssThreadObject(IWbemContext* pContext);
  31. void SetConstructedEssThreadObject(CEssThreadObject* pObject);
  32. void ClearCurrentEssThreadObject();
  33. INTERNAL CPostponedList* GetCurrentPostponedList();
  34. INTERNAL CPostponedList* GetCurrentPostponedEventList();
  35. #define WBEM_REG_ESS WBEM_REG_WBEM L"\\ESS"
  36. /****************************************************************************
  37. CEssSharedLock
  38. *****************************************************************************/
  39. class CEssSharedLock
  40. {
  41. int m_nMode;
  42. int m_cWaitingExclusive;
  43. CRITICAL_SECTION m_cs;
  44. HANDLE m_hOkShared;
  45. HANDLE m_hOkExclusive;
  46. public:
  47. CEssSharedLock() : m_nMode(0), m_cWaitingExclusive(0),
  48. m_hOkShared(NULL), m_hOkExclusive(NULL) {}
  49. BOOL Initialize()
  50. {
  51. _DBG_ASSERT( m_hOkShared == NULL && m_hOkExclusive == NULL );
  52. m_hOkShared = CreateEvent( NULL, TRUE, FALSE, NULL );
  53. if ( m_hOkShared != NULL )
  54. {
  55. m_hOkExclusive = CreateEvent( NULL, FALSE, FALSE, NULL );
  56. if ( m_hOkExclusive != NULL )
  57. {
  58. try
  59. {
  60. InitializeCriticalSection( &m_cs );
  61. }
  62. catch(...)
  63. {
  64. return FALSE;
  65. }
  66. }
  67. }
  68. return m_hOkExclusive != NULL;
  69. }
  70. ~CEssSharedLock()
  71. {
  72. if ( m_hOkShared != NULL )
  73. {
  74. CloseHandle( m_hOkShared );
  75. if ( m_hOkExclusive != NULL )
  76. {
  77. CloseHandle( m_hOkExclusive );
  78. DeleteCriticalSection( &m_cs );
  79. }
  80. }
  81. }
  82. void EnterShared()
  83. {
  84. _DBG_ASSERT( m_hOkShared != NULL );
  85. EnterCriticalSection( &m_cs );
  86. while( m_nMode < 0 || m_cWaitingExclusive > 0 )
  87. {
  88. LeaveCriticalSection( &m_cs );
  89. WaitForSingleObject( m_hOkShared, INFINITE );
  90. EnterCriticalSection( &m_cs );
  91. }
  92. m_nMode++;
  93. ResetEvent( m_hOkExclusive );
  94. LeaveCriticalSection( &m_cs );
  95. }
  96. void EnterExclusive()
  97. {
  98. _DBG_ASSERT( m_hOkExclusive != NULL );
  99. EnterCriticalSection( &m_cs );
  100. while( m_nMode != 0 )
  101. {
  102. m_cWaitingExclusive++;
  103. LeaveCriticalSection( &m_cs );
  104. WaitForSingleObject( m_hOkExclusive, INFINITE );
  105. EnterCriticalSection( &m_cs );
  106. m_cWaitingExclusive--;
  107. }
  108. m_nMode = -1;
  109. ResetEvent( m_hOkShared );
  110. LeaveCriticalSection( &m_cs );
  111. }
  112. void Leave()
  113. {
  114. _DBG_ASSERT( m_nMode != 0 );
  115. BOOL bSignalExclusive = FALSE;
  116. BOOL bSignalShared = FALSE;
  117. EnterCriticalSection( &m_cs );
  118. if ( m_nMode > 0 )
  119. {
  120. if ( --m_nMode == 0 && m_cWaitingExclusive > 0 )
  121. bSignalExclusive = TRUE;
  122. }
  123. else
  124. {
  125. _DBG_ASSERT( m_nMode == -1 );
  126. if ( m_cWaitingExclusive > 0 )
  127. bSignalExclusive = TRUE;
  128. else
  129. bSignalShared = TRUE;
  130. m_nMode = 0;
  131. }
  132. LeaveCriticalSection( &m_cs );
  133. if ( bSignalExclusive )
  134. SetEvent( m_hOkExclusive );
  135. else if ( bSignalShared )
  136. SetEvent( m_hOkShared );
  137. }
  138. };
  139. class CInEssSharedLock
  140. {
  141. CEssSharedLock* m_pLock;
  142. public:
  143. CInEssSharedLock( CEssSharedLock* pLock, BOOL bExclusive )
  144. : m_pLock(pLock)
  145. {
  146. if ( !bExclusive )
  147. pLock->EnterShared();
  148. else
  149. pLock->EnterExclusive();
  150. }
  151. ~CInEssSharedLock()
  152. {
  153. m_pLock->Leave();
  154. }
  155. };
  156. #endif