From 6d9e1270cadfb7850c8bc2e50168db8f0a82efef Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sun, 29 Nov 2009 09:08:07 -0700 Subject: [PATCH] fix race conditions in atomic operations --- src/heap.cpp | 5 ++++- src/machine.cpp | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/heap.cpp b/src/heap.cpp index 740b9985af..5dcd75f7c2 100644 --- a/src/heap.cpp +++ b/src/heap.cpp @@ -76,7 +76,10 @@ markBitAtomic(uintptr_t* map, unsigned i) { uintptr_t* p = map + wordOf(i); uintptr_t v = static_cast(1) << bitOf(i); - while (not atomicCompareAndSwap(p, *p, *p | v)) { } + for (uintptr_t old = *p; + not atomicCompareAndSwap(p, old, old | v); + old = *p) + { } } #endif // USE_ATOMIC_OPERATIONS diff --git a/src/machine.cpp b/src/machine.cpp index 62c4649659..b9ae880225 100644 --- a/src/machine.cpp +++ b/src/machine.cpp @@ -24,9 +24,12 @@ const unsigned NoByte = 0xFFFF; #ifdef USE_ATOMIC_OPERATIONS void -atomicIncrement(unsigned* p, int v) +atomicIncrement(uint32_t* p, int v) { - while (not atomicCompareAndSwap32(p, *p, *p + v)) { } + for (uint32_t old = *p; + not atomicCompareAndSwap32(p, old, old + v); + old = *p) + { } } #endif