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.

225 lines
4.6 KiB

  1. //=================================================================
  2. //
  3. // ResourceManager.h
  4. //
  5. // Copyright (c) 1999-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #ifndef __RESOURCEMANAGER_H__
  9. #define __RESOURCEMANAGER_H__
  10. #include <windows.h>
  11. #include <list>
  12. #include <BrodCast.h>
  13. #include <fwcommon.h>
  14. #include <assertbreak.h>
  15. #include <ccriticalsec.h>
  16. #include <helper.h>
  17. #define DUPLICATE_RELEASE FALSE
  18. class CResourceManager ;
  19. class CResourceList ;
  20. class CResource ;
  21. class CRule ;
  22. class CBinaryRule ;
  23. class CAndRule ;
  24. class COrRule ;
  25. typedef CResource* (*PFN_RESOURCE_INSTANCE_CREATOR) ( PVOID pData ) ;
  26. class CResourceList
  27. {
  28. friend class CResourceManager ;
  29. protected:
  30. CResource* GetResource ( LPVOID pData ) ;
  31. ULONG ReleaseResource ( CResource* pResource ) ;
  32. void ShutDown () ;
  33. protected:
  34. typedef std::list<CResource*> tagInstances ;
  35. tagInstances m_Instances ;
  36. PFN_RESOURCE_INSTANCE_CREATOR m_pfnInstanceCreator ;
  37. GUID guidResourceId ;
  38. CCriticalSec m_csList ;
  39. public:
  40. CResourceList () ;
  41. ~CResourceList () ;
  42. void RemoveFromList ( CResource* pResource ) ;
  43. BOOL LockList(){ m_csList.Enter(); return TRUE; };
  44. BOOL UnLockList(){ m_csList.Leave(); return TRUE; };
  45. public:
  46. BOOL m_bShutDown ;
  47. } ;
  48. class CResourceListAutoLock
  49. {
  50. CResourceList* resourcelist ;
  51. BOOL bLocked ;
  52. public:
  53. CResourceListAutoLock ( CResourceList* list ) :
  54. resourcelist ( list ),
  55. bLocked ( FALSE )
  56. {
  57. if ( resourcelist )
  58. {
  59. bLocked = resourcelist->LockList () ;
  60. }
  61. }
  62. ~CResourceListAutoLock ()
  63. {
  64. if ( bLocked )
  65. {
  66. resourcelist->UnLockList () ;
  67. }
  68. }
  69. };
  70. class CResourceManager
  71. {
  72. protected:
  73. std::list < CResourceList* > m_Resources ;
  74. CStaticCritSec m_csResourceManager ;
  75. public:
  76. CResourceManager () ;
  77. ~CResourceManager () ;
  78. CResource* GetResource ( GUID ResourceId, LPVOID pData ) ;
  79. ULONG ReleaseResource ( GUID ResourceId, CResource* pResource ) ;
  80. BOOL AddInstanceCreator ( GUID ResourceId, PFN_RESOURCE_INSTANCE_CREATOR pfnResourceInstanceCreator ) ;
  81. void CResourceManager :: ForcibleCleanUp () ;
  82. static CResourceManager sm_TheResourceManager ;
  83. };
  84. typedef OnDeleteObj2< GUID, CResource* ,
  85. CResourceManager,
  86. ULONG (CResourceManager:: *)(GUID, CResource*),
  87. &CResourceManager::ReleaseResource > CRelResource;
  88. class CResource
  89. {
  90. protected:
  91. CRule *m_pRules ;
  92. CResourceList *m_pResources ; //pointer to container
  93. LONG m_lRef ;
  94. protected:
  95. virtual BOOL OnAcquire () { return TRUE ; } ;
  96. virtual BOOL OnRelease () { return TRUE ; } ;
  97. virtual BOOL OnInitialAcquire () { return TRUE ; } ;
  98. virtual BOOL OnFinalRelease() { return TRUE ; } ;
  99. BOOL m_bValid;
  100. DWORD m_dwCreationError;
  101. public:
  102. CResource () ;
  103. virtual ~CResource () ;
  104. virtual void RuleEvaluated ( const CRule *a_RuleEvaluated ) = 0;
  105. BOOL Acquire () ;
  106. ULONG Release () ;
  107. void SetParent ( CResourceList *pList ) { m_pResources = pList ; }
  108. virtual BOOL IsOneOfMe ( LPVOID pData ) { return TRUE ; }
  109. virtual BOOL IsValid ( ) { return m_bValid; }
  110. virtual DWORD GetCreationError ( ) { return m_dwCreationError; }
  111. };
  112. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  113. // Rules
  114. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  115. class CRule
  116. {
  117. protected:
  118. CResource* m_pResource ;
  119. LONG m_lRef ;
  120. public:
  121. CRule ( CResource* pResource ) : m_pResource ( pResource ), m_lRef ( 0 ) {}
  122. virtual ~CRule () {} ;
  123. virtual ULONG AddRef ()
  124. {
  125. return ( InterlockedIncrement ( &m_lRef ) ) ;
  126. }
  127. virtual ULONG Release ()
  128. {
  129. LONG lCount ;
  130. lCount = InterlockedDecrement ( &m_lRef );
  131. try
  132. {
  133. if (IsVerboseLoggingEnabled())
  134. {
  135. LogMessage2(L"CRule::Release, count is (approx) %d", m_lRef);
  136. }
  137. }
  138. catch ( ... )
  139. {
  140. }
  141. if ( lCount == 0 )
  142. {
  143. try
  144. {
  145. LogMessage(L"CRule Ref Count = 0");
  146. }
  147. catch ( ... )
  148. {
  149. }
  150. delete this;
  151. }
  152. else if (lCount > 0x80000000)
  153. {
  154. ASSERT_BREAK(DUPLICATE_RELEASE);
  155. LogErrorMessage(L"Duplicate CRule Release()");
  156. }
  157. return lCount ;
  158. }
  159. virtual void Detach ()
  160. {
  161. if ( m_pResource )
  162. {
  163. m_pResource = NULL ;
  164. }
  165. }
  166. //Checkrule returns true if rule is satisfied
  167. virtual BOOL CheckRule () { return FALSE ; }
  168. } ;
  169. #endif //__RESOURCEMANAGER_H__