Skip to content

Commit cc959da

Browse files
code review: make exiting thread safe
1 parent 68f8ec6 commit cc959da

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

Python/context.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,7 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)
229229
{
230230
ENSURE_Context(octx, -1)
231231
PyContext *ctx = (PyContext *)octx;
232-
#ifdef Py_GIL_DISABLED
233-
int already_entered = _Py_atomic_exchange_int(&ctx->ctx_entered, 0);
234-
#else
235-
int already_entered = ctx->ctx_entered;
236-
ctx->ctx_entered = 0;
237-
#endif
232+
int already_entered = FT_ATOMIC_LOAD_INT_RELAXED(ctx->ctx_entered);
238233

239234
if (!already_entered) {
240235
PyErr_Format(PyExc_RuntimeError,
@@ -253,6 +248,7 @@ _PyContext_Exit(PyThreadState *ts, PyObject *octx)
253248
Py_SETREF(ts->context, (PyObject *)ctx->ctx_prev);
254249

255250
ctx->ctx_prev = NULL;
251+
FT_ATOMIC_STORE_INT(ctx->ctx_entered, 0);
256252
context_switched(ts);
257253
return 0;
258254
}

0 commit comments

Comments
 (0)