Thread.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. _impl(malloc(sizeof(pthread_t))),
  45. _running()
  46. {
  47. memset(_impl,0,sizeof(pthread_t));
  48. }
  49. Thread::~Thread()
  50. {
  51. free(_impl);
  52. }
  53. void Thread::start()
  54. {
  55. if (!*_running) {
  56. ++_running;
  57. pthread_create((pthread_t *)_impl,(const pthread_attr_t *)0,&__m_thread_main,(void *)this);
  58. }
  59. }
  60. void Thread::join()
  61. {
  62. void *tmp;
  63. if (*_running)
  64. pthread_join(*((pthread_t *)_impl),&tmp);
  65. }
  66. void Thread::sleep(unsigned long ms)
  67. {
  68. usleep(ms * 1000);
  69. }
  70. void Thread::__intl_run()
  71. {
  72. for(;;) {
  73. _notInit = false;
  74. this->main();
  75. if (_notInit) // UGLY ASS HACK: see main()
  76. usleep(50);
  77. else break;
  78. }
  79. --_running;
  80. }
  81. void Thread::main()
  82. throw()
  83. {
  84. _notInit = true; // UGLY ASS HACK: retry if subclass has not defined virtual function pointer yet
  85. }
  86. } // namespace ZeroTier
  87. #endif
  88. #ifdef _WIN32
  89. #include <Windows.h>
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. DWORD WINAPI __m_thread_main(LPVOID lpParam)
  93. {
  94. ((ZeroTier::Thread *)lpParam)->__intl_run();
  95. return 0;
  96. }
  97. struct __m_thread_info
  98. {
  99. HANDLE threadHandle;
  100. DWORD threadId;
  101. bool started;
  102. };
  103. namespace ZeroTier {
  104. Thread::Thread() :
  105. _impl(malloc(sizeof(__m_thread_info))),
  106. _running()
  107. {
  108. memset(_impl,0,sizeof(__m_thread_info));
  109. }
  110. Thread::~Thread()
  111. {
  112. if (((__m_thread_info *)_impl)->started)
  113. CloseHandle(((__m_thread_info *)_impl)->threadHandle);
  114. free(_impl);
  115. }
  116. void Thread::start()
  117. {
  118. if (!*_running) {
  119. ++_running;
  120. if ((((__m_thread_info *)_impl)->threadHandle = CreateThread(NULL,0,__m_thread_main,this,0,&(((__m_thread_info *)_impl)->threadId))) != NULL) {
  121. ((__m_thread_info *)_impl)->started = true;
  122. }
  123. }
  124. }
  125. void Thread::join()
  126. {
  127. if (*_running)
  128. WaitForSingleObject(((__m_thread_info *)_impl)->threadHandle,INFINITE);
  129. }
  130. void Thread::__intl_run()
  131. {
  132. for(;;) {
  133. _notInit = false;
  134. this->main();
  135. if (_notInit)
  136. Thread::sleep(50);
  137. else break;
  138. }
  139. --_running;
  140. }
  141. void Thread::main()
  142. throw()
  143. {
  144. _notInit = true; // HACK: retry if subclass has not defined virtual function pointer yet
  145. }
  146. struct _Thread_RunInBackgroundData
  147. {
  148. void (*func)(void *);
  149. void *ptr;
  150. HANDLE threadHandle;
  151. DWORD threadId;
  152. };
  153. } // namespace ZeroTier
  154. #endif