1 | package de.uka.ipd.sdq.simucomframework.fork; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | |
5 | import de.uka.ipd.sdq.probespec.framework.RequestContext; |
6 | import de.uka.ipd.sdq.probespec.framework.garbagecollection.IRegionBasedGarbageCollector; |
7 | import de.uka.ipd.sdq.simucomframework.Context; |
8 | import de.uka.ipd.sdq.simucomframework.SimuComSimProcess; |
9 | import de.uka.ipd.sdq.simulation.abstractsimengine.ISimProcess; |
10 | |
11 | /** |
12 | * Base class for ForkBehaviours. Generator creates a specialisation of this and |
13 | * uses it to execute actions in parallel |
14 | * |
15 | * @author Steffen Becker |
16 | * |
17 | */ |
18 | public abstract class ForkedBehaviourProcess extends SimuComSimProcess { |
19 | |
20 | protected Context ctx; |
21 | private ISimProcess myParent; |
22 | protected String assemblyContextID; |
23 | private boolean isAsync; |
24 | private boolean isTerminated = false; |
25 | private IRegionBasedGarbageCollector<RequestContext> blackboardGarbageCollector; |
26 | |
27 | private static Logger logger = Logger.getLogger(ForkedBehaviourProcess.class.getName()); |
28 | |
29 | public ForkedBehaviourProcess(Context myContext, String assemblyContextID, boolean isAsync) { |
30 | super(myContext.getModel(), "Forked Behaviour", myContext.getThread().getRequestContext()); |
31 | |
32 | // use the session id from the parent process |
33 | this.currentSessionId = myContext.getThread().getCurrentSessionId(); |
34 | |
35 | this.ctx = new ForkContext(myContext, this); |
36 | |
37 | this.myParent = myContext.getThread(); |
38 | this.assemblyContextID = assemblyContextID; |
39 | this.isAsync = isAsync; |
40 | this.blackboardGarbageCollector = myContext.getModel().getProbeSpecContext().getBlackboardGarbageCollector(); |
41 | } |
42 | |
43 | @Override |
44 | protected void internalLifeCycle() { |
45 | blackboardGarbageCollector.enterRegion(getRequestContext().rootContext()); |
46 | executeBehaviour(); |
47 | blackboardGarbageCollector.leaveRegion(getRequestContext().rootContext()); |
48 | this.isTerminated = true; |
49 | |
50 | // if this has been synchronous call of the behaviour and the parent has |
51 | // not yet terminated (which may happen under some wired conditions) and |
52 | // the simulation is still running, we can think about triggering the |
53 | // parent again. |
54 | if (!isAsync && !myParent.isTerminated() && simulationIsRunning()) |
55 | myParent.scheduleAt(0); |
56 | else |
57 | logger.debug("Asynch behaviour finished at simtime " + getModel().getSimulationControl().getCurrentSimulationTime()); |
58 | } |
59 | |
60 | private boolean simulationIsRunning() { |
61 | return this.ctx.getModel().getSimulationControl().isRunning(); |
62 | } |
63 | |
64 | /** |
65 | * Template method filled by the generate with the parallel behaviour |
66 | * specified in the PCM's fork action |
67 | */ |
68 | protected abstract void executeBehaviour(); |
69 | |
70 | public boolean isAsync() { |
71 | return isAsync; |
72 | } |
73 | |
74 | /* |
75 | * (non-Javadoc) |
76 | * |
77 | * @see |
78 | * de.uka.ipd.sdq.simulation.abstractsimengine.SimProcess#isTerminated |
79 | * () |
80 | */ |
81 | @Override |
82 | public boolean isTerminated() { |
83 | return this.isTerminated; |
84 | } |
85 | |
86 | } |