Thread.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "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 Atomic
  31. {
  32. #ifdef WIN32
  33. DWORD WINAPI ThreadFunctionStatic(void* data)
  34. {
  35. Thread* thread = static_cast<Thread*>(data);
  36. thread->ThreadFunction();
  37. return 0;
  38. }
  39. #else
  40. void* ThreadFunctionStatic(void* data)
  41. {
  42. Thread* thread = static_cast<Thread*>(data);
  43. thread->ThreadFunction();
  44. #ifdef EMSCRIPTEN
  45. // note: emscripten doesn't have this function but doesn't use threading anyway
  46. // so #ifdef it out to prevent linker warnings
  47. pthread_exit((void*)0);
  48. #endif
  49. return 0;
  50. }
  51. #endif
  52. ThreadID Thread::mainThreadID;
  53. Thread::Thread() :
  54. handle_(0),
  55. shouldRun_(false)
  56. {
  57. }
  58. Thread::~Thread()
  59. {
  60. Stop();
  61. }
  62. bool Thread::Run()
  63. {
  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. #elif !defined(EMSCRIPTEN)
  72. // note: emscripten doesn't have this function but doesn't use
  73. // threading anyway so #ifdef it out to prevent linker warnings
  74. handle_ = new pthread_t;
  75. pthread_attr_t type;
  76. pthread_attr_init(&type);
  77. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  78. pthread_create((pthread_t*)handle_, &type, ThreadFunctionStatic, this);
  79. #endif
  80. return handle_ != 0;
  81. }
  82. void Thread::Stop()
  83. {
  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. #elif !defined(EMSCRIPTEN)
  93. // note: emscripten doesn't have this function but doesn't use
  94. // threading anyway so #ifdef it out to prevent linker warnings
  95. pthread_t* thread = (pthread_t*)handle_;
  96. if (thread)
  97. pthread_join(*thread, 0);
  98. delete thread;
  99. #endif
  100. handle_ = 0;
  101. }
  102. void Thread::SetPriority(int priority)
  103. {
  104. #ifdef WIN32
  105. if (handle_)
  106. SetThreadPriority((HANDLE)handle_, priority);
  107. #endif
  108. #if defined(__linux__) && !defined(ANDROID) && !defined(EMSCRIPTEN)
  109. pthread_t* thread = (pthread_t*)handle_;
  110. if (thread)
  111. pthread_setschedprio(*thread, priority);
  112. #endif
  113. }
  114. void Thread::SetMainThread()
  115. {
  116. mainThreadID = GetCurrentThreadID();
  117. }
  118. ThreadID Thread::GetCurrentThreadID()
  119. {
  120. #ifdef WIN32
  121. return GetCurrentThreadId();
  122. #else
  123. return pthread_self();
  124. #endif
  125. }
  126. bool Thread::IsMainThread()
  127. {
  128. return GetCurrentThreadID() == mainThreadID;
  129. }
  130. }