Skip to content
Draft
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
17 changes: 15 additions & 2 deletions Include/datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,21 @@ 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;
}

/* Macro for access to the UTC singleton */
#define PyDateTime_TimeZone_UTC PyDateTimeAPI->TimeZone_UTC
Expand Down
Loading