[Python-Dev] Re: Atomic operations

Brett Cannon bac@OCF.Berkeley.EDU
Mon, 10 Feb 2003 12:35:27 -0800 (PST)


[Skip Montanaro]

>
>     Brett> I was actually thinking more along the lines of having something
>     Brett> like::
>
>     Brett>  atomic:
>     Brett>      lock()
>     Brett>  ...
>
>     Brett> with atomic basically shutting down threading temporarily until
>     Brett> everything in its body is executed.
>
> This would presumably generate bytecode something like:
>
>     GRAB_GIL
>     lock()
>     ... execute the block's code ...
>     unlock()
>     RELEASE_GIL
>
> Suppose code in your block makes a call to a threading-aware extension
> module.  Won't it obligingly release the GIL if it goes to do some longish
> operation?  If so, wouldn't you then be screwed?
>

Yes, you would be screwed.  As I said, you could screw yourself with your
code.  But could you do something like keep a list of what threads were
running while this atomic state is being run, and then when the GIL is
released that list is checked before any threads just randomly grab for
the GIL?  That would make sure things are still done synchronously.  You
would also have to temporarily disable the bytecode instruction counter
while this atomic state is in affect.

But if the consensus is that this is too difficult to implement or not
worth it I will just drop the idea.

-Brett