Thread.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_THREAD_HPP
  28. #define ZT_THREAD_HPP
  29. #include <stdexcept>
  30. #include "Constants.hpp"
  31. #ifdef __WINDOWS__
  32. #include <WinSock2.h>
  33. #include <Windows.h>
  34. #include <string.h>
  35. #include "Mutex.hpp"
  36. namespace ZeroTier {
  37. template<typename C>
  38. static DWORD WINAPI ___zt_threadMain(LPVOID lpParam)
  39. {
  40. try {
  41. ((C *)lpParam)->threadMain();
  42. } catch ( ... ) {}
  43. return 0;
  44. }
  45. class Thread
  46. {
  47. public:
  48. Thread()
  49. throw()
  50. {
  51. _th = NULL;
  52. _tid = 0;
  53. }
  54. template<typename C>
  55. static inline Thread start(C *instance)
  56. throw(std::runtime_error)
  57. {
  58. Thread t;
  59. t._th = CreateThread(NULL,0,&___zt_threadMain<C>,(LPVOID)instance,0,&t._tid);
  60. if (t._th == NULL)
  61. throw std::runtime_error("CreateThread() failed");
  62. return t;
  63. }
  64. static inline void join(const Thread &t)
  65. {
  66. if (t._th != NULL) {
  67. for(;;) {
  68. DWORD ec = STILL_ACTIVE;
  69. GetExitCodeThread(t._th,&ec);
  70. if (ec == STILL_ACTIVE)
  71. WaitForSingleObject(t._th,1000);
  72. else break;
  73. }
  74. }
  75. }
  76. static inline void sleep(unsigned long ms)
  77. {
  78. Sleep((DWORD)ms);
  79. }
  80. // Not available on *nix platforms
  81. static inline void cancelIO(const Thread &t)
  82. {
  83. if (t._th != NULL)
  84. CancelSynchronousIo(t._th);
  85. }
  86. inline operator bool() const throw() { return (_th != NULL); }
  87. private:
  88. HANDLE _th;
  89. DWORD _tid;
  90. };
  91. } // namespace ZeroTier
  92. #else
  93. #include <stdio.h>
  94. #include <stdlib.h>
  95. #include <string.h>
  96. #include <pthread.h>
  97. #include <unistd.h>
  98. namespace ZeroTier {
  99. template<typename C>
  100. static void *___zt_threadMain(void *instance)
  101. {
  102. try {
  103. ((C *)instance)->threadMain();
  104. } catch ( ... ) {}
  105. return (void *)0;
  106. }
  107. /**
  108. * A thread identifier, and static methods to start and join threads
  109. */
  110. class Thread
  111. {
  112. public:
  113. Thread()
  114. throw()
  115. {
  116. memset(&_tid,0,sizeof(_tid));
  117. _started = false;
  118. }
  119. Thread(const Thread &t)
  120. throw()
  121. {
  122. memcpy(&_tid,&(t._tid),sizeof(_tid));
  123. _started = t._started;
  124. }
  125. inline Thread &operator=(const Thread &t)
  126. throw()
  127. {
  128. memcpy(&_tid,&(t._tid),sizeof(_tid));
  129. _started = t._started;
  130. return *this;
  131. }
  132. /**
  133. * Start a new thread
  134. *
  135. * @param instance Instance whose threadMain() method gets called by new thread
  136. * @return Thread identifier
  137. * @throws std::runtime_error Unable to create thread
  138. * @tparam C Class containing threadMain()
  139. */
  140. template<typename C>
  141. static inline Thread start(C *instance)
  142. throw(std::runtime_error)
  143. {
  144. Thread t;
  145. t._started = true;
  146. if (pthread_create(&t._tid,(const pthread_attr_t *)0,&___zt_threadMain<C>,instance))
  147. throw std::runtime_error("pthread_create() failed, unable to create thread");
  148. return t;
  149. }
  150. /**
  151. * Join to a thread, waiting for it to terminate (does nothing on null Thread values)
  152. *
  153. * @param t Thread to join
  154. */
  155. static inline void join(const Thread &t)
  156. {
  157. if (t._started)
  158. pthread_join(t._tid,(void **)0);
  159. }
  160. /**
  161. * Sleep the current thread
  162. *
  163. * @param ms Number of milliseconds to sleep
  164. */
  165. static inline void sleep(unsigned long ms) { usleep(ms * 1000); }
  166. inline operator bool() const throw() { return (_started); }
  167. private:
  168. pthread_t _tid;
  169. volatile bool _started;
  170. };
  171. } // namespace ZeroTier
  172. #endif // __WINDOWS__ / !__WINDOWS__
  173. #endif