News for hackers. Stuff that matters.
Re:A spin lock in C that does not require a machine level ex... by tsukikage on Sunday May 04, 2014 @08:21PM (#7696424)

    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/Weak_consistency

Re:A spin lock in C that does not require a machine level ex... by mgraczyk on Sunday May 04, 2014 @08:22PM (#7696427)
More importantly, the explicit barriers are needed on ARM chips where writes are permitted to be reordered with reads.
Re:A spin lock in C that does not require a machine level ex... by tsukikage on Sunday May 04, 2014 @08:26PM (#7696439)
Also PowerPC and any other weakly consistent architecture, natch.