minor optimization: avoids multiple checks and only check simulation thread when assertions are enabled

This commit is contained in:
nifi 2010-10-04 10:11:55 +00:00
parent e4e892bc49
commit 5172027bae
3 changed files with 17 additions and 18 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja;
@ -51,11 +51,11 @@ public class EventQueue {
} }
private void addEvent(TimeEvent event) { private void addEvent(TimeEvent event) {
if (event.removed && event.queue != null) { if (event.queue != null) {
if (!event.isScheduled) {
removeFromQueue(event); removeFromQueue(event);
} }
if (event.queue != null) { throw new IllegalStateException("Event is already scheduled: " + event);
throw new IllegalStateException("Event was already scheduled in the past: " + event);
} }
if (first == null) { if (first == null) {
@ -78,8 +78,8 @@ public class EventQueue {
lastPos.nextEvent = event; lastPos.nextEvent = event;
} }
} }
event.removed = false;
event.queue = this; event.queue = this;
event.isScheduled = true;
eventCount++; eventCount++;
} }
@ -112,6 +112,7 @@ public class EventQueue {
pos.nextEvent = null; pos.nextEvent = null;
event.queue = null; event.queue = null;
event.isScheduled = false;
eventCount--; eventCount--;
return true; return true;
} }
@ -142,10 +143,11 @@ public class EventQueue {
tmp.queue = null; tmp.queue = null;
eventCount--; eventCount--;
if (tmp.removed) { if (!tmp.isScheduled) {
/* pop and return another event instead */ /* pop and return another event instead */
return popFirst(); return popFirst();
} }
tmp.isScheduled = false;
return tmp; return tmp;
} }

View file

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 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; package se.sics.cooja;
@ -180,12 +180,9 @@ public class Simulation extends Observable implements Runnable {
* @param time Execution time * @param time Execution time
*/ */
public void scheduleEvent(final TimeEvent e, final long time) { public void scheduleEvent(final TimeEvent e, final long time) {
if (isRunning) {
/* TODO Strict scheduling from simulation thread */ /* TODO Strict scheduling from simulation thread */
if (e.isScheduled()) { assert isSimulationThread() : "Scheduling event from non-simulation thread: " + e;
throw new IllegalStateException("Event already scheduled: " + e);
}
if (isRunning && !isSimulationThread()) {
throw new IllegalStateException("Scheduling event from non-simulation thread: " + e);
} }
eventQueue.addEvent(e, time); eventQueue.addEvent(e, time);
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * 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; package se.sics.cooja;
@ -43,7 +43,7 @@ public abstract class TimeEvent {
protected long time; protected long time;
boolean removed = false; boolean isScheduled = false;
public TimeEvent(long time) { public TimeEvent(long time) {
this(time, null); this(time, null);
@ -59,11 +59,11 @@ public abstract class TimeEvent {
} }
public boolean isScheduled() { public boolean isScheduled() {
return queue != null && !removed; return isScheduled;
} }
public boolean remove() { public boolean remove() {
removed = true; isScheduled = false;
return false; return false;
} }