MTQ.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-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_MTQ_HPP
  28. #define ZT_MTQ_HPP
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <queue>
  32. #include "../node/Constants.hpp"
  33. #include "../node/NonCopyable.hpp"
  34. #include "../node/Utils.hpp"
  35. #ifdef __WINDOWS__
  36. #include <Windows.h>
  37. #else
  38. #include <time.h>
  39. #include <pthread.h>
  40. #endif
  41. namespace ZeroTier {
  42. /**
  43. * A synchronized multithreaded FIFO queue
  44. *
  45. * This is designed for a use case where one thread pushes, the
  46. * other pops.
  47. */
  48. template<typename T>
  49. class MTQ : NonCopyable
  50. {
  51. public:
  52. MTQ()
  53. {
  54. #ifdef __WINDOWS__
  55. _sem = CreateSemaphore(NULL,0,0x7fffffff,NULL);
  56. InitializeCriticalSection(&_cs);
  57. #else
  58. pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0);
  59. pthread_cond_init(&_cond,(const pthread_condattr_t *)0);
  60. #endif
  61. }
  62. ~MTQ()
  63. {
  64. #ifdef __WINDOWS__
  65. CloseHandle(_sem);
  66. DeleteCriticalSection(&_cs);
  67. #else
  68. pthread_cond_destroy(&_cond);
  69. pthread_mutex_destroy(&_mh);
  70. #endif
  71. }
  72. /**
  73. * Push something onto the end of the FIFO and signal waiting thread(s)
  74. *
  75. * @param v Value to push
  76. */
  77. inline void push(const T &v)
  78. {
  79. #ifdef __WINDOWS__
  80. EnterCriticalSection(&_cs);
  81. try {
  82. _q.push(v);
  83. LeaveCriticalSection(&_cs);
  84. ReleaseSemaphore(_sem,1,NULL);
  85. } catch ( ... ) {
  86. LeaveCriticalSection(&_cs);
  87. throw;
  88. }
  89. #else
  90. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  91. try {
  92. _q.push(v);
  93. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  94. pthread_cond_signal(const_cast <pthread_cond_t *>(&_cond));
  95. } catch ( ... ) {
  96. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  97. throw;
  98. }
  99. #endif
  100. }
  101. /**
  102. * Pop fron queue with optional timeout
  103. *
  104. * @param v Result parameter to set to next value
  105. * @param ms Milliseconds timeout or 0 for none
  106. * @return True if v was set to something, false on timeout
  107. */
  108. inline bool pop(T &v,unsigned long ms = 0)
  109. {
  110. #ifdef __WINDOWS__
  111. if (ms > 0)
  112. WaitForSingleObject(_sem,(DWORD)ms);
  113. else WaitForSingleObject(_sem,INFINITE);
  114. EnterCriticalSection(&_cs);
  115. try {
  116. if (_q.empty()) {
  117. LeaveCriticalSection(&_cs);
  118. return false;
  119. } else {
  120. v = _q.front();
  121. _q.pop();
  122. LeaveCriticalSection(&_cs);
  123. return true;
  124. }
  125. } catch ( ... ) {
  126. LeaveCriticalSection(&_cs);
  127. throw;
  128. }
  129. #else
  130. pthread_mutex_lock(const_cast <pthread_mutex_t *>(&_mh));
  131. try {
  132. if (_q.empty()) {
  133. if (ms > 0) {
  134. uint64_t when = Utils::now() + (uint64_t)ms;
  135. struct timespec ts;
  136. ts.tv_sec = (unsigned long)(when / 1000);
  137. ts.tv_nsec = (unsigned long)(when % 1000) * (unsigned long)1000000;
  138. pthread_cond_timedwait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh),&ts);
  139. } else {
  140. pthread_cond_wait(const_cast <pthread_cond_t *>(&_cond),const_cast <pthread_mutex_t *>(&_mh));
  141. }
  142. if (_q.empty()) {
  143. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  144. return false;
  145. }
  146. }
  147. v = _q.front();
  148. _q.pop();
  149. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  150. return true;
  151. } catch ( ... ) {
  152. pthread_mutex_unlock(const_cast <pthread_mutex_t *>(&_mh));
  153. throw;
  154. }
  155. #endif
  156. }
  157. private:
  158. std::queue<T> _q;
  159. #ifdef __WINDOWS__
  160. HANDLE _sem;
  161. CRITICAL_SECTION _cs;
  162. #else
  163. pthread_cond_t _cond;
  164. pthread_mutex_t _mh;
  165. #endif
  166. };
  167. } // namespace ZeroTier
  168. #endif