Overview
TheLibraryMonitor class implements a monitor for the classic readers-writer problem. It allows multiple readers to access the library simultaneously, but writers (the librarian updating the catalog) require exclusive access. The monitor implements a writer-priority policy to prevent writer starvation.
Constructor
Creates a new library monitor with no active readers or writers.Initial State:
activeReaders:0writerActive:falsereaderQueue:[]writerQueue:[]readerGranted:new Set()writerGranted:null
Properties
Count of readers currently inside the library.
true when the librarian is actively updating the catalog (writing).FIFO queue of reader threads blocked and waiting to enter.
FIFO queue of writer threads blocked and waiting to enter.
Set of readers that have been granted permission to enter but haven’t entered yet.
The writer thread that has been granted permission to enter, or
null if none.Methods
enterRead(thread)
Reader thread requests entry to the library.The reader thread attempting to enter.
true: Reader successfully entered the libraryfalse: Reader is blocked and added to the queue
- Already granted: If thread was previously granted permission, consumes permission and increments
activeReaders - Can enter: If no writer is active and no writers are waiting (writer priority), allows entry
- Must wait: Blocks thread if writer is active or writers are waiting
monitorLibrary.js:19
exitRead()
Reader thread exits the library.List of threads awakened by this exit (if this was the last reader).
- Decrements
activeReaderscount - Calls
wakeUpThreads()to potentially wake waiting writers or readers - If this was the last reader and a writer is waiting, the writer may be awakened
monitorLibrary.js:41
enterWrite(thread)
Writer thread requests exclusive access to update the catalog.The writer thread attempting to enter.
true: Writer successfully entered (has exclusive access)false: Writer is blocked and added to the queue
- Already granted: If thread was previously granted permission, consumes permission and sets
writerActive - Can enter: If no writer is active and no readers are present, allows entry
- Must wait: Blocks thread if any readers are present or another writer is active
monitorLibrary.js:47
exitWrite()
Writer thread exits the library, releasing exclusive access.List of threads awakened (either one writer or all waiting readers).
- Sets
writerActivetofalse - Calls
wakeUpThreads()to wake next writer or all waiting readers
monitorLibrary.js:67
wakeUpThreads()
Internal method implementing the monitor’s wake-up policy.List of threads that were awakened and granted permission.
- If a writer is waiting and the library is empty (no active writer, no active readers), wake one writer
- Otherwise, if readers are waiting and no writer is waiting/active, wake all readers
monitorLibrary.js:73
Usage Example
Writer-Priority Policy
The monitor implements writer priority to prevent writer starvation:- Writers don’t starve when many readers arrive continuously
- Once a writer arrives, new readers must wait
- Existing readers finish, then writer gets exclusive access
Grant Mechanism
The monitor uses a grant-based approach:- Wake-up phase:
wakeUpThreads()marks threads as “granted” permission - Entry phase:
enterRead()/enterWrite()checks for granted permission - Consume grant: Thread consumes its permission and enters