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.

383 lines
7.1 KiB

  1. //***************************************************************************
  2. //
  3. // PROVTHRD.H
  4. //
  5. // Module: OLE MS PROVIDER FRAMEWORK
  6. //
  7. // Copyright (c) 1996-2001 Microsoft Corporation, All Rights Reserved
  8. //
  9. //***************************************************************************
  10. #ifndef __PROVTHREAD_PROVTHRD_H__
  11. #define __PROVTHREAD_PROVTHRD_H__
  12. #include <Allocator.h>
  13. #include <Queue.h>
  14. #include <BasicTree.h>
  15. extern WmiAllocator g_Allocator ;
  16. class ProvThreadObject ;
  17. class ProvAbstractTaskObject ;
  18. class ProvTaskObject ;
  19. typedef WmiBasicTree <HANDLE,ProvEventObject *> ScheduleReapContainer ;
  20. typedef WmiBasicTree <HANDLE,ProvEventObject *> :: Iterator ScheduleReapContainerIterator ;
  21. typedef WmiBasicTree <DWORD,ProvThreadObject *> ThreadContainer ;
  22. typedef WmiBasicTree <DWORD,ProvThreadObject *> :: Iterator ThreadContainerIterator ;
  23. typedef WmiQueue <ProvAbstractTaskObject *,8> TaskQueue ;
  24. class ProvThreadObject : private ProvEventObject
  25. {
  26. friend ProvAbstractTaskObject ;
  27. friend BOOL APIENTRY DllMain (
  28. HINSTANCE hInstance,
  29. ULONG ulReason ,
  30. LPVOID pvReserved
  31. ) ;
  32. public:
  33. static LONG s_ReferenceCount ;
  34. private:
  35. // Mutual exclusion mechanism
  36. static CStaticCriticalSection s_Lock ;
  37. ScheduleReapContainer m_ScheduleReapContainer ;
  38. // Thread Name
  39. TCHAR *m_ThreadName ;
  40. // Terminate thread event
  41. ProvEventObject m_ThreadTerminateEvent ;
  42. // TaskObject created if a PostSignalThreadShutdown is called
  43. ProvAbstractTaskObject *m_pShutdownTask ;
  44. // Thread Initialization Event
  45. HANDLE m_ThreadInitialization ;
  46. // thread information
  47. ULONG m_ThreadId ;
  48. HANDLE m_ThreadHandle ;
  49. DWORD m_timeout;
  50. // list of task objects associated with thread object
  51. TaskQueue m_TaskQueue ;
  52. // Evict thread from process
  53. void TerminateThread () ;
  54. // Attach thread to global list of threads
  55. BOOL RegisterThread () ;
  56. // Remove thread from global list of threads
  57. BOOL RemoveThread () ;
  58. private:
  59. // global list of thread objects keyed on thread identifier
  60. static ThreadContainer s_ThreadContainer ;
  61. HANDLE *m_EventContainer ;
  62. ULONG m_EventContainerLength ;
  63. HANDLE *GetEventHandles () ;
  64. ULONG GetEventHandlesSize () ;
  65. void ConstructEventContainer () ;
  66. void Process () ;
  67. BOOL Wait () ;
  68. ProvAbstractTaskObject *GetTaskObject ( HANDLE &eventHandle ) ;
  69. BOOL WaitDispatch ( ULONG t_HandleIndex , BOOL &a_Terminated ) ;
  70. private:
  71. // Thread entry point
  72. static void _cdecl ThreadExecutionProcedure ( void *threadParameter ) ;
  73. // Attach Process
  74. static void ProcessAttach () ;
  75. // Detach Process
  76. static void ProcessDetach ( BOOL a_ProcessDetaching = FALSE ) ;
  77. HANDLE *GetThreadHandleReference () { return &m_ThreadHandle ; }
  78. protected:
  79. public:
  80. ProvThreadObject ( const TCHAR *a_ThreadName = NULL, DWORD a_timeout = INFINITE ) ;
  81. void BeginThread();
  82. virtual ~ProvThreadObject () ;
  83. BOOL WaitForStartup () ;
  84. void SignalThreadShutdown () ;
  85. void PostSignalThreadShutdown () ;
  86. // Get thread information
  87. ULONG GetThreadId () { return m_ThreadId ; }
  88. HANDLE GetThreadHandle () { return m_ThreadHandle ; }
  89. BOOL ScheduleTask ( ProvAbstractTaskObject &a_TaskObject ) ;
  90. BOOL ReapTask ( ProvAbstractTaskObject &a_TaskObject ) ;
  91. virtual void Initialise () {} ;
  92. virtual void Uninitialise () {} ;
  93. virtual void TimedOut() {} ;
  94. // Get Thread object associated with current thread
  95. static ProvThreadObject *GetThreadObject () ;
  96. static BOOL Startup () ;
  97. static void Closedown() ;
  98. } ;
  99. class ProvAbstractTaskObject
  100. {
  101. friend ProvThreadObject ;
  102. private:
  103. // list of thread objects keyed on thread identifier
  104. ThreadContainer m_ThreadContainer ;
  105. CCriticalSection m_Lock ;
  106. ProvEventObject m_CompletionEvent ;
  107. ProvEventObject m_AcknowledgementEvent ;
  108. HANDLE m_ScheduledHandle;
  109. DWORD m_timeout;
  110. BOOL WaitDispatch ( ProvThreadObject *a_ThreadObject , HANDLE a_Handle , BOOL &a_Processed ) ;
  111. BOOL WaitAcknowledgementDispatch ( ProvThreadObject *a_ThreadObject , HANDLE a_Handle , BOOL &a_Processed ) ;
  112. void AttachTaskToThread ( ProvThreadObject &a_ThreadObject ) ;
  113. void DetachTaskFromThread ( ProvThreadObject &a_ThreadObject ) ;
  114. protected:
  115. ProvAbstractTaskObject (
  116. const TCHAR *a_GlobalTaskNameComplete = NULL,
  117. const TCHAR *a_GlobalTaskNameAcknowledgement = NULL,
  118. DWORD a_timeout = INFINITE
  119. ) ;
  120. virtual HANDLE GetHandle() = 0;
  121. public:
  122. virtual ~ProvAbstractTaskObject () ;
  123. virtual void Process () { Complete () ; }
  124. virtual void Exec () {} ;
  125. virtual void Complete () { m_CompletionEvent.Set () ; }
  126. virtual BOOL Wait ( BOOL a_Dispatch = FALSE ) ;
  127. virtual void Acknowledge () { m_AcknowledgementEvent.Set () ; }
  128. virtual BOOL WaitAcknowledgement ( BOOL a_Dispatch = FALSE ) ;
  129. virtual void TimedOut() {} ;
  130. } ;
  131. class ProvTaskObject : public ProvAbstractTaskObject
  132. {
  133. private:
  134. ProvEventObject m_Event;
  135. protected:
  136. public:
  137. ProvTaskObject (
  138. const TCHAR *a_GlobalTaskNameStart = NULL ,
  139. const TCHAR *a_GlobalTaskNameComplete = NULL,
  140. const TCHAR *a_GlobalTaskNameAcknowledgement = NULL,
  141. DWORD a_timeout = INFINITE
  142. ) ;
  143. ~ProvTaskObject () {} ;
  144. void Exec () { m_Event.Set(); }
  145. HANDLE GetHandle () { return m_Event.GetHandle() ; }
  146. } ;
  147. template <typename FT, FT F, BOOL Condition_> class VoidOnDeleteIf
  148. {
  149. private:
  150. BOOL Condition_;
  151. BOOL bExec;
  152. public:
  153. VoidOnDeleteIf ( ): bExec ( FALSE )
  154. {
  155. };
  156. void Exec ( )
  157. {
  158. if ( Condition_ )
  159. {
  160. F ( );
  161. bExec = TRUE;
  162. }
  163. }
  164. ~VoidOnDeleteIf ( )
  165. {
  166. if ( !bExec )
  167. {
  168. Exec ();
  169. }
  170. };
  171. };
  172. template <typename FT, FT F, BOOL const &Condition_> class VoidOnDeleteIfNot
  173. {
  174. private:
  175. BOOL bExec;
  176. public:
  177. VoidOnDeleteIfNot ( ): bExec ( FALSE )
  178. {
  179. };
  180. void Exec ( )
  181. {
  182. if ( !Condition_ )
  183. {
  184. F ( );
  185. bExec = TRUE;
  186. }
  187. }
  188. ~VoidOnDeleteIfNot ( )
  189. {
  190. if ( !bExec )
  191. {
  192. Exec ();
  193. }
  194. };
  195. };
  196. template <typename T, typename FT, FT F> class ProvOnDelete
  197. {
  198. private:
  199. T Val_;
  200. BOOL bExec;
  201. public:
  202. ProvOnDelete ( T Val ): Val_ ( Val ), bExec ( FALSE )
  203. {
  204. };
  205. void Exec ( )
  206. {
  207. F(Val_);
  208. bExec = TRUE;
  209. }
  210. ~ProvOnDelete ( )
  211. {
  212. if ( !bExec )
  213. {
  214. Exec ();
  215. }
  216. };
  217. };
  218. template<class T> class CProvFreeMe
  219. {
  220. protected:
  221. T* m_p;
  222. public:
  223. CProvFreeMe(T* p) : m_p(p){}
  224. ~CProvFreeMe() {free ( m_p );}
  225. };
  226. template<class T> class CProvDeleteMe
  227. {
  228. protected:
  229. T* m_p;
  230. public:
  231. CProvDeleteMe(T* p) : m_p(p){}
  232. ~CProvDeleteMe() {delete m_p;}
  233. };
  234. template<class T> class CProvDeleteMeArray
  235. {
  236. protected:
  237. T* m_p;
  238. public:
  239. CProvDeleteMeArray(T* p) : m_p(p){}
  240. ~CProvDeleteMeArray() {delete [] m_p;}
  241. };
  242. #ifndef __WAITEX__
  243. #define __WAITEX__
  244. template < typename T, typename FT, FT F, int iTime >
  245. class WaitException
  246. {
  247. public:
  248. WaitException ( T Val_ )
  249. {
  250. BOOL bResult = FALSE;
  251. while ( ! bResult )
  252. {
  253. try
  254. {
  255. F ( Val_ );
  256. bResult = TRUE;
  257. }
  258. catch ( ... )
  259. {
  260. }
  261. if ( ! bResult )
  262. {
  263. ::Sleep ( iTime );
  264. }
  265. }
  266. }
  267. };
  268. #endif __WAITEX__
  269. #endif //__PROVTHREAD_PROVTHRD_H__