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.

120 lines
2.6 KiB

  1. /*
  2. *
  3. * NOTES:
  4. *
  5. * REVISIONS:
  6. * ane09DEC92: added code for rundown of threads
  7. * ane30Dec92: increase stack size for threads
  8. * TSC18May93: Removed os2 specific code, split off Threadable into ntthrdbl.cxx
  9. * TSC20May93: Added error logging
  10. * TSC28May93: Added define for theObject. See ntthrdbl.cxx for details
  11. * pcy08Oct93: Removed redefinition of THREAD_EXIT_TIMEOUT
  12. * cad24Nov93: defensive checks for deleteions
  13. * pam02Apr96: changed CreateThread/TerminateThread to _beginthreadex and _endthreadex
  14. * ash25Jun96: Removed dead code and added method to return the threadable object
  15. * pam15Jul96: Changed utils.h to w32utils.h
  16. * ash08Aug96: Added "new" handling capability to each thread
  17. * dma17Apr98: Moved CloseHandle call to destructor to properly close threads.
  18. */
  19. #include "cdefine.h"
  20. #include "_defs.h"
  21. #include <windows.h>
  22. #include <process.h>
  23. #include "apc.h"
  24. #include "thread.h"
  25. #include "apcsemnt.h"
  26. #include "mutexnt.h"
  27. #include "w32utils.h"
  28. #include "err.h"
  29. const int STACK_SIZE = 8192;
  30. DWORD BootstrapThread(LPDWORD PThread)
  31. {
  32. Thread *thread = (Thread *)PThread;
  33. thread->RunMain();
  34. return ((DWORD) ErrNO_ERROR);
  35. }
  36. Thread::~Thread()
  37. {
  38. if (theObject)
  39. {
  40. delete theObject;
  41. theObject = NULL;
  42. }
  43. CloseHandle (theThreadHandle);
  44. }
  45. INT Thread::Start ()
  46. {
  47. unsigned int thread_id;
  48. INT err;
  49. // Assume no error
  50. INT errReturn = ErrNO_ERROR;
  51. theThreadHandle = (HANDLE)_beginthreadex((VOID*)NULL, STACK_SIZE,
  52. (unsigned int (__stdcall *)(void *)) BootstrapThread,
  53. (VOID*)this, 0, &thread_id);
  54. // Do we have a valid thread?
  55. if (!theThreadHandle)
  56. {
  57. // Define the error
  58. errReturn = ErrTHREAD_CREATE_FAILED;
  59. }
  60. err = UtilSelectProcessor(theThreadHandle);
  61. return (errReturn);
  62. }
  63. VOID Thread::RunMain ()
  64. {
  65. if (theObject != (PThreadable) NULL) {
  66. // Run the main member function of the threadable object
  67. theObject->ThreadMain();
  68. }
  69. // Kill the thread
  70. _endthreadex(0);
  71. }
  72. VOID Thread::TerminateThreadNow ()
  73. {
  74. DWORD exit_code;
  75. GetExitCodeThread(theThreadHandle,&exit_code);
  76. // Terminate the thread
  77. if (exit_code == STILL_ACTIVE)
  78. TerminateThread (theThreadHandle, 0L);
  79. // Make sure
  80. WaitForSingleObject (theThreadHandle, INFINITE);
  81. // Close its handle
  82. CloseHandle (theThreadHandle);
  83. // Delete the Threadable object
  84. if (theObject != (PThreadable) NULL)
  85. {
  86. delete theObject;
  87. theObject = (PThreadable) NULL;
  88. }
  89. }
  90. PThreadable Thread::GetThreadableObject()
  91. {
  92. return theObject;
  93. }