Thread.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // Copyright (c) 2008-2015 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 "../Core/Thread.h"
  23. #ifdef WIN32
  24. #include <windows.h>
  25. #else
  26. #include <pthread.h>
  27. #endif
  28. #include "../DebugNew.h"
  29. namespace Urho3D
  30. {
  31. #ifdef WIN32
  32. DWORD WINAPI ThreadFunctionStatic(void* data)
  33. {
  34. Thread* thread = static_cast<Thread*>(data);
  35. thread->ThreadFunction();
  36. return 0;
  37. }
  38. #else
  39. void* ThreadFunctionStatic(void* data)
  40. {
  41. Thread* thread = static_cast<Thread*>(data);
  42. thread->ThreadFunction();
  43. #ifdef EMSCRIPTEN
  44. // note: emscripten doesn't have this function but doesn't use threading anyway
  45. // so #ifdef it out to prevent linker warnings
  46. pthread_exit((void*)0);
  47. #endif
  48. return 0;
  49. }
  50. #endif
  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. // Check if already running
  64. if (handle_)
  65. return false;
  66. shouldRun_ = true;
  67. #ifdef WIN32
  68. handle_ = CreateThread(0, 0, ThreadFunctionStatic, this, 0, 0);
  69. // #else
  70. #elif !defined(EMSCRIPTEN)
  71. // note: emscripten doesn't have this function but doesn't use
  72. // threading anyway so #ifdef it out to prevent linker warnings
  73. handle_ = new pthread_t;
  74. pthread_attr_t type;
  75. pthread_attr_init(&type);
  76. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  77. pthread_create((pthread_t*)handle_, &type, ThreadFunctionStatic, this);
  78. #endif
  79. return handle_ != 0;
  80. }
  81. void Thread::Stop()
  82. {
  83. // Check if already stopped
  84. if (!handle_)
  85. return;
  86. shouldRun_ = false;
  87. #ifdef WIN32
  88. WaitForSingleObject((HANDLE)handle_, INFINITE);
  89. CloseHandle((HANDLE)handle_);
  90. // #else
  91. #elif !defined(EMSCRIPTEN)
  92. // note: emscripten doesn't have this function but doesn't use
  93. // threading anyway so #ifdef it out to prevent linker warnings
  94. pthread_t* thread = (pthread_t*)handle_;
  95. if (thread)
  96. pthread_join(*thread, 0);
  97. delete thread;
  98. #endif
  99. handle_ = 0;
  100. }
  101. void Thread::SetPriority(int priority)
  102. {
  103. #ifdef WIN32
  104. if (handle_)
  105. SetThreadPriority((HANDLE)handle_, priority);
  106. #endif
  107. #if defined(__linux__) && !defined(ANDROID) && !defined(EMSCRIPTEN)
  108. pthread_t* thread = (pthread_t*)handle_;
  109. if (thread)
  110. pthread_setschedprio(*thread, priority);
  111. #endif
  112. }
  113. void Thread::SetMainThread()
  114. {
  115. mainThreadID = GetCurrentThreadID();
  116. }
  117. ThreadID Thread::GetCurrentThreadID()
  118. {
  119. #ifdef WIN32
  120. return GetCurrentThreadId();
  121. #else
  122. return pthread_self();
  123. #endif
  124. }
  125. bool Thread::IsMainThread()
  126. {
  127. return GetCurrentThreadID() == mainThreadID;
  128. }
  129. }