Source code of Windows XP (NT5)
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.

299 lines
5.1 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1994 - 1998
  3. All rights reserved.
  4. Module Name:
  5. notify.hxx
  6. Abstract:
  7. Holds notify definitions
  8. Author:
  9. Albert Ting (AlbertT) 13-Dec-1994
  10. Revision History:
  11. Lazar Ivanov (LazarI) Nov-20-2000, major redesign
  12. --*/
  13. #ifndef _NOTIFY_HXX
  14. #define _NOTIFY_HXX
  15. class MNotifyWork;
  16. /********************************************************************
  17. TNotify
  18. Generally a singleton class that handles all registration of
  19. MNotifyWork.
  20. ********************************************************************/
  21. class TNotify: public MRefCom
  22. {
  23. friend MNotifyWork;
  24. SIGNATURE( 'ntfy' )
  25. SAFE_NEW
  26. public:
  27. TNotify(
  28. VOID
  29. );
  30. VOID
  31. vDelete(
  32. VOID
  33. );
  34. BOOL
  35. bValid(
  36. VOID
  37. ) const
  38. {
  39. return _shEventProcessed && VALID_BASE( MRefCom );
  40. }
  41. STATUS
  42. sRegister(
  43. MNotifyWork* pNotifyWork
  44. );
  45. STATUS
  46. sUnregister(
  47. MNotifyWork* pNotifyWork
  48. );
  49. BOOL
  50. bSuspendCallbacks(
  51. VOID
  52. );
  53. VOID
  54. vResumeCallbacks(
  55. VOID
  56. );
  57. VAR(CCSLock, csResumeSuspend);
  58. private:
  59. enum EOPERATION
  60. {
  61. kEopRegister = 1,
  62. kEopUnregister = 2,
  63. kEopModify = 3,
  64. kEopSuspendRequest = 4,
  65. kEopResumeRequest = 5,
  66. kEopExitRequest = 6,
  67. };
  68. enum _CONSTANTS
  69. {
  70. //
  71. // Minimum interval at which objects get signaled.
  72. //
  73. kSleepTime = 1000,
  74. };
  75. /********************************************************************
  76. The TWait class handles waiting on the group of objects.
  77. Since there are a maximum of 32 wait handles per thread
  78. (in WaitForMultipleObjects), we must create multiple threads.
  79. Each TWait handles one thread.
  80. ********************************************************************/
  81. class TWait
  82. {
  83. public:
  84. enum _CONSTANTS
  85. {
  86. kNotifyWorkMax = MAXIMUM_WAIT_OBJECTS-1
  87. };
  88. TWait(
  89. TNotify* pNotify
  90. );
  91. ~TWait(
  92. VOID
  93. );
  94. //
  95. // Accessed only by vRun thread. If we are modifying,
  96. // then we are in the critical section.
  97. //
  98. DLINK( TWait, Wait );
  99. VAR( TRefLock<TNotify>, pNotify );
  100. COUNT _cNotifyWork;
  101. HANDLE _ahNotifyArea[kNotifyWorkMax + 1];
  102. MNotifyWork* _apNotifyWork[kNotifyWorkMax];
  103. CAutoHandleNT _shThread;
  104. PHANDLE
  105. phNotifys(
  106. VOID
  107. )
  108. {
  109. return &_ahNotifyArea[1];
  110. }
  111. BOOL
  112. bValid(
  113. VOID
  114. ) const
  115. {
  116. return _ahNotifyArea[0] ? TRUE : FALSE;
  117. }
  118. BOOL
  119. bFull(
  120. VOID
  121. ) const
  122. {
  123. return _cNotifyWork == kNotifyWorkMax;
  124. }
  125. VOID
  126. vProcessOperation(
  127. VOID
  128. );
  129. static
  130. VOID
  131. vRun(
  132. TWait* pWait
  133. );
  134. };
  135. struct CS_GUARD
  136. {
  137. //
  138. // protected by _CritSec, and _shEventProcessed
  139. //
  140. MNotifyWork* _pNotifyWork;
  141. EOPERATION _eOperation;
  142. DLINK_BASE( TWait, Wait, Wait );
  143. BOOL _bSuspended;
  144. } CSGuard;
  145. DWORD _dwSleepTime;
  146. CAutoHandleNT _shEventProcessed;
  147. CCSLock _CritSec;
  148. //
  149. // Defined as private since clients must use vDelete().
  150. //
  151. ~TNotify(
  152. VOID
  153. );
  154. VOID
  155. vSendRequest(
  156. TWait* pWait
  157. );
  158. VOID
  159. vExecuteOperation(
  160. MNotifyWork* pNotifyWork,
  161. TWait* pWait,
  162. EOPERATION eOp
  163. );
  164. STATUS
  165. sFindOrCreateWaitObject(
  166. TWait **ppWait
  167. );
  168. //
  169. // Virtual definition for MRefCom.
  170. //
  171. VOID
  172. vRefZeroed(
  173. VOID
  174. );
  175. friend TNotify::TWait;
  176. };
  177. /********************************************************************
  178. MNotifyWork
  179. This represents the work object that has a handle and needs
  180. to be notified when the handle is signaled.
  181. Clients should inherit from this class and override
  182. hEvent() - returns event to be watched
  183. vProcessNotifyWork() - quickly processes and resets event
  184. ********************************************************************/
  185. class MNotifyWork
  186. {
  187. friend TNotify;
  188. friend TNotify::TWait;
  189. SIGNATURE( 'nfwk' )
  190. ALWAYS_VALID
  191. SAFE_NEW
  192. public:
  193. MNotifyWork(
  194. VOID
  195. ) : _pWait( NULL )
  196. {}
  197. virtual
  198. ~MNotifyWork(
  199. VOID
  200. );
  201. private:
  202. //
  203. // Indicates which TWait owns it. If NULL, not owned.
  204. //
  205. TNotify::TWait* _pWait;
  206. //
  207. // Retrieves the event handle associated with this object.
  208. // This handle will be waited on and when it is signaled,
  209. // vProcessNotifyWork will be called.
  210. //
  211. virtual
  212. HANDLE
  213. hEvent(
  214. VOID
  215. ) const = 0;
  216. //
  217. // Called when hEvent() is signaled. This routine must
  218. // return quickly.
  219. //
  220. virtual
  221. VOID
  222. vProcessNotifyWork(
  223. TNotify* pNotify
  224. ) = 0;
  225. };
  226. #endif // ndef _NOTIFY_HXX