[pypy-dev] GIL hacks in pypy

Timothy Baldridge tbaldridge at gmail.com
Fri Jan 13 16:37:29 CET 2012

I'm in the process of writing some semi-multithreaded code in PyPy.
Now I know that PyPy and CPython both have GILs, but what I need is a
CompareAndSwap operation in Python code. So basically I want my python
code to grab the GIL, execute a small block of code, then hand the GIL
back. Python doesn't really give a way to do this. But in CPython we
can do this ugly little hack for getting "free" locks where you
basically set the GIL "remaining" bytecodes count to 32 billion,
execute your code, then return it to the original value. The cool
thing is that you get a lock for basically free.

Now I doubt this method would work with jitted pypy code. Is there a
good way to get cheap "critical sections" or more preferably a CAS
operation in PyPy without resorting to (slow) OS locks? Basically I
want to tell pypy "don't switch threads for the next 10 bytecodes..."



“One of the main causes of the fall of the Roman Empire was
that–lacking zero–they had no way to indicate successful termination
of their C programs.”
(Robert Firth)

More information about the pypy-dev mailing list