summaryrefslogtreecommitdiffstats
path: root/Python/pystate.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2016-03-14 21:07:55 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2016-03-14 21:07:55 (GMT)
commit8a1be61849341528c866924cf69d378ce7790889 (patch)
tree0b59fbc22edbd703ded539758aae78382d4eec7d /Python/pystate.c
parent08572f68a980576e883e4eba1227b59782d5192e (diff)
downloadcpython-8a1be61849341528c866924cf69d378ce7790889.zip
cpython-8a1be61849341528c866924cf69d378ce7790889.tar.gz
cpython-8a1be61849341528c866924cf69d378ce7790889.tar.bz2
Add more checks on the GIL
Issue #10915, #15751, #26558: * PyGILState_Check() now returns 1 (success) before the creation of the GIL and after the destruction of the GIL. It allows to use the function early in Python initialization and late in Python finalization. * Add a flag to disable PyGILState_Check(). Disable PyGILState_Check() when Py_NewInterpreter() is called * Add assert(PyGILState_Check()) to: _Py_dup(), _Py_fstat(), _Py_read() and _Py_write()
Diffstat (limited to 'Python/pystate.c')
-rw-r--r--Python/pystate.c24
1 files changed, 19 insertions, 5 deletions
diff --git a/Python/pystate.c b/Python/pystate.c
index 853e5c7..e8026c5 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -34,6 +34,8 @@ to avoid the expense of doing their own locking).
extern "C" {
#endif
+int _PyGILState_check_enabled = 1;
+
#ifdef WITH_THREAD
#include "pythread.h"
static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
@@ -45,7 +47,7 @@ static PyThread_type_lock head_mutex = NULL; /* Protects interp->tstate_head */
GILState implementation
*/
static PyInterpreterState *autoInterpreterState = NULL;
-static int autoTLSkey = 0;
+static int autoTLSkey = -1;
#else
#define HEAD_INIT() /* Nothing */
#define HEAD_LOCK() /* Nothing */
@@ -449,10 +451,10 @@ PyThreadState_DeleteCurrent()
if (tstate == NULL)
Py_FatalError(
"PyThreadState_DeleteCurrent: no current tstate");
- SET_TSTATE(NULL);
+ tstate_delete_common(tstate);
if (autoInterpreterState && PyThread_get_key_value(autoTLSkey) == tstate)
PyThread_delete_key_value(autoTLSkey);
- tstate_delete_common(tstate);
+ SET_TSTATE(NULL);
PyEval_ReleaseLock();
}
#endif /* WITH_THREAD */
@@ -716,6 +718,7 @@ void
_PyGILState_Fini(void)
{
PyThread_delete_key(autoTLSkey);
+ autoTLSkey = -1;
autoInterpreterState = NULL;
}
@@ -784,8 +787,19 @@ PyGILState_GetThisThreadState(void)
int
PyGILState_Check(void)
{
- PyThreadState *tstate = GET_TSTATE();
- return tstate && (tstate == PyGILState_GetThisThreadState());
+ PyThreadState *tstate;
+
+ if (!_PyGILState_check_enabled)
+ return 1;
+
+ if (autoTLSkey == -1)
+ return 1;
+
+ tstate = GET_TSTATE();
+ if (tstate == NULL)
+ return 0;
+
+ return (tstate == PyGILState_GetThisThreadState());
}
PyGILState_STATE