Thread.hpp 4.0 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. 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. _started = false;
  109. }
  110. Thread(const Thread &t)
  111. throw()
  112. {
  113. memcpy(&_tid,&(t._tid),sizeof(_tid));
  114. _started = t._started;
  115. }
  116. inline Thread &operator=(const Thread &t)
  117. throw()
  118. {
  119. memcpy(&_tid,&(t._tid),sizeof(_tid));
  120. _started = t._started;
  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. throw(std::runtime_error)
  134. {
  135. Thread t;
  136. t._started = true;
  137. if (pthread_create(&t._tid,(const pthread_attr_t *)0,&___zt_threadMain<C>,instance))
  138. throw std::runtime_error("pthread_create() failed, unable to create thread");
  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 throw() { return (_started); }
  158. private:
  159. pthread_t _tid;
  160. volatile bool _started;
  161. };
  162. } // namespace ZeroTier
  163. #endif // __WINDOWS__ / !__WINDOWS__
  164. #endif