001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: /*
020: * Written by Doug Lea with assistance from members of JCP JSR-166
021: * Expert Group and released to the public domain, as explained at
022: * http://creativecommons.org/licenses/publicdomain
023: */
024: package org.apache.openjpa.lib.util.concurrent;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.List;
029:
030: /**
031: * Simple linked list queue used in FIFOSemaphore.
032: * Methods are not synchronized; they depend on synch of callers.
033: * Must be public, since it is used by Semaphore(outside this package).
034: * NOTE: this class is NOT present in java.util.concurrent.
035: */
036: class FIFOWaitQueue extends WaitQueue implements java.io.Serializable {
037:
038: protected transient WaitNode head_ = null;
039: protected transient WaitNode tail_ = null;
040:
041: public FIFOWaitQueue() {
042: }
043:
044: public void insert(WaitNode w) {
045: if (tail_ == null)
046: head_ = tail_ = w;
047: else {
048: tail_.next = w;
049: tail_ = w;
050: }
051: }
052:
053: public WaitNode extract() {
054: if (head_ == null)
055: return null;
056: else {
057: WaitNode w = head_;
058: head_ = w.next;
059: if (head_ == null)
060: tail_ = null;
061: w.next = null;
062: return w;
063: }
064: }
065:
066: public boolean hasNodes() {
067: return head_ != null;
068: }
069:
070: public int getLength() {
071: int count = 0;
072: WaitNode node = head_;
073: while (node != null) {
074: if (node.waiting)
075: count++;
076: node = node.next;
077: }
078: return count;
079: }
080:
081: public Collection getWaitingThreads() {
082: List list = new ArrayList();
083: WaitNode node = head_;
084: while (node != null) {
085: if (node.waiting)
086: list.add(node.owner);
087: node = node.next;
088: }
089: return list;
090: }
091:
092: public boolean isWaiting(Thread thread) {
093: if (thread == null)
094: throw new NullPointerException();
095: for (WaitNode node = head_; node != null; node = node.next) {
096: if (node.waiting && node.owner == thread)
097: return true;
098: }
099: return false;
100: }
101: }
|