Thread.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_THREAD_HPP
  19. #define ZT_THREAD_HPP
  20. #include <stdexcept>
  21. #include "../node/Constants.hpp"
  22. #ifdef __WINDOWS__
  23. #include <WinSock2.h>
  24. #include <Windows.h>
  25. #include <string.h>
  26. #include "../node/Mutex.hpp"
  27. namespace ZeroTier {
  28. template<typename C>
  29. static DWORD WINAPI ___zt_threadMain(LPVOID lpParam)
  30. {
  31. try {
  32. ((C *)lpParam)->threadMain();
  33. } catch ( ... ) {}
  34. return 0;
  35. }
  36. class Thread
  37. {
  38. public:
  39. Thread()
  40. throw()
  41. {
  42. _th = NULL;
  43. _tid = 0;
  44. }
  45. template<typename C>
  46. static inline Thread start(C *instance)
  47. throw(std::runtime_error)
  48. {
  49. Thread t;
  50. t._th = CreateThread(NULL,0,&___zt_threadMain<C>,(LPVOID)instance,0,&t._tid);
  51. if (t._th == NULL)
  52. throw std::runtime_error("CreateThread() failed");
  53. return t;
  54. }
  55. static inline void join(const Thread &t)
  56. {
  57. if (t._th != NULL) {
  58. for(;;) {
  59. DWORD ec = STILL_ACTIVE;
  60. GetExitCodeThread(t._th,&ec);
  61. if (ec == STILL_ACTIVE)
  62. WaitForSingleObject(t._th,1000);
  63. else break;
  64. }
  65. }
  66. }
  67. static inline void sleep(unsigned long ms)
  68. {
  69. Sleep((DWORD)ms);
  70. }
  71. // Not available on *nix platforms
  72. static inline void cancelIO(const Thread &t)
  73. {
  74. if (t._th != NULL)
  75. CancelSynchronousIo(t._th);
  76. }
  77. inline operator bool() const throw() { return (_th != NULL); }
  78. private:
  79. HANDLE _th;
  80. DWORD _tid;
  81. };
  82. } // namespace ZeroTier
  83. #else
  84. #include <stdio.h>
  85. #include <stdlib.h>
  86. #include <string.h>
  87. #include <pthread.h>
  88. #include <unistd.h>
  89. namespace ZeroTier {
  90. template<typename C>
  91. static void *___zt_threadMain(void *instance)
  92. {
  93. try {
  94. ((C *)instance)->threadMain();
  95. } catch ( ... ) {}
  96. return (void *)0;
  97. }
  98. /**
  99. * A thread identifier, and static methods to start and join threads
  100. */
  101. class Thread
  102. {
  103. public:
  104. Thread()
  105. throw()
  106. {
  107. memset(&_tid,0,sizeof(_tid));
  108. pthread_attr_init(&_tattr);
  109. // This corrects for systems with abnormally small defaults (musl) and also
  110. // shrinks the stack on systems with large defaults to save a bit of memory.
  111. pthread_attr_setstacksize(&_tattr,ZT_THREAD_MIN_STACK_SIZE);
  112. _started = false;
  113. }
  114. ~Thread()
  115. {
  116. pthread_attr_destroy(&_tattr);
  117. }
  118. Thread(const Thread &t)
  119. throw()
  120. {
  121. memcpy(&_tid,&(t._tid),sizeof(_tid));
  122. _started = t._started;
  123. }
  124. inline Thread &operator=(const Thread &t)
  125. throw()
  126. {
  127. memcpy(&_tid,&(t._tid),sizeof(_tid));
  128. _started = t._started;
  129. return *this;
  130. }
  131. /**
  132. * Start a new thread
  133. *
  134. * @param instance Instance whose threadMain() method gets called by new thread
  135. * @return Thread identifier
  136. * @throws std::runtime_error Unable to create thread
  137. * @tparam C Class containing threadMain()
  138. */
  139. template<typename C>
  140. static inline Thread start(C *instance)
  141. throw(std::runtime_error)
  142. {
  143. Thread t;
  144. t._started = true;
  145. if (pthread_create(&t._tid,&t._tattr,&___zt_threadMain<C>,instance))
  146. throw std::runtime_error("pthread_create() failed, unable to create thread");
  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 throw() { return (_started); }
  166. private:
  167. pthread_t _tid;
  168. pthread_attr_t _tattr;
  169. volatile bool _started;
  170. };
  171. } // namespace ZeroTier
  172. #endif // __WINDOWS__ / !__WINDOWS__
  173. #endif