summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2023-11-08 22:39:29 (GMT)
committerGitHub <noreply@github.com>2023-11-08 22:39:29 (GMT)
commit31c90d5838e8d6e4c47d98500a34810ccb33a6d4 (patch)
tree5be595b11ca17cf1f1bd5875a69a04b927f10dff /Python/pystate.c
parent0b718e6407da65b838576a2459d630824ca62155 (diff)
downloadcpython-31c90d5838e8d6e4c47d98500a34810ccb33a6d4.zip
cpython-31c90d5838e8d6e4c47d98500a34810ccb33a6d4.tar.gz
cpython-31c90d5838e8d6e4c47d98500a34810ccb33a6d4.tar.bz2
gh-111569: Implement Python critical section API (gh-111571)
Critical sections are helpers to replace the global interpreter lock with finer grained locking. They provide similar guarantees to the GIL and avoid the deadlock risk that plain locking involves. Critical sections are implicitly ended whenever the GIL would be released. They are resumed when the GIL would be acquired. Nested critical sections behave as if the sections were interleaved.
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index b369a56..991d8d2 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -4,6 +4,7 @@
#include "Python.h"
#include "pycore_ceval.h"
#include "pycore_code.h" // stats
+#include "pycore_critical_section.h" // _PyCriticalSection_Resume()
#include "pycore_dtoa.h" // _dtoa_state_INIT()
#include "pycore_emscripten_trampoline.h" // _Py_EmscriptenTrampoline_Init()
#include "pycore_frame.h"
@@ -1911,6 +1912,12 @@ _PyThreadState_Attach(PyThreadState *tstate)
Py_FatalError("thread attach failed");
}
+ // Resume previous critical section. This acquires the lock(s) from the
+ // top-most critical section.
+ if (tstate->critical_section != 0) {
+ _PyCriticalSection_Resume(tstate);
+ }
+
#if defined(Py_DEBUG)
errno = err;
#endif
@@ -1922,6 +1929,9 @@ _PyThreadState_Detach(PyThreadState *tstate)
// XXX assert(tstate_is_alive(tstate) && tstate_is_bound(tstate));
assert(tstate->state == _Py_THREAD_ATTACHED);
assert(tstate == current_fast_get(&_PyRuntime));
+ if (tstate->critical_section != 0) {
+ _PyCriticalSection_SuspendAll(tstate);
+ }
tstate_set_detached(tstate);
tstate_deactivate(tstate);
current_fast_clear(&_PyRuntime);