r/programming • • 4d 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.

67 Upvotes

6 comments sorted by

21

u/ReDucTor 4d 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 3d ago edited 3d 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 3d 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 3d 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

1

u/lookmeat 16h ago

Interesting.

I recommend you look into semaphores. Because it shared a lot of properties, and understanding the semaphore may reveal more about the pros and cons, and also there's something you should explore that I think the article linked explains well: semaphores are often seen as alternatives to mutexes in theory, but in practice they work better as pure control flow (basically to have sync points that aren't about exchanging or accessing resources, but just knowing "something happened now you can go").

I am not saying you are building a semaphore, I have not read the code and certainly not as carefully, but from the article it does seem not only did you build a mutex semaphore hybrid, but it also both waits on the resource and simultaneously signals another thread, which can be messier. This might give you hints to gotchas that your design may or may not have.

And that's the thing, was this built for fun or with a very specific problem in mind? For the former: cool idea, for the latter I wonder what the problem is and if a more complex model build from traditional sync primitives might make more sense, rather than a brand new primitive.

For a ticketing system, would a scheduler make more sense? (That can be done with a semaphore). Further more what kind of operations are done? If the data locked is complex could we convert this into transactions? Would read-write locks reduce contention?

Basically in the real world having to reach for a new kind of mutex is a big red flag that we're solving a problem we shouldn't allow, that were deep in accidental complexity and extending a source of pain rather than ending it.