Thread.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. #include "AtomicCounter.hpp"
  32. #ifdef __WINDOWS__
  33. #include <Windows.h>
  34. #include <string.h>
  35. namespace ZeroTier {
  36. template<typename C>
  37. static DWORD WINAPI ___zt_threadMain(LPVOID lpParam)
  38. {
  39. try {
  40. ((C *)lpParam)->threadMain();
  41. } catch ( ... ) {}
  42. return 0;
  43. }
  44. class Thread
  45. {
  46. public:
  47. Thread()
  48. throw()
  49. {
  50. _th = NULL;
  51. }
  52. template<typename C>
  53. static inline Thread start(C *instance)
  54. throw(std::runtime_error)
  55. {
  56. Thread t;
  57. t._th = CreateThread(NULL,0,&___zt_threadMain<C>,(LPVOID)instance,0,&t._tid);
  58. if (t._th == NULL)
  59. throw std::runtime_error("CreateThread() failed");
  60. return t;
  61. }
  62. static inline void join(const Thread &t)
  63. {
  64. if (t._th != NULL)
  65. WaitForSingleObject(t._th,INFINITE);
  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. private:
  78. HANDLE _th;
  79. DWORD _tid;
  80. };
  81. } // namespace ZeroTier
  82. #else
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <pthread.h>
  87. #include <unistd.h>
  88. namespace ZeroTier {
  89. template<typename C>
  90. static void *___zt_threadMain(void *instance)
  91. {
  92. try {
  93. ((C *)instance)->threadMain();
  94. } catch ( ... ) {}
  95. return (void *)0;
  96. }
  97. /**
  98. * A thread identifier, and static methods to start and join threads
  99. */
  100. class Thread
  101. {
  102. public:
  103. Thread()
  104. throw()
  105. {
  106. memset(&_tid,0,sizeof(_tid));
  107. }
  108. Thread(const Thread &t)
  109. throw()
  110. {
  111. memcpy(&_tid,&(t._tid),sizeof(_tid));
  112. }
  113. inline Thread &operator=(const Thread &t)
  114. throw()
  115. {
  116. memcpy(&_tid,&(t._tid),sizeof(_tid));
  117. return *this;
  118. }
  119. /**
  120. * Start a new thread
  121. *
  122. * @param instance Instance whose threadMain() method gets called by new thread
  123. * @return Thread identifier
  124. * @throws std::runtime_error Unable to create thread
  125. * @tparam C Class containing threadMain()
  126. */
  127. template<typename C>
  128. static inline Thread start(C *instance)
  129. throw(std::runtime_error)
  130. {
  131. Thread t;
  132. if (pthread_create(&t._tid,(const pthread_attr_t *)0,&___zt_threadMain<C>,instance))
  133. throw std::runtime_error("pthread_create() failed, unable to create thread");
  134. return t;
  135. }
  136. /**
  137. * Join to a thread, waiting for it to terminate
  138. *
  139. * @param t Thread to join
  140. */
  141. static inline void join(const Thread &t)
  142. {
  143. pthread_join(t._tid,(void **)0);
  144. }
  145. /**
  146. * Sleep the current thread
  147. *
  148. * @param ms Number of milliseconds to sleep
  149. */
  150. static inline void sleep(unsigned long ms)
  151. {
  152. usleep(ms * 1000);
  153. }
  154. private:
  155. pthread_t _tid;
  156. };
  157. } // namespace ZeroTier
  158. #endif // __WINDOWS__ / !__WINDOWS__
  159. #endif