thread.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. ** Command & Conquer Generals(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 "wwdebug.h"
  21. #include <process.h>
  22. #include <windows.h>
  23. #pragma warning ( push )
  24. #pragma warning ( disable : 4201 )
  25. #include <mmsystem.h>
  26. #pragma warning ( pop )
  27. ThreadClass::ThreadClass() : handle(0), running(false), thread_priority(0)
  28. {
  29. }
  30. ThreadClass::~ThreadClass()
  31. {
  32. Stop();
  33. }
  34. void __cdecl ThreadClass::Internal_Thread_Function(void* params)
  35. {
  36. ThreadClass* tc=reinterpret_cast<ThreadClass*>(params);
  37. tc->running=true;
  38. tc->Thread_Function();
  39. tc->handle=0;
  40. }
  41. void ThreadClass::Execute()
  42. {
  43. WWASSERT(!handle); // Only one thread at a time!
  44. #ifdef _UNIX
  45. // assert(0);
  46. return;
  47. #else
  48. handle=_beginthread(&Internal_Thread_Function,0,this);
  49. SetThreadPriority((HANDLE)handle,THREAD_PRIORITY_NORMAL+thread_priority);
  50. #endif
  51. }
  52. void ThreadClass::Set_Priority(int priority)
  53. {
  54. #ifdef _UNIX
  55. // assert(0);
  56. return;
  57. #else
  58. thread_priority=priority;
  59. if (handle) SetThreadPriority((HANDLE)handle,THREAD_PRIORITY_NORMAL+thread_priority);
  60. #endif
  61. }
  62. void ThreadClass::Stop(unsigned ms)
  63. {
  64. #ifdef _UNIX
  65. // assert(0);
  66. return;
  67. #else
  68. running=false;
  69. unsigned time=timeGetTime();
  70. while (handle) {
  71. if ((timeGetTime()-time)>ms) {
  72. int res=TerminateThread((HANDLE)handle,0);
  73. res; // just to silence compiler warnings
  74. WWASSERT(res); // Thread still not killed!
  75. handle=0;
  76. }
  77. Sleep(0);
  78. }
  79. #endif
  80. }
  81. void ThreadClass::Sleep_Ms(unsigned ms)
  82. {
  83. Sleep(ms);
  84. }
  85. #ifndef _UNIX
  86. HANDLE test_event = ::CreateEvent (NULL, FALSE, FALSE, "");
  87. #endif
  88. void ThreadClass::Switch_Thread()
  89. {
  90. #ifdef _UNIX
  91. return;
  92. #else
  93. // ::SwitchToThread ();
  94. ::WaitForSingleObject (test_event, 1);
  95. // Sleep(1); // Note! Parameter can not be 0 (or the thread switch doesn't occur)
  96. #endif
  97. }
  98. // Return calling thread's unique thread id
  99. unsigned ThreadClass::_Get_Current_Thread_ID()
  100. {
  101. #ifdef _UNIX
  102. return 0;
  103. #else
  104. return GetCurrentThreadId();
  105. #endif
  106. }
  107. bool ThreadClass::Is_Running()
  108. {
  109. return !!handle;
  110. }