Thread.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_THREAD_HPP
  27. #define ZT_THREAD_HPP
  28. #include <stdexcept>
  29. #include "../node/Constants.hpp"
  30. #ifdef __WINDOWS__
  31. #include <WinSock2.h>
  32. #include <Windows.h>
  33. #include <string.h>
  34. #include "../node/Mutex.hpp"
  35. namespace ZeroTier {
  36. template<typename C>
  37. static DWORD WINAPI ___zt_threadMain(LPVOID lpParam)
  38. {
  39. try {
  40. ((C *)lpParam)->threadMain();
  41. } catch ( ... ) {}
  42. return 0;
  43. }
  44. class Thread
  45. {
  46. public:
  47. Thread()
  48. {
  49. _th = NULL;
  50. _tid = 0;
  51. }
  52. template<typename C>
  53. static inline Thread start(C *instance)
  54. {
  55. Thread t;
  56. t._th = CreateThread(NULL,0,&___zt_threadMain<C>,(LPVOID)instance,0,&t._tid);
  57. if (t._th == NULL)
  58. throw std::runtime_error("CreateThread() failed");
  59. return t;
  60. }
  61. static inline void join(const Thread &t)
  62. {
  63. if (t._th != NULL) {
  64. for(;;) {
  65. DWORD ec = STILL_ACTIVE;
  66. GetExitCodeThread(t._th,&ec);
  67. if (ec == STILL_ACTIVE)
  68. WaitForSingleObject(t._th,1000);
  69. else break;
  70. }
  71. }
  72. }
  73. static inline void sleep(unsigned long ms)
  74. {
  75. Sleep((DWORD)ms);
  76. }
  77. // Not available on *nix platforms
  78. static inline void cancelIO(const Thread &t)
  79. {
  80. #if !defined(__MINGW32__) && !defined(__MINGW64__) // CancelSynchronousIo not available in MSYS2
  81. if (t._th != NULL)
  82. CancelSynchronousIo(t._th);
  83. #endif
  84. }
  85. inline operator bool() const { return (_th != NULL); }
  86. private:
  87. HANDLE _th;
  88. DWORD _tid;
  89. };
  90. } // namespace ZeroTier
  91. #else
  92. #include <stdio.h>
  93. #include <stdlib.h>
  94. #include <string.h>
  95. #include <pthread.h>
  96. #include <unistd.h>
  97. namespace ZeroTier {
  98. template<typename C>
  99. static void *___zt_threadMain(void *instance)
  100. {
  101. try {
  102. ((C *)instance)->threadMain();
  103. } catch ( ... ) {}
  104. return (void *)0;
  105. }
  106. /**
  107. * A thread identifier, and static methods to start and join threads
  108. */
  109. class Thread
  110. {
  111. public:
  112. Thread()
  113. {
  114. memset(this,0,sizeof(Thread));
  115. }
  116. Thread(const Thread &t)
  117. {
  118. memcpy(this,&t,sizeof(Thread));
  119. }
  120. inline Thread &operator=(const Thread &t)
  121. {
  122. memcpy(this,&t,sizeof(Thread));
  123. return *this;
  124. }
  125. /**
  126. * Start a new thread
  127. *
  128. * @param instance Instance whose threadMain() method gets called by new thread
  129. * @return Thread identifier
  130. * @throws std::runtime_error Unable to create thread
  131. * @tparam C Class containing threadMain()
  132. */
  133. template<typename C>
  134. static inline Thread start(C *instance)
  135. {
  136. Thread t;
  137. pthread_attr_t tattr;
  138. pthread_attr_init(&tattr);
  139. // This corrects for systems with abnormally small defaults (musl) and also
  140. // shrinks the stack on systems with large defaults to save a bit of memory.
  141. pthread_attr_setstacksize(&tattr,ZT_THREAD_MIN_STACK_SIZE);
  142. if (pthread_create(&t._tid,&tattr,&___zt_threadMain<C>,instance)) {
  143. pthread_attr_destroy(&tattr);
  144. throw std::runtime_error("pthread_create() failed, unable to create thread");
  145. } else {
  146. t._started = true;
  147. pthread_attr_destroy(&tattr);
  148. }
  149. return t;
  150. }
  151. /**
  152. * Join to a thread, waiting for it to terminate (does nothing on null Thread values)
  153. *
  154. * @param t Thread to join
  155. */
  156. static inline void join(const Thread &t)
  157. {
  158. if (t._started)
  159. pthread_join(t._tid,(void **)0);
  160. }
  161. /**
  162. * Sleep the current thread
  163. *
  164. * @param ms Number of milliseconds to sleep
  165. */
  166. static inline void sleep(unsigned long ms) { usleep(ms * 1000); }
  167. inline operator bool() const { return (_started); }
  168. private:
  169. pthread_t _tid;
  170. volatile bool _started;
  171. };
  172. } // namespace ZeroTier
  173. #endif // __WINDOWS__ / !__WINDOWS__
  174. #endif