Thread.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. todo need windows;
  34. #else
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <pthread.h>
  39. #include <unistd.h>
  40. namespace ZeroTier {
  41. template<typename C>
  42. static void *___zt_threadMain(void *instance)
  43. {
  44. try {
  45. ((C *)instance)->threadMain();
  46. } catch ( ... ) {}
  47. return (void *)0;
  48. }
  49. /**
  50. * A thread of a given class type
  51. *
  52. * @tparam C Class using Thread
  53. */
  54. template<typename C>
  55. class Thread
  56. {
  57. public:
  58. Thread()
  59. throw()
  60. {
  61. memset(&_tid,0,sizeof(_tid));
  62. }
  63. Thread(const Thread &t)
  64. throw()
  65. {
  66. memcpy(&_tid,&(t._tid),sizeof(_tid));
  67. }
  68. inline Thread &operator=(const Thread &t)
  69. throw()
  70. {
  71. memcpy(&_tid,&(t._tid),sizeof(_tid));
  72. return *this;
  73. }
  74. /**
  75. * Start a new thread
  76. *
  77. * @param instance Instance whose threadMain() method gets called by new thread
  78. * @return Thread identifier
  79. * @throws std::runtime_error Unable to create thread
  80. */
  81. static inline Thread start(C *instance)
  82. throw(std::runtime_error)
  83. {
  84. Thread t;
  85. if (pthread_create(&t._tid,(const pthread_attr_t *)0,&___zt_threadMain<C>,instance))
  86. throw std::runtime_error("pthread_create() failed, unable to create thread");
  87. return t;
  88. }
  89. /**
  90. * Join to a thread, waiting for it to terminate
  91. *
  92. * @param t Thread to join
  93. */
  94. static inline void join(const Thread &t)
  95. {
  96. pthread_join(t._tid,(void **)0);
  97. }
  98. /**
  99. * Sleep the current thread
  100. *
  101. * @param ms Number of milliseconds to sleep
  102. */
  103. static inline void sleep(unsigned long ms)
  104. {
  105. usleep(ms * 1000);
  106. }
  107. private:
  108. pthread_t _tid;
  109. };
  110. } // namespace ZeroTier
  111. #endif // __WINDOWS__ / !__WINDOWS__
  112. #endif