ThreadMentor: Visualizing Monitors: Part I

Three windows are important to visualizing the execution behavior of monitors. They are the main window, History Graph window and Monitor window. In the following, only those new materials that are not covered on previous pages will be discussed. Refer to Visualizing Thread Execution for thread execution, Visualizing Mutex Locks for mutex locks, and Visualizing Semaphores for semaphores.

The Main Window

Click on the Monitor to display all monitors created so far. The display area is divided into four columns. Column Monitor Name displays the names of monitors, column Active Thread displays the name of thread that is running in the monitor, and WOut shows the number of threads that are waiting (outside of of the monitor) to enter the monitor. If there is no active thread, column Active Thread displays --. The meaning of column WIn will be discussed on a later page. In the figure below, thread IncThread1 is running in monitor CounterMonitor, and this monitor has 7 other threads waiting to enter.

The History Graph Window

The activities of monitors are shown with three tags MB, ME and MA as shown below. When a thread tries to enter a monitor by calling a monitor procedure (actually, when method MonitorBegin() is called), tag MB (i.e., monitor begin) is shown. Similarly, when a thread exits a monitor by calling method MonitorEnd(), tag ME is shown. However, since a thread may not successfully enter a monitor when a monitor procedure is called, it is blocked outside of the monitor. Consequently, following a tag MB it is likely to have a red bar indicating the thread is blocked. Tag MA means that a thread becomes active in the monitor, meaning it is running in the monitor. Because a thread that calls a monitor procedure is blocked initially, after waiting for some time, it is allowed to enter because the running thread in the monitor exits. Once a thread becomes the active one, a MA tag is shown. Therefore, to the left (resp., right) of a MA tag, the history bar is red (resp., green), meaning the thread is blocked (i.e., active - running in the monitor). Because a thread must be executing in a monitor to call method MonitorEnd(), right before and right after a tag ME, the history bar of that thread is in green, meaning it is running.

Due to the mutual exclusion property of a monitor, when a thread exits, another thread can enter, and there is no more than one thread can be executing in a monitor. Because of this, when we see a ME tag, meaning a thread exits the monitor, we can expect that another thread that has a MB tag will be able to enter. In many situations, we want to know which thread causes a particular thread to become active. To do so, click on the Monitor button in the History Graph. A line segment will be drawn between the corresponding ME and MA pair. For example, in the above snapshot, thread IncThread1 executed a MonitorEnd() to exit the monitor (tag ME). After this, the monitor is empty and allows thread DecThread1 to be active (tag MA). Similarly, when thread DecThread1 exits, it causes thread DecThread2 to become active. In this way, one can easily reconstruct all the activities of monitors. However, which monitor is involved and from which statement a MonitorBegin() or a MonitorEnd() is called? The answer to the second part of this question is the source window. For example, click on the first ME tag of thread IncThread to bring up a source window with the call to method MonitorEnd() highlighted:

The answer to the first part of the above question is the Monitor window.

The Monitor Window

Clicking on a monitor name in the display area of the main window brings up the Monitor window of that monitor. The name of this window is the name of the monitor. The top row in the window shows the active thread name. If there is no thread running in the monitor, -- is shown. There are three subwindows: Threads Waiting To Enter, Threads Reentering Monitor and Condition Variables. For now, only the first one is relevant. The last two will be discussed on a later page. The Threads Waiting To Enter subwindow lists all threads that are waiting to enter the monitor outside of the monitor. Thus, the snapshot below shows that, currently, thread IncThread1 is running, and seven threads DecThread1, DecThread2, IncThread2, DecThread2, IncThread4, IncThread5 and IncThread3 are waiting outside of the monitor. This number, 7, is also shown under the column with heading WOut in the display area of the main window. Finally, each monitor must be declared with a monitor type, which will be discussed later on Monitor Types. The possible types are Hoare and Mesa. The type of a monitor is part of the window name and is also shown near the bottom of a Monitor window.