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.

279 lines
5.3 KiB

  1. /*++
  2. Copyright (c) 1998-2000 Microsoft Corporation
  3. Module Name:
  4. thrpool.h
  5. Abstract:
  6. Contains the Win32 Thread Pooling Class, ThreadPool
  7. Author:
  8. Tad Brockway (tadb) 9/99
  9. Revision History:
  10. --*/
  11. #ifndef __THRPOOL_H__
  12. #define __THRPOOL_H__
  13. #include "drobject.h"
  14. #include "smartptr.h"
  15. ///////////////////////////////////////////////////////////////
  16. //
  17. // Defines
  18. //
  19. #define INVALID_THREADPOOLREQUEST NULL
  20. #define THRPOOL_DEFAULTMINTHREADS 5
  21. #define THRPOOL_DEFAULTMAXTHREADS 40
  22. ///////////////////////////////////////////////////////////////
  23. //
  24. // Types
  25. //
  26. typedef DWORD (*ThreadPoolFunc)(PVOID clientData, HANDLE cancelEvent);
  27. typedef DWORD (_ThreadPoolFunc)(PVOID clientData, HANDLE cancelEvent);
  28. typedef void *ThreadPoolRequest;
  29. ///////////////////////////////////////////////////////////////
  30. //
  31. // ThreadPoolRequest
  32. //
  33. // A single request for the pool to service.
  34. //
  35. class ThreadPoolReq : public RefCount
  36. {
  37. public:
  38. ThreadPoolFunc _func;
  39. PVOID _clientData;
  40. HANDLE _completionEvent;
  41. DWORD _completionStatus;
  42. virtual DRSTRING ClassName() { return _T("ThreadPoolReq"); }
  43. };
  44. ///////////////////////////////////////////////////////////////
  45. //
  46. // ThreadPool
  47. //
  48. // Thread Pooling Class
  49. //
  50. class ThreadPool;
  51. class ThreadPool : public DrObject {
  52. private:
  53. ULONG _threadCount;
  54. BOOL _initialized;
  55. //
  56. // Thread list.
  57. //
  58. LIST_ENTRY _threadListHead;
  59. //
  60. // How long (in ms) to wait for a thread to exit before
  61. // killing. INFINITE if we should block indefinitely.
  62. //
  63. DWORD _threadExitTimeout;
  64. //
  65. // Lock
  66. //
  67. CRITICAL_SECTION _cs;
  68. #ifdef DC_DEBUG
  69. LONG _lockCount;
  70. #endif
  71. //
  72. // Max/Min Number of Threads
  73. //
  74. ULONG _maxThreads;
  75. ULONG _minThreads;
  76. //
  77. // Represent a single thread in the pool.
  78. //
  79. typedef struct tagTHREADPOOL_THREAD
  80. {
  81. DWORD _tid;
  82. ThreadPool *_pool;
  83. HANDLE _threadHandle;
  84. HANDLE _synchronizationEvent;
  85. BOOL _exitFlag;
  86. SmartPtr<ThreadPoolReq > _pendingRequest;
  87. LIST_ENTRY _listEntry;
  88. } THREADPOOL_THREAD, *PTHREADPOOL_THREAD;
  89. //
  90. // Remove a thread from the pool.
  91. //
  92. VOID RemoveThreadFromPool(
  93. PTHREADPOOL_THREAD thread,
  94. DWORD timeOut=INFINITE
  95. );
  96. //
  97. // Add a new thread to the pool and return it.
  98. //
  99. PTHREADPOOL_THREAD AddNewThreadToPool();
  100. //
  101. // Call the function associated with a pending thread request.
  102. //
  103. VOID HandlePendingRequest(PTHREADPOOL_THREAD thr);
  104. //
  105. // Locking Functions
  106. //
  107. VOID Lock();
  108. VOID Unlock();
  109. //
  110. // PooledThread Routines
  111. //
  112. static DWORD _PooledThread(PTHREADPOOL_THREAD thr);
  113. DWORD PooledThread(PTHREADPOOL_THREAD thr);
  114. //
  115. // Notify a thread to shut down, wait for it to finish, and clean up.
  116. //
  117. VOID CleanUpThread(PTHREADPOOL_THREAD thread, DWORD timeout);
  118. public:
  119. //
  120. // Constructor/Destructor
  121. //
  122. ThreadPool(ULONG minThreads=THRPOOL_DEFAULTMINTHREADS,
  123. ULONG maxThreads=THRPOOL_DEFAULTMAXTHREADS,
  124. DWORD threadExitTimeout=60000);
  125. virtual ~ThreadPool();
  126. VOID RemoveAllThreads();
  127. //
  128. // Initialize an Instance of this Class.
  129. //
  130. DWORD Initialize();
  131. //
  132. // Submit an asynchronous request to a thread in the pool.
  133. //
  134. ThreadPoolRequest SubmitRequest(
  135. ThreadPoolFunc func, PVOID clientData,
  136. HANDLE completionEvent = NULL
  137. );
  138. //
  139. // Return the completion status for a request.
  140. //
  141. DWORD GetRequestCompletionStatus(ThreadPoolRequest req);
  142. //
  143. // Return a pointer to the client data for a request.
  144. //
  145. PVOID GetRequestClientData(ThreadPoolRequest req);
  146. //
  147. // Return the current number of threads in the pool.
  148. //
  149. ULONG GetThreadCount() {
  150. return _threadCount;
  151. }
  152. //
  153. // Close a request submitted by a call to SubmitRequest. This
  154. // should be called after the request is finished.
  155. //
  156. VOID CloseRequest(ThreadPoolRequest req);
  157. //
  158. // Return the class name.
  159. //
  160. virtual DRSTRING ClassName() { return TEXT("ThreadPool"); }
  161. };
  162. ///////////////////////////////////////////////////////////////
  163. //
  164. // ThreadPool Inline Members
  165. //
  166. inline VOID ThreadPool::Lock()
  167. {
  168. DC_BEGIN_FN("ThreadPool::Lock");
  169. ASSERT(_initialized);
  170. TRC_NRM((TB, _T("Lock count is now %ld."), _lockCount));
  171. EnterCriticalSection(&_cs);
  172. #if DBG
  173. _lockCount++;
  174. #endif
  175. DC_END_FN();
  176. }
  177. inline VOID ThreadPool::Unlock()
  178. {
  179. DC_BEGIN_FN("ThreadPool::Unlock");
  180. ASSERT(_initialized);
  181. #if DBG
  182. _lockCount--;
  183. TRC_NRM((TB, _T("Lock count is now %ld."), _lockCount));
  184. ASSERT(_lockCount >= 0);
  185. #endif
  186. LeaveCriticalSection(&_cs);
  187. DC_END_FN();
  188. }
  189. //
  190. // Unit-Test Functions that Tests Thread Pools in the Background
  191. //
  192. #if DBG
  193. void ThreadPoolTestInit();
  194. void ThreadPoolTestShutdown();
  195. #endif
  196. #endif