News for hackers. Stuff that matters.
Re:Why Spinlocks Are Bad On iOS by umanwizard on Wednesday December 23, 2015 @04:05PM (#10785638)
> since threads spinning on a spinlock are always runnable, this means that if there's enough high-QOS threads waiting on a lock held by a lower-QOS thread, the thread that owns the lock will never execute.

Is this true? OSSpinLock (at least on OS X) calls thread_switch for 1ms every 1024 iterations (which as far as I understand is basically sleeping[1]). Presumably it's not runnable while it's sleeping?

After 100 * 1024 iterations it will also ask to be throttled to the lowest priority for 1ms, again with thread_switch.

[1] http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_s...

Re:Why Spinlocks Are Bad On iOS by eridius on Wednesday December 23, 2015 @04:11PM (#10785670)
I didn't go into OSSpinLock specifics because I didn't want the distraction. The naive spinlock shown in the post certainly is always runnable. OSSpinLock's behavior helps with priority inversions, but it doesn't solve the issue with QOS. If there's enough threads of a high QOS blocked on an OSSpinLock it can still livelock. Only truly unbounded backoff can solve that in all cases, but that can lead to high-QOS threads being delayed for a long time (according to the Apple engineer it could result in "tens of seconds" of delay, which is obviously not acceptable for a high-QOS thread).
Re:Why Spinlocks Are Bad On iOS by comex on Wednesday December 23, 2015 @04:54PM (#10785908)
Why not "just" fall back to a mutex after enough spinning? I guess this would require an extra branch in the unlocking function (to check whether there are threads blocked in the kernel that need to be woken up), but that doesn't sound too awful especially as the branch would very rarely be taken. (It would sort of break OSSpinLock's ABI though. Sort of.)

AKA an adaptive mutex.

Re:Why Spinlocks Are Bad On iOS by eridius on Wednesday December 23, 2015 @05:41PM (#10786095)
(rewritten after looking up Linux's adaptive mutex)

Assuming you mean something like http://stackoverflow.com/a/25168942/582, you basically need a full mutex anyway, so I'm not sure why you wouldn't just use a mutex directly. I don't think you can really implement this adaptive mutex in userland because you can't just use a spinlock + a mutex (you'd have to lock the mutex every time anyway, which defeats the point of having the spinlock) and syscalls are not considered public API (the kernel is allowed to change syscalls between versions, libc/libsystem is the only public API for them).

FWIW pthread mutexes were sped up 2-2.5x in "new OSs" to compensate for spinlocks being illegal (https://twitter.com/catfish_man/status/676852111527706624).

Re:Why Spinlocks Are Bad On iOS by pcwalton on Wednesday December 23, 2015 @06:53PM (#10786345)
> you'd have to lock the mutex every time anyway

I don't understand. Could you explain this in more detail?

In Linux, adaptive mutexes are implemented entirely in userspace with futex as the primitive syscall. This is because atomic CAS (cmpxchg, ldrex/strex, etc) suffices to ensure synchronization during the spinning.

Re:Why Spinlocks Are Bad On iOS by eridius on Wednesday December 23, 2015 @08:12PM (#10786542)
syscalls aren't public API on Darwin platforms (e.g. iOS, OS X). The only supported interface to syscalls is libc/libsystem. And I'm not aware of any equivalent to futex. So AFAIK there's no way to implement a userspace mutex that isn't just a wrapper around a system-provided mutex (e.g. pthread_mutex, or a serial libdispatch queue). And if you're wrapping pthread_mutex, adding a spinlock doesn't provide any benefit.

FWIW, libdispatch itself definitely uses Mach SPI. For example, skimming through the source, it calls thread_switch() using a non-public option (there's 3 non-public options to thread_switch now, two of which are intended for dealing with the QOS issues that my article talked about; the private os_lock_handoff_s that the objc runtime uses internally uses one called SWITCH_OPTION_OSLOCK_WAIT and libdispatch appears to use the other, SWITCH_OPTION_OSLOCK_DEPRESS, as well as the third option which was added just for libdispatch called SWITCH_OOPTION_DISPATCH_CONTENTION).