home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / version-1-0 / OSProject / p2 / Synch.c < prev    next >
Text File  |  2006-04-05  |  8KB  |  211 lines

  1. code Synch
  2.  
  3.   -- OS Class: Project 2
  4.   --
  5.   -- <PUT YOUR NAME HERE>
  6.  
  7. -----------------------------  Semaphore  ---------------------------------
  8.  
  9.   behavior Semaphore
  10.     -- This class provides the following methods:
  11.     --    Signal()  ...also known as "Up" or "V"...
  12.     --         Increment the semaphore count.  Wake up a thread if
  13.     --         there are any waiting.  This operation always executes
  14.     --         quickly and will not suspend the thread.
  15.     --    Wait()   ...also known as "Down" or "P"...
  16.     --         Decrement the semaphore count.  If the count would go
  17.     --         negative, wait for some other thread to do a Signal()
  18.     --         first.  Conceptually, the count will never go negative.
  19.     --    Init(initialCount)
  20.     --         Each semaphore must be initialized.  Normally, you should
  21.     --         invoke this method, providing an 'initialCount' of zero.
  22.     --         If the semaphore is initialized with 0, then a Wait()
  23.     --         operation before any Signal() will wait for the first
  24.     --         Signal().  If initialized with i, then it is as if i Signal()
  25.     --         operations have been performed already.
  26.     --
  27.     -- NOTE: You should never look at a semaphore's count since the value you
  28.     -- retrieve may be out-of-date, due to other threads performing Wait and/or
  29.     -- Signal operations since the retrieval of the count.
  30.  
  31.       ----------  Semaphore . Init  ----------
  32.  
  33.       method Init (initialCount: int)
  34.           if initialCount < 0
  35.             FatalError ("Semaphore created with initialCount < 0")
  36.           endIf
  37.           count = initialCount
  38.           waitingThreads = new List [Thread]
  39.         endMethod
  40.  
  41.       ----------  Semaphore . Signal  ----------
  42.  
  43.       method Signal ()
  44.           var
  45.             oldIntStat: int
  46.             t: ptr to Thread
  47.           oldIntStat = SetInterruptsTo (DISABLED)
  48.           if count == 0x7fffffff
  49.             FatalError ("Semaphore count overflowed during 'Signal' operation")
  50.           endIf
  51.           count = count + 1
  52.           if count <= 0
  53.             t = waitingThreads.Remove ()
  54.             t.status = READY
  55.             readyList.AddToEnd (t)
  56.           endIf
  57.           oldIntStat = SetInterruptsTo (oldIntStat)
  58.         endMethod
  59.  
  60.       ----------  Semaphore . Wait  ----------
  61.  
  62.       method Wait ()
  63.           var
  64.             oldIntStat: int
  65.           oldIntStat = SetInterruptsTo (DISABLED)
  66.           if count == 0x80000000
  67.             FatalError ("Semaphore count underflowed during 'Wait' operation")
  68.           endIf
  69.           count = count - 1
  70.           if count < 0
  71.             waitingThreads.AddToEnd (currentThread)
  72.             currentThread.Sleep ()
  73.           endIf
  74.           oldIntStat = SetInterruptsTo (oldIntStat)
  75.         endMethod
  76.  
  77.   endBehavior
  78.  
  79. -----------------------------  Mutex  ---------------------------------
  80.  
  81.   behavior Mutex
  82.     -- This class provides the following methods:
  83.     --    Lock()
  84.     --         Acquire the mutex if free, otherwise wait until the mutex is
  85.     --         free and then get it.
  86.     --    Unlock()
  87.     --         Release the mutex.  If other threads are waiting, then
  88.     --         wake up one so it can then lock the mutex next.
  89.     --    Init()
  90.     --         Each mutex must be initialized.
  91.     --    IsHeldByCurrentThread()
  92.     --         Return TRUE iff the current (invoking) thread holds a lock
  93.     --         on the mutex.
  94.  
  95.     method Init ()
  96.         FatalError ("Unimplemented method")
  97.       endMethod
  98.  
  99.     method Lock ()
  100.         FatalError ("Unimplemented method")
  101.       endMethod
  102.  
  103.     method Unlock ()
  104.         FatalError ("Unimplemented method")
  105.       endMethod
  106.  
  107.     method IsHeldByCurrentThread () returns bool
  108.         FatalError ("Unimplemented method")
  109.         return false
  110.       endMethod
  111.  
  112.   endBehavior
  113.  
  114. -----------------------------  Condition  ---------------------------------
  115.  
  116.   behavior Condition
  117.     -- This class is used to implement monitors.  Each monitor will have a
  118.     -- mutex lock and one or more condition variables.  The lock ensures that
  119.     -- only one process at a time may execute code in the monitor.  Within the
  120.     -- monitor code, a thread can execute Wait() and Signal() operations
  121.     -- on the condition variables to make sure certain condions are met.
  122.     --
  123.     -- The condition variables here implement "Mesa-style" semantics, which
  124.     -- means that in the time between a Signal() operation and the awakening
  125.     -- and execution of the corrsponding waiting thread, other threads may
  126.     -- have snuck in and run.  The waiting thread should always re-check the
  127.     -- data to ensure that the condition which was signalled is still true.
  128.     --
  129.     -- This class provides the following methods:
  130.     --    Wait(mutex)
  131.     --         This method assumes the mutex has alreasy been locked.
  132.     --         It unlocks it, and goes to sleep waiting for a signal on
  133.     --         this condition.  When the signal is received, this method
  134.     --         re-awakens, re-locks the mutex, and returns.
  135.     --    Signal(mutex)
  136.     --         If there are any threads waiting on this condition, this
  137.     --         method will wake up the oldest and schedule it to run.
  138.     --         However, since this thread holds the mutex and never unlocks
  139.     --         it, the newly awakened thread will be forced to wait before
  140.     --         it can re-acquire the mutex and resume execution.
  141.     --    Broadcast(mutex)
  142.     --         This method is like Signal() except that it wakes up all
  143.     --         threads waiting on this condition, not just the next one.
  144.     --    Init()
  145.     --         Each condition must be initialized.
  146.  
  147.       ----------  Condition . Init  ----------
  148.  
  149.       method Init ()
  150.           waitingThreads = new List [Thread]
  151.         endMethod
  152.  
  153.       ----------  Condition . Wait  ----------
  154.  
  155.       method Wait (mutex: ptr to Mutex)
  156.           var
  157.             oldIntStat: int
  158.           if ! mutex.IsHeldByCurrentThread ()
  159.             FatalError ("Attempt to wait on condition when mutex is not held")
  160.           endIf
  161.           oldIntStat = SetInterruptsTo (DISABLED)
  162.           mutex.Unlock ()
  163.           waitingThreads.AddToEnd (currentThread)
  164.           currentThread.Sleep ()
  165.           mutex.Lock ()
  166.           oldIntStat = SetInterruptsTo (oldIntStat)
  167.         endMethod
  168.  
  169.       ----------  Condition . Signal  ----------
  170.  
  171.       method Signal (mutex: ptr to Mutex)
  172.           var
  173.             oldIntStat: int
  174.             t: ptr to Thread
  175.           if ! mutex.IsHeldByCurrentThread ()
  176.             FatalError ("Attempt to signal a condition when mutex is not held")
  177.           endIf
  178.           oldIntStat = SetInterruptsTo (DISABLED)
  179.           t = waitingThreads.Remove ()
  180.           if t
  181.             t.status = READY
  182.             readyList.AddToEnd (t)
  183.           endIf
  184.           oldIntStat = SetInterruptsTo (oldIntStat)
  185.         endMethod
  186.  
  187.       ----------  Condition . Broadcast  ----------
  188.  
  189.       method Broadcast (mutex: ptr to Mutex)
  190.           var
  191.             oldIntStat: int
  192.             t: ptr to Thread
  193.           if ! mutex.IsHeldByCurrentThread ()
  194.             FatalError ("Attempt to broadcast a condition when lock is not held")
  195.           endIf
  196.           oldIntStat = SetInterruptsTo (DISABLED)
  197.           while true
  198.             t = waitingThreads.Remove ()
  199.             if t == null
  200.               break
  201.             endIf
  202.             t.status = READY
  203.             readyList.AddToEnd (t)
  204.           endWhile
  205.           oldIntStat = SetInterruptsTo (oldIntStat)
  206.         endMethod
  207.  
  208.   endBehavior
  209.  
  210. endCode
  211.