[C++-sig] Handling multiple GIL accesses

Niall Douglas s_sourceforge at nedprod.com
Mon Jun 10 01:14:05 CEST 2013


In terms of specific steps to avoid problems, it's been too long ago 
since I wrote those GIL management patches for TnFOX for me to be 
able to comment usefully. Getting the sequences correct took a lot of 
trial and error at the time. All I can say is if you're seeing weird 
hangs under load, the TnFOX patches may be useful, or they may be so 
bitrotted by now that they're not. A race detector might be useful, 
clang's threadsanitizer is one of the better implementations.

Still, good luck with your work.

Niall


On 6 Jun 2013 at 0:11, Adam Preble wrote:

> Here's the double lock I'm afraid of:
> 
> 1. Embedding program invokes some Python code with its embedded interpreter
> 2. The embedded interpreter, through running that code, ends up executing
> some code actually in the embedded program's environment.
> 3. This code back in the embedded program has another thread (here we go)
> that calls to some interfaces, of which one is written in Python.
> 4. The wrapper needs to acquire the GIL since it's going back into Python,
> and it's going into it from another thread entirely.
> 
> At #4 right there I thought it was tricky when trying to do this.  I guess
> the important thing to do is to make sure to store a unique state from each
> call to PyGILState_Ensure and to keep those in order when using
> PyGILState_Release.  I think when I originally did this, I had a global
> pointer, which was bad news.
> 
> I wanted to make sure I understand how to handle a particularly disruptive
> situation when embedding Python in another language.  I've been following
> this with embedding Python in C++, and I also looked at this with embedding
> CPython in C# using Python.NET.  The Python.NET library looks like it's
> surviving without double locking.  Looking at its implementation, I wanted
> to review for posterity's sake how this is supposed to work.  I know when I
> was first trying to do this in C++, I didn't really pull it off, and I
> wanted the next poor fool that's Googling for this to have a good shot at
> it.
> 
> So do I have it?
> 


-- 
Any opinions or advice expressed here do NOT reflect those
of my employer BlackBerry Inc.
Work Portfolio: http://careers.stackoverflow.com/nialldouglas/



-------------- next part --------------
A non-text attachment was scrubbed...
Name: SMime.p7s
Type: application/x-pkcs7-signature
Size: 6061 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20130609/e81c7285/attachment.bin>


More information about the Cplusplus-sig mailing list