forked from nmcl/JavaSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.java
More file actions
255 lines (215 loc) · 6.19 KB
/
Scheduler.java
File metadata and controls
255 lines (215 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne
* and others contributors as indicated
* by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* (C) 1990-2008,
*/
/*
* Copyright (C) 1996, 1997, 1998,
*
* Department of Computing Science,
* The University,
* Newcastle upon Tyne,
* UK.
*
* $Id: Scheduler.java,v 1.3 1998/12/07 08:28:10 nmcl Exp $
*/
package org.javasim;
import java.util.NoSuchElementException;
import org.javasim.internal.SimulationProcessIterator;
import org.javasim.internal.SimulationProcessList;
/**
* This is the scheduler: the heart of the simulation system.
*
* Note: unlike in SIMULA, an active process is removed from the simulation
* queue prior to being activated.
*
* @author marklittle
*
*/
public class Scheduler extends Thread
{
/**
* Get the current simulation time.
*
* @return the current simulation time.
*/
public static double currentTime ()
{
return Scheduler.SimulatedTime;
}
/**
* This routine resets the simulation time to zero and removes all
* entries from the scheduler queue (as their times may no longer
* be valid). Whatever operation caused the processes to become
* suspended will raise the RestartSimulation exception, which the
* application should catch. It should then perform any work necessary
* to put the process back in a state ready for restarting the simulation
* before calling Cancel on the process.
*
* @throws SimulationException if an error occurs.
*/
static synchronized void reset () throws SimulationException
{
boolean finished = false;
SimulationProcess tmp = SimulationProcess.current();
// set resetting process to idle
Scheduler.unschedule(tmp); // remove from queue
do
{
try
{
tmp = Scheduler.ReadyQueue.remove();
tmp.deactivate();
}
catch (NoSuchElementException e)
{
finished = true;
}
} while (!finished);
finished = false;
SimulationProcessIterator iter = new SimulationProcessIterator(SimulationProcess.allProcesses);
do
{
try
{
tmp = iter.get();
/*
* Every process must be in Suspend, so we call Resume
* and get each one to check whether the simulation is
* restarting. If it is, it raises an exception and waits
* for the user to cancel the process after setting it
* to become ready to restart.
*/
tmp.resumeProcess();
/*
* Wait for this process to become idle again.
*/
while (!tmp.idle())
Thread.yield();
}
catch (NullPointerException e)
{
finished = true;
}
} while (!finished);
Scheduler.SimulatedTime = 0.0;
SimulationProcess.Current = null;
SimulationProcess.allProcesses = new SimulationProcessList();
}
private Scheduler ()
{
}
/**
* It is possible that the currently active process may remove itself
* from the simulation queue. In which case we don't want to suspend the
* process since it needs to continue to run. The return value indicates
* whether or not to call suspend on the currently active process.
*/
static synchronized boolean schedule () throws SimulationException
{
if (Simulation.isStarted())
{
SimulationProcess p = SimulationProcess.current();
try
{
/*
* For some reason when executing tests in junit an old and dead
* thread appears in the simulation queue. Have only ever seen this
* be a single thread instance, but it is reproducible every time.
*
* https://github.com/nmcl/JavaSim/issues/64
*
* Will try to find out what actually causes this and remove the
* workaround eventually.
*
* https://github.com/nmcl/JavaSim/issues/76
*/
SimulationProcess.Current = Scheduler.ReadyQueue.remove();
boolean done = true;
do
{
if (SimulationProcess.Current != null)
{
if (SimulationProcess.Current.getThreadGroup() == null)
{
SimulationProcess.Current = Scheduler.ReadyQueue.remove();
p = SimulationProcess.current();
done = false;
}
else
done = true;
}
else
throw new NoSuchElementException();
}
while (!done);
}
catch (NoSuchElementException e)
{
System.out.println("Simulation queue empty.");
return false;
}
catch (NullPointerException e)
{
System.out.println("Simulation queue empty.");
return false;
}
if (SimulationProcess.Current.evtime() < 0)
throw new SimulationException("Invalid SimulationProcess wakeup time.");
else
Scheduler.SimulatedTime = SimulationProcess.Current.evtime();
if (p != SimulationProcess.Current)
{
// Simulation.printQueue();
SimulationProcess.Current.resumeProcess();
return true;
}
else
return false;
}
else
throw new SimulationException("Simulation not started.");
}
static synchronized void unschedule (SimulationProcess p)
{
try
{
Scheduler.ReadyQueue.remove(p); // remove from queue
}
catch (NoSuchElementException e)
{
}
p.deactivate();
}
static SimulationProcessList getQueue ()
{
synchronized (theScheduler)
{
return ReadyQueue;
}
}
static double getSimulationTime ()
{
synchronized (theScheduler)
{
return SimulatedTime;
}
}
private static double SimulatedTime = 0.0;
private static SimulationProcessList ReadyQueue = new SimulationProcessList();
static Scheduler theScheduler = new Scheduler();
}