1 | package de.uka.ipd.sdq.scheduler.processes.impl; |
2 | |
3 | import de.uka.ipd.sdq.probfunction.math.util.MathTools; |
4 | import de.uka.ipd.sdq.scheduler.ISchedulableProcess; |
5 | import de.uka.ipd.sdq.scheduler.SchedulerModel; |
6 | import de.uka.ipd.sdq.scheduler.priority.IPriority; |
7 | import de.uka.ipd.sdq.scheduler.priority.IPriorityUpdateStrategy; |
8 | import de.uka.ipd.sdq.scheduler.processes.IActiveProcess; |
9 | import de.uka.ipd.sdq.scheduler.queueing.IRunQueue; |
10 | |
11 | public class ProcessWithPriority extends PreemptiveProcess { |
12 | |
13 | private IPriority staticPriority; |
14 | private IPriority dynamicPriority; |
15 | private IPriorityUpdateStrategy priorityUpdateStrategy; |
16 | private static boolean in_front_if_priority_changed = false; |
17 | |
18 | public ProcessWithPriority(SchedulerModel model, ISchedulableProcess process, |
19 | IPriority staticPriority) { |
20 | super(model, process); |
21 | this.staticPriority = staticPriority; |
22 | this.dynamicPriority = staticPriority; |
23 | this.priorityUpdateStrategy = null; |
24 | } |
25 | |
26 | public IPriority getStaticPriority() { |
27 | return staticPriority; |
28 | } |
29 | |
30 | public IPriority getDynamicPriority() { |
31 | return dynamicPriority; |
32 | } |
33 | |
34 | public void setPriorityUpdateStrategy( |
35 | IPriorityUpdateStrategy priorityUpdateStrategy) { |
36 | this.priorityUpdateStrategy = priorityUpdateStrategy; |
37 | } |
38 | |
39 | public void updatePriority(){ |
40 | if (this.priorityUpdateStrategy != null) |
41 | this.priorityUpdateStrategy.update(this); |
42 | } |
43 | |
44 | public boolean hasBonus() { |
45 | return dynamicPriority.greaterThan(staticPriority); |
46 | } |
47 | |
48 | /** |
49 | * Sets the dynamic priority back to the original, static priority. |
50 | */ |
51 | public void resetDynamicPriority() { |
52 | changePriority(staticPriority); |
53 | } |
54 | |
55 | public void decreasePriority() { |
56 | changePriority(dynamicPriority.decrease()); |
57 | } |
58 | |
59 | public boolean setToStaticPriorityWithBonus(int bonus) { |
60 | return changePriority(staticPriority.addBonus(bonus)); |
61 | } |
62 | |
63 | private boolean changePriority(IPriority new_priority) { |
64 | if (!dynamicPriority.equals(new_priority)){ |
65 | IRunQueue q = null; |
66 | if (isReady() && (q = getRunQueue()) != null){ |
67 | q.removeProcess(this); |
68 | dynamicPriority = new_priority; |
69 | q.addProcess(this,in_front_if_priority_changed); |
70 | } else { |
71 | dynamicPriority = new_priority; |
72 | } |
73 | return true; |
74 | } |
75 | return false; |
76 | } |
77 | |
78 | @Override |
79 | public void update() { |
80 | super.update(); |
81 | updatePriority(); |
82 | } |
83 | |
84 | @Override |
85 | public String toString() { |
86 | return getName() + " (" + MathTools.round( getTimeslice().getRemainingTime(), 0.1) +", " + getDynamicPriority() + ")"; |
87 | } |
88 | |
89 | @Override |
90 | public IActiveProcess createNewInstance(ISchedulableProcess process) { |
91 | ProcessWithPriority p = new ProcessWithPriority(getModel(), process, staticPriority); |
92 | p.dynamicPriority = staticPriority; |
93 | if (this.priorityUpdateStrategy != null){ |
94 | p.priorityUpdateStrategy = this.priorityUpdateStrategy.cloneFor(p); |
95 | } |
96 | p.setTimeSlice(this.getTimeslice().clone()); |
97 | p.updatePriority(); |
98 | return p; |
99 | } |
100 | } |