Bugs item #1720250, was opened at 2007-05-16 19:46
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1720250&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Kuno Ospald (kunoospald)
Assigned to: Nobody/Anonymous (nobody)
Summary: PyGILState_Ensure does not acquires GIL

Initial Comment:
The function PyGILState_Ensure doesn't acquire the GIL if current thread state 
is valid. In contrast to that PyGILState_Release deletes the thread state 
(PyThreadState_DeleteCurrent) which releases the GIL which got not acquired 
before (=> mutex->owned = -2).

Here is an example which locks at PyRun_SimpleString:

// initialize the Python interpreter
Py_Initialize();

PyEval_InitThreads();

// release the GIL as PyEval_InitThreads 
// implicitly acquires the GIL
PyEval_ReleaseLock();

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

PyRun_SimpleString("import random\n");

PyGILState_Release(gstate); 

In that simple example the problem can be fixed by removing the call to 
PyEval_ReleaseLock. But that is needed for applications that call into the 
interpreter from multiple threads. 

The only solution I could found up to that point is the following:

// initialize the Python interpreter
Py_Initialize();

PyEval_InitThreads();

PyThreadState* tcur = PyThreadState_Get() ;

PyThreadState_Swap(NULL);
PyThreadState_Clear(tcur);
PyThreadState_Delete(tcur);

// release the GIL as PyEval_InitThreads 
// implicitly acquires the GIL
PyEval_ReleaseLock();

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

PyRun_SimpleString("import random\n");

PyGILState_Release(gstate);

Which seems to works fine. But I think that this behavior of PyGILState_Ensure 
should be either documented or fixed.

Thanks,
Kuno



----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1720250&group_id=5470
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to