From bcc9696314d90f882be0579ceb6cdb252372b9f0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 21 Jun 2022 14:25:29 +0200 Subject: [PATCH 1/2] 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. --- Python/condvar.h | 6 +++--- Python/thread_pthread.h | 26 +++++++++++++------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Python/condvar.h b/Python/condvar.h index e5df7ff132802f..4ddc5311cf8fad 100644 --- a/Python/condvar.h +++ b/Python/condvar.h @@ -68,9 +68,9 @@ void _PyThread_cond_after(long long us, struct timespec *abs); Py_LOCAL_INLINE(int) PyCOND_TIMEDWAIT(PyCOND_T *cond, PyMUTEX_T *mut, long long us) { - struct timespec abs; - _PyThread_cond_after(us, &abs); - int ret = pthread_cond_timedwait(cond, mut, &abs); + struct timespec abs_timeout; + _PyThread_cond_after(us, &abs_timeout); + int ret = pthread_cond_timedwait(cond, mut, &abs_timeout); if (ret == ETIMEDOUT) { return 1; } diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index a5719c39bc0125..401365dbf26ee9 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -156,23 +156,23 @@ _PyThread_cond_init(PyCOND_T *cond) return pthread_cond_init(cond, condattr_monotonic); } + void _PyThread_cond_after(long long us, struct timespec *abs) { + _PyTime_t timeout = _PyTime_FromMicrosecondsClamp(us); + _PyTime_t t; #ifdef CONDATTR_MONOTONIC if (condattr_monotonic) { - clock_gettime(CLOCK_MONOTONIC, abs); - abs->tv_sec += us / 1000000; - abs->tv_nsec += (us % 1000000) * 1000; - abs->tv_sec += abs->tv_nsec / 1000000000; - abs->tv_nsec %= 1000000000; - return; + t = _PyTime_GetMonotonicClock(); } + else #endif - - struct timespec ts; - MICROSECONDS_TO_TIMESPEC(us, ts); - *abs = ts; + { + t = _PyTime_GetSystemClock(); + } + t = _PyTime_Add(t, timeout); + _PyTime_AsTimespec_clamp(t, abs); } @@ -639,9 +639,9 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds, goto unlock; } - struct timespec abs; + struct timespec abs_timeout; if (microseconds > 0) { - _PyThread_cond_after(microseconds, &abs); + _PyThread_cond_after(microseconds, &abs_timeout); } // Continue trying until we get the lock @@ -649,7 +649,7 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds, while (1) { if (microseconds > 0) { status = pthread_cond_timedwait(&thelock->lock_released, - &thelock->mut, &abs); + &thelock->mut, &abs_timeout); if (status == 1) { break; } From cc4a72776dd78b90a9bb1f750cfdfd88fa0dcbe8 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 21 Jun 2022 15:16:55 +0200 Subject: [PATCH 2/2] Remove MICROSECONDS_TO_TIMESPEC() --- Python/thread_pthread.h | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index 401365dbf26ee9..c310d72abd2d58 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -113,19 +113,6 @@ #endif -#define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \ -do { \ - struct timeval tv; \ - gettimeofday(&tv, NULL); \ - tv.tv_usec += microseconds % 1000000; \ - tv.tv_sec += microseconds / 1000000; \ - tv.tv_sec += tv.tv_usec / 1000000; \ - tv.tv_usec %= 1000000; \ - ts.tv_sec = tv.tv_sec; \ - ts.tv_nsec = tv.tv_usec * 1000; \ -} while(0) - - /* * pthread_cond support */ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy