nachos.threads
Class ThreadQueue

java.lang.Object
  extended by nachos.threads.ThreadQueue
Direct Known Subclasses:
PriorityScheduler.PriorityQueue

public abstract class ThreadQueue
extends Object

Schedules access to some sort of resource with limited access constraints. A thread queue can be used to share this limited access among multiple threads.

Examples of limited access in Nachos include:

  1. the right for a thread to use the processor. Only one thread may run on the processor at a time.
  2. the right for a thread to acquire a specific lock. A lock may be held by only one thread at a time.
  3. the right for a thread to return from Semaphore.P() when the semaphore is 0. When another thread calls Semaphore.V(), only one thread waiting in Semaphore.P() can be awakened.
  4. the right for a thread to be woken while sleeping on a condition variable. When another thread calls Condition.wake(), only one thread sleeping on the condition variable can be awakened.
  5. the right for a thread to return from KThread.join(). Threads are not allowed to return from join() until the target thread has finished.
All these cases involve limited access because, for each of them, it is not necessarily possible (or correct) for all the threads to have simultaneous access. Some of these cases involve concrete resources (e.g. the processor, or a lock); others are more abstract (e.g. waiting on semaphores, condition variables, or join).

All thread queue methods must be invoked with interrupts disabled.


Constructor Summary
ThreadQueue()
           
 
Method Summary
abstract  void acquire(KThread thread)
          Notify this thread queue that a thread has received access, without going through request() and nextThread().
abstract  KThread nextThread()
          Notify this thread queue that another thread can receive access.
abstract  void print()
          Print out all the threads waiting for access, in no particular order.
abstract  void waitForAccess(KThread thread)
          Notify this thread queue that the specified thread is waiting for access.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadQueue

public ThreadQueue()
Method Detail

waitForAccess

public abstract void waitForAccess(KThread thread)
Notify this thread queue that the specified thread is waiting for access. This method should only be called if the thread cannot immediately obtain access (e.g. if the thread wants to acquire a lock but another thread already holds the lock).

A thread must not simultaneously wait for access to multiple resources. For example, a thread waiting for a lock must not also be waiting to run on the processor; if a thread is waiting for a lock it should be sleeping.

However, depending on the specific objects, it may be acceptable for a thread to wait for access to one object while having access to another. For example, a thread may attempt to acquire a lock while holding another lock. Note, though, that the processor cannot be held while waiting for access to anything else.

Parameters:
thread - the thread waiting for access.

nextThread

public abstract KThread nextThread()
Notify this thread queue that another thread can receive access. Choose and return the next thread to receive access, or null if there are no threads waiting.

If the limited access object transfers priority, and if there are other threads waiting for access, then they will donate priority to the returned thread.

Returns:
the next thread to receive access, or null if there are no threads waiting.

acquire

public abstract void acquire(KThread thread)
Notify this thread queue that a thread has received access, without going through request() and nextThread(). For example, if a thread acquires a lock that no other threads are waiting for, it should call this method.

This method should not be called for a thread returned from nextThread().

Parameters:
thread - the thread that has received access, but was not returned from nextThread().

print

public abstract void print()
Print out all the threads waiting for access, in no particular order.