001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.thread;
028:
029: import java.util.List;
030:
031: import org.cougaar.util.log.Logger;
032: import org.cougaar.util.log.Logging;
033:
034: /**
035: * This is the standard implementation of {@link RightsSelector}. It
036: * uses a round-robin approach to offer rights equally among its
037: * own {@link Scheduler}'s thread and those of its children.
038: */
039: class RoundRobinSelector implements RightsSelector {
040: private static final Logger logger = Logging
041: .getLogger(RoundRobinSelector.class);
042: // Holds the next index of the round-robin selection. A value of
043: // -1 refers to the local queue, rather than any of the children.
044: private int currentIndex = -1;
045: protected PropagatingScheduler scheduler;
046:
047: public void setScheduler(PropagatingScheduler scheduler) {
048: this .scheduler = scheduler;
049: }
050:
051: private SchedulableObject checkNextPending(List<TreeNode> children) {
052: // Conceptually this should be synchronized on 'children'.
053: // Unfortunately the nature of what it's doing makes that
054: // impossible. The result is that this code will in some
055: // circumstances run while an 'add' call is in progress on the
056: // list. This will show up as a null in the list. Just
057: // ignore it and hope for the best...In theory all it means
058: // is that the newly added child will miss its first turn.
059: SchedulableObject handoff = null;
060: int child_count = children.size();
061: if (currentIndex == -1) {
062: handoff = scheduler.popQueue();
063: currentIndex = child_count == 0 ? -1 : 0;
064: } else {
065: TreeNode child_node = children.get(currentIndex++);
066: if (currentIndex == child_count) {
067: currentIndex = -1;
068: }
069: if (child_node == null) {
070: logger.warn(scheduler + "has a null child");
071: return null;
072: }
073:
074: Scheduler child = child_node.getScheduler(scheduler
075: .getLane());
076: if (!scheduler.allowRightFor(child)) {
077: return null;
078: }
079:
080: handoff = child.getNextPending();
081: // We're the parent of the Scheduler to which the handoff
082: // is given. Increase the local count.
083: if (handoff != null) {
084: scheduler.incrementRunCount(child);
085: }
086: }
087: return handoff;
088: }
089:
090: public SchedulableObject getNextPending() {
091: int initialIndex = currentIndex;
092: List<TreeNode> children = scheduler.getTreeNode().getChildren();
093: SchedulableObject handoff = null;
094: do {
095: handoff = checkNextPending(children);
096: if (handoff != null) {
097: return handoff;
098: }
099: } while (currentIndex != initialIndex);
100: return null;
101: }
102: }
|