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.

128 lines
3.0 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. * v-stebe 29Jan2002 Removed call to TerminateThread in method TerminateThreadNow
  20. * to resolve prefast error.
  21. */
  22. #include "cdefine.h"
  23. #include "_defs.h"
  24. #include <windows.h>
  25. #include <process.h>
  26. #include "apc.h"
  27. #include "thread.h"
  28. #include "apcsemnt.h"
  29. #include "mutexnt.h"
  30. #include "w32utils.h"
  31. #include "err.h"
  32. const int STACK_SIZE = 8192;
  33. DWORD BootstrapThread(LPDWORD PThread)
  34. {
  35. Thread *thread = (Thread *)PThread;
  36. thread->RunMain();
  37. return ((DWORD) ErrNO_ERROR);
  38. }
  39. Thread::~Thread()
  40. {
  41. if (theObject)
  42. {
  43. delete theObject;
  44. theObject = NULL;
  45. }
  46. CloseHandle (theThreadHandle);
  47. }
  48. INT Thread::Start ()
  49. {
  50. unsigned int thread_id;
  51. INT err;
  52. // Assume no error
  53. INT errReturn = ErrNO_ERROR;
  54. theThreadHandle = (HANDLE)_beginthreadex((VOID*)NULL, STACK_SIZE,
  55. (unsigned int (__stdcall *)(void *)) BootstrapThread,
  56. (VOID*)this, 0, &thread_id);
  57. // Do we have a valid thread?
  58. if (!theThreadHandle)
  59. {
  60. // Define the error
  61. errReturn = ErrTHREAD_CREATE_FAILED;
  62. }
  63. err = UtilSelectProcessor(theThreadHandle);
  64. return (errReturn);
  65. }
  66. VOID Thread::RunMain ()
  67. {
  68. if (theObject != (PThreadable) NULL) {
  69. // Run the main member function of the threadable object
  70. theObject->ThreadMain();
  71. }
  72. // Kill the thread
  73. _endthreadex(0);
  74. }
  75. VOID Thread::TerminateThreadNow ()
  76. {
  77. DWORD exit_code;
  78. GetExitCodeThread(theThreadHandle,&exit_code);
  79. // Terminate the thread
  80. if (exit_code == STILL_ACTIVE) {
  81. // SRB: Changed from TerminateThread(theThreadHandle, 0L)
  82. // Call exit on the threadable
  83. if (theObject) {
  84. theObject->Exit();
  85. }
  86. }
  87. // Make sure
  88. WaitForSingleObject (theThreadHandle, INFINITE);
  89. // Close its handle
  90. CloseHandle (theThreadHandle);
  91. // Delete the Threadable object
  92. if (theObject != (PThreadable) NULL)
  93. {
  94. delete theObject;
  95. theObject = (PThreadable) NULL;
  96. }
  97. }
  98. PThreadable Thread::GetThreadableObject()
  99. {
  100. return theObject;
  101. }