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.

143 lines
5.5 KiB

  1. //
  2. // MODULE: ThreadPool.h
  3. //
  4. // PURPOSE: interface for classes for high level of pool thread activity
  5. //
  6. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  7. //
  8. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  9. //
  10. // AUTHOR: Joe Mabel, based on earlier (8-2-96) work by Roman Mach
  11. //
  12. // ORIGINAL DATE: 9/23/98
  13. //
  14. // NOTES:
  15. // 1.
  16. //
  17. // Version Date By Comments
  18. //--------------------------------------------------------------------
  19. // V0.1 - RM Original
  20. // V3.0 9/23/98 JM better encapsulation & some chages to algorithm
  21. //
  22. #if !defined(AFX_THREADPOOL_H__0F43119D_5247_11D2_95FC_00C04FC22ADD__INCLUDED_)
  23. #define AFX_THREADPOOL_H__0F43119D_5247_11D2_95FC_00C04FC22ADD__INCLUDED_
  24. #if _MSC_VER >= 1000
  25. #pragma once
  26. #endif // _MSC_VER >= 1000
  27. #include <time.h>
  28. #include "stateless.h"
  29. class CPoolQueue; // forward reference
  30. class APGTSContext; // forward reference
  31. // Status class for pool threads, used by both the thread pool in order to handle stuck threads
  32. // as well as by the status page for reporting on thread status. If time permits, we should
  33. // convert the member variable to private and add get-set methods in order to be consistent with
  34. // the rest of the code.
  35. class CPoolThreadStatus
  36. {
  37. public:
  38. time_t m_timeCreated; // time CThreadPool::CThreadControl object was constructed
  39. // If this CPoolThreadStatusobject is the return of a thread
  40. // status request, 0 here implies there is no such
  41. // CThreadPool::CThreadControl object
  42. DWORD m_seconds; // time elapsed since last started or finished a request.
  43. // That is, how long this thread has been working on a task
  44. // (or how long since it finished its last task)
  45. // If task was never started - this is time since m_timeCreated
  46. bool m_bWorking; // true = currently working on a request.
  47. bool m_bFailed; // true = encountered a majorly unexpected situation &
  48. // chose to exit.
  49. CString m_strTopic; // current topic we are working on,
  50. // It is not initialized in the constructor so it can be zero length
  51. // Is used to transport the current topic name back to the status page.
  52. CString m_strBrowser; // Current client browser. Used to transport the browser name back to
  53. // the status page.
  54. CString m_strClientIP; // Current Client IP address. Used to transport the client IP address
  55. // back to the status page.
  56. CPoolThreadStatus() :
  57. m_timeCreated(0), m_seconds(0), m_bWorking(false), m_bFailed(false)
  58. {};
  59. bool operator < (const CPoolThreadStatus&) const {return false;}
  60. bool operator == (const CPoolThreadStatus&) const {return false;}
  61. };
  62. class CSniffConnector;
  63. class CThreadPool
  64. {
  65. friend class CDBLoadConfiguration;
  66. private:
  67. class CThreadControl
  68. {
  69. private:
  70. HANDLE m_hThread; // thread handle
  71. HANDLE m_hevDone; // Thread uses this event only to say, effectively,
  72. // "outta here" as it dies.
  73. HANDLE m_hMutex; // protect access to m_time, m_bWorking.
  74. bool m_bExit; // set true when either a normal queued-up task or an explicit
  75. // "kill" wants the thread to break out of its loop.
  76. CPoolQueue *m_pPoolQueue; // point to the one and only instance of CPoolQueue
  77. time_t m_timeCreated; // time this object was constructed
  78. time_t m_time; // time last started or finished a request; init'd 0, but will
  79. // be non-zero if thread ever used.
  80. bool m_bWorking; // true = currently working on a request.
  81. bool m_bFailed; // true = encountered a majorly unexpected situation &
  82. // chose to exit.
  83. CNameStateless m_strBrowser; // Current client browser.
  84. CNameStateless m_strClientIP; // Current Client IP address.
  85. APGTSContext *m_pContext; // pointer to context of a request.
  86. CSniffConnector *m_pSniffConnector; // pointer to sniff connector base class,
  87. // the only purpose of storing this pointer
  88. // as member variable is to pass it to
  89. // constructor of APGTSContext
  90. public:
  91. CThreadControl(CSniffConnector*);
  92. ~CThreadControl();
  93. // This function may throw an exceptions of type CGeneralException.
  94. DWORD Initialize(CPoolQueue * pPoolQueue);
  95. void Kill(DWORD milliseconds);
  96. bool WaitForThreadToFinish(DWORD milliseconds);
  97. void WorkingStatus(CPoolThreadStatus & status);
  98. time_t GetTimeCreated() const;
  99. private:
  100. static UINT WINAPI PoolTask( LPVOID lpParams );
  101. bool ProcessRequest();
  102. void PoolTaskLoop();
  103. void Lock();
  104. void Unlock();
  105. bool Exit();
  106. };
  107. public:
  108. CThreadPool(CPoolQueue * pPoolQueue, CSniffConnector * pSniffConnector);
  109. ~CThreadPool();
  110. DWORD GetStatus() const; // get any error during construction
  111. DWORD GetWorkingThreadCount() const;
  112. void ExpandPool(DWORD dwDesiredThreadCount);
  113. bool ReinitializeThread(DWORD i);
  114. void ReinitializeStuckThreads();
  115. bool ThreadStatus(DWORD i, CPoolThreadStatus &status);
  116. private:
  117. void DestroyThreads();
  118. private:
  119. DWORD m_dwErr;
  120. CThreadControl **m_ppThreadCtl; // thread management
  121. CSniffConnector *m_pSniffConnector; // pointer to sniff connector base class,
  122. // the only purpose of storing this pointer
  123. // as member variable is to pass it to
  124. // constructor of CThreadControl
  125. DWORD m_dwWorkingThreadCount; // threads actually created
  126. CPoolQueue *m_pPoolQueue; // Keeps track of user requests queued up to be serviced
  127. // by working threads (a.k.a. "pool threads")
  128. };
  129. #endif // !defined(AFX_THREADPOOL_H__0F43119D_5247_11D2_95FC_00C04FC22ADD__INCLUDED_)