libatomic: perform write before context switch
authorBrian Norris <banorris@uci.edu>
Thu, 7 Jun 2012 00:34:47 +0000 (17:34 -0700)
committerBrian Norris <banorris@uci.edu>
Thu, 7 Jun 2012 00:43:32 +0000 (17:43 -0700)
commitc9aa5d5129131b926d7589d831f4bb6f31393e0a
treefcefb3fb0411153c99c5c772b5406c07d3a2a007
parent3c9fb84ea4beccce0117cfcc6a1a7f0abc5f7e1a
libatomic: perform write before context switch

Atomic actions block a thread at the "switch_to_master" function call, so under
the current structure (which isn't quite fit for relaxed modeling yet...) we
should perform the memory write before calling "switch_to_master".

If not, we can observe sequences like the following, where x is an atomic
variable. All actions are seq_cst:

 Initially, x = 0

 Thread    Action
 ------    ------
 1         r1 = x;       // r1 = 0
 1          x = r1 + 1;  //  x = 1, not stored yet?
 2         r2 = x;       // r2 = 0
 2          x = r2       //  x = 1, not stored yet?

Then, depending on scheduling, Thread 1 or Thread 2 might complete first, with
its write being performed *after* it receives control again.
libatomic.cc