News for hackers. Stuff that matters.
Re:Mutexes are faster than Spinlocks by rwem on Saturday January 04, 2020 @10:54AM (#21955518)
It doesn’t flush the entire cache (that would be a disaster) but it does shoot down the cache line containing the lock in all cores other than the one that acquired the lock.

The real issue with spin locks is fairness. There’s no assurance that any given thread will ever make progress. A thread could starve forever. Production-ready mutexes like absl::Mutex make efforts toward fairness, even if they don’t have hard guarantees.

Re:Mutexes are faster than Spinlocks by the8472 on Saturday January 04, 2020 @11:47AM (#21955856)
> but it does shoot down the cache line containing the lock in all cores other than the one that acquired the lock.

Well, as long as you do the test-CAS instead of the pure-CAS approach not every loop iteration results in cache line bouncing.

Plus intel has introduced the MWAIT[0] instruction to implement something similar to futex in hardware, i.e. the hyperthread can sleep until another core updates the cacheline in question.

[0] https://www.felixcloutier.com/x86/mwait

Re:Mutexes are faster than Spinlocks by atq2119 on Saturday January 04, 2020 @12:48PM (#21956223)
That's true, though MWAIT can only be used from kernel mode. At least the docs say that you get a #UD exception if you attempt to use it from user mode.
Re:Mutexes are faster than Spinlocks by the8472 on Saturday January 04, 2020 @12:55PM (#21956264)
Right, there's UMWAIT[0] for that, but that's quite new.

[0] https://www.felixcloutier.com/x86/umwait

Re:Mutexes are faster than Spinlocks by xtacy on Saturday January 04, 2020 @11:09AM (#21955625)
Aren't most modern spinlock implementations are ticket-based, which ensures fairness among waiters (FIFO-like)? The linux kernel implementations most definitely are.

I agree that the naive implementations are not.

Re:Mutexes are faster than Spinlocks by temac on Saturday January 04, 2020 @04:25PM (#21957687)
Completely fair locks can have extremely terrible throughput performance, so depending what you are doing with them, they might not be a good idea...