3

Is there any way to implement a fast spinlock on iOS that reverts to blocking on an OS primitive if and only if there's contention? I'm looking for something equivalent to these implementations:

http://locklessinc.com/articles/keyed_events/ (Fast Mutex) http://locklessinc.com/articles/mutex_cv_futex/

The intent is to use this in places where we've determined that a spinlock is ideal but where we want to account for the rare possibility that another, potentially lower priority, thread is holding the lock in which case we want to block on a kernel primitive and be woken only when the other thread releases the lock.

Falcon
  • 445
  • 3
  • 10

1 Answers1

3

There's finally an answer to this question: os_unfair_lock does what you're asking for. It's equivalent to a spinlock when uncontended, but doesn't busywait and donates priority when contended like a mutex does.

Catfish_Man
  • 41,261
  • 11
  • 67
  • 84