Thread.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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 (t._th != NULL)
  81. CancelSynchronousIo(t._th);
  82. }
  83. inline operator bool() const { return (_th != NULL); }
  84. private:
  85. HANDLE _th;
  86. DWORD _tid;
  87. };
  88. } // namespace ZeroTier
  89. #else
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. #include <string.h>
  93. #include <pthread.h>
  94. #include <unistd.h>
  95. namespace ZeroTier {
  96. template<typename C>
  97. static void *___zt_threadMain(void *instance)
  98. {
  99. try {
  100. ((C *)instance)->threadMain();
  101. } catch ( ... ) {}
  102. return (void *)0;
  103. }
  104. /**
  105. * A thread identifier, and static methods to start and join threads
  106. */
  107. class Thread
  108. {
  109. public:
  110. Thread()
  111. {
  112. memset(this,0,sizeof(Thread));
  113. }
  114. Thread(const Thread &t)
  115. {
  116. memcpy(this,&t,sizeof(Thread));
  117. }
  118. inline Thread &operator=(const Thread &t)
  119. {
  120. memcpy(this,&t,sizeof(Thread));
  121. return *this;
  122. }
  123. /**
  124. * Start a new thread
  125. *
  126. * @param instance Instance whose threadMain() method gets called by new thread
  127. * @return Thread identifier
  128. * @throws std::runtime_error Unable to create thread
  129. * @tparam C Class containing threadMain()
  130. */
  131. template<typename C>
  132. static inline Thread start(C *instance)
  133. {
  134. Thread t;
  135. pthread_attr_t tattr;
  136. pthread_attr_init(&tattr);
  137. // This corrects for systems with abnormally small defaults (musl) and also
  138. // shrinks the stack on systems with large defaults to save a bit of memory.
  139. pthread_attr_setstacksize(&tattr,ZT_THREAD_MIN_STACK_SIZE);
  140. if (pthread_create(&t._tid,&tattr,&___zt_threadMain<C>,instance)) {
  141. pthread_attr_destroy(&tattr);
  142. throw std::runtime_error("pthread_create() failed, unable to create thread");
  143. } else {
  144. t._started = true;
  145. pthread_attr_destroy(&tattr);
  146. }
  147. return t;
  148. }
  149. /**
  150. * Join to a thread, waiting for it to terminate (does nothing on null Thread values)
  151. *
  152. * @param t Thread to join
  153. */
  154. static inline void join(const Thread &t)
  155. {
  156. if (t._started)
  157. pthread_join(t._tid,(void **)0);
  158. }
  159. /**
  160. * Sleep the current thread
  161. *
  162. * @param ms Number of milliseconds to sleep
  163. */
  164. static inline void sleep(unsigned long ms) { usleep(ms * 1000); }
  165. inline operator bool() const { return (_started); }
  166. private:
  167. pthread_t _tid;
  168. volatile bool _started;
  169. };
  170. } // namespace ZeroTier
  171. #endif // __WINDOWS__ / !__WINDOWS__
  172. #endif