gh-130421: Fix data race on timebase initialization (gh-130592)

Windows and macOS require precomputing a "timebase" in order to convert
OS timestamps into nanoseconds. Retrieve and compute this value during
runtime initialization to avoid data races when accessing the time.
This commit is contained in:
Sam Gross 2025-02-27 08:27:54 -05:00 committed by GitHub
parent 45a24f54af
commit d027787c8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 45 additions and 48 deletions

View file

@ -23,6 +23,7 @@ extern "C" {
#include "pycore_pymem.h" // struct _pymem_allocators
#include "pycore_pythread.h" // struct _pythread_runtime_state
#include "pycore_signal.h" // struct _signals_runtime_state
#include "pycore_time.h" // struct _PyTime_runtime_state
#include "pycore_tracemalloc.h" // struct _tracemalloc_runtime_state
#include "pycore_typeobject.h" // struct _types_runtime_state
#include "pycore_unicodeobject.h" // struct _Py_unicode_runtime_state
@ -168,6 +169,7 @@ typedef struct pyruntimestate {
struct _Py_float_runtime_state float_state;
struct _Py_unicode_runtime_state unicode_state;
struct _types_runtime_state types;
struct _Py_time_runtime_state time;
#if defined(__EMSCRIPTEN__) && defined(PY_CALL_TRAMPOLINE)
// Used in "Python/emscripten_trampoline.c" to choose between type

View file

@ -331,6 +331,18 @@ extern double _PyTimeFraction_Resolution(
const _PyTimeFraction *frac);
// --- _Py_time_runtime_state ------------------------------------------------
struct _Py_time_runtime_state {
#if defined(MS_WINDOWS) || defined(__APPLE__)
_PyTimeFraction base;
#else
char _unused;
#endif
};
extern PyStatus _PyTime_Init(struct _Py_time_runtime_state *state);
#ifdef __cplusplus
}
#endif