mirror of
https://github.com/python/cpython.git
synced 2026-02-21 22:50:55 +00:00
gh-141563: make PyDateTime_IMPORT thread-safe (#144210)
This commit is contained in:
parent
d99f3fc474
commit
dd30f16ac0
2 changed files with 18 additions and 2 deletions
|
|
@ -196,8 +196,23 @@ typedef struct {
|
|||
/* Define global variable for the C API and a macro for setting it. */
|
||||
static PyDateTime_CAPI *PyDateTimeAPI = NULL;
|
||||
|
||||
#define PyDateTime_IMPORT \
|
||||
PyDateTimeAPI = (PyDateTime_CAPI *)PyCapsule_Import(PyDateTime_CAPSULE_NAME, 0)
|
||||
static inline PyDateTime_CAPI *
|
||||
_PyDateTime_IMPORT(void) {
|
||||
PyDateTime_CAPI *val = _Py_atomic_load_ptr(&PyDateTimeAPI);
|
||||
if (val == NULL) {
|
||||
PyDateTime_CAPI *capi = (PyDateTime_CAPI *)PyCapsule_Import(
|
||||
PyDateTime_CAPSULE_NAME, 0);
|
||||
if (capi != NULL) {
|
||||
/* if the compare exchange fails then in that case
|
||||
another thread would have initialized it */
|
||||
_Py_atomic_compare_exchange_ptr(&PyDateTimeAPI, &val, (void *)capi);
|
||||
return capi;
|
||||
}
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
#define PyDateTime_IMPORT _PyDateTime_IMPORT()
|
||||
|
||||
/* Macro for access to the UTC singleton */
|
||||
#define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fix thread safety of :c:macro:`! PyDateTime_IMPORT`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue