Re: [Pyobjc-dev] Achieving 1.0....
Brought to you by:
ronaldoussoren
From: Jack J. <Jac...@cw...> - 2002-10-28 09:56:14
|
On Monday, Oct 28, 2002, at 10:00 Europe/Amsterdam, Ronald Oussoren wrote: > All, > > I just remembered one implementation issue that should be worked on > before reaching a 1.0 release: Thread safety. > > The current code is probably save for multi-threading, but I haven't > really checked this. Basicly I removed all static/global buffers while > working on the code. > > There are some more issues: > 1. Python has a global interpreter lock. When one thread owns this > lock, no > other thread can execute python code. The bridge currently never > gives up > the GIL when moving into native code and we probably should. I have > not yet > researched what we should do with callbacks from Objective-C to > Python. > > E.g. the NSApplicationMain wrapper should release the GIL before > calling > the actuall function, but how do you regain the GIL during calls to > methods > implemented in Python? I have sample code to do this, based on pthreads. You can have it if you want it, but it's probably just as easy to recode it (as you'll have to replace the pthreads stuff anyway, I would guess). The trick I used is to put pthread's per-thread data to work (I assume ObjC threads have a similar feature, yes?). You allocate an item of per-thread global data, which is automatically initialized to zero in all threads. Then when you switch from Python->ObjC you store the current PyThreadState in there. When you switch from PObjC to Python you get the per-thread global data item. If it is non-null it is the PyThreadState and you simply do PyEval_AcquireThread. If it is null then this is a thread that has never yet made the Python->ObjC transition. As it is apparently executing ObjC code right now it must have been created outside of Python. So, you create a new PyThreadState for it (with PyThreadState_New), store that in the per-thread global data and call PyEval_AcquireThread. -- - Jack Jansen <Jac...@or...> http://www.cwi.nl/~jack - - If I can't dance I don't want to be part of your revolution -- Emma Goldman - |