thread.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _WIN32_WINNT 0x0400
  19. #include "thread.h"
  20. #include "except.h"
  21. #include "wwdebug.h"
  22. #include <process.h>
  23. #include <windows.h>
  24. #pragma warning ( push )
  25. #pragma warning ( disable : 4201 )
  26. #include "systimer.h"
  27. #pragma warning ( pop )
  28. ThreadClass::ThreadClass(const char *thread_name, ExceptionHandlerType exception_handler) : handle(0), running(false), thread_priority(0)
  29. {
  30. if (thread_name) {
  31. assert(strlen(thread_name) < sizeof(ThreadName) - 1);
  32. strcpy(ThreadName, thread_name);
  33. } else {
  34. strcpy(ThreadName, "No name");;
  35. }
  36. ExceptionHandler = exception_handler;
  37. }
  38. ThreadClass::~ThreadClass()
  39. {
  40. Stop();
  41. }
  42. void __cdecl ThreadClass::Internal_Thread_Function(void* params)
  43. {
  44. ThreadClass* tc=reinterpret_cast<ThreadClass*>(params);
  45. tc->running=true;
  46. tc->ThreadID = GetCurrentThreadId();
  47. #ifdef _WIN32
  48. Register_Thread_ID(tc->ThreadID, tc->ThreadName);
  49. if (tc->ExceptionHandler != NULL) {
  50. __try {
  51. tc->Thread_Function();
  52. } __except(tc->ExceptionHandler(GetExceptionCode(), GetExceptionInformation())) {};
  53. } else {
  54. tc->Thread_Function();
  55. }
  56. #else //_WIN32
  57. tc->Thread_Function();
  58. #endif //_WIN32
  59. #ifdef _WIN32
  60. Unregister_Thread_ID(tc->ThreadID, tc->ThreadName);
  61. #endif // _WIN32
  62. tc->handle=0;
  63. tc->ThreadID = 0;
  64. }
  65. void ThreadClass::Execute()
  66. {
  67. WWASSERT(!handle); // Only one thread at a time!
  68. #ifdef _UNIX
  69. // assert(0);
  70. return;
  71. #else
  72. handle=_beginthread(&Internal_Thread_Function,0,this);
  73. SetThreadPriority((HANDLE)handle,THREAD_PRIORITY_NORMAL+thread_priority);
  74. WWDEBUG_SAY(("ThreadClass::Execute: Started thread %s, thread ID is %X\n", ThreadName, handle));
  75. #endif
  76. }
  77. void ThreadClass::Set_Priority(int priority)
  78. {
  79. #ifdef _UNIX
  80. // assert(0);
  81. return;
  82. #else
  83. thread_priority=priority;
  84. if (handle) SetThreadPriority((HANDLE)handle,THREAD_PRIORITY_NORMAL+thread_priority);
  85. #endif
  86. }
  87. void ThreadClass::Stop(unsigned ms)
  88. {
  89. #ifdef _UNIX
  90. // assert(0);
  91. return;
  92. #else
  93. running=false;
  94. unsigned time=TIMEGETTIME();
  95. while (handle) {
  96. if ((TIMEGETTIME()-time)>ms) {
  97. int res=TerminateThread((HANDLE)handle,0);
  98. WWASSERT(res); // Thread still not killed!
  99. handle=0;
  100. }
  101. Sleep(0);
  102. }
  103. #endif
  104. }
  105. void ThreadClass::Sleep_Ms(unsigned ms)
  106. {
  107. Sleep(ms);
  108. }
  109. #ifndef _UNIX
  110. HANDLE test_event = ::CreateEvent (NULL, FALSE, FALSE, "");
  111. #endif
  112. void ThreadClass::Switch_Thread()
  113. {
  114. #ifdef _UNIX
  115. return;
  116. #else
  117. // ::SwitchToThread ();
  118. ::WaitForSingleObject (test_event, 1);
  119. // Sleep(1); // Note! Parameter can not be 0 (or the thread switch doesn't occur)
  120. #endif
  121. }
  122. // Return calling thread's unique thread id
  123. unsigned ThreadClass::_Get_Current_Thread_ID()
  124. {
  125. #ifdef _UNIX
  126. return 0;
  127. #else
  128. return GetCurrentThreadId();
  129. #endif
  130. }
  131. bool ThreadClass::Is_Running()
  132. {
  133. return !!handle;
  134. }