Thread.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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. #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. pthread_exit((void*)0);
  44. return 0;
  45. }
  46. #endif
  47. Thread::Thread() :
  48. handle_(0),
  49. shouldRun_(false)
  50. {
  51. }
  52. Thread::~Thread()
  53. {
  54. Stop();
  55. }
  56. bool Thread::Start()
  57. {
  58. // Check if already running
  59. if (handle_)
  60. return false;
  61. shouldRun_ = true;
  62. #ifdef _WIN32
  63. handle_ = CreateThread(0, 0, ThreadFunctionStatic, this, 0, 0);
  64. #else
  65. handle_ = new pthread_t;
  66. pthread_attr_t type;
  67. pthread_attr_init(&type);
  68. pthread_attr_setdetachstate(&type, PTHREAD_CREATE_JOINABLE);
  69. pthread_create((pthread_t*)handle_, &type, ThreadFunctionStatic, this);
  70. #endif
  71. return handle_ != 0;
  72. }
  73. void Thread::Stop()
  74. {
  75. shouldRun_ = false;
  76. #ifdef _WIN32
  77. WaitForSingleObject((HANDLE)handle_, INFINITE);
  78. CloseHandle((HANDLE)handle_);
  79. #else
  80. pthread_t* thread = (pthread_t*)handle_;
  81. if (thread)
  82. {
  83. pthread_join(thread, 0);
  84. pthread_destroy(thread);
  85. }
  86. delete thread;
  87. #endif
  88. handle_ = 0;
  89. }
  90. void Thread::SetPriority(int priority)
  91. {
  92. #ifdef _WIN32
  93. if (handle_)
  94. SetThreadPriority((HANDLE)handle_, priority);
  95. #endif
  96. /// \todo Implement on pthreads
  97. }