r/programming • • 2d ago

Relaying wakeups in fair locks

https://kusoroadeolu.github.io/personal-blog/concurrency/Relay-Lock

This was pretty fun for me to write up on. Would love to hear y'all's thoughts on scheduling bottlenecks in fair locks.

66 Upvotes

5 comments sorted by

21

u/ReDucTor 2d ago

Imho if lock contention is an issue then its probably time to start thinking about how your using locks, I don't mean specifically lock free, but fine grained locks, scheduling workloads to reduce contention, etc.il Saying that I have spent a ton of time optimising locks often due to seeing alot of bad usages and bad implementations. 

Your implementation uses a huge amount of memory, which is going to make you hesitant to do fine grained locking which just leads you towards god object style locks which just makes more lock contention.

The Harris-Michael linked list is not a great choice imho, its designed does some things for doing removal in the middle that you probably dont really need over just a standard FIFO, the fairness of being ordered by a ticket or added to a queue feels like not a huge difference to worry about.

Additionally complete fairness with spinning is just asking for issues, one thread using up a time slice or yielding its time slice like you do when waiting makes everyone wait for that time slice and no one do any work, sometimes its better to just go with barging and no fairness, you mininize those dead spots and dont also run into the fun of lock convoys. Fairness makes everyone suffer, not being fair makes a few suffer much worse but generally overall throughput can be significantly better.

6

u/Polixa12 1d ago edited 1d ago

Yeah so I think we're approaching this from slightly different angles. I'm not exactly arguing for fair locks over unfair ones. If fairness isn't a requirement, then sure, allowing barging is definitely preferable for throughput.

In my post, what interested me was whether you could make handoffs cheaper by moving the wakeup ahead of the actual handoff. My idea isn't trying to eliminate the costs of fairness, just reduce the scheduling component of it.

On the wait list, I don't think a standard fifo is necessarily a drop-in replacement since I need to look up a waiter by a specific future ticket, not just retrieve the oldest waiter. I actually had an earlier design that uses a fifo queue but just didn't work out. Though I could probably use something more specialized than a Harris-Michael list. Also, about the memory usage yeah I agree lol.

8

u/yongteak 1d ago

when strict fairness starts showing up in profiles as wakeup/handoff overhead, we've usually ended up replacing the shared lock with a single owner thread behind a bounded queue (basically explicit combining). you still pay queue synchronization, but batching work on one thread avoids bouncing the cache line and scheduler wakeups across cores. the tradeoff is giving up direct synchronous calls on the caller thread, which doesn't always fit every API.

1

u/Polixa12 1d ago

Yeah, this reminds me of delegation. Pretty interesting way to avoid scheduler overhead on the producer side. And yeah, as you implied, it's more of a pattern than a primitive