[Pythonmac-SIG] Re: ThreadSafety and Callbacks

Martin v. Loewis martin@v.loewis.de
18 Apr 2002 08:25:16 +0200


Jack Jansen <Jack.Jansen@cwi.nl> writes:

> - Release the GIL when we do a toolbox call. *BUT* this works only for
> toolbox calls under our control. If some library we call (Tk, for
> instance) does a toolbox call we may end up in a callback routine
> while still holding the GIL.

Doesn't the callback for Tk come out in Tcl first, with Tcl then
dispatching to _tkinter?

Also, inside _tkinter, you do *not* hold the GIL, only the Tcl lock.

> - When we enter a callback determine whether the GIL is held. If it is
> not held we simply acquire it and arrange for it to be released when
> we're returning from the callback.

That shouldn't be necessary, IMO: you should always know inside the
callback whether you hold the GIL.

> - If the GIL is held we determine who holds it (through the per-thread
> stored value). If it is us then there's no problem and we do the
> callback, making sure we don't release te GIL at the end.
> - If the GIL is held by someone else we also attempt to obtain it,
> blocking us until the other thread releases it. Again we free it at
> the end of the callback.

I would think just acquiring the GIL in any case should be enough.

Regards,
Martin