News for hackers. Stuff that matters.
Re:Mutexes are faster than Spinlocks by ww520 on Saturday January 04, 2020 @01:00PM (#21956284)
It seems to be doing similar logic.

1. Does a CAS with compare_exchange_weak() at line 69.

2. Then call lock_slow() at line 72 to do spinlocking (Guh!).

3. The call to parking_lot_core::park() at line 256 seems to sleep wait.

Re:Mutexes are faster than Spinlocks by acqq on Saturday January 04, 2020 @02:16PM (#21956752)
So this it acquires fully in userspace if there's no contention, it even spins if there is contention, and then if that wasn't enough it lets the thread sleep with the timeout.

Which matches the description of that library:

https://github.com/Amanieu/parking_lot

"This library provides implementations of Mutex, RwLock, Condvar and Once that are smaller, faster and more flexible than those in the Rust standard library"

"Uncontended lock acquisition and release is done through fast inline paths which only require a single atomic operation.

Microcontention (a contended lock with a short critical section) is efficiently handled by spinning a few times while trying to acquire a lock.

The locks are adaptive and will suspend a thread after a few failed spin attempts."

The only thing that I'm missing is how often the sleeping threads wake, as a bad constant can increase the CPU use.

Re:Mutexes are faster than Spinlocks by temac on Saturday January 04, 2020 @04:20PM (#21957653)
Does it really sleep for a specified period instead of doing directed wake-ups? If so that's very far from ideal...
Re:Mutexes are faster than Spinlocks by bdash on Saturday January 04, 2020 @08:13PM (#21958889)
No, a thread that fails to acquire the mutex sleeps until the thread that is releasing the mutex explicitly wakes it. On Linux this is achieved via FUTEX_WAIT / FUTEX_WAKE.
Re:Mutexes are faster than Spinlocks by ww520 on Saturday January 04, 2020 @03:02PM (#21957076)
Ah, thanks for the info.

I would imagine the unlocking call would wake up the sleeping thread so it won't keep waking up periodically.

The user-mode only implementation sounds interesting. The only downside is it won't work with inter-process locking.

Re:Mutexes are faster than Spinlocks by AstralStorm on Saturday January 04, 2020 @10:56PM (#21959532)
How does it know that said critical section is short? Guessing?

Likely a hard linear-quadratic expanding threshold. They tend to waste a lot of CPU power in specific algorithms.

There are better, more intrusive ways of solving this problem on language level.

Re:Mutexes are faster than Spinlocks by bdash on Sunday January 05, 2020 @12:29AM (#21959833)
It doesn't attempt to determine the size of the critical section, AFAIK. I think it's saying that sleeping immediately when failing to acquire the lock would significantly hurt small critical sections, and so instead they spin a few times to avoid punishing those scenarios.
Re:Mutexes are faster than Spinlocks by bluejekyll on Saturday January 04, 2020 @01:12PM (#21956361)
Yes. I just meant to say that it’s not a pthread based implementation, potentially making it more portable.
Re:Mutexes are faster than Spinlocks by asveikau on Saturday January 04, 2020 @10:15PM (#21959386)
The P in pthread is the P from Posix which stands for portable.

Now, the "zero syscalls for uncontended pthread mutices via futex" optimization is Linux specific and may not be replicated elsewhere. Or it may. It's not Posix, but I know for instance win32 critical sections look a lot like spinlocks when not contended but do syscalls to block, which sounds a lot like a futex. So that would put that technique as dating to the 1990s at the latest. Futex landed in 2002.

Re:Mutexes are faster than Spinlocks by bluejekyll on Sunday January 05, 2020 @11:24AM (#21961951)
> The P in pthread is the P from Posix which stands for portable.

What’s your point here? Posix is portable across posix compliant systems, it is not portable beyond that.

There are many systems that are posix, but Rust targets more than that.

Re:Mutexes are faster than Spinlocks by asveikau on Sunday January 05, 2020 @09:26PM (#21965919)
Avoiding pthreads because they aren't portable and replacing them with a spinlock sounds a little bit like madness, that was my point.

What is the non-posix target you have in mind? Windows? Seems like conditionally compiling against either pthreads or win32 critical section [or anything else] is a feasible thing and reasonable action. Maybe even the spinlock as a last resort.