Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequentially-consistent atomic load on x86

I'm interested in sequentially-consistent load operation on x86.

As far as I see from assembler listing, generated by compiler it is implemented as a plain load on x86, however plain loads as far as I know guaranteed to have acquire semantics, while plain stores are guaranteed to have release.

Sequentially-consistent store is implemented as locked xchg, while load as plain load. That sounds strange to me, could you please explain this in details?

added

Just found in internet, that sequentially-consistent atomic load could be done as simple mov as long as store is done with locked xchg, but there was no proof and no links to documentation.

like image 974
axe Avatar asked Dec 12 '22 15:12

axe


2 Answers

A plain MOV on x86 is sufficient for an atomic sequentially consistent load, as long as SC stores are done with LOCKed instructions, the value is correctly aligned, and "normal" WB cache mode is used.

See my blog post at http://www.justsoftwaresolutions.co.uk/threading/intel-memory-ordering-and-c++-memory-model.html for the full mapping, and the Intel processor docs at http://developer.intel.com/products/processor/manuals/index.htm for the details of the allowed orderings.

If you use "WC" cache mode or "non-temporal" instructions such as MOVNTI then all bets are off, as the processor doesn't necessarily write the data back to main memory in a timely manner.

like image 53
Anthony Williams Avatar answered Jan 18 '23 05:01

Anthony Williams


Reads on x86 are by nature atomic, so long as they are aligned, the section under the MOV instruction in the intel assembly manuals vol 2A should mention this, same with the LOCK prefix. Other volumes may also mention this

however, if you want an atomic read, you can use _InterlockedExchangeAdd((LONG*)&var,0) aka LOCK XADD, this will yield the old value, but won't change its value, the same can be done with InterlockCompareExchange((LONG*)&var,var,var) aka LOCK CMPXCHG, but IMO, there is no need for this

like image 32
Necrolis Avatar answered Jan 18 '23 05:01

Necrolis