Private
isTrack if the mutex is currently locked.
Private
waitingQueue for handling the lock requests when the mutex is already locked.
Lock the mutex.
If the mutex is free to be locked, the locking happens immediately and the caller can carry out its prorected operations. When the mutex is locked, any subsequent caller that is trying to lock the mutex is forced to wait.
A promise that is resolved when the mutex is given to the caller. The returned promise (that is itself a function pointer) must be called to free (unlock) the mutex.
Private
unlockUnlock the mutex.
This method is returned to the caller indicating that the mutex is locked. After completing the operations protected by the mutex, the caller must call this method to unlock (release) the mutex. If the caller fails to do so, the mutex will remain locked.
When the mutex is unlocked by the first caller and there are several callers waiting to lock the mutex, the waiting queue is processed and the mutex is locked immediately for the next caller in waiting.
Simple mutex with a waiting queue.
The mutex can be acquired by calling the
lock()
method which returns a promise with a function pointing to theunlock()
method. Afterwards the caller can carry out the operations that are protected by the mutex. Then the caller who holds the mutex must call the previously returned function to unlock the mutex. If the caller fails to do so, the mutex will remain locked.If the mutex is free to be locked, the locking happens immediately and the caller can carry out its prorected operations. When the mutex is locked, any subsequent caller that is trying to lock the mutex is forced to wait. In this case , the actual acquire functionality is not executed but pushed to the waiting queue. When the mutex is unlocked by the first caller, the queue is processed and the mutex is locked immediately for the next in waiting, i.e. resolving the promise by returning a function pointer (to the mutex unlock method) for the caller.
Example