keyed-semaphores icon indicating copy to clipboard operation
keyed-semaphores copied to clipboard

Read Write locks

Open maleet opened this issue 11 months ago • 3 comments

Could read lock also be implemented?

For now I use ReaderWriterLockSlim for this and for key based access I added dictinoary to hold instances... keyed-semaphore already have this for write part... If only many-reads part could be added here... :)

maleet avatar Mar 26 '24 05:03 maleet

Interesting proposition, if I understand correctly you basically need the same concurrent bookkeeping this library already has, but with keys mapped to a ReaderWriterLockSlim. Then you would probably want methods like LockRead or LockWrite.

I would definitely be willing to help and review this KeyedReaderWriterLocks you have in mind, but I hope you understand I won't take the time to implement all of this myself.

amoerie avatar Mar 27 '24 07:03 amoerie

Yes exactly. There are also some methods for upgradeable read...

Usage for me is in cache implementation, where there are many reads and if update, delete, reset occurs then all reads should be locked until operation is done. For upgrade... if first read will trigger cache initialization then I should switch to write and all other reads should wait until it's initialized. For keyed part... there are many caches and all these should have their own instances of locks.

Currently in this lib all locks are write locks so if there is lock then all others are in waiting list. Looks like methods for Read (and upgradeable read) should be added. But I understand this part can be PR for this lib and I can be author for that :)

maleet avatar Mar 27 '24 08:03 maleet

Sounds good! If you set up an initial PR we can work from there. I would like to keep the changes to the existing code as minimal as possible by the way, because I'm running a bunch of mission critical software with it. But we can see where this leads. 👍

amoerie avatar Mar 27 '24 08:03 amoerie