Thread.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Thread.h"
  24. #include "../IO/Log.h"
  25. #ifdef _WIN32
  26. #include <windows.h>
  27. #else
  28. #include <pthread.h>
  29. #endif
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. #ifdef ATOMIC_THREADING
  34. #ifdef _WIN32
  35. static DWORD WINAPI ThreadFunctionStatic(void* data)
  36. {
  37. Thread* thread = static_cast<Thread*>(data);
  38. thread->ThreadFunction();
  39. return 0;
  40. }
  41. #else
  42. static void* ThreadFunctionStatic(void* data)
  43. {
  44. Thread* thread = static_cast<Thread*>(data);
  45. thread->ThreadFunction();
  46. pthread_exit((void*)0);
  47. return 0;
  48. }
  49. #endif
  50. #endif // ATOMIC_THREADING
  51. ThreadID Thread::mainThreadID;
  52. Thread::Thread() :
  53. handle_(0),
  54. shouldRun_(false)
  55. {
  56. }
  57. Thread::~Thread()
  58. {
  59. Stop();
  60. }
  61. bool Thread::Run()
  62. {
  63. #ifdef ATOMIC_THREADING
  64. // Check if already running
  65. if (handle_)
  66. return false;
  67. shouldRun_ = true;
  68. #ifdef _WIN32
  69. handle_ = CreateThread(0, 0, ThreadFunctionStatic, this, 0, 0);
  70. #else
  71. handle_ = new pthread_t;
  72. pthread_attr_t type;
  73. pthread_attr_init(&type);
  74. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  75. pthread_create((pthread_t*)handle_, &type, ThreadFunctionStatic, this);
  76. #endif
  77. return handle_ != 0;
  78. #else
  79. return false;
  80. #endif // ATOMIC_THREADING
  81. }
  82. void Thread::Stop()
  83. {
  84. #ifdef ATOMIC_THREADING
  85. // Check if already stopped
  86. if (!handle_)
  87. return;
  88. shouldRun_ = false;
  89. #ifdef _WIN32
  90. WaitForSingleObject((HANDLE)handle_, INFINITE);
  91. CloseHandle((HANDLE)handle_);
  92. #else
  93. pthread_t* thread = (pthread_t*)handle_;
  94. if (thread)
  95. pthread_join(*thread, 0);
  96. delete thread;
  97. #endif
  98. handle_ = 0;
  99. #endif // ATOMIC_THREADING
  100. }
  101. void Thread::SetPriority(int priority)
  102. {
  103. #ifdef ATOMIC_THREADING
  104. #ifdef _WIN32
  105. if (handle_)
  106. SetThreadPriority((HANDLE)handle_, priority);
  107. #elif defined(__linux__) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
  108. pthread_t* thread = (pthread_t*)handle_;
  109. if (thread)
  110. pthread_setschedprio(*thread, priority);
  111. #endif
  112. #endif // ATOMIC_THREADING
  113. }
  114. void Thread::SetMainThread()
  115. {
  116. mainThreadID = GetCurrentThreadID();
  117. }
  118. ThreadID Thread::GetCurrentThreadID()
  119. {
  120. #ifdef ATOMIC_THREADING
  121. #ifdef _WIN32
  122. return GetCurrentThreadId();
  123. #else
  124. return pthread_self();
  125. #endif
  126. #else
  127. return ThreadID();
  128. #endif // ATOMIC_THREADING
  129. }
  130. bool Thread::IsMainThread()
  131. {
  132. #ifdef ATOMIC_THREADING
  133. return GetCurrentThreadID() == mainThreadID;
  134. #else
  135. return true;
  136. #endif // ATOMIC_THREADING
  137. }
  138. // ATOMIC BEGIN
  139. void Thread::Kill()
  140. {
  141. #ifdef ATOMIC_THREADING
  142. #ifdef ATOMIC_PLATFORM_WINDOWS
  143. if (handle_)
  144. {
  145. DWORD exitCode = EXIT_SUCCESS;
  146. TerminateThread((HANDLE)handle_, exitCode);
  147. }
  148. #elif defined(ATOMIC_PLATFORM_OSX)
  149. ATOMIC_LOGINFO("Thread::Kill() - macOS implementation required");
  150. #elif defined(ATOMIC_PLATFORM_LINUX)
  151. pthread_t* thread = (pthread_t*)handle_;
  152. if (thread)
  153. {
  154. pthread_cancel(*thread);
  155. }
  156. #endif
  157. #endif // ATOMIC_THREADING
  158. }
  159. // ATOMIC END
  160. }