Skip to content

Commit 33096fe

Browse files
committed
The PyCOND_TIMEDWAIT must use microseconds for the timeout argument
in order to have the same resolution as pthreads condition variables. At the same time, it must be large enough to accept 31 bits of milliseconds, which is the maximum timeout value in the windows API. A PY_LONG_LONG of microseconds fullfills both requirements. This closes issue python#20737
1 parent ba74885 commit 33096fe

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

Python/condvar.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#include <pthread.h>
6161

6262
#define PyCOND_ADD_MICROSECONDS(tv, interval) \
63-
do { \
63+
do { /* TODO: add overflow and truncation checks */ \
6464
tv.tv_usec += (long) interval; \
6565
tv.tv_sec += tv.tv_usec / 1000000; \
6666
tv.tv_usec %= 1000000; \
@@ -89,7 +89,7 @@ do { \
8989

9090
/* return 0 for success, 1 on timeout, -1 on error */
9191
Py_LOCAL_INLINE(int)
92-
PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, long us)
92+
PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, PY_LONG_LONG us)
9393
{
9494
int r;
9595
struct timespec ts;
@@ -270,9 +270,9 @@ PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs)
270270
}
271271

272272
Py_LOCAL_INLINE(int)
273-
PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long us)
273+
PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, PY_LONG_LONG us)
274274
{
275-
return _PyCOND_WAIT_MS(cv, cs, us/1000);
275+
return _PyCOND_WAIT_MS(cv, cs, (DWORD)(us/1000));
276276
}
277277

278278
Py_LOCAL_INLINE(int)
@@ -363,9 +363,9 @@ PyCOND_WAIT(PyCOND_T *cv, PyMUTEX_T *cs)
363363
* 2 to indicate that we don't know.
364364
*/
365365
Py_LOCAL_INLINE(int)
366-
PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, long us)
366+
PyCOND_TIMEDWAIT(PyCOND_T *cv, PyMUTEX_T *cs, PY_LONG_LONG us)
367367
{
368-
return SleepConditionVariableSRW(cv, cs, us/1000, 0) ? 2 : -1;
368+
return SleepConditionVariableSRW(cv, cs, (DWORD)(us/1000), 0) ? 2 : -1;
369369
}
370370

371371
Py_LOCAL_INLINE(int)

Python/thread_nt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ EnterNonRecursiveMutex(PNRMUTEX mutex, DWORD milliseconds)
7777
/* wait at least until the target */
7878
DWORD now, target = GetTickCount() + milliseconds;
7979
while (mutex->locked) {
80-
if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, milliseconds*1000) < 0) {
80+
if (PyCOND_TIMEDWAIT(&mutex->cv, &mutex->cs, (PY_LONG_LONG)milliseconds*1000) < 0) {
8181
result = WAIT_FAILED;
8282
break;
8383
}

0 commit comments

Comments
 (0)