Thread.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 identifier, and static methods to start and join threads
  51. */
  52. class Thread
  53. {
  54. public:
  55. Thread()
  56. throw()
  57. {
  58. memset(&_tid,0,sizeof(_tid));
  59. }
  60. Thread(const Thread &t)
  61. throw()
  62. {
  63. memcpy(&_tid,&(t._tid),sizeof(_tid));
  64. }
  65. inline Thread &operator=(const Thread &t)
  66. throw()
  67. {
  68. memcpy(&_tid,&(t._tid),sizeof(_tid));
  69. return *this;
  70. }
  71. /**
  72. * Start a new thread
  73. *
  74. * @param instance Instance whose threadMain() method gets called by new thread
  75. * @return Thread identifier
  76. * @throws std::runtime_error Unable to create thread
  77. * @tparam C Class containing threadMain()
  78. */
  79. template<typename C>
  80. static inline Thread start(C *instance)
  81. throw(std::runtime_error)
  82. {
  83. Thread t;
  84. if (pthread_create(&t._tid,(const pthread_attr_t *)0,&___zt_threadMain<C>,instance))
  85. throw std::runtime_error("pthread_create() failed, unable to create thread");
  86. return t;
  87. }
  88. /**
  89. * Join to a thread, waiting for it to terminate
  90. *
  91. * @param t Thread to join
  92. */
  93. static inline void join(const Thread &t)
  94. {
  95. pthread_join(t._tid,(void **)0);
  96. }
  97. /**
  98. * Sleep the current thread
  99. *
  100. * @param ms Number of milliseconds to sleep
  101. */
  102. static inline void sleep(unsigned long ms)
  103. {
  104. usleep(ms * 1000);
  105. }
  106. private:
  107. pthread_t _tid;
  108. };
  109. } // namespace ZeroTier
  110. #endif // __WINDOWS__ / !__WINDOWS__
  111. #endif