forked from nmcl/JavaSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulationEntity.java
More file actions
275 lines (221 loc) · 8.47 KB
/
SimulationEntity.java
File metadata and controls
275 lines (221 loc) · 8.47 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* 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,
*/
package org.javasim;
public class SimulationEntity extends SimulationProcess
{
public void finalize ()
{
super.finalize();
}
/**
* Interrupt the given process (which *must* be in Wait or WaitFor), and
* resume it. If immediate resumption is required then this process will be
* suspended (placed back on to the scheduler queue for "immediate"
* resumption when the interrupted process has finished).
*
* @param toInterrupt the process to interrupt.
* @param immediate specify whether immediate resumption is required.
* @throws SimulationException if there is a problem.
* @throws RestartException if the simulation has been restarted.
*/
public void interrupt (SimulationEntity toInterrupt, boolean immediate)
throws SimulationException, RestartException
{
if (toInterrupt.terminated())
throw new SimulationException("Entity already terminated.");
if (!toInterrupt._waiting)
throw new SimulationException("Entity not waiting.");
toInterrupt._interrupted = true;
// remove from queue for "immediate" activation
Scheduler.unschedule(toInterrupt); // remove from queue and prepare to
// suspend
// will take over when this process is suspended
toInterrupt.reactivateAt(SimulationProcess.currentTime(), true);
/*
* Put "this" on to queue and suspend so that interrupted process can
* run.
*/
if (immediate)
reactivateAt(SimulationProcess.currentTime());
}
/**
* Trigger this instance.
*/
public final void trigger ()
{
_triggered = true;
_waiting = false;
}
/**
* @return whether or not this instance is currently waiting.
*/
public final boolean isWaiting ()
{
return _waiting;
}
/**
* Must wake up any waiting process before we "die". Currently only a single
* process can wait on this condition, but this may change to a list later.
*/
public void terminate ()
{
/*
* Resume waiting process before this one "dies".
*/
if (_isWaitedOnBy != null)
{
// remove from queue for "immediate" activation
try
{
_isWaitedOnBy.cancel();
_isWaitedOnBy.reactivateAt(SimulationProcess.currentTime(), true);
}
catch (RestartException e)
{
}
catch (SimulationException e)
{
}
_isWaitedOnBy = null;
}
super.terminate();
}
protected SimulationEntity()
{
super();
_isWaitedOnBy = null;
_interrupted = _triggered = _waiting = false;
}
/**
* Wait for specified period of time. If this process is interrupted then
* the InterruptedException is thrown.
*
* @param waitTime the time to wait.
* @throws SimulationException thrown if an error occurs.
* @throws RestartException thrown if the simulation has been restarted.
* @throws InterruptedException thrown if this instance has been interrupted.
*/
protected void timedWait (double waitTime) throws SimulationException,
RestartException, InterruptedException
{
_waiting = true;
try
{
hold(waitTime);
}
catch (SimulationException e)
{
throw new SimulationException("Invalid entity.");
}
_waiting = false;
if (_interrupted)
{
_interrupted = false;
throw new InterruptedException();
}
}
/**
* Suspends the current process until the process in the parameter has been
* terminated. If the calling process is interrupted before the 'controller'
* is terminated, then the InterruptedException is thrown. If the boolean
* parameter is true then the controller is reactivated immediately.
*
* @param controller the process upon whose termination this instance will be resumed.
* @param reAct indicate whether or not the controlling process should be activated now.
* @throws SimulationException thrown if an error occurs.
* @throws RestartException thrown if the simulation has been restarted.
* @throws InterruptedException thrown if this instance has been interrupted.
*/
protected void waitFor (SimulationEntity controller, boolean reAct)
throws SimulationException, RestartException, InterruptedException
{
if (controller == this) // can't wait on self!
throw new SimulationException("WaitFor cannot wait on self.");
controller._isWaitedOnBy = this; // resume when controller terminates
// make sure this is ready to run
try
{
if (reAct)
controller.reactivateAt(SimulationProcess.currentTime(), true);
}
catch (SimulationException e)
{
}
_waiting = true;
// we don't go back on to queue as controller will wake us
cancel();
_waiting = _interrupted = false;
// if we have been successful then terminated = true
if (!controller.terminated())
throw new InterruptedException();
}
/**
* Suspends the current process until the process in the parameter has been
* terminated. If the calling process is interrupted before the 'controller'
* is terminated, then the InterruptedException is thrown. The controller
* will not be reactivated immediately.
*
* @param controller the process upon whose termination this instance will be resumed.
* @throws SimulationException thrown if an error occurs.
* @throws RestartException thrown if the simulation has been restarted.
* @throws InterruptedException thrown if this instance has been interrupted.
*/
protected void waitFor (SimulationEntity controller)
throws SimulationException, RestartException, InterruptedException
{
waitFor(controller, false);
}
/**
* The calling process is placed onto the trigger queue and should only be
* restarted pending some application specific event which uses the trigger
* queue. The InterruptedException is thrown if the caller is interrupted
* rather than being triggered.
*
* @param _queue the queue to place this process.
* @throws SimulationException thrown if an error occurs.
* @throws RestartException thrown if the simulation has been restarted.
* @throws InterruptedException thrown if this instance has been interrupted.
*/
protected void waitForTrigger (TriggerQueue _queue)
throws SimulationException, RestartException, InterruptedException
{
_queue.insert(this);
_interrupted = false;
_waiting = true;
cancel(); // remove from queue and suspend
// indicate whether this was triggered successfully or interrupted
if (_triggered)
_triggered = false;
else
throw new InterruptedException();
}
/**
* Currently, a process which is waiting on a semaphore cannot be
* interrupted - its wait status is not set.
*/
protected void waitForSemaphore (Semaphore _sem) throws RestartException
{
_sem.get(this);
}
private SimulationEntity _isWaitedOnBy;
private boolean _interrupted;
private boolean _triggered;
private boolean _waiting;
}