From 5172027bae994cf5d6e9afa8620fd73bd0fdb389 Mon Sep 17 00:00:00 2001 From: nifi Date: Mon, 4 Oct 2010 10:11:55 +0000 Subject: [PATCH] minor optimization: avoids multiple checks and only check simulation thread when assertions are enabled --- tools/cooja/java/se/sics/cooja/EventQueue.java | 16 +++++++++------- tools/cooja/java/se/sics/cooja/Simulation.java | 11 ++++------- tools/cooja/java/se/sics/cooja/TimeEvent.java | 8 ++++---- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/tools/cooja/java/se/sics/cooja/EventQueue.java b/tools/cooja/java/se/sics/cooja/EventQueue.java index a7a725d05..24eb4b719 100644 --- a/tools/cooja/java/se/sics/cooja/EventQueue.java +++ b/tools/cooja/java/se/sics/cooja/EventQueue.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: EventQueue.java,v 1.9 2009/10/27 10:06:59 fros4943 Exp $ + * $Id: EventQueue.java,v 1.10 2010/10/04 10:11:55 nifi Exp $ */ package se.sics.cooja; @@ -51,11 +51,11 @@ public class EventQueue { } private void addEvent(TimeEvent event) { - if (event.removed && event.queue != null) { - removeFromQueue(event); - } if (event.queue != null) { - throw new IllegalStateException("Event was already scheduled in the past: " + event); + if (!event.isScheduled) { + removeFromQueue(event); + } + throw new IllegalStateException("Event is already scheduled: " + event); } if (first == null) { @@ -78,8 +78,8 @@ public class EventQueue { lastPos.nextEvent = event; } } - event.removed = false; event.queue = this; + event.isScheduled = true; eventCount++; } @@ -112,6 +112,7 @@ public class EventQueue { pos.nextEvent = null; event.queue = null; + event.isScheduled = false; eventCount--; return true; } @@ -142,10 +143,11 @@ public class EventQueue { tmp.queue = null; eventCount--; - if (tmp.removed) { + if (!tmp.isScheduled) { /* pop and return another event instead */ return popFirst(); } + tmp.isScheduled = false; return tmp; } diff --git a/tools/cooja/java/se/sics/cooja/Simulation.java b/tools/cooja/java/se/sics/cooja/Simulation.java index 0b2305fe8..0f52fe781 100644 --- a/tools/cooja/java/se/sics/cooja/Simulation.java +++ b/tools/cooja/java/se/sics/cooja/Simulation.java @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * - * $Id: Simulation.java,v 1.65 2010/08/13 09:53:33 fros4943 Exp $ + * $Id: Simulation.java,v 1.66 2010/10/04 10:11:56 nifi Exp $ */ package se.sics.cooja; @@ -180,12 +180,9 @@ public class Simulation extends Observable implements Runnable { * @param time Execution time */ public void scheduleEvent(final TimeEvent e, final long time) { - /* TODO Strict scheduling from simulation thread */ - if (e.isScheduled()) { - throw new IllegalStateException("Event already scheduled: " + e); - } - if (isRunning && !isSimulationThread()) { - throw new IllegalStateException("Scheduling event from non-simulation thread: " + e); + if (isRunning) { + /* TODO Strict scheduling from simulation thread */ + assert isSimulationThread() : "Scheduling event from non-simulation thread: " + e; } eventQueue.addEvent(e, time); } diff --git a/tools/cooja/java/se/sics/cooja/TimeEvent.java b/tools/cooja/java/se/sics/cooja/TimeEvent.java index c96a69bbb..24388c580 100644 --- a/tools/cooja/java/se/sics/cooja/TimeEvent.java +++ b/tools/cooja/java/se/sics/cooja/TimeEvent.java @@ -26,7 +26,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: TimeEvent.java,v 1.4 2009/03/03 13:42:45 fros4943 Exp $ + * $Id: TimeEvent.java,v 1.5 2010/10/04 10:11:55 nifi Exp $ */ package se.sics.cooja; @@ -43,7 +43,7 @@ public abstract class TimeEvent { protected long time; - boolean removed = false; + boolean isScheduled = false; public TimeEvent(long time) { this(time, null); @@ -59,11 +59,11 @@ public abstract class TimeEvent { } public boolean isScheduled() { - return queue != null && !removed; + return isScheduled; } public boolean remove() { - removed = true; + isScheduled = false; return false; }