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.

186 lines
4.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: thread.hxx
  7. //
  8. // Contents: Job scheduler worker thread class definition.
  9. //
  10. // Classes: CWorkerThread
  11. //
  12. // Functions: None.
  13. //
  14. // History: 25-Oct-95 MarkBl Created
  15. // 15-Feb-01 Jbenton Bug 315702 - Increase initial stack size
  16. // for worker threads
  17. //
  18. //--------------------------------------------------------------------------
  19. #ifndef __THREAD_HXX__
  20. #define __THREAD_HXX__
  21. class CWorkerThread;
  22. #define WORKER_STACK_SIZE 8192
  23. //+-------------------------------------------------------------------------
  24. //
  25. // Class: WorkerThread
  26. //
  27. // Purpose: Encapsulates the functions of a worker thread. A worker
  28. // thread is a thread within the job scheduler that is capable
  29. // of being scheduled to perform various tasks.
  30. //
  31. // Notes: None.
  32. //
  33. //--------------------------------------------------------------------------
  34. class CWorkerThread : public CDLink
  35. {
  36. public:
  37. CWorkerThread() : _hThread(NULL), _hWorkAvailable(NULL), _pTask(NULL) {
  38. TRACE3(CWorkerThread, CWorkerThread);
  39. // Initialize() completes remaining initialization.
  40. }
  41. ~CWorkerThread();
  42. HRESULT AssignTask(CTask * pTask);
  43. HANDLE GetHandle(void) const { return(_hThread); }
  44. HRESULT Initialize(void);
  45. CWorkerThread * Next(void) { return((CWorkerThread *)CDLink::Next()); }
  46. CWorkerThread * Prev(void) { return((CWorkerThread *)CDLink::Prev()); }
  47. HRESULT StartWorking(void);
  48. private:
  49. HANDLE _hThread;
  50. HANDLE _hWorkAvailable;
  51. CTask * _pTask;
  52. };
  53. //+-------------------------------------------------------------------------
  54. //
  55. // Class: CWorkerThreadQueue
  56. //
  57. // Purpose: Queue of CWorkerThread.
  58. //
  59. // Notes: None.
  60. //
  61. //--------------------------------------------------------------------------
  62. class CWorkerThreadQueue : public CQueue
  63. {
  64. public:
  65. CWorkerThreadQueue(void) {
  66. TRACE3(CWorkerThreadQueue, CWorkerThreadQueue);
  67. }
  68. ~CWorkerThreadQueue() {
  69. TRACE3(CWorkerThreadQueue, ~CWorkerThreadQueue);
  70. }
  71. void AddElement(CWorkerThread * pWrkThrd) {
  72. schDebugOut((DEB_USER3,
  73. "CWorkerThreadQueue::AddElement(0x%x) pWrkThrd(0x%x)\n",
  74. this,
  75. pWrkThrd));
  76. CQueue::AddElement(pWrkThrd);
  77. }
  78. CWorkerThread * GetFirstElement(void) {
  79. TRACE3(CWorkerThreadQueue, GetFirstElement);
  80. return((CWorkerThread *)CQueue::GetFirstElement());
  81. }
  82. CWorkerThread * RemoveElement(void) {
  83. TRACE3(CWorkerThreadQueue, RemoveElement);
  84. return((CWorkerThread *)CQueue::RemoveElement());
  85. }
  86. CWorkerThread * RemoveElement(CWorkerThread * pWrkThrd) {
  87. schDebugOut((DEB_USER3,
  88. "CWorkerThreadQueue::RemoveElement(0x%x) pWrkThrd(0x%x)\n",
  89. this,
  90. pWrkThrd));
  91. return((CWorkerThread *)CQueue::RemoveElement(pWrkThrd));
  92. }
  93. };
  94. //+-------------------------------------------------------------------------
  95. //
  96. // Class: CWorkerThreadMgr
  97. //
  98. // Purpose: Free worker thread pool management class.
  99. //
  100. // Notes: None.
  101. //
  102. //--------------------------------------------------------------------------
  103. class CWorkerThreadMgr
  104. {
  105. public:
  106. CWorkerThreadMgr(void)
  107. : _cThreadCount(0),
  108. _hThreadTerminationEvent(NULL) {
  109. TRACE3(CWorkerThreadMgr, CWorkerThreadMgr);
  110. InitializeCriticalSection(&_csThreadMgrCritSection);
  111. }
  112. ~CWorkerThreadMgr();
  113. void AddThread(CWorkerThread * pWrkThrd);
  114. // The returned count is considered valid only while the service is
  115. // in a pending service stop state.
  116. //
  117. DWORD GetThreadCount(void) { return(_cThreadCount); }
  118. BOOL Initialize(void);
  119. CWorkerThread * RemoveThread(void);
  120. CWorkerThread * RemoveThread(HANDLE hThread);
  121. BOOL Shutdown(BOOL fWait);
  122. void SignalThreadCreation(void) {
  123. InterlockedIncrement((LONG *)&_cThreadCount);
  124. }
  125. void SignalThreadTermination(void) {
  126. InterlockedDecrement((LONG *)&_cThreadCount);
  127. SetEvent(_hThreadTerminationEvent);
  128. }
  129. private:
  130. // Guards this object during multi-threaded access.
  131. //
  132. CRITICAL_SECTION _csThreadMgrCritSection;
  133. // Queue of free worker threads.
  134. //
  135. CWorkerThreadQueue _WorkerThreadQueue;
  136. // This count is a superset of the _WorkerThreadQueue count.
  137. // _gcThreadCount = # busy + # free.
  138. //
  139. DWORD _cThreadCount;
  140. // Signalled by the worker threads on termination.
  141. //
  142. HANDLE _hThreadTerminationEvent;
  143. };
  144. #endif // __THREAD_HXX__