1,277
edits
Changes
→Priority Scheduler: formatting
==Priority Scheduler==
===Implementation===
;New state variables
* {{c|PriorityQueue.holder}} - this ThreadState corresponds to the holder of the resource signified by the
* {{c|PriorityQueue.waitQueue}} - an ArrayList of ThreadStates waiting on this resource. Unsorted.
* {{c|PriorityQueue.dirty}} - set to true when a new thread is added to the queue, or any of the queues in the waitQueue flag themselves as dirty.
* {{c|PriorityQueue.effective}} - the cached highest of the effective priorities in the waitQueue. This value is invalidated while dirty is true.
At this point, recalculation is simple. The effective priority of a thread is the maximum of its own actual priority and the priorities of all the {{c|PriorityQueue}}s that it currently holds. The effective priority of a {{c|PriorityQueue}} is the maximum effective priority of all the threads waiting on it (if the queue is supposed to donate priority), and so on and so forth in a mutually recursive manner.
;Implementation details
* {{c|PriorityQueue.nextThread()}}* :This method retrieves and removes the highest priority thread off the Priority-Time {{c|ArrayList}}. It then flags the previous holder of this resource as {{c|dirty}}, and removes the queue from that holder's resource list. It then sets the retrieved thread to this queue's new {{c|holder}}, and flags that thread as {{c|dirty }} as well.* {{c|PriorityQueue.acquire(KThread thread)}}* :Sets the {{c|holder }} of this queue to the specified thread, bypassing the queue. Resets previous {{c|holder }} and sets {{c|dirty }} flags as in {{c|nextThread()}}.* {{c|PriorityQueue.pickNextThread()}}* :Simply retrieves the highest priority thread off this queue.* {{c|PriorityQueue.setDirty()}}* :Set this queue's {{c|dirty }} flag, and calls {{c|setDirty }} on the current holder of this thread.* {{c|PriorityQueue.getEffectivePriority()}}* :If this queue is "{{c|dirty"}}, returns the maximum of each of the ThreadStates{{c|ThreadState}}s{{c|.getEffectivePriority() }} in this queue. Those calls in turn become mutually recursive when they call {{c|getEffectivePriority ()}} on the {{c|PriorityQueues }} in their myResrouces{{c|myResources}}.* {{c|ThreadState.setPriority() }}* :This method will change the actual priority of the thread associated with the {{c|ThreadState}}. It then does calls {{c|setDirty() }} on this thread.* {{c|ThreadState.setDirty()}}* :Sets the {{c|dirty }} flag on this thread, then calls {{c|setDirty()}} on each of the PriorityQueues {{c|PriorityQueue}}s that the thread is waiting for, does setDirty on them. Mutually recursive.* {{c|ThreadState.getEffectivePriority}}* :Like the analogue of this function in {{c|PriorityQueue}}, returns the (cached) priority if this thread is not {{c|dirty}}; otherwise, otherwise recalculates by returning the max of the effective priorities of the PriorityQueues {{c|PriorityQueue}}s in {{c|myResources}}.
===Testing===
===Pseudocode===
;PriorityQueue {{c|<pre> public void waitForAccess(KThread thread) add this thread to my waitQueue thread.waitForAccess(this) public void acquire(KThread thread) if I have a holder and I transfer priority, remove myself from the holder's resource list thread.acquire(this) public KThread nextThread() if I have a holder and I transfer priority, remove myself from the holder's resource list if waitQueue is empty, return null ThreadState firstThread = pickNextThread(); remove firstThread from waitQueue firstThread.acquire(this); return firstThread } public int getEffectivePriority() if I do not transfer priority, return minimum priority if (dirty) effective = minimum priority; for each ThreadState t in waitQueue effective = MAX(effective, t.getEffectivePriority()) dirty = false; return effective; public void setDirty() if I do not transfer priority, there is no need to recurse, return dirty = true; if I have a holder, holder.setDirty() protected ThreadState pickNextThread() ThreadState ret = null for each ThreadState ts in waitQueue if ret is null OR ts has higher priority/time ranking than ret set ret to ts return ret;</pre>}} ;ThreadState{{c|<pre>public int getPriority() return non-donated priority. public int getEffectivePriority() if (dirty) { effective = non-donated priority for each PriorityQueue pq that I am currently holding effective = MAX(effective, pq.getEffectivePriority) } return effective;
public void acquire(PriorityQueue waitQueue)
add waitQueue to myResources list
if waitQueue was in my waitingOn list, remove it
setWaitQueue's holder to me
setDirty();</pre>}}
==Boat.java==