> Peterson's algorithm or Lamport's Bakery algorithm.
Those are the algorithms that you have to resort to when you don't even have atomic swap, as I recall. With ARMv5 you can use code like your example. I have had success with something like this:
bool locked;
void lock() {
while (atomic_swap(locked,true)) {
sched_yield();
}
}
void unlock() {
locked = false;
}
This works well IFF:
- The probability of contention is low.
- You have tens, not thousands, of threads.
- You care only about average delays, not worst case.
- You don't care about RT threads, priority inversion etc.
The really great thing about this code is that the fast (uncontended) path is just a couple of instructions as long as the compiler inlines it, making it maybe an order of magnitude faster than something like pthread_mutex_lock().
I was trying to recall how to extend this to use a futex() call instead of sched_yield(). It gets tricky....