condvar.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Portable condition variable support for windows and pthreads.
  3. * Everything is inline, this header can be included where needed.
  4. *
  5. * APIs generally return 0 on success and non-zero on error,
  6. * and the caller needs to use its platform's error mechanism to
  7. * discover the error (errno, or GetLastError())
  8. *
  9. * Note that some implementations cannot distinguish between a
  10. * condition variable wait time-out and successful wait. Most often
  11. * the difference is moot anyway since the wait condition must be
  12. * re-checked.
  13. * PyCOND_TIMEDWAIT, in addition to returning negative on error,
  14. * thus returns 0 on regular success, 1 on timeout
  15. * or 2 if it can't tell.
  16. *
  17. * There are at least two caveats with using these condition variables,
  18. * due to the fact that they may be emulated with Semaphores on
  19. * Windows:
  20. * 1) While PyCOND_SIGNAL() will wake up at least one thread, we
  21. * cannot currently guarantee that it will be one of the threads
  22. * already waiting in a PyCOND_WAIT() call. It _could_ cause
  23. * the wakeup of a subsequent thread to try a PyCOND_WAIT(),
  24. * including the thread doing the PyCOND_SIGNAL() itself.
  25. * The same applies to PyCOND_BROADCAST(), if N threads are waiting
  26. * then at least N threads will be woken up, but not necessarily
  27. * those already waiting.
  28. * For this reason, don't make the scheduling assumption that a
  29. * specific other thread will get the wakeup signal
  30. * 2) The _mutex_ must be held when calling PyCOND_SIGNAL() and
  31. * PyCOND_BROADCAST().
  32. * While e.g. the posix standard strongly recommends that the mutex
  33. * associated with the condition variable is held when a
  34. * pthread_cond_signal() call is made, this is not a hard requirement,
  35. * although scheduling will not be "reliable" if it isn't. Here
  36. * the mutex is used for internal synchronization of the emulated
  37. * Condition Variable.
  38. */
  39. #ifndef _CONDVAR_IMPL_H_
  40. #define _CONDVAR_IMPL_H_
  41. #include "Python.h"
  42. #include "pycore_condvar.h"
  43. #ifdef _POSIX_THREADS
  44. /*
  45. * POSIX support
  46. */
  47. /* These private functions are implemented in Python/thread_pthread.h */
  48. int _PyThread_cond_init(PyCOND_T *cond);
  49. void _PyThread_cond_after(long long us, struct timespec *abs);
  50. /* The following functions return 0 on success, nonzero on error */
  51. #define PyMUTEX_INIT(mut) pthread_mutex_init((mut), NULL)
  52. #define PyMUTEX_FINI(mut) pthread_mutex_destroy(mut)
  53. #define PyMUTEX_LOCK(mut) pthread_mutex_lock(mut)
  54. #define PyMUTEX_UNLOCK(mut) pthread_mutex_unlock(mut)
  55. #define PyCOND_INIT(cond) _PyThread_cond_init(cond)
  56. #define PyCOND_FINI(cond) pthread_cond_destroy(cond)
  57. #define PyCOND_SIGNAL(cond) pthread_cond_signal(cond)
  58. #define PyCOND_BROADCAST(cond) pthread_cond_broadcast(cond)
  59. #define PyCOND_WAIT(cond, mut) pthread_cond_wait((cond), (mut))
  60. /* return 0 for success, 1 on timeout, -1 on error */
  61. Py_LOCAL_INLINE(int)
  62. PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, long long us)
  63. {
  64. struct timespec abs_timeout;
  65. _PyThread_cond_after(us, &abs_timeout);
  66. int ret = pthread_cond_timedwait(cond, mut, &abs_timeout);
  67. if (ret == ETIMEDOUT) {
  68. return 1;
  69. }
  70. if (ret) {
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. #elif defined(NT_THREADS)
  76. /*
  77. * Windows (XP, 2003 server and later, as well as (hopefully) CE) support
  78. *
  79. * Emulated condition variables ones that work with XP and later, plus
  80. * example native support on VISTA and onwards.
  81. */
  82. #if _PY_EMULATED_WIN_CV
  83. /* The mutex is a CriticalSection object and
  84. The condition variables is emulated with the help of a semaphore.
  85. This implementation still has the problem that the threads woken
  86. with a "signal" aren't necessarily those that are already
  87. waiting. It corresponds to listing 2 in:
  88. http://birrell.org/andrew/papers/ImplementingCVs.pdf
  89. Generic emulations of the pthread_cond_* API using
  90. earlier Win32 functions can be found on the web.
  91. The following read can be give background information to these issues,
  92. but the implementations are all broken in some way.
  93. http://www.cse.wustl.edu/~schmidt/win32-cv-1.html
  94. */
  95. Py_LOCAL_INLINE(int)
  96. PyMUTEX_INIT(PyMUTEX_T *cs)
  97. {
  98. InitializeCriticalSection(cs);
  99. return 0;
  100. }
  101. Py_LOCAL_INLINE(int)
  102. PyMUTEX_FINI(PyMUTEX_T *cs)
  103. {
  104. DeleteCriticalSection(cs);
  105. return 0;
  106. }
  107. Py_LOCAL_INLINE(int)
  108. PyMUTEX_LOCK(PyMUTEX_T *cs)
  109. {
  110. EnterCriticalSection(cs);
  111. return 0;
  112. }
  113. Py_LOCAL_INLINE(int)
  114. PyMUTEX_UNLOCK(PyMUTEX_T *cs)
  115. {
  116. LeaveCriticalSection(cs);
  117. return 0;
  118. }
  119. Py_LOCAL_INLINE(int)
  120. PyCOND_INIT(PyCOND_T *cv)
  121. {
  122. /* A semaphore with a "large" max value, The positive value
  123. * is only needed to catch those "lost wakeup" events and
  124. * race conditions when a timed wait elapses.
  125. */
  126. cv->sem = CreateSemaphore(NULL, 0, 100000, NULL);
  127. if (cv->sem==NULL)
  128. return -1;
  129. cv->waiting = 0;
  130. return 0;
  131. }
  132. Py_LOCAL_INLINE(int)
  133. PyCOND_FINI(PyCOND_T *cv)
  134. {
  135. return CloseHandle(cv->sem) ? 0 : -1;
  136. }
  137. /* this implementation can detect a timeout. Returns 1 on timeout,
  138. * 0 otherwise (and -1 on error)
  139. */
  140. Py_LOCAL_INLINE(int)
  141. _PyCOND_WAIT_MS(PyCOND_T *cv, PyMUTEX_T *cs, DWORD ms)
  142. {
  143. DWORD wait;
  144. cv->waiting++;
  145. PyMUTEX_UNLOCK(cs);
  146. /* "lost wakeup bug" would occur if the caller were interrupted here,
  147. * but we are safe because we are using a semaphore which has an internal
  148. * count.
  149. */
  150. wait = WaitForSingleObjectEx(cv->sem, ms, FALSE);
  151. PyMUTEX_LOCK(cs);
  152. if (wait != WAIT_OBJECT_0)
  153. --cv->waiting;
  154. /* Here we have a benign race condition with PyCOND_SIGNAL.
  155. * When failure occurs or timeout, it is possible that
  156. * PyCOND_SIGNAL also decrements this value
  157. * and signals releases the mutex. This is benign because it
  158. * just means an extra spurious wakeup for a waiting thread.
  159. * ('waiting' corresponds to the semaphore's "negative" count and
  160. * we may end up with e.g. (waiting == -1 && sem.count == 1). When
  161. * a new thread comes along, it will pass right through, having
  162. * adjusted it to (waiting == 0 && sem.count == 0).
  163. */
  164. if (wait == WAIT_FAILED)
  165. return -1;
  166. /* return 0 on success, 1 on timeout */
  167. return wait != WAIT_OBJECT_0;
  168. }
  169. Py_LOCAL_INLINE(int)
  170. PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs)
  171. {
  172. int result = _PyCOND_WAIT_MS(cv, cs, INFINITE);
  173. return result >= 0 ? 0 : result;
  174. }
  175. Py_LOCAL_INLINE(int)
  176. PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long long us)
  177. {
  178. return _PyCOND_WAIT_MS(cv, cs, (DWORD)(us/1000));
  179. }
  180. Py_LOCAL_INLINE(int)
  181. PyCOND_SIGNAL(PyCOND_T *cv)
  182. {
  183. /* this test allows PyCOND_SIGNAL to be a no-op unless required
  184. * to wake someone up, thus preventing an unbounded increase of
  185. * the semaphore's internal counter.
  186. */
  187. if (cv->waiting > 0) {
  188. /* notifying thread decreases the cv->waiting count so that
  189. * a delay between notify and actual wakeup of the target thread
  190. * doesn't cause a number of extra ReleaseSemaphore calls.
  191. */
  192. cv->waiting--;
  193. return ReleaseSemaphore(cv->sem, 1, NULL) ? 0 : -1;
  194. }
  195. return 0;
  196. }
  197. Py_LOCAL_INLINE(int)
  198. PyCOND_BROADCAST(PyCOND_T *cv)
  199. {
  200. int waiting = cv->waiting;
  201. if (waiting > 0) {
  202. cv->waiting = 0;
  203. return ReleaseSemaphore(cv->sem, waiting, NULL) ? 0 : -1;
  204. }
  205. return 0;
  206. }
  207. #else /* !_PY_EMULATED_WIN_CV */
  208. Py_LOCAL_INLINE(int)
  209. PyMUTEX_INIT(PyMUTEX_T *cs)
  210. {
  211. InitializeSRWLock(cs);
  212. return 0;
  213. }
  214. Py_LOCAL_INLINE(int)
  215. PyMUTEX_FINI(PyMUTEX_T *cs)
  216. {
  217. return 0;
  218. }
  219. Py_LOCAL_INLINE(int)
  220. PyMUTEX_LOCK(PyMUTEX_T *cs)
  221. {
  222. AcquireSRWLockExclusive(cs);
  223. return 0;
  224. }
  225. Py_LOCAL_INLINE(int)
  226. PyMUTEX_UNLOCK(PyMUTEX_T *cs)
  227. {
  228. ReleaseSRWLockExclusive(cs);
  229. return 0;
  230. }
  231. Py_LOCAL_INLINE(int)
  232. PyCOND_INIT(PyCOND_T *cv)
  233. {
  234. InitializeConditionVariable(cv);
  235. return 0;
  236. }
  237. Py_LOCAL_INLINE(int)
  238. PyCOND_FINI(PyCOND_T *cv)
  239. {
  240. return 0;
  241. }
  242. Py_LOCAL_INLINE(int)
  243. PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs)
  244. {
  245. return SleepConditionVariableSRW(cv, cs, INFINITE, 0) ? 0 : -1;
  246. }
  247. /* This implementation makes no distinction about timeouts. Signal
  248. * 2 to indicate that we don't know.
  249. */
  250. Py_LOCAL_INLINE(int)
  251. PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long long us)
  252. {
  253. return SleepConditionVariableSRW(cv, cs, (DWORD)(us/1000), 0) ? 2 : -1;
  254. }
  255. Py_LOCAL_INLINE(int)
  256. PyCOND_SIGNAL(PyCOND_T *cv)
  257. {
  258. WakeConditionVariable(cv);
  259. return 0;
  260. }
  261. Py_LOCAL_INLINE(int)
  262. PyCOND_BROADCAST(PyCOND_T *cv)
  263. {
  264. WakeAllConditionVariable(cv);
  265. return 0;
  266. }
  267. #endif /* _PY_EMULATED_WIN_CV */
  268. #endif /* _POSIX_THREADS, NT_THREADS */
  269. #endif /* _CONDVAR_IMPL_H_ */