This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author pitrou
Recipients Paxxi, jyasskin, mark.dickinson, paul.moore, pitrou, serhiy.storchaka, steve.dower, tim.golden, vstinner, zach.ware
Date 2017-06-25.09:06:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1498381611.44.0.478489490651.issue30747@psf.upfronthosting.co.za>
In-reply-to
Content
The general issue those macros want to prevent is that modern CPUs have a tendency to execute a lot of stuff out-of-order, *including* memory operations.  From the perspective of a single hardware core (or thread, really), that's fine since it  has a logically consistent view of the machine's state (it knows which operations have been reordered, which values have been committed to cache or not, etc.).  But what happens when another hardware core examines in-memory state at the same time? It might find values changing in a different order than the programmer had intended. If it's important that the visible order hasn't been changed, you have a bug.

Note that C "volatile" is not enough: it only prevents the *compiler* from re-ordering or eliding memory accesses, but not the CPU.  As such, "volatile" is only useful if you have a single word-sized piece of state that you need to inspect from several threads at once.  But the eval loop uses several of them, and therefore needs to prevent the CPU from writing or reading them in the wrong order (which may produce synchronization bugs such as deadlocks).

Also note that traditionally, x86 has a "strong" memory ordering model which prevents nasty kinds of reorderings to happen.  But other architectures such as ARM or SPARC can have much weaker memory models (IIRC, we had sporadic hangs on an ARM buildbot a long time ago, because of that, but I can't find a reference).

I admit I'm unable to vouch that the current code is correct.  Jeffrey Yasskin did the original change adding the Py_atomic types and using them in the GIL implementation.
History
Date User Action Args
2017-06-25 09:06:51pitrousetrecipients: + pitrou, paul.moore, mark.dickinson, vstinner, jyasskin, tim.golden, zach.ware, serhiy.storchaka, steve.dower, Paxxi
2017-06-25 09:06:51pitrousetmessageid: <1498381611.44.0.478489490651.issue30747@psf.upfronthosting.co.za>
2017-06-25 09:06:51pitroulinkissue30747 messages
2017-06-25 09:06:50pitroucreate