[pypy-commit] pypy default: * only mark the tstate as released after we called PyGILState_Release()

arigo pypy.commits at gmail.com
Tue Feb 21 02:14:44 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r90237:8e04fbcbc608
Date: 2017-02-21 08:14 +0100
http://bitbucket.org/pypy/pypy/changeset/8e04fbcbc608/

Log:	* only mark the tstate as released after we called
	PyGILState_Release() enough times to balance the number of calls to
	PyGILState_Ensure()

	* make potential errors print a descriptive message

diff --git a/pypy/module/cpyext/pystate.py b/pypy/module/cpyext/pystate.py
--- a/pypy/module/cpyext/pystate.py
+++ b/pypy/module/cpyext/pystate.py
@@ -41,8 +41,7 @@
     NULL.  If the lock has been created, the current thread must not have
     acquired it, otherwise deadlock ensues.  (This function is available even
     when thread support is disabled at compile time.)"""
-    ec = space.getexecutioncontext()
-    ec.cpyext_threadstate_is_current = True
+    PyThreadState_Swap(space, tstate)
 
 @cpython_api([], lltype.Void)
 def PyEval_InitThreads(space):
@@ -199,7 +198,11 @@
     if not ec.cpyext_threadstate_is_current:
         old_tstate = lltype.nullptr(PyThreadState.TO)
     if tstate:
-        assert tstate == state.get_thread_state(space)
+        if tstate != state.get_thread_state(space):
+            print "Error in cpyext, CPython compatibility layer:"
+            print "PyThreadState_Swap() cannot be used to switch to another"
+            print "different PyThreadState right now"
+            raise AssertionError
         ec.cpyext_threadstate_is_current = True
     else:
         ec.cpyext_threadstate_is_current = False
@@ -279,8 +282,8 @@
     else:
         assert ec.cpyext_gilstate_counter_noleave == 0
         assert oldstate == PyGILState_UNLOCKED
+        ec.cpyext_threadstate_is_current = False
         space.threadlocals.leave_thread(space)
-    ec.cpyext_threadstate_is_current = False
 
 @cpython_api([], PyInterpreterState, error=CANNOT_FAIL)
 def PyInterpreterState_Head(space):


More information about the pypy-commit mailing list