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.ArrayList;
030: import java.util.List;
031: import java.util.Timer;
032:
033: import org.cougaar.core.service.ThreadService;
034:
035: /**
036: * This class is used to represent the hierarchy of the standard
037: * layered {@link ThreadService}.
038: */
039: final class TreeNode {
040: private static Timer timer;
041: private static boolean isActive = true;
042:
043: private final List<TreeNode> children;
044: private final String name;
045: private final Scheduler[] schedulers;
046: private final ThreadPool[] pools;
047: private final TreeNode parent;
048: private int defaultLane = ThreadService.BEST_EFFORT_LANE; // parameter?
049:
050: TreeNode(Scheduler[] schedulers, ThreadPool[] pools, String name,
051: ThreadServiceProxy parentService) {
052: this .name = name;
053: children = new ArrayList<TreeNode>();
054: this .schedulers = schedulers;
055: this .pools = pools;
056: for (int i = 0; i < schedulers.length; i++)
057: schedulers[i].setTreeNode(this );
058: TreeNode parent = parentService == null ? null : parentService
059: .getTreeNode();
060: this .parent = parent;
061: if (parent != null)
062: parent.addChild(this );
063: }
064:
065: int getDefaultLane() {
066: return defaultLane;
067: }
068:
069: void setDefaultLane(int lane) {
070: defaultLane = lane;
071: }
072:
073: int getLaneCount() {
074: return schedulers.length;
075: }
076:
077: TreeNode getParent() {
078: return parent;
079: }
080:
081: void addChild(TreeNode child) {
082: synchronized (children) {
083: children.add(child);
084: }
085: }
086:
087: List<TreeNode> getChildren() {
088: return children;
089: }
090:
091: int iterateOverRunningThreads(ThreadStatusService.Body body) {
092: int count = 0;
093: for (int i = 0; i < pools.length; i++) {
094: count += pools[i].iterateOverRunningThreads(body);
095: }
096: return count;
097: }
098:
099: int iterateOverQueuedThreads(ThreadStatusService.Body body) {
100: int count = 0;
101: for (int i = 0; i < schedulers.length; i++)
102: count += schedulers[i].iterateOverQueuedThreads(body);
103: if (children != null) {
104: synchronized (children) {
105: for (int i = 0, n = children.size(); i < n; i++) {
106: TreeNode child = children.get(i);
107: count += child.iterateOverQueuedThreads(body);
108: }
109: }
110: }
111: return count;
112: }
113:
114: Scheduler getScheduler(int lane) {
115: return schedulers[lane];
116: }
117:
118: String getName() {
119: return name;
120: }
121:
122: ThreadPool getPool(int lane) {
123: return pools[lane];
124: }
125:
126: static synchronized Timer timer() {
127: if (timer == null && isActive) {
128: timer = new Timer(true);
129: }
130: return timer;
131: }
132:
133: static void releaseTimer() {
134: isActive = false;
135: if (timer != null) {
136: timer.cancel();
137: timer = null;
138: }
139: }
140:
141: }
|