From c9aa5d5129131b926d7589d831f4bb6f31393e0a Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Wed, 6 Jun 2012 17:34:47 -0700 Subject: [PATCH] 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libatomic.cc b/libatomic.cc index 88d7fbf..507f685 100644 --- a/libatomic.cc +++ b/libatomic.cc @@ -5,8 +5,8 @@ void atomic_store_explicit(struct atomic_object *obj, int value, memory_order order) { DBG(); - model->switch_to_master(new ModelAction(ATOMIC_WRITE, order, obj, value)); obj->value = value; + model->switch_to_master(new ModelAction(ATOMIC_WRITE, order, obj, value)); } int atomic_load_explicit(struct atomic_object *obj, memory_order order) -- 2.34.1