[Pyrex] [Cython-dev] Callbacks from threads and PyGILState_Ensure/PyGILState_Release

Greg Ewing greg.ewing at canterbury.ac.nz
Mon Sep 17 01:21:13 CEST 2007


Stefan Behnel wrote:
> Greg uses words like "triggers the generation of code to acquire the
> GIL if necessary around the body." The "if" is futile. It will always have to
> do that, as it can't know who calls the function in what context.

The nogil signature is what tells Pyrex about the calling
context. If that signature is present in the function's
declaration, Pyrex knows that it can't assume the GIL is
held. So, if any Python operations are performed within
the function body, the GIL needs to be acquired.

For this purpose, "performing a Python operation" includes
calling any function that hasn't been declared with a
nogil signature. Pyrex has to assume that such a function
could perform Python operations, and needs the GIL.

> Pyrex must still figure
> out if it needs to acquire the GIL or not. We're fighting Turing here.

No, we're not. If we were trying to figure out *exactly*
when the GIL was needed and when it wasn't, under all
conditions, with no help from the programmer, then we
would be fighting Turing. But we're taking a conservative
approach by assuming the GIL is needed unless we can
prove that it isn't, and relying on declarations to
fill in Pyrex's gaps in knowledge.

Declarations are needed in certain key places. Then
Pyrex checks that everything else is consistent with
those declarations.

--
Greg



More information about the Pyrex mailing list