nachos.threads
Class Scheduler

java.lang.Object
  extended by nachos.threads.Scheduler
Direct Known Subclasses:
PriorityScheduler, RoundRobinScheduler

public abstract class Scheduler
extends Object

Coordinates a group of thread queues of the same kind.

See Also:
ThreadQueue

Constructor Summary
Scheduler()
          Allocate a new scheduler.
 
Method Summary
 boolean decreasePriority()
          If possible, lower the priority of the current thread user in some scheduler-dependent way, preferably by the same amount as would a call to increasePriority().
 int getEffectivePriority()
          Get the effective priority of the current thread.
 int getEffectivePriority(KThread thread)
          Get the effective priority of the specified thread.
 int getPriority()
          Get the priority of the current thread.
 int getPriority(KThread thread)
          Get the priority of the specified thread.
 boolean increasePriority()
          If possible, raise the priority of the current thread in some scheduler-dependent way.
abstract  ThreadQueue newThreadQueue(boolean transferPriority)
          Allocate a new thread queue.
 void setPriority(int priority)
          Set the priority of the current thread.
 void setPriority(KThread thread, int priority)
          Set the priority of the specified thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Scheduler

public Scheduler()
Allocate a new scheduler.

Method Detail

newThreadQueue

public abstract ThreadQueue newThreadQueue(boolean transferPriority)
Allocate a new thread queue. If transferPriority is true, then threads waiting on the new queue will transfer their "priority" to the thread that has access to whatever is being guarded by the queue. This is the mechanism used to partially solve priority inversion.

If there is no definite thread that can be said to have "access" (as in the case of semaphores and condition variables), this parameter should be false, indicating that no priority should be transferred.

The processor is a special case. There is clearly no purpose to donating priority to a thread that already has the processor. When the processor wait queue is created, this parameter should be false.

Otherwise, it is beneficial to donate priority. For example, a lock has a definite owner (the thread that holds the lock), and a lock is always released by the same thread that acquired it, so it is possible to help a high priority thread waiting for a lock by donating its priority to the thread holding the lock. Therefore, a queue for a lock should be created with this parameter set to true.

Similarly, when a thread is asleep in join() waiting for the target thread to finish, the sleeping thread should donate its priority to the target thread. Therefore, a join queue should be created with this parameter set to true.

Parameters:
transferPriority - true if the thread that has access should receive priority from the threads that are waiting on this queue.
Returns:
a new thread queue.

getPriority

public int getPriority(KThread thread)
Get the priority of the specified thread. Must be called with interrupts disabled.

Parameters:
thread - the thread to get the priority of.
Returns:
the thread's priority.

getPriority

public int getPriority()
Get the priority of the current thread. Equivalent to getPriority(KThread.currentThread()).

Returns:
the current thread's priority.

getEffectivePriority

public int getEffectivePriority(KThread thread)
Get the effective priority of the specified thread. Must be called with interrupts disabled.

The effective priority of a thread is the priority of a thread after taking into account priority donations.

For a priority scheduler, this is the maximum of the thread's priority and the priorities of all other threads waiting for the thread through a lock or a join.

For a lottery scheduler, this is the sum of the thread's tickets and the tickets of all other threads waiting for the thread through a lock or a join.

Parameters:
thread - the thread to get the effective priority of.
Returns:
the thread's effective priority.

getEffectivePriority

public int getEffectivePriority()
Get the effective priority of the current thread. Equivalent to getEffectivePriority(KThread.currentThread()).

Returns:
the current thread's priority.

setPriority

public void setPriority(KThread thread,
                        int priority)
Set the priority of the specified thread. Must be called with interrupts disabled.

Parameters:
thread - the thread to set the priority of.
priority - the new priority.

setPriority

public void setPriority(int priority)
Set the priority of the current thread. Equivalent to setPriority(KThread.currentThread(), priority).

Parameters:
priority - the new priority.

increasePriority

public boolean increasePriority()
If possible, raise the priority of the current thread in some scheduler-dependent way.

Returns:
true if the scheduler was able to increase the current thread's priority.

decreasePriority

public boolean decreasePriority()
If possible, lower the priority of the current thread user in some scheduler-dependent way, preferably by the same amount as would a call to increasePriority().

Returns:
true if the scheduler was able to decrease the current thread's priority.