| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5572870 commit 7b3c252
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -223,8 +223,11 @@ typedef struct pyruntimestate { | |||
| 223 | 223 | int initialized; | |
| 224 | 224 | ||
| 225 | 225 | /* Set by Py_FinalizeEx(). Only reset to NULL if Py_Initialize() | |
| 226 | - is called again. */ | ||
| 227 | - PyThreadState *finalizing; | ||
| 226 | + is called again. | ||
| 227 | + | ||
| 228 | + Use _PyRuntimeState_GetFinalizing() and _PyRuntimeState_SetFinalizing() | ||
| 229 | + to access it, don't access it directly. */ | ||
| 230 | + _Py_atomic_address _finalizing; | ||
| 228 | 231 | ||
| 229 | 232 | struct pyinterpreters { | |
| 230 | 233 | PyThread_type_lock mutex; | |
@@ -279,17 +282,25 @@ PyAPI_FUNC(PyStatus) _PyRuntime_Initialize(void); | |||
| 279 | 282 | ||
| 280 | 283 | PyAPI_FUNC(void) _PyRuntime_Finalize(void); | |
| 281 | 284 | ||
| 282 | - #define _Py_CURRENTLY_FINALIZING(runtime, tstate) \ | ||
| 283 | - (runtime->finalizing == tstate) | ||
| 285 | + static inline PyThreadState* | ||
| 286 | + _PyRuntimeState_GetFinalizing(_PyRuntimeState *runtime) { | ||
| 287 | + return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->_finalizing); | ||
| 288 | + } | ||
| 289 | + | ||
| 290 | + static inline void | ||
| 291 | + _PyRuntimeState_SetFinalizing(_PyRuntimeState *runtime, PyThreadState *tstate) { | ||
| 292 | + _Py_atomic_store_relaxed(&runtime->_finalizing, (uintptr_t)tstate); | ||
| 293 | + } | ||
| 284 | 294 | ||
| 285 | 295 | PyAPI_FUNC(int) _Py_IsMainInterpreter(PyThreadState* tstate); | |
| 286 | 296 | ||
| 287 | 297 | ||
| 288 | 298 | /* Variable and macro for in-line access to current thread | |
| 289 | 299 | and interpreter state */ | |
| 290 | 300 | ||
| 291 | - #define _PyRuntimeState_GetThreadState(runtime) \ | ||
| 292 | - ((PyThreadState*)_Py_atomic_load_relaxed(&(runtime)->gilstate.tstate_current)) | ||
| 301 | + static inline PyThreadState* _PyRuntimeState_GetThreadState(_PyRuntimeState *runtime) { | ||
| 302 | + return (PyThreadState*)_Py_atomic_load_relaxed(&runtime->gilstate.tstate_current); | ||
| 303 | + } | ||
| 293 | 304 | ||
| 294 | 305 | /* Get the current Python thread state. | |
| 295 | 306 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -240,7 +240,8 @@ exit_thread_if_finalizing(PyThreadState *tstate) | |||
| 240 | 240 | { | |
| 241 | 241 | _PyRuntimeState *runtime = tstate->interp->runtime; | |
| 242 | 242 | /* _Py_Finalizing is protected by the GIL */ | |
| 243 | - if (runtime->finalizing != NULL && !_Py_CURRENTLY_FINALIZING(runtime, tstate)) { | ||
| 243 | + PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime); | ||
| 244 | + if (finalizing != NULL && finalizing != tstate) { | ||
| 244 | 245 | drop_gil(&runtime->ceval, tstate); | |
| 245 | 246 | PyThread_exit_thread(); | |
| 246 | 247 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -103,7 +103,7 @@ _PyRuntime_Finalize(void) | |||
| 103 | 103 | int | |
| 104 | 104 | _Py_IsFinalizing(void) | |
| 105 | 105 | { | |
| 106 | - return _PyRuntime.finalizing != NULL; | ||
| 106 | + return _PyRuntimeState_GetFinalizing(&_PyRuntime) != NULL; | ||
| 107 | 107 | } | |
| 108 | 108 | ||
| 109 | 109 | /* Hack to force loading of object files */ | |
@@ -507,7 +507,7 @@ pycore_init_runtime(_PyRuntimeState *runtime, | |||
| 507 | 507 | * threads still hanging around from a previous Py_Initialize/Finalize | |
| 508 | 508 | * pair :( | |
| 509 | 509 | */ | |
| 510 | - runtime->finalizing = NULL; | ||
| 510 | + _PyRuntimeState_SetFinalizing(runtime, NULL); | ||
| 511 | 511 | ||
| 512 | 512 | PyStatus status = _Py_HashRandomization_Init(config); | |
| 513 | 513 | if (_PyStatus_EXCEPTION(status)) { | |
@@ -1366,7 +1366,7 @@ Py_FinalizeEx(void) | |||
| 1366 | 1366 | ||
| 1367 | 1367 | /* Remaining threads (e.g. daemon threads) will automatically exit | |
| 1368 | 1368 | after taking the GIL (in PyEval_RestoreThread()). */ | |
| 1369 | - runtime->finalizing = tstate; | ||
| 1369 | + _PyRuntimeState_SetFinalizing(runtime, tstate); | ||
| 1370 | 1370 | runtime->initialized = 0; | |
| 1371 | 1371 | runtime->core_initialized = 0; | |
| 1372 | 1372 | ||
@@ -2131,8 +2131,9 @@ static void | |||
| 2131 | 2131 | fatal_error_dump_runtime(FILE *stream, _PyRuntimeState *runtime) | |
| 2132 | 2132 | { | |
| 2133 | 2133 | fprintf(stream, "Python runtime state: "); | |
| 2134 | - if (runtime->finalizing) { | ||
| 2135 | - fprintf(stream, "finalizing (tstate=%p)", runtime->finalizing); | ||
| 2134 | + PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime); | ||
| 2135 | + if (finalizing) { | ||
| 2136 | + fprintf(stream, "finalizing (tstate=%p)", finalizing); | ||
| 2136 | 2137 | } | |
| 2137 | 2138 | else if (runtime->initialized) { | |
| 2138 | 2139 | fprintf(stream, "initialized"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -292,7 +292,7 @@ PyInterpreterState_Clear(PyInterpreterState *interp) | |||
| 292 | 292 | Py_CLEAR(interp->after_forkers_parent); | |
| 293 | 293 | Py_CLEAR(interp->after_forkers_child); | |
| 294 | 294 | #endif | |
| 295 | - if (runtime->finalizing == NULL) { | ||
| 295 | + if (_PyRuntimeState_GetFinalizing(runtime) == NULL) { | ||
| 296 | 296 | _PyWarnings_Fini(interp); | |
| 297 | 297 | } | |
| 298 | 298 | // XXX Once we have one allocator per interpreter (i.e. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -289,8 +289,9 @@ _PySys_ClearAuditHooks(void) | |||
| 289 | 289 | /* Must be finalizing to clear hooks */ | |
| 290 | 290 | _PyRuntimeState *runtime = &_PyRuntime; | |
| 291 | 291 | PyThreadState *ts = _PyRuntimeState_GetThreadState(runtime); | |
| 292 | - assert(!ts || _Py_CURRENTLY_FINALIZING(runtime, ts)); | ||
| 293 | - if (!ts || !_Py_CURRENTLY_FINALIZING(runtime, ts)) { | ||
| 292 | + PyThreadState *finalizing = _PyRuntimeState_GetFinalizing(runtime); | ||
| 293 | + assert(!ts || finalizing == ts); | ||
| 294 | + if (!ts || finalizing != ts) { | ||
| 294 | 295 | return; | |
| 295 | 296 | } | |
| 296 | 297 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments