Thread.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifdef _WIN32
  25. #include <windows.h>
  26. #else
  27. #include <pthread.h>
  28. #endif
  29. #include "../DebugNew.h"
  30. namespace Urho3D
  31. {
  32. #ifdef URHO3D_THREADING
  33. #ifdef _WIN32
  34. static DWORD WINAPI ThreadFunctionStatic(void* data)
  35. {
  36. Thread* thread = static_cast<Thread*>(data);
  37. thread->ThreadFunction();
  38. return 0;
  39. }
  40. #else
  41. static void* ThreadFunctionStatic(void* data)
  42. {
  43. auto* thread = static_cast<Thread*>(data);
  44. thread->ThreadFunction();
  45. pthread_exit((void*)0);
  46. return 0;
  47. }
  48. #endif
  49. #endif // URHO3D_THREADING
  50. ThreadID Thread::mainThreadID;
  51. Thread::Thread() :
  52. handle_(nullptr),
  53. shouldRun_(false)
  54. {
  55. }
  56. Thread::~Thread()
  57. {
  58. Stop();
  59. }
  60. bool Thread::Run()
  61. {
  62. #ifdef URHO3D_THREADING
  63. // Check if already running
  64. if (handle_)
  65. return false;
  66. shouldRun_ = true;
  67. #ifdef _WIN32
  68. handle_ = CreateThread(nullptr, 0, ThreadFunctionStatic, this, 0, nullptr);
  69. #else
  70. handle_ = new pthread_t;
  71. pthread_attr_t type;
  72. pthread_attr_init(&type);
  73. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  74. pthread_create((pthread_t*)handle_, &type, ThreadFunctionStatic, this);
  75. #endif
  76. return handle_ != nullptr;
  77. #else
  78. return false;
  79. #endif // URHO3D_THREADING
  80. }
  81. void Thread::Stop()
  82. {
  83. #ifdef URHO3D_THREADING
  84. // Check if already stopped
  85. if (!handle_)
  86. return;
  87. shouldRun_ = false;
  88. #ifdef _WIN32
  89. WaitForSingleObject((HANDLE)handle_, INFINITE);
  90. CloseHandle((HANDLE)handle_);
  91. #else
  92. auto* thread = (pthread_t*)handle_;
  93. if (thread)
  94. pthread_join(*thread, 0);
  95. delete thread;
  96. #endif
  97. handle_ = nullptr;
  98. #endif // URHO3D_THREADING
  99. }
  100. void Thread::SetPriority(int priority)
  101. {
  102. #ifdef URHO3D_THREADING
  103. #ifdef _WIN32
  104. if (handle_)
  105. SetThreadPriority((HANDLE)handle_, priority);
  106. #elif defined(__linux__) && !defined(__ANDROID__) && !defined(__EMSCRIPTEN__)
  107. auto* thread = (pthread_t*)handle_;
  108. if (thread)
  109. pthread_setschedprio(*thread, priority);
  110. #endif
  111. #endif // URHO3D_THREADING
  112. }
  113. void Thread::SetMainThread()
  114. {
  115. mainThreadID = GetCurrentThreadID();
  116. }
  117. ThreadID Thread::GetCurrentThreadID()
  118. {
  119. #ifdef URHO3D_THREADING
  120. #ifdef _WIN32
  121. return GetCurrentThreadId();
  122. #else
  123. return pthread_self();
  124. #endif
  125. #else
  126. return ThreadID();
  127. #endif // URHO3D_THREADING
  128. }
  129. bool Thread::IsMainThread()
  130. {
  131. #ifdef URHO3D_THREADING
  132. return GetCurrentThreadID() == mainThreadID;
  133. #else
  134. return true;
  135. #endif // URHO3D_THREADING
  136. }
  137. }