Thread.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_THREAD_HPP
  14. #define ZT_THREAD_HPP
  15. #include "../node/Constants.hpp"
  16. #include <stdexcept>
  17. #ifdef __WINDOWS__
  18. #include "../node/Mutex.hpp"
  19. #include <string.h>
  20. #include <windows.h>
  21. #include <winsock2.h>
  22. namespace ZeroTier {
  23. template <typename C> static DWORD WINAPI ___zt_threadMain(LPVOID lpParam)
  24. {
  25. try {
  26. ((C*)lpParam)->threadMain();
  27. }
  28. catch (...) {
  29. }
  30. return 0;
  31. }
  32. class Thread {
  33. public:
  34. Thread()
  35. {
  36. _th = NULL;
  37. _tid = 0;
  38. }
  39. template <typename C> static inline Thread start(C* instance)
  40. {
  41. Thread t;
  42. t._th = CreateThread(NULL, 0, &___zt_threadMain<C>, (LPVOID)instance, 0, &t._tid);
  43. if (t._th == NULL)
  44. throw std::runtime_error("CreateThread() failed");
  45. return t;
  46. }
  47. static inline void join(const Thread& t)
  48. {
  49. if (t._th != NULL) {
  50. for (;;) {
  51. DWORD ec = STILL_ACTIVE;
  52. GetExitCodeThread(t._th, &ec);
  53. if (ec == STILL_ACTIVE)
  54. WaitForSingleObject(t._th, 1000);
  55. else
  56. break;
  57. }
  58. }
  59. }
  60. static inline void sleep(unsigned long ms)
  61. {
  62. Sleep((DWORD)ms);
  63. }
  64. // Not available on *nix platforms
  65. static inline void cancelIO(const Thread& t)
  66. {
  67. #if ! defined(__MINGW32__) && ! defined(__MINGW64__) // CancelSynchronousIo not available in MSYS2
  68. if (t._th != NULL)
  69. CancelSynchronousIo(t._th);
  70. #endif
  71. }
  72. inline operator bool() const
  73. {
  74. return (_th != NULL);
  75. }
  76. private:
  77. HANDLE _th;
  78. DWORD _tid;
  79. };
  80. } // namespace ZeroTier
  81. #else
  82. #include <pthread.h>
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <unistd.h>
  87. namespace ZeroTier {
  88. template <typename C> static void* ___zt_threadMain(void* instance)
  89. {
  90. try {
  91. ((C*)instance)->threadMain();
  92. }
  93. catch (...) {
  94. }
  95. return (void*)0;
  96. }
  97. /**
  98. * A thread identifier, and static methods to start and join threads
  99. */
  100. class Thread {
  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> static inline Thread start(C* instance)
  124. {
  125. Thread t;
  126. pthread_attr_t tattr;
  127. pthread_attr_init(&tattr);
  128. // This corrects for systems with abnormally small defaults (musl) and also
  129. // shrinks the stack on systems with large defaults to save a bit of memory.
  130. pthread_attr_setstacksize(&tattr, ZT_THREAD_MIN_STACK_SIZE);
  131. if (pthread_create(&t._tid, &tattr, &___zt_threadMain<C>, instance)) {
  132. pthread_attr_destroy(&tattr);
  133. throw std::runtime_error("pthread_create() failed, unable to create thread");
  134. }
  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)
  157. {
  158. usleep(ms * 1000);
  159. }
  160. inline operator bool() const
  161. {
  162. return (_started);
  163. }
  164. private:
  165. pthread_t _tid;
  166. volatile bool _started;
  167. };
  168. } // namespace ZeroTier
  169. #endif // __WINDOWS__ / !__WINDOWS__
  170. #endif