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.

151 lines
4.3 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. thread_manager.hxx
  5. Abstract:
  6. Provides thread creation and cleanup management
  7. Author:
  8. Jeffrey Wall (jeffwall) 11-28-2000
  9. Revision History:
  10. --*/
  11. #ifndef _THREADMANAGER_H_
  12. #define _THREADMANAGER_H_
  13. #include <thread_pool.h>
  14. typedef void (WINAPI *PTHREAD_STOP_ROUTINE)(PVOID);
  15. typedef PTHREAD_STOP_ROUTINE LPTHREAD_STOP_ROUTINE;
  16. typedef BOOL (WINAPI *PTHREAD_DECISION_ROUTINE )(PVOID);
  17. typedef PTHREAD_DECISION_ROUTINE LPTHREAD_DECISION_ROUTINE;
  18. #define SIGNATURE_THREAD_MANAGER ((DWORD) 'NAMT')
  19. #define SIGNATURE_THREAD_MANAGER_FREE ((DWORD) 'xAMT')
  20. #define SIGNATURE_THREAD_PARAM ((DWORD) 'RAPT')
  21. #define SIGNATURE_THREAD_PARAM_FREE ((DWORD) 'xAPT')
  22. class THREAD_MANAGER
  23. {
  24. private:
  25. DWORD m_dwSignature;
  26. public:
  27. static HRESULT CreateThreadManager(THREAD_MANAGER ** ppThreadManager,
  28. THREAD_POOL * pPool,
  29. THREAD_POOL_DATA * pPoolData);
  30. VOID TerminateThreadManager(LPTHREAD_STOP_ROUTINE lpStopAddress,
  31. LPVOID lpParameter);
  32. BOOL CreateThread(LPTHREAD_START_ROUTINE lpStartAddress,
  33. LPVOID lpParameter);
  34. VOID RequestThread(LPTHREAD_START_ROUTINE lpStartAddress,
  35. LPVOID lpStartParameter);
  36. private:
  37. // use create and terminate
  38. THREAD_MANAGER(THREAD_POOL * pPool,
  39. THREAD_POOL_DATA * pPoolData);
  40. ~THREAD_MANAGER();
  41. // not implemented
  42. THREAD_MANAGER(const THREAD_MANAGER&);
  43. THREAD_MANAGER& operator=(const THREAD_MANAGER&);
  44. HRESULT Initialize();
  45. VOID DrainThreads(LPTHREAD_STOP_ROUTINE lpStopAddress,
  46. LPVOID lpParameter);
  47. /*++
  48. Struct Description:
  49. Storage for parameters passed to ThreadManagerThread
  50. Members:
  51. pThreadFunc - thread function to call
  52. pvThreadArg - arguments to pass to thread function
  53. pThreadManager - pointer to ThreadManager associated with current thread
  54. hThreadSelf - handle returned from call to CreateThread
  55. dwRequestTime - time that thread request was made
  56. --*/
  57. struct THREAD_PARAM
  58. {
  59. THREAD_PARAM() :
  60. dwSignature(SIGNATURE_THREAD_PARAM),
  61. pThreadFunc(NULL),
  62. pvThreadArg(NULL),
  63. pThreadManager(NULL),
  64. dwRequestTime(NULL),
  65. fCallbackOnCreation(FALSE)
  66. {
  67. }
  68. ~THREAD_PARAM()
  69. {
  70. DBG_ASSERT(SIGNATURE_THREAD_PARAM == dwSignature);
  71. dwSignature = SIGNATURE_THREAD_PARAM_FREE;
  72. }
  73. DWORD dwSignature;
  74. LPTHREAD_START_ROUTINE pThreadFunc;
  75. LPVOID pvThreadArg;
  76. THREAD_MANAGER *pThreadManager;
  77. DWORD dwRequestTime;
  78. BOOL fCallbackOnCreation;
  79. };
  80. static DWORD ThreadManagerThread(LPVOID);
  81. static VOID ControlTimerCallback(PVOID lpParameter,
  82. BOOLEAN TimerOrWaitFired);
  83. VOID DetermineThreadAction();
  84. BOOL DoThreadCreation(THREAD_PARAM * pParam);
  85. VOID CreatedSuccessfully(THREAD_PARAM * pParam);
  86. VOID RemoveThread(THREAD_PARAM * pParam);
  87. VOID DoThreadParking();
  88. BOOL DoThreadUnParking();
  89. static VOID ParkThread(DWORD dwErrorCode,
  90. DWORD dwNumberOfBytesTransferred,
  91. LPOVERLAPPED lpo);
  92. CRITICAL_SECTION m_CriticalSection;
  93. BOOL m_fShuttingDown;
  94. BOOL m_fWaitingForCreationCallback;
  95. HANDLE m_hTimer;
  96. THREAD_PARAM *m_pParam;
  97. ULONG m_ulContextSwitchCount;
  98. LONG m_lTotalThreads;
  99. LONG m_lParkedThreads;
  100. HANDLE m_hParkEvent;
  101. HANDLE m_hShutdownEvent;
  102. THREAD_POOL *m_pPool;
  103. THREAD_POOL_DATA *m_pPoolData;
  104. LARGE_INTEGER m_liOriginalTotal;
  105. LARGE_INTEGER m_liOriginalBusy;
  106. };
  107. #endif // _THREADMANAGER_H_