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.

122 lines
3.2 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. //
  36. //-----------------------------------------------------------------------------
  37. #ifndef THRDPOOL_H
  38. #define THRDPOOL_H
  39. //
  40. // This is the blob that is passed thro the completion port
  41. //
  42. typedef struct _WorkContextEnv
  43. {
  44. OVERLAPPED Ov; // needed by Post/GetQueuedCompletionStatus
  45. PVOID pvWorkContext; // actual work context - user defined
  46. } WorkContextEnv, *LPWorkContextEnv;
  47. //
  48. // Base worker thread class
  49. //
  50. class CWorkerThread
  51. {
  52. public:
  53. //
  54. // Constructor, destructor
  55. //
  56. CWorkerThread();
  57. virtual ~CWorkerThread();
  58. //
  59. // class initializer - should be called once before this class is used
  60. //
  61. static BOOL InitClass( DWORD dwConcurrency );
  62. //
  63. // class terminator - should be called once when done using the class
  64. //
  65. static BOOL TermClass();
  66. //
  67. // clients should call this to post work items
  68. //
  69. BOOL PostWork(PVOID pvWorkContext);
  70. //
  71. // expose shutdown event
  72. //
  73. HANDLE QueryShutdownEvent() { return m_hShutdownEvent; }
  74. protected:
  75. //
  76. // derived method called when work items are posted
  77. //
  78. virtual VOID WorkCompletion(PVOID pvWorkContext) = 0;
  79. private:
  80. //
  81. // check for matching InitClass(), TermClass() calls
  82. //
  83. static LONG m_lInitCount;
  84. //
  85. // handle to completion port
  86. //
  87. static HANDLE m_hCompletionPort;
  88. //
  89. // handle to worker thread
  90. //
  91. HANDLE m_hThread;
  92. //
  93. // shutdown event
  94. //
  95. HANDLE m_hShutdownEvent;
  96. //
  97. // thread function
  98. //
  99. static DWORD __stdcall ThreadDispatcher(PVOID pvWorkerThread);
  100. //
  101. // block on GetQueuedCompletionStatus for work items
  102. //
  103. VOID GetWorkCompletion(VOID);
  104. };
  105. #endif // #ifndef THRDPOOL_H