Thread.hpp 3.9 KB

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