-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alf
committed
Aug 30, 2012
1 parent
3036663
commit 13ec3e8
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/cz/cuni/mff/d3s/deeco/scheduling/MultithreadedSchedulerJPF.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package cz.cuni.mff.d3s.deeco.scheduling; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import cz.cuni.mff.d3s.deeco.invokable.SchedulableProcess; | ||
import cz.cuni.mff.d3s.deeco.invokable.TriggeredSchedulableProcess; | ||
import cz.cuni.mff.d3s.deeco.knowledge.KnowledgeManager; | ||
|
||
/** | ||
* Special scheduler for testing via JPF. | ||
* | ||
* Each task has a separate thread and is executed in the endless cycle. | ||
* Timing is ignored (due to GC - see comments in code) | ||
* | ||
* @author alf | ||
* | ||
*/ | ||
public class MultithreadedSchedulerJPF extends Scheduler { | ||
|
||
private Set<Thread> threads; | ||
|
||
public MultithreadedSchedulerJPF(KnowledgeManager km) { | ||
super(); | ||
this.km = km; | ||
this.threads = new HashSet<Thread>(); | ||
|
||
|
||
// JPF Optimization -> earlier class loading a clinit call (in the single threaded part) | ||
// Shrinks state space | ||
@SuppressWarnings("unused") | ||
ETriggerType e = ETriggerType.COORDINATOR; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
if (!running) { | ||
for (SchedulableProcess sp : periodicProcesses) { | ||
startPeriodicProcess(sp, | ||
((ProcessPeriodicSchedule) sp.scheduling).interval); | ||
} | ||
for (TriggeredSchedulableProcess tsp : triggeredProcesses) { | ||
if (true) { | ||
assert(false); // ALF: Unsupported now | ||
} | ||
km.registerListener(tsp); | ||
} | ||
km.switchListening(true); | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
if (running) { | ||
for (Thread t : threads) { | ||
t.interrupt(); | ||
} | ||
km.switchListening(false); | ||
} | ||
} | ||
|
||
private void startPeriodicProcess(SchedulableProcess process, long period) { | ||
// Note Period is intentionally ignored - GC | ||
// Because of the stop the world Garbage collector, which can postpone all threads and then any thread can execute its own action | ||
Thread t = new Thread(new PeriodicProcessRunner(process)); | ||
threads.add(t); | ||
t.start(); | ||
} | ||
|
||
class PeriodicProcessRunner implements Runnable { | ||
|
||
final private SchedulableProcess process; | ||
|
||
public PeriodicProcessRunner(SchedulableProcess process) { | ||
this.process = process; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (!Thread.interrupted()) | ||
{ | ||
try { | ||
process.invoke(); | ||
} catch (Exception e) { | ||
System.out.println("Process scheduled exception!"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |