[PATCH 4/4] RTS: fix pushWSDeque to invoke write barrier when element is added

Karel Gardas karel.gardas at centrum.cz
Sat Jul 9 18:07:00 CEST 2011


This patch fixes RTS' pushWSDeque function. We need to invoke
write barrier after element is added to the queue and before moving
bottom. The reason for this is possible write reordering on modern CPUs
(e.g. ARMv7MP) where setting of element might be done later after moving
bottom. When such situation happen other thread might be waiting to steal
data from the queue and when bottom is moved it eagerly steals undefined
data from the queue since setting of element has not happened yet.
---
 rts/WSDeque.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/rts/WSDeque.c b/rts/WSDeque.c
index 090a549..11ae8a1 100644
--- a/rts/WSDeque.c
+++ b/rts/WSDeque.c
@@ -279,6 +279,15 @@ pushWSDeque (WSDeque* q, void * elem)
     }
 
     q->elements[b & sz] = elem;
+    /*
+       KG: we need to put write barrier here since otherwise we might
+       end with elem not added to q->elements, but q->bottom already
+       modified (write reordering) and with stealWSDeque_ failing
+       later when invoked from another thread since it thinks elem is
+       there (in case there is just added element in the queue). This
+       issue concretely hit me on ARMv7 multi-core CPUs
+     */
+    write_barrier();
     q->bottom = b + 1;
     
     ASSERT_WSDEQUE_INVARIANTS(q); 
-- 
1.7.4.3


--------------050104050904010908060008--



More information about the Cvs-ghc mailing list