News for hackers. Stuff that matters.
Re:A spin lock in C that does not require a machine level ex... by jacquesm on Sunday May 04, 2014 @08:22PM (#7696428)
Hello dear Keith,

I can see a few problems with this code, please consider the following constructive criticism.

  for (;;) {
    if (state->g2 == locker_id) {
      return;
    }
    while (state->g2 != SPIN_LOCK_NO_LOCKER) {
      if (spin_wait_callback != NULL) {
        spin_wait_callback();
      }
You don't need this 'continue', the loop will do that just fine without it.

      continue;
    }
The following four lines are problematic:

    state->g1 = locker_id;

    if (state->g2 == SPIN_LOCK_NO_LOCKER) {
Imagine the consequences of a task switch right here.

      state->g2 = state->g1;
This if does nothing.

      if (state->g1 == locker_id) {
And you're going to do the following if without all the extra conditionals above when you do not return from here the next time you iterate through the loop. So all that extra testing of state->g1 above does nothing.

        if (state->g2 == locker_id) {
          return;
        }
      }
    }
  }

I think I can see what you're trying to do here but once you fix the basic problems with the code and you test it under very high load locking and unlocking a resource you'll find that fairly soon two threads will be accessing the resource at the same time.

Try a very tight loop that locks a counter, does a load, increment, store on it and then unlocks the counter again. Keep separate tallies per thread and use a third thread to keep track of the expected sum and the actual sum.

That should show you reasonably quickly why atomicity in the basic operation of acquiring a lock is a base requirement.

Locking issues almost never show up when you are running lightly loaded on a single core. But the lack of ensuring all cores see the same information, that race conditions can't occur and that instructions are executed in the right order will show up with a terrible certainty under load on multiple cores.

Re:A spin lock in C that does not require a machine level ex... by keithgabryelski on Sunday May 04, 2014 @08:34PM (#7696457)
so, the attempt here (line 32 which you believe is problematic) I think can be pre-empted.

if thread 1 is pre-empted by thread 2 and thread 2 is able to get to the same point (line 33) then G1 will be equal to 2. both threads will assign G2 = 2 and thread 2 will win the race -- thread 1 will loop and begin to spin again.

have i missed something?

Re:A spin lock in C that does not require a machine level ex... by jacquesm on Sunday May 04, 2014 @08:36PM (#7696466)
Yes, task switches unfortunately do not happen at the level of 'C' code but between machine instructions!

Run your compiler -S and save the output, then try to reason through the consequences of a context switch in between every instruction with multiple threads contending for the lock. And have a look at the manual for your CPU to learn about out-of-order execution and cache coherency between multiple cores / cpus.

Some interesting reading on the subject:

https://www.kernel.org/doc/Documentation/memory-barriers.txt

If you want to do this entirely in userspace and you must avoid atomic instructions for some reason consider abstracting out your locking to a separate process and use IPC, first come first serve.