Skip to contentSkip to conversation

tests-aio-multithread: use atomic_read properly

PATCH Applied Paolo Bonzini 4 participants 4 messages Feb 27, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tests-aio-multithread: use atomic_read properly

nodes[id].next is written by other threads. If atomic_read is not used (matching atomic_set in mcs_mutex_lock!) the compiler can optimize the whole "if" away!

---
 tests/test-aio-multithread.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/test-aio-multithread.c +2 -2
index f11e990..8b0b40e 100644
@@ -309,7 +309,7 @@ static void mcs_mutex_lock(void)
 static void mcs_mutex_unlock(void)
 {
     int next;
-    if (nodes[id].next == -1) {
+    if (atomic_read(&nodes[id].next) == -1) {
         if (atomic_read(&mutex_head) == id &&
             atomic_cmpxchg(&mutex_head, id, -1) == id) {
             /* Last item in the list, exit.  */
@@ -323,7 +323,7 @@ static void mcs_mutex_unlock(void)
     }
 
     /* Wake up the next in line.  */
-    next = nodes[id].next;
+    next = atomic_read(&nodes[id].next);
     nodes[next].locked = 0;
     qemu_futex_wake(&nodes[next].locked, 1);
 }
Signature · 1 line
2.9.3
Tested-by on v1 1/1
#1 lore raw
Quoted 7 lines · On Mon, 27 Feb 2017 12:17:26 +0100 Paolo Bonzini <[email protected]> wrote:

nodes[id].next is written by other threads. If atomic_read is not used (matching atomic_set in mcs_mutex_lock!) the compiler can optimize the whole "if" away!

---

Cool ! I can use travis again :)

Quoted 25 lines
 tests/test-aio-multithread.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/test-aio-multithread.c +2 -2
index f11e990..8b0b40e 100644
@@ -309,7 +309,7 @@ static void mcs_mutex_lock(void)
 static void mcs_mutex_unlock(void)
 {
     int next;
-    if (nodes[id].next == -1) {
+    if (atomic_read(&nodes[id].next) == -1) {
         if (atomic_read(&mutex_head) == id &&
             atomic_cmpxchg(&mutex_head, id, -1) == id) {
             /* Last item in the list, exit.  */
@@ -323,7 +323,7 @@ static void mcs_mutex_unlock(void)
     }
 
     /* Wake up the next in line.  */
-    next = nodes[id].next;
+    next = atomic_read(&nodes[id].next);
     nodes[next].locked = 0;
     qemu_futex_wake(&nodes[next].locked, 1);
 }
#2 lore raw
Quoted 7 lines · Greg Kurz
Quoted 4 lines · On Mon, 27 Feb 2017 12:17:26 +0100 Paolo Bonzini <[email protected]> wrote:

nodes[id].next is written by other threads. If atomic_read is not used (matching atomic_set in mcs_mutex_lock!) the compiler can optimize the whole "if" away!

Quoted 8 lines
Quoted 3 lines

---

Cool ! I can use travis again :)

Thanks; applied to master as a build fix.

-- PMM

#3 lore raw
In reply to Paolo Bonzini
Quoted 9 lines · Paolo Bonzini

nodes[id].next is written by other threads. If atomic_read is not used (matching atomic_set in mcs_mutex_lock!) the compiler can optimize the whole "if" away!

---
 tests/test-aio-multithread.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan