mirror of
https://github.com/oliverschmidt/contiki.git
synced 2024-12-22 10:30:13 +00:00
Fix SafeRandom logic
This commit is contained in:
parent
6f86e0bd92
commit
f17fc20fe7
@ -45,17 +45,30 @@ public class SafeRandom extends Random {
|
|||||||
|
|
||||||
Simulation sim = null;
|
Simulation sim = null;
|
||||||
Thread initThread = null;
|
Thread initThread = null;
|
||||||
|
Boolean simStarted = false;
|
||||||
|
|
||||||
private void assertSimThread() {
|
private void assertSimThread() {
|
||||||
// It we are in the simulation thread everything is fine (the default)
|
|
||||||
// sim can be null, because setSeed is called by the super-constructor.
|
// sim can be null, because setSeed is called by the super-constructor.
|
||||||
if(sim != null && !sim.isSimulationThread()) {
|
if(sim == null) return;
|
||||||
// The thread initializing the simulation might differ from the simulation thread.
|
|
||||||
// If they are the same that is ok, too.
|
// If we are in the simulation thread, everything is fine (the default)
|
||||||
|
if(sim.isSimulationThread()) {
|
||||||
|
simStarted = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simulation has not started yet. Allow one initialisation thread.
|
||||||
|
if(!simStarted) {
|
||||||
if(initThread == null) initThread = Thread.currentThread();
|
if(initThread == null) initThread = Thread.currentThread();
|
||||||
if(Thread.currentThread() == initThread ) return;
|
if(Thread.currentThread() == initThread ) return;
|
||||||
throw new RuntimeException("A random-function was not called from the simulation thread. This can break things!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//This is done via the GUI - reproducibility is lost anyway!
|
||||||
|
if(javax.swing.SwingUtilities.isEventDispatchThread()) return;
|
||||||
|
|
||||||
|
// Some other threads seems to access the PNRG. This must not happen.
|
||||||
|
throw new RuntimeException("A random-function was not called from the simulation thread. This can break things!");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SafeRandom(Simulation sim) {
|
public SafeRandom(Simulation sim) {
|
||||||
|
Loading…
Reference in New Issue
Block a user