| 1 | package de.uka.ipd.sdq.scheduler.resources.passive; |
| 2 | |
| 3 | import de.uka.ipd.sdq.scheduler.IRunningProcess; |
| 4 | import de.uka.ipd.sdq.scheduler.ISchedulableProcess; |
| 5 | import de.uka.ipd.sdq.scheduler.LoggingWrapper; |
| 6 | import de.uka.ipd.sdq.scheduler.SchedulerModel; |
| 7 | import de.uka.ipd.sdq.scheduler.priority.IPriorityBoost; |
| 8 | import de.uka.ipd.sdq.scheduler.processes.IActiveProcess; |
| 9 | import de.uka.ipd.sdq.scheduler.processes.impl.PreemptiveProcess; |
| 10 | import de.uka.ipd.sdq.scheduler.processes.impl.ProcessWithPriority; |
| 11 | import de.uka.ipd.sdq.scheduler.resources.IResourceInstance; |
| 12 | import de.uka.ipd.sdq.scheduler.resources.active.SimActiveResource; |
| 13 | import de.uka.ipd.sdq.scheduler.sensors.IPassiveResourceSensor; |
| 14 | |
| 15 | public class SimFairPassiveResource extends SimAbstractPassiveResource { |
| 16 | |
| 17 | private PassiveResourceObservee observee; |
| 18 | private int available; |
| 19 | |
| 20 | public SimFairPassiveResource(SchedulerModel model, int capacity, String name, String id, |
| 21 | IPriorityBoost priority_boost, SimActiveResource managing_resource) { |
| 22 | super(model, capacity, name, id, priority_boost, managing_resource); |
| 23 | observee = new PassiveResourceObservee(); |
| 24 | available = capacity; |
| 25 | } |
| 26 | |
| 27 | private boolean canProceed(IRunningProcess process, int num) { |
| 28 | return (waiting_queue.isEmpty() || waiting_queue.peek().getProcess() |
| 29 | .equals(process)) |
| 30 | && num <= available; |
| 31 | } |
| 32 | |
| 33 | public boolean acquire(ISchedulableProcess sched_process, int num, |
| 34 | boolean timeout, double timeoutValue) { |
| 35 | |
| 36 | // AM: Copied from AbstractActiveResource: If simulation is stopped, |
| 37 | // allow all processes to finish |
| 38 | if (!getModel().getSimulationControl().isRunning()) { |
| 39 | // Do nothing, but allows calling process to complete |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | observee.fireRequest(sched_process, num); |
| 44 | PreemptiveProcess process = (PreemptiveProcess) main_resource |
| 45 | .lookUp(sched_process); |
| 46 | if (canProceed(process, num)) { |
| 47 | grantAccess(process, num); |
| 48 | return true; |
| 49 | } else { |
| 50 | LoggingWrapper.log("Process " + process + " is waiting for " + num |
| 51 | + " of " + this); |
| 52 | WaitingProcess waiting_process = new WaitingProcess(process, num); |
| 53 | fromRunningToWaiting(waiting_process, false); |
| 54 | process.getSchedulableProcess().passivate(); |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | public void release(ISchedulableProcess sched_process, int num) { |
| 60 | |
| 61 | // AM: Copied from AbstractActiveResource: If simulation is stopped, |
| 62 | // allow all processes to finish |
| 63 | if (!getModel().getSimulationControl().isRunning()) { |
| 64 | // Do nothing, but allows calling process to complete |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | IActiveProcess process = main_resource.lookUp(sched_process); |
| 69 | |
| 70 | LoggingWrapper.log("Process " + process + " releases " + num + " of " |
| 71 | + this); |
| 72 | available += num; |
| 73 | observee.fireRelease(sched_process, num); |
| 74 | notifyWaitingProcesses(process.getLastInstance()); |
| 75 | } |
| 76 | |
| 77 | private void notifyWaitingProcesses(IResourceInstance current) { |
| 78 | WaitingProcess waitingProcess = waiting_queue.peek(); |
| 79 | if (waitingProcess != null) { |
| 80 | if (tryToDequeueProcess(waitingProcess)) |
| 81 | fromWaitingToReady(waitingProcess, current); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private void grantAccess(PreemptiveProcess process, int num) { |
| 86 | LoggingWrapper.log("Process " + process + " acquires " + num + " of " |
| 87 | + this); |
| 88 | punish(process); |
| 89 | boostPriority(process); |
| 90 | available -= num; |
| 91 | observee.fireAquire(process.getSchedulableProcess(), num); |
| 92 | assert available >= 0 : "More resource than available have been acquired!"; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tries to remove the given process from the waiting queue and get access |
| 97 | * of the required number of passive resources. |
| 98 | * |
| 99 | * @param waitingProcess |
| 100 | * @return True if the process was successfully dequeued and activated, |
| 101 | * otherwise false. |
| 102 | */ |
| 103 | private boolean tryToDequeueProcess(WaitingProcess waitingProcess) { |
| 104 | if (canProceed(waitingProcess.getProcess(), waitingProcess |
| 105 | .getNumRequested())) { |
| 106 | grantAccess((ProcessWithPriority) waitingProcess.getProcess(), |
| 107 | waitingProcess.getNumRequested()); |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | public void addObserver(IPassiveResourceSensor observer) { |
| 114 | observee.addObserver(observer); |
| 115 | } |
| 116 | |
| 117 | public void removeObserver(IPassiveResourceSensor observer) { |
| 118 | observee.removeObserver(observer); |
| 119 | } |
| 120 | |
| 121 | public int getAvailable() { |
| 122 | return available; |
| 123 | } |
| 124 | |
| 125 | } |