timcassell / ProtoPromise

Robust and efficient library for management of asynchronous operations in C#/.Net.
MIT License
147 stars 13 forks source link

`AsyncReaderWriterLock` Contention Strategy #278

Closed timcassell closed 11 months ago

timcassell commented 11 months ago

AsyncReaderWriterLock was designed with a balanced contention strategy in order to prevent any starvation (best for the general case). However, some use cases could benefit from a different strategy. If it can be implemented cheaply (which I think it can), it may be worth it to add the ability to override the contention strategy.

public enum ContentionStrategy
{
    Balanced,
    PrioritizeWriters,
    PrioritizeReaders,
    PrioritizeUpgradeableReaders
}

cc @timonkrebs

timonkrebs commented 11 months ago

That would be useful.

I have a use case where it is preferable when specific scopes can be prioritized.

So maybe something like ContentionStrategy.PrioritizeSelective could also be done.

I think of something like this:

private readonly AsyncReaderWriterLock _rwl = new AsyncReaderWriterLock();

public async Promise DoStuffAsync()
{
    using (await _rwl.UpgradeableReaderLockAsync(Priority.Low))
    {
        // Reader lock is shared, multiple readers can enter at the same time.
    }

    using (await _rwl.ReaderLockAsync(Priority.High))
    {
        // Prioritized Reader lock is shared, multiple readers can enter at the same time.
    }

    using (await _rwl.WriterLockAsync(Priority.Middle))
    {
        // Writer lock is mutually exclusive, only one writer can enter at a time, and no readers can enter while a writer is entered.
    }
}

Maybe ContentionStrategy.PrioritizeSelective could be a replacement for all other ContentionStrategies other then Balanced?

timcassell commented 11 months ago

@timonkrebs That's an interesting idea. I'm not sure how feasible or cheap that would be to implement. I have to consider the strategy both when locks are acquired as well as when they are released. If different locks of the same type can have different priorities dynamically, that's not just a simple field in the class. And that sounds like you want a lock to "jump the line" if it's higher priority than a pending lock, correct?

timonkrebs commented 11 months ago

Yes I think of it something like "jump the line". I think it could be done with 9 queues. For every case one queue. Like

Not sure if it is worth the added complexity. I only mentioned it because I think it could be done without to much effort.

timcassell commented 11 months ago

My idea here was to simply pass the strategy into the constructor new AsyncReaderWriterLock(ContentionStrategy.PrioritizeReaders), with that I can store the strategy in a field and use it to adjust the behavior almost for free (not adding extra branches).

Adding 9 queues could probably work, but it would definitely make the implementation much more complex, and it'd make the memory footprint of the lock much bigger. I don't think it's worth it to add that to the lock as it would slow down other more common cases where dynamic prioritization is not needed. This seems like a case where you'd want a specialized lock for your special need.

timonkrebs commented 11 months ago

This seems like a case where you'd want a specialized lock for your special need.

That is probably true... Your initial solution is probably better for most cases. My needs are quite specialized. I wrote a new kind of lock that I call AsymmetricLock for my needs.