1 | package de.uka.ipd.sdq.scheduler.queueing.runqueues; |
2 | |
3 | import java.util.Hashtable; |
4 | import java.util.Map; |
5 | |
6 | import de.uka.ipd.sdq.scheduler.processes.IActiveProcess; |
7 | import de.uka.ipd.sdq.scheduler.queueing.IRunQueue; |
8 | import de.uka.ipd.sdq.scheduler.resources.IResourceInstance; |
9 | |
10 | public abstract class AbstractRunQueue implements IRunQueue { |
11 | |
12 | protected Map<IActiveProcess, IResourceInstance> running_on_table; |
13 | |
14 | protected AbstractRunQueue() { |
15 | running_on_table = new Hashtable<IActiveProcess, IResourceInstance>(); |
16 | } |
17 | |
18 | public int getCurrentLoad() { |
19 | int running = running_on_table.size(); |
20 | int waiting = numWaitingProcesses(); |
21 | return running + waiting ; |
22 | } |
23 | |
24 | public boolean isEmpty() { |
25 | return getCurrentLoad() == 0; |
26 | } |
27 | |
28 | public boolean removeProcess(IActiveProcess process) { |
29 | return running_on_table.remove(process) != null |
30 | || removePendingProcess(process); |
31 | } |
32 | |
33 | public boolean contains(IActiveProcess process) { |
34 | return running_on_table.containsKey(process) || containsPending(process); |
35 | } |
36 | |
37 | public void addProcess(IActiveProcess process, boolean inFront) { |
38 | process.setRunQueue(this); |
39 | addProcessToRunQueue(process,inFront); |
40 | } |
41 | |
42 | public boolean containsRunning(IActiveProcess process) { |
43 | return running_on_table.containsKey(process); |
44 | } |
45 | |
46 | public void removeRunning(IActiveProcess process) { |
47 | assert running_on_table.containsKey(process) : "Process '" + process + "' not running."; |
48 | running_on_table.remove(process); |
49 | } |
50 | |
51 | public void setRunningOn(IActiveProcess process, IResourceInstance instance) { |
52 | assert running_on_table.get(process) == null; |
53 | assert !running_on_table.values().contains(instance); |
54 | running_on_table.put(process, instance); |
55 | } |
56 | |
57 | public IResourceInstance runningOn(IActiveProcess process) { |
58 | return running_on_table.get(process); |
59 | } |
60 | |
61 | public boolean isIdle(IResourceInstance instance) { |
62 | return !instance.processAssigned() |
63 | && (running_on_table.size() > 1 || numWaitingProcesses() == 0); |
64 | } |
65 | |
66 | public abstract IActiveProcess getNextRunnableProcess( |
67 | IResourceInstance instance); |
68 | |
69 | /** |
70 | * Template Method. Returns |
71 | * |
72 | * @param process |
73 | * @return |
74 | */ |
75 | public abstract boolean containsPending(IActiveProcess process); |
76 | |
77 | /** |
78 | * Template Method. Returns the number of processs waiting in the queue. |
79 | * |
80 | * @return |
81 | */ |
82 | protected abstract int numWaitingProcesses(); |
83 | |
84 | /** |
85 | * Template method. Removes the given process from the queue. |
86 | * |
87 | * @param process |
88 | */ |
89 | public abstract boolean removePendingProcess(IActiveProcess process); |
90 | |
91 | /** |
92 | * Template method. |
93 | * |
94 | * @param process |
95 | * @param inFront |
96 | */ |
97 | protected abstract void addProcessToRunQueue(IActiveProcess process, boolean inFront); |
98 | |
99 | } |