<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://xinu.cs.mu.edu/index.php?action=history&amp;feed=atom&amp;title=Adding_Monitors_To_Xinu</id>
	<title>Adding Monitors To Xinu - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://xinu.cs.mu.edu/index.php?action=history&amp;feed=atom&amp;title=Adding_Monitors_To_Xinu"/>
	<link rel="alternate" type="text/html" href="https://xinu.cs.mu.edu/index.php?title=Adding_Monitors_To_Xinu&amp;action=history"/>
	<updated>2026-06-15T16:30:54Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>https://xinu.cs.mu.edu/index.php?title=Adding_Monitors_To_Xinu&amp;diff=3655&amp;oldid=prev</id>
		<title>Amallen: created page</title>
		<link rel="alternate" type="text/html" href="https://xinu.cs.mu.edu/index.php?title=Adding_Monitors_To_Xinu&amp;diff=3655&amp;oldid=prev"/>
		<updated>2010-07-31T23:40:06Z</updated>

		<summary type="html">&lt;p&gt;created page&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= What are Monitors? =&lt;br /&gt;
&lt;br /&gt;
Java monitors act as locks guarding fields and methods of&lt;br /&gt;
an object. Each Java object is associated with one monitor. The ''synchronized'' keyword requires a thread to acquire the monitor lock associated with a synchronized&lt;br /&gt;
method’s target object before executing the body. Two different synchronized methods belonging to the same object&lt;br /&gt;
both depend on the same lock as the monitor is associated&lt;br /&gt;
with the object, and not the methods.&lt;br /&gt;
&lt;br /&gt;
A thread which has already acquired a lock does not&lt;br /&gt;
wait when attempting to acquire that same lock – this is&lt;br /&gt;
the primary difference between Java-style monitors and typical counting semaphores. Any thread holding a&lt;br /&gt;
lock must perform an unlock action once for each corresponding lock action before releasing the lock.&lt;br /&gt;
&lt;br /&gt;
= Monitors in Xinu =&lt;br /&gt;
&lt;br /&gt;
The monitors we add to Xinu contain an associated semaphore, a thread ownership ID, and a count tracking the number of locks performed without corresponding unlocks.&lt;br /&gt;
Thus, a monitor count begins at zero, every successful lock&lt;br /&gt;
action increases the count by one, and every unlock action&lt;br /&gt;
decreases the count by one. They are handled by a monitor table similar to other structure tables in Xinu such as the mailbox table, semaphore table, and thread table.&lt;br /&gt;
&lt;br /&gt;
The two important functions associated with monitors are ''lock'' and ''unlock''. Full &amp;lt;code&amp;gt;'''c'''-code&amp;lt;/code&amp;gt; for these functions is provided below. It is important to notice that these functions themselves must be &amp;quot;synchronized&amp;quot; to ensure correctness. In the code provided we have disabled interrupts, but a more lightweight synchronization mechanism could also be used.&lt;br /&gt;
&lt;br /&gt;
lock.c&lt;br /&gt;
&amp;lt;pre&amp;gt;#include &amp;lt;thread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;semaphore.h&amp;gt;&lt;br /&gt;
#include &amp;lt;monitor.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * Current thread attempts to grab the lock on a monitor.&lt;br /&gt;
 * If another thread does not hold the lock the current thread owns the lock&lt;br /&gt;
 * and increases the monitor's count by one. Otherwise, the thread waits.&lt;br /&gt;
 * @param mon  target monitor&lt;br /&gt;
 * @return OK on success, SYSERR on failure&lt;br /&gt;
 */&lt;br /&gt;
