Thread.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "Thread.hpp"
  28. #if defined(__APPLE__) || defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <pthread.h>
  33. #include <unistd.h>
  34. #include <stdexcept>
  35. extern "C" {
  36. static void *__m_thread_main(void *ptr)
  37. {
  38. ((ZeroTier::Thread *)ptr)->__intl_run();
  39. return (void *)0;
  40. }
  41. }
  42. namespace ZeroTier {
  43. Thread::Thread() :
  44. suicidalThread(false),
  45. _impl(malloc(sizeof(pthread_t))),
  46. _running()
  47. {
  48. memset(_impl,0,sizeof(pthread_t));
  49. }
  50. Thread::~Thread()
  51. {
  52. free(_impl);
  53. }
  54. void Thread::start()
  55. {
  56. if (!*_running) {
  57. ++_running;
  58. pthread_create((pthread_t *)_impl,(const pthread_attr_t *)0,&__m_thread_main,(void *)this);
  59. }
  60. }
  61. void Thread::join()
  62. {
  63. void *tmp;
  64. if (*_running)
  65. pthread_join(*((pthread_t *)_impl),&tmp);
  66. }
  67. void Thread::sleep(unsigned long ms)
  68. {
  69. usleep(ms);
  70. }
  71. void Thread::__intl_run()
  72. {
  73. for(;;) {
  74. _notInit = false;
  75. this->main();
  76. if (suicidalThread) {
  77. delete this;
  78. return;
  79. }
  80. if (_notInit) // UGLY ASS HACK: see main()
  81. usleep(50);
  82. else break;
  83. }
  84. --_running;
  85. }
  86. void Thread::main()
  87. throw()
  88. {
  89. _notInit = true; // UGLY ASS HACK: retry if subclass has not defined virtual function pointer yet
  90. }
  91. } // namespace ZeroTier
  92. #endif
  93. #ifdef _WIN32
  94. #include <Windows.h>
  95. #include <stdio.h>
  96. #include <stdlib.h>
  97. DWORD WINAPI __m_thread_main(LPVOID lpParam)
  98. {
  99. ((ZeroTier::Thread *)lpParam)->__intl_run();
  100. return 0;
  101. }
  102. struct __m_thread_info
  103. {
  104. HANDLE threadHandle;
  105. DWORD threadId;
  106. bool started;
  107. };
  108. namespace ZeroTier {
  109. Thread::Thread() :
  110. suicidalThread(false),
  111. _impl(malloc(sizeof(__m_thread_info))),
  112. _running()
  113. {
  114. memset(_impl,0,sizeof(__m_thread_info));
  115. }
  116. Thread::~Thread()
  117. {
  118. if (((__m_thread_info *)_impl)->started)
  119. CloseHandle(((__m_thread_info *)_impl)->threadHandle);
  120. free(_impl);
  121. }
  122. void Thread::start()
  123. {
  124. if (!*_running) {
  125. ++_running;
  126. if ((((__m_thread_info *)_impl)->threadHandle = CreateThread(NULL,0,__m_thread_main,this,0,&(((__m_thread_info *)_impl)->threadId))) != NULL) {
  127. ((__m_thread_info *)_impl)->started = true;
  128. }
  129. }
  130. }
  131. void Thread::join()
  132. {
  133. if (*_running)
  134. WaitForSingleObject(((__m_thread_info *)_impl)->threadHandle,INFINITE);
  135. }
  136. void Thread::__intl_run()
  137. {
  138. for(;;) {
  139. _notInit = false;
  140. this->main();
  141. if (suicidalThread) {
  142. delete this;
  143. return;
  144. }
  145. if (_notInit)
  146. Thread::sleep(50);
  147. else break;
  148. }
  149. --_running;
  150. }
  151. void Thread::main()
  152. throw()
  153. {
  154. _notInit = true; // HACK: retry if subclass has not defined virtual function pointer yet
  155. }
  156. struct _Thread_RunInBackgroundData
  157. {
  158. void (*func)(void *);
  159. void *ptr;
  160. HANDLE threadHandle;
  161. DWORD threadId;
  162. };
  163. } // namespace ZeroTier
  164. #endif