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.

275 lines
7.0 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // WorkThrd.h
  7. //
  8. // Abstract:
  9. // Definition of the CWorkerThread class.
  10. //
  11. // Implementation File:
  12. // WorkThrd.cpp
  13. //
  14. // Author:
  15. // David Potter (davidp) November 17, 1997
  16. //
  17. // Revision History:
  18. //
  19. // Notes:
  20. //
  21. /////////////////////////////////////////////////////////////////////////////
  22. #ifndef __WORKTHRD_H_
  23. #define __WORKTHRD_H_
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Forward Class Declarations
  26. /////////////////////////////////////////////////////////////////////////////
  27. class CWorkerThread;
  28. /////////////////////////////////////////////////////////////////////////////
  29. // External Class Declarations
  30. /////////////////////////////////////////////////////////////////////////////
  31. /////////////////////////////////////////////////////////////////////////////
  32. // Include Files
  33. /////////////////////////////////////////////////////////////////////////////
  34. #ifndef _EXCOPER_H_
  35. #include "ExcOper.h" // for CNTException
  36. #endif
  37. /////////////////////////////////////////////////////////////////////////////
  38. // Type Definitions
  39. /////////////////////////////////////////////////////////////////////////////
  40. // Worker thread function codes.
  41. enum
  42. {
  43. WTF_EXIT = -1, // Ask the thread to exit.
  44. WTF_NONE = 0, // No function.
  45. WTF_USER = 1000 // User functions start here.
  46. };
  47. /////////////////////////////////////////////////////////////////////////////
  48. //++
  49. //
  50. // class CWorkerThread
  51. //
  52. // Purpose:
  53. // This class provides a means of calling functions in a worker thread
  54. // and allowing a UI application to still respond to Windows messages.
  55. // The user of this class owns the input and output data pointed to
  56. // by this class.
  57. //
  58. // Inheritance:
  59. // CWorkerThread
  60. //
  61. //--
  62. /////////////////////////////////////////////////////////////////////////////
  63. class CWorkerThread
  64. {
  65. public:
  66. //
  67. // Construction and destruction.
  68. //
  69. // Default constructor
  70. CWorkerThread( void )
  71. : m_hThread( NULL )
  72. , m_hMutex( NULL )
  73. , m_hInputEvent( NULL )
  74. , m_hOutputEvent( NULL)
  75. , m_idThread( 0 )
  76. , m_bThreadExiting( FALSE )
  77. , m_nFunction( WTF_NONE )
  78. , m_pvParam1( NULL )
  79. , m_pvParam2( NULL )
  80. , m_dwOutputStatus( ERROR_SUCCESS )
  81. , m_nte( ERROR_SUCCESS )
  82. , m_pfnOldWndProc( NULL )
  83. , m_hCurrentCursor( NULL )
  84. {
  85. } //*** CWorkerThread()
  86. // Destructor
  87. ~CWorkerThread( void )
  88. {
  89. ATLASSERT( m_nFunction == WTF_NONE );
  90. ATLASSERT( m_pvParam1 == NULL );
  91. ATLASSERT( m_pvParam2 == NULL );
  92. Cleanup();
  93. ATLASSERT( m_bThreadExiting );
  94. } //*** ~CWorkerThread()
  95. // Create the thread
  96. DWORD CreateThread( void );
  97. // Ask the thread to exit
  98. void QuitThread( IN HWND hwnd = NULL )
  99. {
  100. ATLASSERT( ! m_bThreadExiting );
  101. CWaitCursor wc;
  102. CallThreadFunction( hwnd, WTF_EXIT, NULL, NULL );
  103. } //*** QuitThread()
  104. // Call a function supported by the thread
  105. DWORD CallThreadFunction(
  106. IN HWND hwnd,
  107. IN LONG nFunction,
  108. IN OUT PVOID pvParam1 = NULL,
  109. IN OUT PVOID pvParam2 = NULL
  110. );
  111. // Wait for the thread to exit
  112. DWORD WaitForThreadToExit( IN HWND hwnd );
  113. public:
  114. //
  115. // Accessor functions.
  116. //
  117. // Get the thread handle
  118. operator HANDLE( void ) const
  119. {
  120. return m_hThread;
  121. } //*** operator HANDLE()
  122. // Get the thread handle
  123. HANDLE HThreadHandle( void ) const
  124. {
  125. return m_hThread;
  126. } //*** HThreadHandle()
  127. // Get the thread ID
  128. operator DWORD( void ) const
  129. {
  130. return m_idThread;
  131. } //*** operator DWORD()
  132. // Get exception information resulting from a thread function call
  133. CNTException & Nte( void )
  134. {
  135. return m_nte;
  136. } //*** Nte()
  137. // Get exception information resulting from a thread function call
  138. operator CNTException *( void )
  139. {
  140. return &m_nte;
  141. } //*** operator CNTException *()
  142. protected:
  143. //
  144. // Synchronization data.
  145. //
  146. HANDLE m_hThread; // Handle for the thread.
  147. HANDLE m_hMutex; // Handle for the mutex used to call a
  148. // function in the thread.
  149. HANDLE m_hInputEvent; // Handle for the event used by the calling
  150. // thread to signal the worker thread that
  151. // there is work to do.
  152. HANDLE m_hOutputEvent; // Handle for the event used by the worker
  153. // thread to signal the calling thread
  154. // that the work has been completed.
  155. UINT m_idThread; // ID for the thread.
  156. BOOL m_bThreadExiting; // Determine if thread is exiting or not.
  157. //
  158. // Data used as input or produced by the thread.
  159. //
  160. LONG m_nFunction; // ID of the function to perform.
  161. PVOID m_pvParam1; // Parameter 1 with function-specific data.
  162. PVOID m_pvParam2; // Parameter 2 with function-specific data.
  163. DWORD m_dwOutputStatus; // Status returned from the function.
  164. CNTException m_nte; // Exception information from the function.
  165. //
  166. // Data and methods for handling WM_SETCURSOR messages.
  167. //
  168. WNDPROC m_pfnOldWndProc; // Old window procedure for the parent window.
  169. HCURSOR m_hCurrentCursor; // Cursor to display while waiting for thread call to complete.
  170. // Window procedure for subclassing the parent window
  171. static LRESULT WINAPI S_ParentWndProc(
  172. IN HWND hwnd,
  173. IN UINT uMsg,
  174. IN WPARAM wParam,
  175. IN LPARAM lParam
  176. );
  177. //
  178. // Thread worker functions.
  179. //
  180. // Static thread procedure
  181. static UINT __stdcall S_ThreadProc( IN OUT LPVOID pvThis );
  182. // Thread function handler
  183. virtual DWORD ThreadFunctionHandler(
  184. IN LONG nFunction,
  185. IN OUT PVOID pvParam1,
  186. IN OUT PVOID pvParam2
  187. ) = 0;
  188. //
  189. // Helper functions.
  190. //
  191. // Prepare a window to wait for a thread operation
  192. void PrepareWindowToWait( IN HWND hwnd );
  193. // Cleanup a window after waiting for a thread operation
  194. void CleanupWindowAfterWait( IN HWND hwnd );
  195. // Cleanup objects
  196. virtual void Cleanup( void )
  197. {
  198. if ( m_hThread != NULL )
  199. {
  200. if ( ! m_bThreadExiting && (m_nFunction != WTF_EXIT) )
  201. {
  202. QuitThread();
  203. } // if: thread hasn't exited yet
  204. ATLTRACE( _T("CWorkerThread::Cleanup() - Closing thread handle\n") );
  205. CloseHandle( m_hThread );
  206. m_hThread = NULL;
  207. } // if: thread created
  208. if ( m_hMutex != NULL )
  209. {
  210. ATLTRACE( _T("CWorkerThread::Cleanup() - Closing mutex handle\n") );
  211. CloseHandle( m_hMutex );
  212. m_hMutex = NULL;
  213. } // if: mutex created
  214. if ( m_hInputEvent != NULL )
  215. {
  216. ATLTRACE( _T("CWorkerThread::Cleanup() - Closing input event handle\n") );
  217. CloseHandle( m_hInputEvent );
  218. m_hInputEvent = NULL;
  219. } // if: input event created
  220. if ( m_hOutputEvent != NULL )
  221. {
  222. ATLTRACE( _T("CWorkerThread::Cleanup() - Closing output event handle\n") );
  223. CloseHandle( m_hOutputEvent );
  224. m_hOutputEvent = NULL;
  225. } // if: output event created
  226. } //*** Cleanup()
  227. }; // class CWorkerThread
  228. /////////////////////////////////////////////////////////////////////////////
  229. #endif // __WORKTHRD_H_