Source code of Windows XP (NT5)
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.

213 lines
4.9 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corp., 1992 **/
  4. /**********************************************************************/
  5. /*
  6. w32thred.hxx
  7. Class declarations for the WIN32_THREAD class.
  8. <Multi-Line, more detailed synopsis>
  9. FILE HISTORY:
  10. KeithMo 21-Jan-1992 Created.
  11. */
  12. #ifndef _W32THRED_HXX_
  13. #define _W32THRED_HXX_
  14. #include "w32sync.hxx"
  15. //
  16. // The enumerator contains all possible thread states.
  17. //
  18. typedef enum _THREAD_STATE
  19. {
  20. Embryonic, // thread has not been started yet
  21. Starting, // thread is in the process of starting
  22. Running, // thread is running normally
  23. Stopping, // thread is shutting down
  24. Dead, // thread has stopped
  25. Terminated // thread has been forcibly terminated
  26. } THREAD_STATE;
  27. /*************************************************************************
  28. NAME: WIN32_THREAD
  29. SYNOPSIS:
  30. INTERFACE: WIN32_THREAD - Class constructor.
  31. ~WIN32_THREAD - Class destructor.
  32. PARENT: BASE
  33. USES:
  34. CAVEATS:
  35. NOTES:
  36. If pszLoadLibrary is specified, the thread will first LoadLibrary
  37. the library before beginning processing, then FreeLibraryAndExitThread
  38. after processing. Use this parameter if you are concerned that
  39. the thread might not terminate before the caller calls FreeLibrary;
  40. the extra LoadLibrary ensures that the library reference count will
  41. be >0 until the thread is finished.
  42. Do not call Terminate() if pszLoadLibrary is specified. The thread
  43. must exit via Exit(), DeleteAndExit(), or by Main() and PostMain()
  44. completing normally, if the library reference count is to remain
  45. correct.
  46. Do not delete the WIN32_THREAD object without deleting the thread
  47. if pszLoadLibrary is specified. The destructor calls through to
  48. Terminate().
  49. HISTORY:
  50. KeithMo 21-Jan-1992 Created.
  51. JonN 20-May-1993 Changed DeleteAndTerminate to DeleteAndExit
  52. JonN 30-Nov-1993 Added pszLoadLibrary
  53. **************************************************************************/
  54. DLL_CLASS WIN32_THREAD : public WIN32_SYNC_BASE
  55. {
  56. private:
  57. //
  58. // The thread ID & exit code for this thread.
  59. //
  60. UINT _idThread;
  61. UINT _nExitCode;
  62. //
  63. // These are the name and handle of the library to load and then free.
  64. //
  65. NLS_STR _nlsLoadLibrary;
  66. HINSTANCE _hLoadLibrary;
  67. //
  68. // This value contains the current state of the thread.
  69. //
  70. THREAD_STATE _state;
  71. //
  72. // We'll give a pointer to this method to the CreateThread API.
  73. //
  74. static DWORD StartThread( LPVOID lpParam );
  75. //
  76. // These private methods query/set the thread state.
  77. //
  78. THREAD_STATE QueryState( VOID ) const
  79. { return _state; }
  80. VOID SetState( THREAD_STATE NewState )
  81. { _state = NewState; }
  82. protected:
  83. //
  84. // This method will exit the thread. Since this method must
  85. // *never* be called by another thread, it is protected.
  86. //
  87. VOID Exit( UINT nExitCode );
  88. //
  89. // DeleteAndExit is like Exit() except it deletes "this".
  90. //
  91. // Warning: The thread must be allocated on the heap.
  92. // Execution should never be returned.
  93. //
  94. VOID DeleteAndExit( UINT nExitCode ) ;
  95. //
  96. // These methods are invoked by StartThread. These should
  97. // be replaced by private methods in the derived subclasses.
  98. //
  99. virtual APIERR PreMain( VOID );
  100. virtual APIERR Main( VOID );
  101. virtual APIERR PostMain( VOID );
  102. public:
  103. //
  104. // Usual constructor/destructor goodies.
  105. //
  106. WIN32_THREAD( BOOL fSuspended = FALSE,
  107. UINT cbStack = 0,
  108. const TCHAR * pszLoadLibrary = NULL );
  109. virtual ~WIN32_THREAD( VOID );
  110. //
  111. // Query the thread ID & exit code.
  112. //
  113. UINT QueryID( VOID ) const
  114. { return _idThread; }
  115. UINT QueryExitCode( VOID ) const
  116. { return _nExitCode; }
  117. //
  118. // Is the thread running?
  119. //
  120. BOOL IsRunning( VOID ) const
  121. { return QueryState() == Running; }
  122. BOOL IsRunnable( VOID ) const
  123. { return ( QueryState() == Starting ) ||
  124. ( QueryState() == Running ) ||
  125. ( QueryState() == Stopping ); }
  126. //
  127. // Set/Query thread priority.
  128. //
  129. APIERR SetPriority( INT nPriority );
  130. INT QueryPriority( VOID );
  131. //
  132. // Suspend/Resume thread.
  133. //
  134. APIERR Suspend( VOID );
  135. APIERR Resume( VOID );
  136. //
  137. // Put the thread to sleep.
  138. //
  139. VOID Sleep( UINT cMilliseconds );
  140. //
  141. // Terminate the thread.
  142. //
  143. APIERR Terminate( UINT nExitCode );
  144. }; // class WIN32_THREAD
  145. #endif // _W32THRED_HXX_