Thread.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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. private:
  72. HANDLE _th;
  73. DWORD _tid;
  74. };
  75. } // namespace ZeroTier
  76. #else
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <pthread.h>
  81. #include <unistd.h>
  82. namespace ZeroTier {
  83. template<typename C>
  84. static void *___zt_threadMain(void *instance)
  85. {
  86. try {
  87. ((C *)instance)->threadMain();
  88. } catch ( ... ) {}
  89. return (void *)0;
  90. }
  91. /**
  92. * A thread identifier, and static methods to start and join threads
  93. */
  94. class Thread
  95. {
  96. public:
  97. Thread()
  98. throw()
  99. {
  100. memset(&_tid,0,sizeof(_tid));
  101. }
  102. Thread(const Thread &t)
  103. throw()
  104. {
  105. memcpy(&_tid,&(t._tid),sizeof(_tid));
  106. }
  107. inline Thread &operator=(const Thread &t)
  108. throw()
  109. {
  110. memcpy(&_tid,&(t._tid),sizeof(_tid));
  111. return *this;
  112. }
  113. /**
  114. * Start a new thread
  115. *
  116. * @param instance Instance whose threadMain() method gets called by new thread
  117. * @return Thread identifier
  118. * @throws std::runtime_error Unable to create thread
  119. * @tparam C Class containing threadMain()
  120. */
  121. template<typename C>
  122. static inline Thread start(C *instance)
  123. throw(std::runtime_error)
  124. {
  125. Thread t;
  126. if (pthread_create(&t._tid,(const pthread_attr_t *)0,&___zt_threadMain<C>,instance))
  127. throw std::runtime_error("pthread_create() failed, unable to create thread");
  128. return t;
  129. }
  130. /**
  131. * Join to a thread, waiting for it to terminate
  132. *
  133. * @param t Thread to join
  134. */
  135. static inline void join(const Thread &t)
  136. {
  137. pthread_join(t._tid,(void **)0);
  138. }
  139. /**
  140. * Sleep the current thread
  141. *
  142. * @param ms Number of milliseconds to sleep
  143. */
  144. static inline void sleep(unsigned long ms)
  145. {
  146. usleep(ms * 1000);
  147. }
  148. private:
  149. pthread_t _tid;
  150. };
  151. } // namespace ZeroTier
  152. #endif // __WINDOWS__ / !__WINDOWS__
  153. #endif