1 | package de.uka.ipd.sdq.simucomframework.fork; |
2 | |
3 | import org.apache.log4j.Logger; |
4 | |
5 | import de.uka.ipd.sdq.simucomframework.SimuComSimProcess; |
6 | |
7 | /** |
8 | * Helper to execute a fork action in the PCM. Implements the |
9 | * barrier design pattern. |
10 | * @author Steffen Becker |
11 | * |
12 | */ |
13 | public class ForkExecutor { |
14 | private ForkedBehaviourProcess[] forks; |
15 | private SimuComSimProcess parent; |
16 | private static Logger logger = |
17 | Logger.getLogger(ForkExecutor.class.getName()); |
18 | |
19 | /** |
20 | * Initialise the barrier with the forks to spawn and the parent process |
21 | * which is continoued when all forks are done |
22 | * @param parent The parent simulation thread |
23 | * @param forks The threads to run in parallel |
24 | */ |
25 | public ForkExecutor(SimuComSimProcess parent, ForkedBehaviourProcess[] forks) { |
26 | this.forks = forks; |
27 | this.parent = parent; |
28 | } |
29 | |
30 | /** |
31 | * Execute the child threads in parallel waiting for them to finish |
32 | */ |
33 | public void run() { |
34 | logger.debug("Running parallel operations"); |
35 | double start = parent.getModel().getSimulationControl().getCurrentSimulationTime(); |
36 | for(ForkedBehaviourProcess p : forks) |
37 | p.scheduleAt(0); |
38 | while(checkIfRemainingChildrenRun()) |
39 | parent.passivate(); |
40 | logger.debug("Forks took: "+(parent.getModel().getSimulationControl().getCurrentSimulationTime()-start)); |
41 | } |
42 | |
43 | /** |
44 | * @return True if there are child forks still running. This needs not be |
45 | * threadsafe as desmoj always exeutes only a single thread |
46 | */ |
47 | private boolean checkIfRemainingChildrenRun() { |
48 | for(ForkedBehaviourProcess p : forks) |
49 | if (!p.isAsync() && !p.isTerminated()) |
50 | return true; |
51 | return false; |
52 | } |
53 | } |