3 ms·
if (state->g2 == SPIN_LOCK_NO_LOCKER) { state->g2 = state->g1; if (state->g1 == locker_id) { if (state->g2 == locker_id) { return; } }
by tsukikage 12y ago
if (state->g2 == SPIN_LOCK_NO_LOCKER) {
state->g2 = state->g1;
if (state->g1 == locker_id) {
if (state->g2 == locker_id) {
return;
}
}
}
You need to insert memory barriers between the writes and the reads. Without, on systems with multiple cores, other cores may not see the state changes in program order, which will make it possible for multiple threads to conclude they won the race.
http://en.wikipedia.org/wiki/Memory_barrier http://en.wikipedia.org/wiki/Memory_barrier
http://en.wikipedia.org/wiki/Weak_consistency http://en.wikipedia.org/wiki/Weak_consistency