Skip to content

Commit f8fa1d0

Browse files
bgamariMarge Bot
authored andcommitted
ghc-prim: Use C11 atomics
Previously `ghc-prim`'s atomic wrappers used the legacy `__sync_*` family of C builtins. Here we refactor these to rather use the appropriate C11 atomic equivalents, allowing us to be more explicit about the expected ordering semantics.
1 parent 86ad1af commit f8fa1d0

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

libraries/ghc-prim/cbits/atomic.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,28 +279,36 @@ extern StgWord hs_cmpxchg8(StgWord x, StgWord old, StgWord new);
279279
StgWord
280280
hs_cmpxchg8(StgWord x, StgWord old, StgWord new)
281281
{
282-
return __sync_val_compare_and_swap((volatile StgWord8 *) x, (StgWord8) old, (StgWord8) new);
282+
StgWord8 expected = (StgWord8) old;
283+
__atomic_compare_exchange_n((StgWord8 *) x, &expected, (StgWord8) new, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
284+
return expected;
283285
}
284286

285287
extern StgWord hs_cmpxchg16(StgWord x, StgWord old, StgWord new);
286288
StgWord
287289
hs_cmpxchg16(StgWord x, StgWord old, StgWord new)
288290
{
289-
return __sync_val_compare_and_swap((volatile StgWord16 *) x, (StgWord16) old, (StgWord16) new);
291+
StgWord16 expected = (StgWord16) old;
292+
__atomic_compare_exchange_n((StgWord16 *) x, &expected, (StgWord16) new, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
293+
return expected;
290294
}
291295

292296
extern StgWord hs_cmpxchg32(StgWord x, StgWord old, StgWord new);
293297
StgWord
294298
hs_cmpxchg32(StgWord x, StgWord old, StgWord new)
295299
{
296-
return __sync_val_compare_and_swap((volatile StgWord32 *) x, (StgWord32) old, (StgWord32) new);
300+
StgWord32 expected = (StgWord32) old;
301+
__atomic_compare_exchange_n((StgWord32 *) x, &expected, (StgWord32) new, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
302+
return expected;
297303
}
298304

299305
extern StgWord64 hs_cmpxchg64(StgWord x, StgWord64 old, StgWord64 new);
300306
StgWord64
301307
hs_cmpxchg64(StgWord x, StgWord64 old, StgWord64 new)
302308
{
303-
return __sync_val_compare_and_swap((volatile StgWord64 *) x, old, new);
309+
StgWord64 expected = (StgWord64) old;
310+
__atomic_compare_exchange_n((StgWord64 *) x, &expected, (StgWord64) new, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
311+
return expected;
304312
}
305313

306314
// Atomic exchange operations

0 commit comments

Comments
 (0)