01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.core.thread;
28:
29: /**
30: * The standard hiearchical thread service implementation uses this
31: * extension of {@link Scheduler} to handle the propagation of rights.
32: */
33: public class PropagatingScheduler extends Scheduler {
34: private RightsSelector selector;
35:
36: public PropagatingScheduler(ThreadListenerProxy listenerProxy) {
37: super (listenerProxy);
38:
39: // Default selector
40: selector = new RoundRobinSelector();
41: selector.setScheduler(this );
42: }
43:
44: public void setRightsSelector(RightsSelector selector) {
45: this .selector = selector;
46: selector.setScheduler(this );
47: }
48:
49: boolean requestRights(Scheduler requestor) {
50: if (!allowRightFor(requestor)) {
51: return false;
52: }
53: TreeNode parent_node = getTreeNode().getParent();
54: if (parent_node == null) {
55: // This is the root
56: return super .requestRights(requestor);
57: } else {
58: synchronized (this ) {
59: if (!checkLocalRights()) {
60: return false;
61: }
62: ++rightsRequestCount;
63: }
64: Scheduler parent = parent_node.getScheduler(getLane());
65: boolean ok = parent.requestRights(this );
66: // If our parent gave us a right, increase our local count
67: synchronized (this ) {
68: if (ok) {
69: incrementRunCount(this );
70: }
71: --rightsRequestCount;
72: }
73: return ok;
74: }
75: }
76:
77: void releaseRights(Scheduler consumer) {
78: TreeNode parent_node = getTreeNode().getParent();
79: if (parent_node == null) {
80: // This is the root
81: super .releaseRights(consumer);
82: } else {
83: // In this simple scheduler, layers other than root always
84: // give up the right at this point (root may hand it off).
85: decrementRunCount(this );
86: Scheduler parent = parent_node.getScheduler(getLane());
87: parent.releaseRights(this );
88: }
89: }
90:
91: SchedulableObject getNextPending() {
92: return selector.getNextPending();
93: }
94:
95: }
|