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
* 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) {
if (event.queue != null) {
if (!event.isScheduled) {
removeFromQueue(event);
}
if (event.queue != null) {
throw new IllegalStateException("Event was already scheduled in the past: " + 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;
}

View file

@ -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) {
if (isRunning) {
/* 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);
assert isSimulationThread() : "Scheduling event from non-simulation thread: " + e;
}
eventQueue.addEvent(e, time);
}

View file

@ -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;
}