Used for creating read and write locks.
Used for storing the amount of active read operations on a resource.
Used to generate identifiers with the given suffixes.
count
is used for the identifier used to store the counter.
read
and write
are used for the 2 types of locks that are needed.
Private
Readonly
lockerPrivate
Readonly
storagePrivate
Readonly
suffixesPrivate
getThis key is used for storing the count of active read operations.
Private
getThis is the identifier for the read lock: the lock that is used to safely update and read the count.
Private
getThis is the identifier for the write lock, making sure there is at most 1 write operation active.
Private
incrementUpdates the count with the given modifier. Creates the data if it didn't exist yet. Deletes the data when the count reaches zero.
Private
postSafely decreases the count after the read operation is finished.
Private
preSafely updates the count before starting a read operation.
Private
withSafely runs an action on the count.
Run the given function while the resource is locked. The lock will be released when the (async) input function resolves. This function should be used for operations that only require reading the resource.
A promise resolving when the lock is released.
Identifier of the resource that needs to be locked.
A function to execute while the resource is locked.
Run the given function while the resource is locked. The lock will be released when the (async) input function resolves. This function should be used for operations that could modify the resource.
A promise resolving when the lock is released.
Identifier of the resource that needs to be locked.
A function to execute while the resource is locked.
A ReadWriteLocker that allows for multiple simultaneous read operations. Write operations will be blocked as long as read operations are not finished. New read operations are allowed while this is going on, which will cause write operations to wait longer.
Based on https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock#Using_two_mutexes . As soon as 1 read lock request is made, the write lock is locked. Internally a counter keeps track of the amount of active read locks. Only when this number reaches 0 will the write lock be released again. The internal read lock is only locked to increase/decrease this counter and is released afterwards. This allows for multiple read operations, although only 1 at the time can update the counter, which means there can still be a small waiting period if there are multiple simultaneous read operations.