Skip to content

Commit

Permalink
optimistic bundle lock avoidance in sim thread
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinawalsh committed Jan 31, 2019
1 parent b310161 commit aae0c4e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/com/cburch/logisim/circuit/CircuitWires.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private static Value pullValue(Value base, Value pullTo) {
// derived data
private Bounds bounds = Bounds.EMPTY_BOUNDS;

private BundleMap masterBundleMap = null;
private volatile BundleMap masterBundleMap = null;

CircuitWires() {
}
Expand Down Expand Up @@ -595,15 +595,23 @@ void draw(ComponentDrawContext context, Collection<Component> hidden) {
// the components and wires, so to avoid deadlock, only the AWT should
// create the new bundle map.

private class BundleMapGetter implements Runnable {
BundleMap result;
public void run() {
result = getBundleMap();
}
}

/*synchronized*/ private BundleMap getBundleMap() {
BundleMap ret = masterBundleMap; // volatile read by AWT or simulation thread
if (ret != null)
return ret;
if (SwingUtilities.isEventDispatchThread()) {
// AWT event thread.
if (masterBundleMap != null)
return masterBundleMap;
BundleMap ret = new BundleMap();
ret = new BundleMap();
try {
computeBundleMap(ret);
masterBundleMap = ret;
masterBundleMap = ret; // volatile write by AWT thread
} catch (Exception t) {
ret.invalidate();
System.err.println(t.getLocalizedMessage());
Expand All @@ -612,13 +620,12 @@ void draw(ComponentDrawContext context, Collection<Component> hidden) {
} else {
// Simulation thread.
try {
final BundleMap ret[] = new BundleMap[1];
SwingUtilities.invokeAndWait(new Runnable() {
public void run() { ret[0] = getBundleMap(); }
});
return ret[0];
} catch (Exception e) {
BundleMap ret = new BundleMap();
BundleMapGetter awtThread = new BundleMapGetter();
SwingUtilities.invokeAndWait(awtThread);
return awtThread.result;
} catch (Exception t) {
System.err.println(t.getLocalizedMessage());
ret = new BundleMap();
ret.invalidate();
return ret;
}
Expand Down Expand Up @@ -725,7 +732,7 @@ WireSet getWireSet(Wire start) {
// query methods
//
boolean isMapVoided() {
return masterBundleMap == null;
return masterBundleMap == null; // volatile read by simulation thread
}

//
Expand Down Expand Up @@ -903,6 +910,6 @@ private void voidBundleMap() {
// This should really only be called by AWT thread, but main() also
// calls it during startup. It should not be called by the simulation
// thread.
masterBundleMap = null;
masterBundleMap = null; // volatile write by AWT thread (and sometimes main/startup)
}
}
28 changes: 28 additions & 0 deletions src/com/cburch/logisim/circuit/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,42 @@ public void run() {
private ArrayList<Listener> listeners = new ArrayList<>();
private Object lock = new Object();

// private class Dummy extends UniquelyNamedThread {
// Dummy() { super("dummy"); }
// public void run() {
// try {
// while(true) {
// Thread.sleep(6000);
// long t = System.currentTimeMillis();
// long e = t + 6000;
// long sum = 0;
// while (t < e) {
// t = System.currentTimeMillis();
// sum += (t % 100);
// }
// System.out.printf("t=%d sum=%d\n", t, sum);
// }
// } catch (Exception e) { }
// }
// }

public Simulator() {
simThread = new SimThread(this);
// UniquelyNamedThread dummy1= new Dummy();
// UniquelyNamedThread dummy2 = new Dummy();
// UniquelyNamedThread dummy3 = new Dummy();

try {
simThread.setPriority(simThread.getPriority() - 1);
// dummy1.setPriority(dummy1.getPriority() - 1);
// dummy2.setPriority(dummy2.getPriority() - 1);
// dummy3.setPriority(dummy3.getPriority() - 1);
} catch (IllegalArgumentException | SecurityException e) { }

simThread.start();
// dummy1.start();
// dummy2.start();
// dummy3.start();

setTickFrequency(AppPreferences.TICK_FREQUENCY.get().doubleValue());
}
Expand Down

0 comments on commit aae0c4e

Please sign in to comment.