fix race conditions in atomic operations

This commit is contained in:
Joel Dice 2009-11-29 09:08:07 -07:00
parent 1558b85acf
commit 6d9e1270ca
2 changed files with 9 additions and 3 deletions

View File

@ -76,7 +76,10 @@ markBitAtomic(uintptr_t* map, unsigned i)
{
uintptr_t* p = map + wordOf(i);
uintptr_t v = static_cast<uintptr_t>(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

View File

@ -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