001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.tcp.connectioncache.impl.concurrent;
038:
039: import com.sun.xml.ws.transport.tcp.connectioncache.spi.concurrent.ConcurrentQueue;
040:
041: public class ConcurrentQueueImpl<V> implements ConcurrentQueue<V> {
042: // This implementation of ConcurrentQueue is unsynchronized, for use in
043: // other implementations that manage concurrency with locks.
044: //
045: // Structure: Head points to a node containing a null value, which is a special marker.
046: // head.next is the first element, head.prev is the last. The queue is empty if
047: // head.next == head.prev == head.
048: final Entry<V> head = new Entry<V>(null);
049: int count = 0;
050:
051: public ConcurrentQueueImpl() {
052: head.next = head;
053: head.prev = head;
054: }
055:
056: private final class Entry<V> {
057: Entry<V> next = null;
058: Entry<V> prev = null;
059: private HandleImpl<V> handle;
060:
061: Entry(V value) {
062: handle = new HandleImpl<V>(this , value);
063: }
064:
065: HandleImpl<V> handle() {
066: return handle;
067: }
068: }
069:
070: private final class HandleImpl<V> implements Handle<V> {
071: private Entry<V> entry;
072: private final V value;
073: private boolean valid;
074:
075: HandleImpl(Entry<V> entry, V value) {
076: this .entry = entry;
077: this .value = value;
078: this .valid = true;
079: }
080:
081: Entry<V> entry() {
082: return entry;
083: }
084:
085: public V value() {
086: return value;
087: }
088:
089: /** Delete the element corresponding to this handle
090: * from the queue. Takes constant time.
091: */
092: public boolean remove() {
093: if (!valid) {
094: return false;
095: }
096:
097: valid = false;
098:
099: entry.next.prev = entry.prev;
100: entry.prev.next = entry.next;
101: count--;
102:
103: entry.prev = null;
104: entry.next = null;
105: entry.handle = null;
106: entry = null;
107: valid = false;
108: return true;
109: }
110: }
111:
112: public int size() {
113: return count;
114: }
115:
116: /** Add a new element to the tail of the queue.
117: * Returns a handle for the element in the queue.
118: */
119: public Handle<V> offer(V arg) {
120: if (arg == null)
121: throw new IllegalArgumentException(
122: "Argument cannot be null");
123:
124: Entry<V> entry = new Entry<V>(arg);
125:
126: entry.next = head;
127: entry.prev = head.prev;
128: head.prev.next = entry;
129: head.prev = entry;
130: count++;
131:
132: return entry.handle();
133: }
134:
135: /** Return an element from the head of the queue.
136: * The element is removed from the queue.
137: */
138: public V poll() {
139: Entry<V> first = null;
140: V value = null;
141:
142: first = head.next;
143: if (first == head)
144: return null;
145: else {
146: value = first.handle().value();
147:
148: // assert that the following expression returns true!
149: first.handle().remove();
150: }
151:
152: // Once first is removed from the queue, it is invisible to other threads,
153: // so we don't need to synchronize here.
154: first.next = null;
155: first.prev = null;
156: return value;
157: }
158: }
|