syscall lock(monitor mon)&lt;br /&gt;
{&lt;br /&gt;
    register struct sement *semptr;&lt;br /&gt;
    register struct monent *monptr;&lt;br /&gt;
    irqmask im;&lt;br /&gt;
&lt;br /&gt;
    im = disable();&lt;br /&gt;
    if (isbadmon(mon))&lt;br /&gt;
    {&lt;br /&gt;
        restore(im);&lt;br /&gt;
        return SYSERR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    monptr = &amp;amp;montab[mon];&lt;br /&gt;
    semptr = &amp;amp;semtab[monptr-&amp;gt;sem];&lt;br /&gt;
&lt;br /&gt;
    /* if no thread owns the lock, the current thread claims it */&lt;br /&gt;
    if (NOOWNER == monptr-&amp;gt;owner)&lt;br /&gt;
    {&lt;br /&gt;
        monptr-&amp;gt;owner = thrcurrent; /* current thread now owns the lock    */&lt;br /&gt;
        (monptr-&amp;gt;count)++;          /* add 1 &amp;quot;lock&amp;quot; to the monitor's count */&lt;br /&gt;
        wait(monptr-&amp;gt;sem);          /* this thread owns the semaphore      */&lt;br /&gt;
    }&lt;br /&gt;
    else&lt;br /&gt;
    {&lt;br /&gt;
        /* if current thread owns the lock increase count; dont wait on sem */&lt;br /&gt;
        if (thrcurrent == monptr-&amp;gt;owner)&lt;br /&gt;
        {&lt;br /&gt;
            (monptr-&amp;gt;count)++;&lt;br /&gt;
        }&lt;br /&gt;
        /* if another thread owns the lock, wait on sem until monitor is free */&lt;br /&gt;
        else&lt;br /&gt;
        {&lt;br /&gt;
            wait(monptr-&amp;gt;sem);&lt;br /&gt;
            monptr-&amp;gt;owner = thrcurrent;&lt;br /&gt;
            (monptr-&amp;gt;count)++;&lt;br /&gt;
&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    restore(im);&lt;br /&gt;
    return OK;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
unlock.c&lt;br /&gt;
&amp;lt;pre&amp;gt;#include &amp;lt;thread.h&amp;gt;&lt;br /&gt;
#include &amp;lt;semaphore.h&amp;gt;&lt;br /&gt;
#include &amp;lt;monitor.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * unlock a monitor. If monitor's count is 0, then free the monitor&lt;br /&gt;
 * and signal the monitor's semaphore.&lt;br /&gt;
 * @param mon  target monitor&lt;br /&gt;
 * @return OK on success, SYSERR on failure&lt;br /&gt;
 */&lt;br /&gt;
syscall unlock(monitor mon)&lt;br /&gt;
{&lt;br /&gt;
    register struct sement *semptr;&lt;br /&gt;
    register struct monent *monptr;&lt;br /&gt;
    irqmask im;&lt;br /&gt;
&lt;br /&gt;
    im = disable();&lt;br /&gt;
    if (isbadmon(mon))&lt;br /&gt;
    {&lt;br /&gt;
        restore(im);&lt;br /&gt;
        return SYSERR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    monptr = &amp;amp;montab[mon];&lt;br /&gt;
    semptr = &amp;amp;semtab[monptr-&amp;gt;sem];&lt;br /&gt;
&lt;br /&gt;
    /* safety check: this unlock call does not have an associated lock call */&lt;br /&gt;
    if (monptr-&amp;gt;count &amp;lt;= 0)&lt;br /&gt;
    {&lt;br /&gt;
        restore(im);&lt;br /&gt;
        return SYSERR;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /* decrement the monitor's count signifying one &amp;quot;unlock&amp;quot; */&lt;br /&gt;
    (monptr-&amp;gt;count)--;&lt;br /&gt;
&lt;br /&gt;
    /* if this is the top-level unlock call, then free this monitor's lock */&lt;br /&gt;
    if (monptr-&amp;gt;count == 0)&lt;br /&gt;
    {&lt;br /&gt;
        monptr-&amp;gt;owner = NOOWNER;&lt;br /&gt;
        signal(monptr-&amp;gt;sem);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    restore(im);&lt;br /&gt;
    return OK;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Amallen</name></author>
		
	</entry>
</feed>