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.

377 lines
7.2 KiB

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