Skip to content

Commit 86ec0f9

Browse files
author
Davi Arnaut
committed
Bug#56760: my_atomics failures on osx10.5-x86-64bit
The problem was due to a misuse of GCC asm constraints used to implement a atomic load. On x86_64, the load was implemented as a cmpxchg which implicitly uses the eax register as a source and destination operand, yet the dummy value used for comparison wasn't being properly loaded into eax (and other problems). The core problem is that cmpxchg is unnecessary as a load on x86_64 as there are other simpler instructions such as xadd. Even though, such instructions are only used to have a memory barrier as load and stores are atomic by definition. Hence, the solution is to explicitly issue the required CPU and compiler barriers.
1 parent 1ab37fd commit 86ec0f9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

include/atomic/x86-gcc.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@
7878
: "memory")
7979

8080
/*
81-
Actually 32-bit reads/writes are always atomic on x86
82-
But we add LOCK_prefix here anyway to force memory barriers
81+
Actually 32/64-bit reads/writes are always atomic on x86_64,
82+
nonetheless issue memory barriers as appropriate.
8383
*/
8484
#define make_atomic_load_body(S) \
85-
ret=0; \
86-
asm volatile (LOCK_prefix "; cmpxchg %2, %0" \
87-
: "=m" (*a), "=a" (ret) \
88-
: "r" (ret), "m" (*a) \
89-
: "memory")
85+
/* Serialize prior load and store operations. */ \
86+
asm volatile ("mfence" ::: "memory"); \
87+
ret= *a; \
88+
/* Prevent compiler from reordering instructions. */ \
89+
asm volatile ("" ::: "memory")
9090
#define make_atomic_store_body(S) \
9191
asm volatile ("; xchg %0, %1;" \
9292
: "=m" (*a), "+r" (v) \

0 commit comments

Comments
 (0)