Skip to content

Commit 89af5d5

Browse files
committed
Make PyGILStateEnsure reentrant
1 parent 78be345 commit 89af5d5

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

graalpython/com.oracle.graal.python.cext/include/pystate.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (c) 2018, 2022, Oracle and/or its affiliates.
1+
/* Copyright (c) 2018, 2023, Oracle and/or its affiliates.
22
* Copyright (C) 1996-2020 Python Software Foundation
33
*
44
* Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2
@@ -97,9 +97,8 @@ PyAPI_FUNC(PyFrameObject*) PyThreadState_GetFrame(PyThreadState *tstate);
9797
PyAPI_FUNC(uint64_t) PyThreadState_GetID(PyThreadState *tstate);
9898
#endif
9999

100-
typedef
101-
enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
102-
PyGILState_STATE;
100+
/* GraalVM change: we need more state bits */
101+
typedef int PyGILState_STATE;
103102

104103

105104
/* Ensure that the current thread is ready to call the Python

graalpython/com.oracle.graal.python.cext/src/pystate.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,12 @@ int PyState_RemoveModule(struct PyModuleDef* def) {
118118

119119
// This function has a different implementation on NFI in capi_native.c
120120
PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure() {
121-
int res = GraalPyTruffleGILState_Ensure();
122-
return res ? PyGILState_LOCKED : PyGILState_UNLOCKED;
121+
return GraalPyTruffleGILState_Ensure();
123122
}
124123

125124
// This function has a different implementation on NFI in capi_native.c
126125
PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE state) {
127-
if (state == PyGILState_LOCKED) {
126+
if (state) {
128127
GraalPyTruffleGILState_Release();
129128
}
130129
}

graalpython/com.oracle.graal.python.jni/src/capi_native.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,15 +465,27 @@ void finalizeCAPI() {
465465
GraalPy_set_PyObject_ob_refcnt = nop_GraalPy_set_PyObject_ob_refcnt;
466466
}
467467

468+
#define _PYGILSTATE_LOCKED 0x1
469+
#define _PYGILSTATE_ATTACHED 0x2
470+
468471
PyAPI_FUNC(PyGILState_STATE) PyGILState_Ensure() {
469-
(*TRUFFLE_CONTEXT)->attachCurrentThread(TRUFFLE_CONTEXT);
470-
int res = PyTruffleGILState_Ensure();
471-
return res ? PyGILState_LOCKED : PyGILState_UNLOCKED;
472+
int result = 0;
473+
if ((*TRUFFLE_CONTEXT)->getTruffleEnv(TRUFFLE_CONTEXT) == NULL) {
474+
(*TRUFFLE_CONTEXT)->attachCurrentThread(TRUFFLE_CONTEXT);
475+
result |= _PYGILSTATE_ATTACHED;
476+
}
477+
int locked = PyTruffleGILState_Ensure();
478+
if (locked) {
479+
result |= _PYGILSTATE_LOCKED;
480+
}
481+
return result;
472482
}
473483

474484
PyAPI_FUNC(void) PyGILState_Release(PyGILState_STATE state) {
475-
if (state == PyGILState_LOCKED) {
485+
if (state & _PYGILSTATE_LOCKED) {
476486
PyTruffleGILState_Release();
487+
}
488+
if (state & _PYGILSTATE_ATTACHED) {
477489
(*TRUFFLE_CONTEXT)->detachCurrentThread(TRUFFLE_CONTEXT);
478490
}
479491
}

0 commit comments

Comments
 (0)