Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-112606: Use monotonic clock when supported in semaphore waiting in parking_lot.c #112614

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions Python/parking_lot.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
BUCKET_INIT(buckets, 256),
};

// macOS supports CLOCK_MONOTONIC, but not pthread_condattr_setclock
#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined(CLOCK_MONOTONIC) \
&& !defined(_Py_USE_SEMAPHORES)
#define CONDATTR_MONOTONIC
#endif

// Condattr for monotonic clock types
static pthread_condattr_t *condattr;

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

‘condattr’ defined but not used [-Wunused-variable]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

‘condattr’ defined but not used [-Wunused-variable]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

‘condattr’ defined but not used [-Wunused-variable]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

‘condattr’ defined but not used [-Wunused-variable]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check failure on line 59 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Windows (free-threaded) / build (arm64)

syntax error: missing '{' before '*' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]
// NULL if monotonic clock is not supported
#if defined(CONDATTR_MONOTONIC)
pthread_condattr_init(condattr);

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

parameter names (without types) in function declaration

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

data definition has no type or storage class

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

type defaults to ‘int’ in declaration of ‘pthread_condattr_init’ [-Wimplicit-int]

Check warning on line 62 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

parameter names (without types) in function declaration
pthread_condattr_setclock(condattr, CLOCK_MONOTONIC);

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.1.3)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (1.1.1w)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu SSL tests with OpenSSL (3.0.11)

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threaded) / build and test

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test

expected ‘)’ before numeric constant

Check failure on line 63 in Python/parking_lot.c

View workflow job for this annotation

GitHub Actions / Address sanitizer

expected ‘)’ before numeric constant
#endif

void
_PySemaphore_Init(_PySemaphore *sema)
{
Expand All @@ -70,7 +84,7 @@
if (pthread_mutex_init(&sema->mutex, NULL) != 0) {
Py_FatalError("parking_lot: pthread_mutex_init failed");
}
if (pthread_cond_init(&sema->cond, NULL)) {
if (pthread_cond_init(&sema->cond, condattr)) {
Py_FatalError("parking_lot: pthread_cond_init failed");
}
sema->counter = 0;
Expand Down Expand Up @@ -118,10 +132,18 @@
if (timeout >= 0) {
struct timespec ts;

#if defined(CLOCK_MONOTONIC)
_PyTime_t deadline = _PyTime_Add(_PyTime_GetMonotonicClock(), timeout);
_PyTime_AsTimespec_clamp(deadline, &ts);

err = sem_clockwait(&sema->platform_sem, CLOCK_MONOTONIC, &ts);
#else
// does not support CLOCK_MONOTONIC, use system clock
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
_PyTime_AsTimespec(deadline, &ts);
_PyTime_AsTimespec_clamp(deadline, &ts);

err = sem_timedwait(&sema->platform_sem, &ts);
#endif
}
else {
err = sem_wait(&sema->platform_sem);
Expand Down Expand Up @@ -150,8 +172,13 @@
if (timeout >= 0) {
struct timespec ts;

#if defined(CONDATTR_MONOTONIC)
_PyTime_t deadline = _PyTime_Add(_PyTime_GetMonotonicClock(), timeout);
_PyTime_AsTimespec_clamp(deadline, &ts);
#else
_PyTime_t deadline = _PyTime_Add(_PyTime_GetSystemClock(), timeout);
_PyTime_AsTimespec(deadline, &ts);
_PyTime_AsTimespec_clamp(deadline, &ts);
#endif

err = pthread_cond_timedwait(&sema->cond, &sema->mutex, &ts);
}
Expand Down
Loading