condition_variable.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #ifndef __ZMQ_CONDITON_VARIABLE_HPP_INCLUDED__
  25. #define __ZMQ_CONDITON_VARIABLE_HPP_INCLUDED__
  26. #include "err.hpp"
  27. #include "mutex.hpp"
  28. // Condition variable class encapsulates OS mutex in a platform-independent way.
  29. #if defined(ZMQ_USE_CV_IMPL_NONE)
  30. namespace zmq
  31. {
  32. class condition_variable_t
  33. {
  34. public:
  35. inline condition_variable_t () { zmq_assert (false); }
  36. inline int wait (mutex_t *mutex_, int timeout_)
  37. {
  38. zmq_assert (false);
  39. return -1;
  40. }
  41. inline void broadcast () { zmq_assert (false); }
  42. ZMQ_NON_COPYABLE_NOR_MOVABLE (condition_variable_t)
  43. };
  44. }
  45. #elif defined(ZMQ_USE_CV_IMPL_WIN32API)
  46. #include "windows.hpp"
  47. namespace zmq
  48. {
  49. class condition_variable_t
  50. {
  51. public:
  52. inline condition_variable_t () { InitializeConditionVariable (&_cv); }
  53. inline int wait (mutex_t *mutex_, int timeout_)
  54. {
  55. int rc = SleepConditionVariableCS (&_cv, mutex_->get_cs (), timeout_);
  56. if (rc != 0)
  57. return 0;
  58. rc = GetLastError ();
  59. if (rc != ERROR_TIMEOUT)
  60. win_assert (rc);
  61. errno = EAGAIN;
  62. return -1;
  63. }
  64. inline void broadcast () { WakeAllConditionVariable (&_cv); }
  65. private:
  66. CONDITION_VARIABLE _cv;
  67. ZMQ_NON_COPYABLE_NOR_MOVABLE (condition_variable_t)
  68. };
  69. }
  70. #elif defined(ZMQ_USE_CV_IMPL_STL11)
  71. #include <condition_variable>
  72. namespace zmq
  73. {
  74. class condition_variable_t
  75. {
  76. public:
  77. condition_variable_t () ZMQ_DEFAULT;
  78. int wait (mutex_t *mutex_, int timeout_)
  79. {
  80. // this assumes that the mutex mutex_ has been locked by the caller
  81. int res = 0;
  82. if (timeout_ == -1) {
  83. _cv.wait (
  84. *mutex_); // unlock mtx and wait cv.notify_all(), lock mtx after cv.notify_all()
  85. } else if (_cv.wait_for (*mutex_, std::chrono::milliseconds (timeout_))
  86. == std::cv_status::timeout) {
  87. // time expired
  88. errno = EAGAIN;
  89. res = -1;
  90. }
  91. return res;
  92. }
  93. void broadcast ()
  94. {
  95. // this assumes that the mutex associated with _cv has been locked by the caller
  96. _cv.notify_all ();
  97. }
  98. private:
  99. std::condition_variable_any _cv;
  100. ZMQ_NON_COPYABLE_NOR_MOVABLE (condition_variable_t)
  101. };
  102. }
  103. #elif defined(ZMQ_USE_CV_IMPL_VXWORKS)
  104. #include <sysLib.h>
  105. namespace zmq
  106. {
  107. class condition_variable_t
  108. {
  109. public:
  110. inline condition_variable_t () ZMQ_DEFAULT;
  111. inline ~condition_variable_t ()
  112. {
  113. scoped_lock_t l (_listenersMutex);
  114. for (size_t i = 0; i < _listeners.size (); i++) {
  115. semDelete (_listeners[i]);
  116. }
  117. }
  118. inline int wait (mutex_t *mutex_, int timeout_)
  119. {
  120. //Atomically releases lock, blocks the current executing thread,
  121. //and adds it to the list of threads waiting on *this. The thread
  122. //will be unblocked when broadcast() is executed.
  123. //It may also be unblocked spuriously. When unblocked, regardless
  124. //of the reason, lock is reacquired and wait exits.
  125. SEM_ID sem = semBCreate (SEM_Q_PRIORITY, SEM_EMPTY);
  126. {
  127. scoped_lock_t l (_listenersMutex);
  128. _listeners.push_back (sem);
  129. }
  130. mutex_->unlock ();
  131. int rc;
  132. if (timeout_ < 0)
  133. rc = semTake (sem, WAIT_FOREVER);
  134. else {
  135. int ticksPerSec = sysClkRateGet ();
  136. int timeoutTicks = (timeout_ * ticksPerSec) / 1000 + 1;
  137. rc = semTake (sem, timeoutTicks);
  138. }
  139. {
  140. scoped_lock_t l (_listenersMutex);
  141. // remove sem from listeners
  142. for (size_t i = 0; i < _listeners.size (); i++) {
  143. if (_listeners[i] == sem) {
  144. _listeners.erase (_listeners.begin () + i);
  145. break;
  146. }
  147. }
  148. semDelete (sem);
  149. }
  150. mutex_->lock ();
  151. if (rc == 0)
  152. return 0;
  153. if (rc == S_objLib_OBJ_TIMEOUT) {
  154. errno = EAGAIN;
  155. return -1;
  156. }
  157. return -1;
  158. }
  159. inline void broadcast ()
  160. {
  161. scoped_lock_t l (_listenersMutex);
  162. for (size_t i = 0; i < _listeners.size (); i++) {
  163. semGive (_listeners[i]);
  164. }
  165. }
  166. private:
  167. mutex_t _listenersMutex;
  168. std::vector<SEM_ID> _listeners;
  169. ZMQ_NON_COPYABLE_NOR_MOVABLE (condition_variable_t)
  170. };
  171. }
  172. #elif defined(ZMQ_USE_CV_IMPL_PTHREADS)
  173. #include <pthread.h>
  174. #if defined(__ANDROID_API__) && __ANDROID_API__ < 21
  175. #define ANDROID_LEGACY
  176. extern "C" int pthread_cond_timedwait_monotonic_np (pthread_cond_t *,
  177. pthread_mutex_t *,
  178. const struct timespec *);
  179. #endif
  180. namespace zmq
  181. {
  182. class condition_variable_t
  183. {
  184. public:
  185. inline condition_variable_t ()
  186. {
  187. pthread_condattr_t attr;
  188. pthread_condattr_init (&attr);
  189. #if !defined(ZMQ_HAVE_OSX) && !defined(ANDROID_LEGACY)
  190. pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
  191. #endif
  192. int rc = pthread_cond_init (&_cond, &attr);
  193. posix_assert (rc);
  194. }
  195. inline ~condition_variable_t ()
  196. {
  197. int rc = pthread_cond_destroy (&_cond);
  198. posix_assert (rc);
  199. }
  200. inline int wait (mutex_t *mutex_, int timeout_)
  201. {
  202. int rc;
  203. if (timeout_ != -1) {
  204. struct timespec timeout;
  205. #ifdef ZMQ_HAVE_OSX
  206. timeout.tv_sec = 0;
  207. timeout.tv_nsec = 0;
  208. #else
  209. rc = clock_gettime (CLOCK_MONOTONIC, &timeout);
  210. posix_assert (rc);
  211. #endif
  212. timeout.tv_sec += timeout_ / 1000;
  213. timeout.tv_nsec += (timeout_ % 1000) * 1000000;
  214. if (timeout.tv_nsec >= 1000000000) {
  215. timeout.tv_sec++;
  216. timeout.tv_nsec -= 1000000000;
  217. }
  218. #ifdef ZMQ_HAVE_OSX
  219. rc = pthread_cond_timedwait_relative_np (
  220. &_cond, mutex_->get_mutex (), &timeout);
  221. #elif defined(ANDROID_LEGACY)
  222. rc = pthread_cond_timedwait_monotonic_np (
  223. &_cond, mutex_->get_mutex (), &timeout);
  224. #else
  225. rc =
  226. pthread_cond_timedwait (&_cond, mutex_->get_mutex (), &timeout);
  227. #endif
  228. } else
  229. rc = pthread_cond_wait (&_cond, mutex_->get_mutex ());
  230. if (rc == 0)
  231. return 0;
  232. if (rc == ETIMEDOUT) {
  233. errno = EAGAIN;
  234. return -1;
  235. }
  236. posix_assert (rc);
  237. return -1;
  238. }
  239. inline void broadcast ()
  240. {
  241. int rc = pthread_cond_broadcast (&_cond);
  242. posix_assert (rc);
  243. }
  244. private:
  245. pthread_cond_t _cond;
  246. ZMQ_NON_COPYABLE_NOR_MOVABLE (condition_variable_t)
  247. };
  248. }
  249. #endif
  250. #endif