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.

165 lines
4.7 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1997, Microsoft Corporation
  4. //
  5. // File: thrdpool.h
  6. //
  7. // Contents: definitions needed for clients of the thrdpool lib
  8. //
  9. // Description: The thrdpool library defines the CWorkerThread base class
  10. // Users of this lib should define their own derived class
  11. // that inherits from CWorkerThread. Each CWorkerThread object
  12. // has a thread that is used to do some work. It is also
  13. // associated with a common completion port that is used to
  14. // queue work items. All worker threads will normally block on
  15. // GetQueuedCompletionStatus(). Clients of the CWorkerThread
  16. // objects 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. // NOTE: the base class has no knowledge of the type of work
  22. // getting done. It just manages the details of getting work
  23. // requests and distributing it to threads in its pool. This
  24. // allows the derived class to focus on processing the actual
  25. // work item without bothering about queueing etc.
  26. //
  27. // Completion ports are used merely to leverage its queueing
  28. // semantics and not for I/O. If the work done by each thread
  29. // is fairly small, LIFO semantics of completion ports will
  30. // reduce context switches.
  31. //
  32. // Functions:
  33. //
  34. // History: 03/15/97 Rajeev Rajan (rajeevr) Created
  35. // 11/11/97 Adapted for DAV usage
  36. //
  37. //-----------------------------------------------------------------------------
  38. #ifndef _THRDPOOL_H_
  39. #define _THRDPOOL_H_
  40. #include <autoptr.h>
  41. #include <singlton.h>
  42. // CPoolManager --------------------------------------------------------------
  43. //
  44. class CDavWorkContext;
  45. class CDavWorkerThread;
  46. class CPoolManager : private Singleton<CPoolManager>
  47. {
  48. //
  49. // Friend declarations required by Singleton template
  50. //
  51. friend class Singleton<CPoolManager>;
  52. private:
  53. // Completion port for WorkerThreads
  54. //
  55. auto_handle<HANDLE> m_hCompletionPort;
  56. // Array of worker threads
  57. //
  58. enum { CTHRD_WORKER = 5 };
  59. CDavWorkerThread * m_rgpdwthrd[CTHRD_WORKER];
  60. // CREATORS
  61. //
  62. // Declared private to ensure that arbitrary instances
  63. // of this class cannot be created. The Singleton
  64. // template (declared as a friend above) controls
  65. // the sole instance of this class.
  66. //
  67. CPoolManager() {}
  68. ~CPoolManager();
  69. BOOL FInitPool(DWORD dwConcurrency = CTHRD_WORKER);
  70. VOID TerminateWorkers();
  71. // NOT IMPLEMENTED
  72. //
  73. CPoolManager(const CPoolManager& x);
  74. CPoolManager& operator=(const CPoolManager& x);
  75. public:
  76. // STATICS
  77. //
  78. static BOOL FInit()
  79. {
  80. if ( CreateInstance().FInitPool() )
  81. return TRUE;
  82. DestroyInstance();
  83. return FALSE;
  84. }
  85. static VOID Deinit()
  86. {
  87. DestroyInstance();
  88. }
  89. static BOOL PostWork (CDavWorkContext * pwc);
  90. static BOOL PostDelayedWork (CDavWorkContext * pwc,
  91. DWORD dwMsecDelay);
  92. static HANDLE GetIOCPHandle()
  93. {
  94. return Instance().m_hCompletionPort.get();
  95. }
  96. };
  97. // CDavWorkContext --------------------------------------------------------------
  98. //
  99. // Work context base class for work items posted to the thread pool.
  100. //
  101. // Note: this class is NOT refcounted. Lifetime of work items is determined
  102. // external to the thread pool mechanism. In particular, if a particular
  103. // derived work item class needs to have an indefinite lifetime, it is up
  104. // to the derived class to provide that functionality. E.g. a derived work
  105. // item can have a refcount. Code that posts the work item would then
  106. // add a reference before posting and release the reference (possibly destroying
  107. // the object if is the last ref) in its DwDoWork() call.
  108. //
  109. // The reason for this is that not ALL work items may be refcounted.
  110. // In fact, some may be static....
  111. //
  112. class CDavWorkContext
  113. {
  114. private:
  115. // NOT IMPLEMENTED
  116. //
  117. CDavWorkContext(const CDavWorkContext& x);
  118. CDavWorkContext& operator=(const CDavWorkContext& x);
  119. DWORD m_cbTransferred;
  120. DWORD m_dwLastError;
  121. LPOVERLAPPED m_po;
  122. public:
  123. CDavWorkContext() :
  124. m_cbTransferred(0),
  125. m_dwLastError(ERROR_SUCCESS),
  126. m_po(NULL)
  127. {
  128. }
  129. virtual ~CDavWorkContext() = 0;
  130. virtual DWORD DwDoWork () = 0;
  131. void SetCompletionStatusData(DWORD cbTransferred,
  132. DWORD dwLastError,
  133. LPOVERLAPPED po)
  134. {
  135. m_cbTransferred = cbTransferred;
  136. m_dwLastError = dwLastError;
  137. m_po = po;
  138. }
  139. DWORD CbTransferred() const { return m_cbTransferred; }
  140. DWORD DwLastError() const { return m_dwLastError; }
  141. LPOVERLAPPED GetOverlapped() const { return m_po; }
  142. };
  143. #endif