Thread.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. {
  41. _th = NULL;
  42. _tid = 0;
  43. }
  44. template<typename C>
  45. static inline Thread start(C *instance)
  46. {
  47. Thread t;
  48. t._th = CreateThread(NULL,0,&___zt_threadMain<C>,(LPVOID)instance,0,&t._tid);
  49. if (t._th == NULL)
  50. throw std::runtime_error("CreateThread() failed");
  51. return t;
  52. }
  53. static inline void join(const Thread &t)
  54. {
  55. if (t._th != NULL) {
  56. for(;;) {
  57. DWORD ec = STILL_ACTIVE;
  58. GetExitCodeThread(t._th,&ec);
  59. if (ec == STILL_ACTIVE)
  60. WaitForSingleObject(t._th,1000);
  61. else break;
  62. }
  63. }
  64. }
  65. static inline void sleep(unsigned long ms)
  66. {
  67. Sleep((DWORD)ms);
  68. }
  69. // Not available on *nix platforms
  70. static inline void cancelIO(const Thread &t)
  71. {
  72. if (t._th != NULL)
  73. CancelSynchronousIo(t._th);
  74. }
  75. inline operator bool() const { return (_th != NULL); }
  76. private:
  77. HANDLE _th;
  78. DWORD _tid;
  79. };
  80. } // namespace ZeroTier
  81. #else
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <pthread.h>
  86. #include <unistd.h>
  87. namespace ZeroTier {
  88. template<typename C>
  89. static void *___zt_threadMain(void *instance)
  90. {
  91. try {
  92. ((C *)instance)->threadMain();
  93. } catch ( ... ) {}
  94. return (void *)0;
  95. }
  96. /**
  97. * A thread identifier, and static methods to start and join threads
  98. */
  99. class Thread
  100. {
  101. public:
  102. Thread()
  103. {
  104. memset(this,0,sizeof(Thread));
  105. }
  106. Thread(const Thread &t)
  107. {
  108. memcpy(this,&t,sizeof(Thread));
  109. }
  110. inline Thread &operator=(const Thread &t)
  111. {
  112. memcpy(this,&t,sizeof(Thread));
  113. return *this;
  114. }
  115. /**
  116. * Start a new thread
  117. *
  118. * @param instance Instance whose threadMain() method gets called by new thread
  119. * @return Thread identifier
  120. * @throws std::runtime_error Unable to create thread
  121. * @tparam C Class containing threadMain()
  122. */
  123. template<typename C>
  124. static inline Thread start(C *instance)
  125. {
  126. Thread t;
  127. pthread_attr_t tattr;
  128. pthread_attr_init(&tattr);
  129. // This corrects for systems with abnormally small defaults (musl) and also
  130. // shrinks the stack on systems with large defaults to save a bit of memory.
  131. pthread_attr_setstacksize(&tattr,ZT_THREAD_MIN_STACK_SIZE);
  132. if (pthread_create(&t._tid,&tattr,&___zt_threadMain<C>,instance)) {
  133. pthread_attr_destroy(&tattr);
  134. throw std::runtime_error("pthread_create() failed, unable to create thread");
  135. } else {
  136. t._started = true;
  137. pthread_attr_destroy(&tattr);
  138. }
  139. return t;
  140. }
  141. /**
  142. * Join to a thread, waiting for it to terminate (does nothing on null Thread values)
  143. *
  144. * @param t Thread to join
  145. */
  146. static inline void join(const Thread &t)
  147. {
  148. if (t._started)
  149. pthread_join(t._tid,(void **)0);
  150. }
  151. /**
  152. * Sleep the current thread
  153. *
  154. * @param ms Number of milliseconds to sleep
  155. */
  156. static inline void sleep(unsigned long ms) { usleep(ms * 1000); }
  157. inline operator bool() const { return (_started); }
  158. private:
  159. pthread_t _tid;
  160. volatile bool _started;
  161. };
  162. } // namespace ZeroTier
  163. #endif // __WINDOWS__ / !__WINDOWS__
  164. #endif