Thread.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Thread.h"
  25. #ifdef WIN32
  26. #include <windows.h>
  27. #else
  28. #include <pthread.h>
  29. #endif
  30. #include "DebugNew.h"
  31. namespace Urho3D
  32. {
  33. #ifdef WIN32
  34. DWORD WINAPI ThreadFunctionStatic(void* data)
  35. {
  36. Thread* thread = static_cast<Thread*>(data);
  37. thread->ThreadFunction();
  38. return 0;
  39. }
  40. #else
  41. void* ThreadFunctionStatic(void* data)
  42. {
  43. Thread* thread = static_cast<Thread*>(data);
  44. thread->ThreadFunction();
  45. pthread_exit((void*)0);
  46. return 0;
  47. }
  48. #endif
  49. Thread::Thread() :
  50. handle_(0),
  51. shouldRun_(false)
  52. {
  53. }
  54. Thread::~Thread()
  55. {
  56. Stop();
  57. }
  58. bool Thread::Start()
  59. {
  60. // Check if already running
  61. if (handle_)
  62. return false;
  63. shouldRun_ = true;
  64. #ifdef WIN32
  65. handle_ = CreateThread(0, 0, ThreadFunctionStatic, this, 0, 0);
  66. #else
  67. handle_ = new pthread_t;
  68. pthread_attr_t type;
  69. pthread_attr_init(&type);
  70. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  71. pthread_create((pthread_t*)handle_, &type, ThreadFunctionStatic, this);
  72. #endif
  73. return handle_ != 0;
  74. }
  75. void Thread::Stop()
  76. {
  77. // Check if already stopped
  78. if (!handle_)
  79. return;
  80. shouldRun_ = false;
  81. #ifdef WIN32
  82. WaitForSingleObject((HANDLE)handle_, INFINITE);
  83. CloseHandle((HANDLE)handle_);
  84. #else
  85. pthread_t* thread = (pthread_t*)handle_;
  86. if (thread)
  87. pthread_join(*thread, 0);
  88. delete thread;
  89. #endif
  90. handle_ = 0;
  91. }
  92. void Thread::SetPriority(int priority)
  93. {
  94. #ifdef WIN32
  95. if (handle_)
  96. SetThreadPriority((HANDLE)handle_, priority);
  97. #endif
  98. #if defined(__linux__) && !defined(ANDROID)
  99. pthread_t* thread = (pthread_t*)handle_;
  100. if (thread)
  101. pthread_setschedprio(*thread, priority);
  102. #endif
  103. }
  104. }