Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Thread.cxx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright 2015 Pierre Ossman for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifdef WIN32
  19. #include <windows.h>
  20. #else
  21. #include <pthread.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #endif
  25. #include <rdr/Exception.h>
  26. #include <os/Mutex.h>
  27. #include <os/Thread.h>
  28. using namespace os;
  29. Thread::Thread() : running(false), threadId(NULL)
  30. {
  31. mutex = new Mutex;
  32. #ifdef WIN32
  33. threadId = new HANDLE;
  34. #else
  35. threadId = new pthread_t;
  36. #endif
  37. }
  38. Thread::~Thread()
  39. {
  40. #ifdef WIN32
  41. delete (HANDLE*)threadId;
  42. #else
  43. if (isRunning())
  44. pthread_cancel(*(pthread_t*)threadId);
  45. delete (pthread_t*)threadId;
  46. #endif
  47. delete mutex;
  48. }
  49. void Thread::start()
  50. {
  51. AutoMutex a(mutex);
  52. #ifdef WIN32
  53. *(HANDLE*)threadId = CreateThread(NULL, 0, startRoutine, this, 0, NULL);
  54. if (*(HANDLE*)threadId == NULL)
  55. throw rdr::SystemException("Failed to create thread", GetLastError());
  56. #else
  57. int ret;
  58. sigset_t all, old;
  59. // Creating threads from libraries is a bit evil, so mitigate the
  60. // issue by at least avoiding signals on these threads
  61. sigfillset(&all);
  62. ret = pthread_sigmask(SIG_SETMASK, &all, &old);
  63. if (ret != 0)
  64. throw rdr::SystemException("Failed to mask signals", ret);
  65. ret = pthread_create((pthread_t*)threadId, NULL, startRoutine, this);
  66. pthread_sigmask(SIG_SETMASK, &old, NULL);
  67. if (ret != 0)
  68. throw rdr::SystemException("Failed to create thread", ret);
  69. #endif
  70. running = true;
  71. }
  72. void Thread::wait()
  73. {
  74. if (!isRunning())
  75. return;
  76. #ifdef WIN32
  77. DWORD ret;
  78. ret = WaitForSingleObject(*(HANDLE*)threadId, INFINITE);
  79. if (ret != WAIT_OBJECT_0)
  80. throw rdr::SystemException("Failed to join thread", GetLastError());
  81. #else
  82. int ret;
  83. ret = pthread_join(*(pthread_t*)threadId, NULL);
  84. if (ret != 0)
  85. throw rdr::SystemException("Failed to join thread", ret);
  86. #endif
  87. }
  88. bool Thread::isRunning()
  89. {
  90. AutoMutex a(mutex);
  91. return running;
  92. }
  93. size_t Thread::getSystemCPUCount()
  94. {
  95. #ifdef WIN32
  96. SYSTEM_INFO si;
  97. size_t count;
  98. DWORD mask;
  99. GetSystemInfo(&si);
  100. count = 0;
  101. for (mask = si.dwActiveProcessorMask;mask != 0;mask >>= 1) {
  102. if (mask & 0x1)
  103. count++;
  104. }
  105. if (count > si.dwNumberOfProcessors)
  106. count = si.dwNumberOfProcessors;
  107. return count;
  108. #else
  109. long ret;
  110. ret = sysconf(_SC_NPROCESSORS_ONLN);
  111. if (ret == -1)
  112. return 0;
  113. return ret;
  114. #endif
  115. }
  116. #ifdef WIN32
  117. long unsigned __stdcall Thread::startRoutine(void* data)
  118. #else
  119. void* Thread::startRoutine(void* data)
  120. #endif
  121. {
  122. Thread *self;
  123. self = (Thread*)data;
  124. try {
  125. self->worker();
  126. } catch(...) {
  127. }
  128. self->mutex->lock();
  129. self->running = false;
  130. self->mutex->unlock();
  131. return 0;
  132. }