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 / OSProject / p2 / Synch.c < prev    next >
Text File  |  2009-04-03  |  8KB  |  219 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.     --    Up()  ...also known as "V" or "Signal"...
  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.     --    Down()   ...also known as "P" or "Wait"...
  16.     --         Decrement the semaphore count.  If the count would go
  17.     --         negative, wait for some other thread to do an Up()
  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 Down()
  23.     --         operation before any Up() will wait for the first
  24.     --         Up().  If initialized with i, then it is as if i Up()
  25.     --         operations have been performed already.
  26.     --
  27.     -- NOTE: The user should never look at a semaphore's count since the value
  28.     -- retrieved may be out-of-date, due to other threads performing Up() or
  29.     -- Down() 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 . Up  ----------
  42.  
  43.       method Up ()
  44.           var
  45.             oldIntStat: int
  46.             t: ptr to Thread
  47.           oldIntStat = SetInterruptsTo (DISABLED)
  48.           if count == 0x7fffffff
  49.             FatalError ("Semaphore count overflowed during 'Up' 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 . Down  ----------
  61.  
  62.       method Down ()
  63.           var
  64.             oldIntStat: int
  65.           oldIntStat = SetInterruptsTo (DISABLED)
  66.           if count == 0x80000000
  67.             FatalError ("Semaphore count underflowed during 'Down' 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 the oldest one and give it the lock.
  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.       ----------  Mutex . Init  ----------
  96.  
  97.       method Init ()
  98.           FatalError ("Unimplemented method")
  99.         endMethod
  100.  
  101.       ----------  Mutex . Lock  ----------
  102.  
  103.       method Lock ()
  104.           FatalError ("Unimplemented method")
  105.         endMethod
  106.  
  107.       ----------  Mutex . Unlock  ----------
  108.  
  109.       method Unlock ()
  110.           FatalError ("Unimplemented method")
  111.         endMethod
  112.  
  113.       ----------  Mutex . IsHeldByCurrentThread  ----------
  114.  
  115.       method IsHeldByCurrentThread () returns bool
  116.           FatalError ("Unimplemented method")
  117.           return false
  118.         endMethod
  119.  
  120.   endBehavior
  121.  
  122. -----------------------------  Condition  ---------------------------------
  123.  
  124.   behavior Condition
  125.     -- This class is used to implement monitors.  Each monitor will have a
  126.     -- mutex lock and one or more condition variables.  The lock ensures that
  127.     -- only one process at a time may execute code in the monitor.  Within the
  128.     -- monitor code, a thread can execute Wait() and Signal() operations
  129.     -- on the condition variables to make sure certain condions are met.
  130.     --
  131.     -- The condition variables here implement "Mesa-style" semantics, which
  132.     -- means that in the time between a Signal() operation and the awakening
  133.     -- and execution of the corrsponding waiting thread, other threads may
  134.     -- have snuck in and run.  The waiting thread should always re-check the
  135.     -- data to ensure that the condition which was signalled is still true.
  136.     --
  137.     -- This class provides the following methods:
  138.     --    Wait(mutex)
  139.     --         This method assumes the mutex has alreasy been locked.
  140.     --         It unlocks it, and goes to sleep waiting for a signal on
  141.     --         this condition.  When the signal is received, this method
  142.     --         re-awakens, re-locks the mutex, and returns.
  143.     --    Signal(mutex)
  144.     --         If there are any threads waiting on this condition, this
  145.     --         method will wake up the oldest and schedule it to run.
  146.     --         However, since this thread holds the mutex and never unlocks
  147.     --         it, the newly awakened thread will be forced to wait before
  148.     --         it can re-acquire the mutex and resume execution.
  149.     --    Broadcast(mutex)
  150.     --         This method is like Signal() except that it wakes up all
  151.     --         threads waiting on this condition, not just the next one.
  152.     --    Init()
  153.     --         Each condition must be initialized.
  154.  
  155.       ----------  Condition . Init  ----------
  156.  
  157.       method Init ()
  158.           waitingThreads = new List [Thread]
  159.         endMethod
  160.  
  161.       ----------  Condition . Wait  ----------
  162.  
  163.       method Wait (mutex: ptr to Mutex)
  164.           var
  165.             oldIntStat: int
  166.           if ! mutex.IsHeldByCurrentThread ()
  167.             FatalError ("Attempt to wait on condition when mutex is not held")
  168.           endIf
  169.           oldIntStat = SetInterruptsTo (DISABLED)
  170.           mutex.Unlock ()
  171.           waitingThreads.AddToEnd (currentThread)
  172.           currentThread.Sleep ()
  173.           mutex.Lock ()
  174.           oldIntStat = SetInterruptsTo (oldIntStat)
  175.         endMethod
  176.  
  177.       ----------  Condition . Signal  ----------
  178.  
  179.       method Signal (mutex: ptr to Mutex)
  180.           var
  181.             oldIntStat: int
  182.             t: ptr to Thread
  183.           if ! mutex.IsHeldByCurrentThread ()
  184.             FatalError ("Attempt to signal a condition when mutex is not held")
  185.           endIf
  186.           oldIntStat = SetInterruptsTo (DISABLED)
  187.           t = waitingThreads.Remove ()
  188.           if t
  189.             t.status = READY
  190.             readyList.AddToEnd (t)
  191.           endIf
  192.           oldIntStat = SetInterruptsTo (oldIntStat)
  193.         endMethod
  194.  
  195.       ----------  Condition . Broadcast  ----------
  196.  
  197.       method Broadcast (mutex: ptr to Mutex)
  198.           var
  199.             oldIntStat: int
  200.             t: ptr to Thread
  201.           if ! mutex.IsHeldByCurrentThread ()
  202.             FatalError ("Attempt to broadcast a condition when lock is not held")
  203.           endIf
  204.           oldIntStat = SetInterruptsTo (DISABLED)
  205.           while true
  206.             t = waitingThreads.Remove ()
  207.             if t == null
  208.               break
  209.             endIf
  210.             t.status = READY
  211.             readyList.AddToEnd (t)
  212.           endWhile
  213.           oldIntStat = SetInterruptsTo (oldIntStat)
  214.         endMethod
  215.  
  216.   endBehavior
  217.  
  218. endCode
  219.