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.

206 lines
5.6 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1997, Microsoft Corporation
  4. //
  5. // File: thrdpl2.h
  6. //
  7. // Contents: definitions needed for clients of the thrdpool2 lib
  8. //
  9. // Description: The thrdpool library defines the CThreadPool base class.
  10. // Users of this lib should define their own derived class
  11. // that inherits from CThreadPool. A CThreadPool object
  12. // has a set of threads that is used to do some work. It also
  13. // has a common completion port that is used to queue work items.
  14. // All worker threads will normally block on
  15. // GetQueuedCompletionStatus(). Clients of the CThreadPool
  16. // object will call PostWork() to get work done. This will
  17. // result in one of the worker threads returning from
  18. // GetQueuedCompletionStatus() and calling the derived class'
  19. // WorkCompletion() routine with a pvContext.
  20. //
  21. // CThreadPool provides the following features:
  22. // - creation with an initial number of threads
  23. // - deletion
  24. // - ability to submit work items
  25. // - grow pool of threads
  26. // - shrink pool of threads
  27. //
  28. // NOTE: the base class has no knowledge of the type of work
  29. // getting done. It just manages the details of getting work
  30. // requests and distributing it to threads in its pool. This
  31. // allows the derived class to focus on processing the actual
  32. // work item without bothering about queueing etc.
  33. //
  34. // Completion ports are used merely to leverage its queueing
  35. // semantics and not for I/O. If the work done by each thread
  36. // is fairly small, LIFO semantics of completion ports will
  37. // reduce context switches.
  38. //
  39. // Functions:
  40. //
  41. // History: 09/18/97 Rajeev Rajan (rajeevr) Created
  42. //
  43. //-----------------------------------------------------------------------------
  44. #ifndef THRDPL2_H
  45. #define THRDPL2_H
  46. //
  47. // Base thread pool class
  48. //
  49. class CThreadPool
  50. {
  51. public:
  52. //
  53. // Constructor, destructor
  54. //
  55. CThreadPool();
  56. virtual ~CThreadPool();
  57. //
  58. // creates the required number of worker threads and completion port
  59. //
  60. BOOL Initialize( DWORD dwConcurrency, DWORD dwMaxThreads, DWORD dwInitThreads );
  61. //
  62. // shutdown the thread pool
  63. //
  64. BOOL Terminate( BOOL fFailedInit = FALSE, BOOL fShrinkPool = TRUE );
  65. //
  66. // clients should call this to post work items
  67. //
  68. BOOL PostWork(PVOID pvWorkContext);
  69. //
  70. // expose shutdown event
  71. //
  72. HANDLE QueryShutdownEvent() { return m_hShutdownEvent; }
  73. //
  74. // A job represents a series of PostWork() items
  75. //
  76. VOID BeginJob( PVOID pvContext );
  77. //
  78. // Wait for job to complete ie all PostWork() items are done
  79. //
  80. DWORD WaitForJob( DWORD dwTimeout );
  81. //
  82. // Job context is used to process all work items in a job
  83. //
  84. PVOID QueryJobContext() { return m_pvContext; }
  85. //
  86. // shrink pool by dwNumThreads
  87. //
  88. BOOL ShrinkPool( DWORD dwNumThreads );
  89. //
  90. // grow pool by dwNumThreads
  91. //
  92. BOOL GrowPool( DWORD dwNumThreads );
  93. //
  94. // Shrink all the existing threads
  95. //
  96. VOID ShrinkAll();
  97. protected:
  98. //
  99. // derived method called when work items are posted
  100. //
  101. virtual VOID WorkCompletion(PVOID pvWorkContext) = 0;
  102. //
  103. // For those who has knowledge about automatic shutdown of the thread
  104. // pool, this function is used as an interface for implementing
  105. // shutting down the thread pool for itself. The function is called
  106. // when the last thread in the pool goes away because of shutdown
  107. // event has been fired.
  108. //
  109. // The reason for this interface is: in some scenarios the shutting
  110. // down thread is from the same thread pool and it will cause deadlock.
  111. // Users of thread pool who expects this will happen should not call
  112. // WaitForJob, and should call Terminate and delete in this call back.
  113. //
  114. virtual VOID AutoShutdown() {
  115. //
  116. // People who doesn't care about this function does a no-op
  117. //
  118. };
  119. private:
  120. friend DWORD __stdcall ThreadDispatcher(PVOID pvWorkerThread);
  121. //
  122. // check for matching Init(), Term() calls
  123. //
  124. LONG m_lInitCount;
  125. //
  126. // handle to completion port
  127. //
  128. HANDLE m_hCompletionPort;
  129. //
  130. // shutdown event
  131. //
  132. HANDLE m_hShutdownEvent;
  133. //
  134. // array of worker thread handles
  135. //
  136. HANDLE* m_rgThrdpool;
  137. //
  138. // array of thread id's. BUGBUG: may be able to get rid of this if
  139. // we have per thread handle
  140. //
  141. DWORD* m_rgdwThreadId;
  142. //
  143. // number of worker threads
  144. //
  145. DWORD m_dwNumThreads;
  146. //
  147. // max number of worker threads
  148. //
  149. DWORD m_dwMaxThreads;
  150. //
  151. // count work items in current job
  152. //
  153. LONG m_lWorkItems;
  154. //
  155. // event used to sync job completion
  156. //
  157. HANDLE m_hJobDone;
  158. //
  159. // context for current job
  160. //
  161. PVOID m_pvContext;
  162. //
  163. // crit sect to protect incs/decs to m_lWorkItems
  164. //
  165. CRITICAL_SECTION m_csCritItems;
  166. //
  167. // access completion port - needed by worker threads
  168. //
  169. HANDLE QueryCompletionPort() { return m_hCompletionPort; }
  170. //
  171. // thread function
  172. //
  173. static DWORD __stdcall ThreadDispatcher(PVOID pvWorkerThread);
  174. };
  175. #endif // #ifndef THRDPL2_H