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.

223 lines
8.2 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: CTHREAD.HXX
  7. //
  8. // Contents: Thread management classes.
  9. //
  10. // Classes: CTThreadObject
  11. // CTThreadCreator
  12. // CTThread
  13. //
  14. // History: 30-Jun-94 XimingZ Created
  15. // 15-Nov-94 XimingZ Updated - added more methods
  16. //
  17. //--------------------------------------------------------------------------
  18. #if !defined(WIN16)
  19. #ifndef __CTHREAD_HXX__
  20. #define __CTHREAD_HXX__
  21. // Option bit values used in CTThreadCreator::CreateThreads
  22. #define CTCOPT_ASYNC_START 0x1
  23. #define CTCOPT_NOT_ATOMIC 0x2
  24. // Count of creator controlled events, at least 2
  25. #define CTC_CEVENTS 2
  26. // Debug object declaration
  27. DH_DECLARE;
  28. // Forword class declaration
  29. class CTThreadCreator;
  30. class CTThread;
  31. // Internal thread start routine
  32. DWORD APIENTRY CTThreadStartRoutine (PVOID pvArg);
  33. //+-------------------------------------------------------------------
  34. //
  35. // Class: CTThreadObject
  36. //
  37. // Purpose: Class for storing thread data shared by CThreadCreator
  38. // and CTThread.
  39. //
  40. // History: 15-Nov-94 XimingZ Created
  41. //
  42. //--------------------------------------------------------------------
  43. class CTThreadObject
  44. {
  45. friend CTThread;
  46. friend CTThreadCreator;
  47. private:
  48. typedef struct tagCTTHRDSTATUS
  49. {
  50. HRESULT hr; // Thread result
  51. DWORD dwThreadId; // Thread Id
  52. BOOL fRunning; // Is thread running?
  53. } CTTHRDSTATUS, *PCTTHRDSTATUS;
  54. // Data
  55. CRITICAL_SECTION _critical; // Object of CRITICAL_SECTION
  56. BOOL _fContinue; // Threads can continue normally?
  57. ULONG _cMaxThreads; // Count of threads requested
  58. ULONG _cThreads; // Count of threads actually created
  59. HANDLE _ahEvents[CTC_CEVENTS]; // Creator controlled Events
  60. ULONG _ulEvent; // Index to the next event in _ahEvent
  61. // Each element of this array is an array of events, each controlled
  62. // by a unique child thread
  63. HANDLE *_ahThrdEvents;
  64. PCTTHRDSTATUS _aThrdStatus; // Array of child thread status
  65. CTThread **_apThreads; // Array of CTThread instances for child
  66. // threads
  67. DWORD _dwOptions; // Options passed in thru CreateThreads
  68. ULONG _cRefs; // Reference count
  69. CTThreadCreator *_pCreator; // Pointer to the creator of this object
  70. PVOID *_apvThrdArg; // If not NULL, it points to thread arg
  71. // array passed by CreateThreads and the
  72. // elements of the array should be
  73. // deleted on exit by this object
  74. HANDLE *_ahThreads; // Thread handle array
  75. ULONG _cThrdArgs; // Count of elements in _apvThrdArg
  76. PTHREAD_START_ROUTINE _pthreadRoutine; // Thread routine entry
  77. // Methods
  78. CTThreadObject (CTThreadCreator *pCreator);
  79. ~CTThreadObject (VOID);
  80. HRESULT AddThread (CTThread *pThread);
  81. HRESULT DelThread (ULONG ulIndex);
  82. HANDLE GetThreadEvent (ULONG ulIndex);
  83. HRESULT Create (PTHREAD_START_ROUTINE pfunc,
  84. PVOID pvArg);
  85. VOID CleanupThreads (VOID);
  86. ULONG AddRef (VOID);
  87. ULONG Release (VOID);
  88. HRESULT CreateThreads (ULONG *pcThreads,
  89. PTHREAD_START_ROUTINE pfunc,
  90. PVOID *apvArg,
  91. DWORD dwOptions,
  92. BOOL fDeleteOnExit);
  93. HRESULT WaitForThreadsReady (DWORD dwTimeout);
  94. HRESULT WaitForThreadsDone (DWORD dwTimeout);
  95. VOID InformThreads (BOOL fContinue);
  96. // Inline methods
  97. HANDLE GetEvent (ULONG ulEvent)
  98. { return _ahEvents[ulEvent]; };
  99. BOOL ThreadsCanContinue (VOID) { return _fContinue; };
  100. DWORD GetOptions (VOID) { return _dwOptions; };
  101. PTHREAD_START_ROUTINE GetThreadRoutine(VOID)
  102. {return _pthreadRoutine;};
  103. VOID EnterCriticalBlock (VOID) {EnterCriticalSection(&_critical);};
  104. VOID LeaveCriticalBlock (VOID) {LeaveCriticalSection(&_critical);};
  105. public:
  106. // None
  107. };
  108. //+-------------------------------------------------------------------
  109. //
  110. // Class: CTThreadCreator
  111. //
  112. // Purpose: Class for thread creators.
  113. //
  114. // History: 30-Jun-94 XimingZ Created
  115. //
  116. //--------------------------------------------------------------------
  117. class CTThreadCreator
  118. {
  119. private:
  120. CTThreadObject *_pthrdObj;
  121. BOOL _fDeleteOnExit; // Is CTThreadObject responsible
  122. // for deleting the thread arguments?
  123. public:
  124. CTThreadCreator (VOID);
  125. CTThreadCreator (BOOL fDeleteOnExit);
  126. ~CTThreadCreator (VOID);
  127. HRESULT CreateThreads (ULONG *pcThreads,
  128. PTHREAD_START_ROUTINE pfunc,
  129. PVOID *apvArg,
  130. DWORD dwOptions);
  131. HRESULT WaitForThreadsReady (DWORD dwTimeout);
  132. HRESULT WaitForThreadsDone (DWORD dwTimeout);
  133. BOOL AnyThreadsFailed (VOID);
  134. VOID ContinueThreads (VOID);
  135. VOID TerminateThreads (VOID);
  136. VOID QuitThreads (VOID);
  137. VOID InformThreads (BOOL fContinue);
  138. HRESULT GetThreadResult (ULONG ulIndex,
  139. HRESULT *phr,
  140. DWORD *pdwThreadId,
  141. BOOL *pfRunning);
  142. HRESULT GetThreadHandle (ULONG ulIndex,
  143. HANDLE *phThread,
  144. BOOL fDuplicate);
  145. };
  146. //+-------------------------------------------------------------------
  147. //
  148. // Class: CTThread
  149. //
  150. // Purpose: Class for threads created by CTThreadCreator.
  151. //
  152. // History: 30-Jun-94 XimingZ Created
  153. //
  154. //--------------------------------------------------------------------
  155. class CTThread
  156. {
  157. friend CTThreadObject;
  158. friend ULONG APIENTRY CTThreadStartRoutine (PVOID pvArg);
  159. private:
  160. // Data
  161. CTThreadObject *_pthrdObj; // Pointer to instance of
  162. // CTThreadObject storing data
  163. ULONG _ulEvent; // Next creator event to wait for
  164. ULONG _ulIndex; // Index of this thread
  165. PVOID _pvArg; // User data passed to this thread
  166. // Methods
  167. CTThread (PVOID pvArg,
  168. CTThreadObject *pthrdObj,
  169. ULONG ulIndex);
  170. ~CTThread (VOID);
  171. BOOL CanContinue (VOID);
  172. DWORD GetOptions (VOID);
  173. PTHREAD_START_ROUTINE GetThreadRoutine (VOID);
  174. VOID SetStart (VOID);
  175. VOID SetTerminate (VOID);
  176. public:
  177. VOID SetReady (VOID);
  178. HRESULT WaitForCreator (DWORD dwTimeout);
  179. VOID SetResult (HRESULT hr);
  180. // Inline methods
  181. PVOID GetArg (VOID) { return _pvArg; };
  182. ULONG GetIndex (VOID) { return _ulIndex; };
  183. };
  184. #endif // __CTHREAD_HXX__
  185. #endif // !defined(WIN16)