8000 gh-74953: _PyThread_cond_after() uses _PyTime_t · python/cpython@bcc9696 · GitHub
[go: up one dir, main page]

Skip to content

Commit bcc9696

Browse files
committed
gh-74953: _PyThread_cond_after() uses _PyTime_t
pthread _PyThread_cond_after() implementation now uses the _PyTime_t type to handle properly overflow: clamp to the maximum value.
1 parent c735d54 commit bcc9696

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

Python/condvar.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ void _PyThread_cond_after(long long us, struct timespec *abs);
6868
Py_LOCAL_INLINE(int)
6969
PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, long long us)
7070
{
71-
struct timespec abs;
72-
_PyThread_cond_after(us, &abs);
73-
int ret = pthread_cond_timedwait(cond, mut, &abs);
71+
struct timespec abs_timeout;
72+
_PyThread_cond_after(us, &abs_timeout);
73+
int ret = pthread_cond_timedwait(cond, mut, &abs_timeout);
7474
if (ret == ETIMEDOUT) {
7575
return 1;
7676
}

Python/thread_pthread.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,23 +156,23 @@ _PyThread_cond_init(PyCOND_T *cond)
156156
return pthread_cond_init(cond, condattr_monotonic);
157157
}
158158

159+
159160
void
160161
_PyThread_cond_after(long long us, struct timespec *abs)
161162
{
163+
_PyTime_t timeout = _PyTime_FromMicrosecondsClamp(us);
164+
_PyTime_t t;
162165
#ifdef CONDATTR_MONOTONIC
163166
if (condattr_monotonic) {
164-
clock_gettime(CLOCK_MONOTONIC, abs);
165-
abs->tv_sec += us / 1000000;
166-
abs->tv_nsec += (us % 1000000) * 1000;
167-
abs->tv_sec += abs->tv_nsec / 1000000000;
168-
abs->tv_nsec %= 1000000000;
169-
return;
167+
t = _PyTime_GetMonotonicClock();
170168
}
169+
else
171170
#endif
172-
173-
struct timespec ts;
174-
MICROSECONDS_TO_TIMESPEC(us, ts);
175-
*abs = ts;
171+
{
172+
t = _PyTime_GetSystemClock();
173+
}
174+
t = _PyTime_Add(t, timeout);
175+
_PyTime_AsTimespec_clamp(t, abs);
176176
}
177177

178178

@@ -639,17 +639,17 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
639639
goto unlock;
640640
}
641641

642-
struct timespec abs;
642+
struct timespec abs_timeout;
643643
if (microseconds > 0) {
644-
_PyThread_cond_after(microseconds, &abs);
644+
_PyThread_cond_after(microseconds, &abs_timeout);
645645
}
646646
// Continue trying until we get the lock
647647

648648
// mut must be locked by me -- part of the condition protocol
649649
while (1) {
650650
if (microseconds > 0) {
651651
status = pthread_cond_timedwait(&thelock->lock_released,
652-
&thelock->mut, &abs);
652+
&thelock->mut, &abs_timeout);
653653
if (status == 1) {
654654
break;
655655
}

0 commit comments

Comments
 (0)
0