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 ConcurrentQueueBlockingImpl<V> implements
042: ConcurrentQueue<V> {
043: // This implementation of ConcurrentQueue uses a single lock, which must be
044: // acquired to update the list. Every operation on this class updates the
045: // structure, so read/write locking is probably not useful.
046: //
047: // Trying to build a lock-free implementation runs into the usual problems:
048: // we need to atomically update more than one location at a time in the structure.
049: // Short of a transactional memory implementation, we would either need a complicated
050: // implementation implementing recursive fixup, or something like the Ladan-Mozes and
051: // Shavit algorithm (see "An Optimistic Approach to Lock-Free FIFO Queues"
052: // at http://people.csail.mit.edu/edya/publications/publicationsAndPatents.htm)
053: // that delays fixing up one direction in a double linked list. However, that
054: // algorithm does not consider general deletion, and I don't know whether that
055: // capability can be easily added or not.
056: // Any of these approaches are quite complicated, and so we won't go there yet.
057: // As always, first make it work, then make it fast(er), but only if necessary.
058: //
059: // Structure: Head points to a node containing a null value, which is a special marker.
060: // head.next is the first element, head.prev is the last. The queue is empty if
061: // head.next == head.prev == head.
062: final Entry<V> head = new Entry<V>(null);
063: final Object lock = new Object();
064: int count = 0;
065:
066: private final class Entry<V> {
067: Entry<V> next = null;
068: Entry<V> prev = null;
069: private HandleImpl<V> handle;
070:
071: Entry(V value) {
072: handle = new HandleImpl<V>(this , value);
073: }
074:
075: HandleImpl<V> handle() {
076: return handle;
077: }
078: }
079:
080: private final class HandleImpl<V> implements Handle<V> {
081: private Entry<V> entry;
082: private final V value;
083: private boolean valid;
084:
085: HandleImpl(Entry<V> entry, V value) {
086: this .entry = entry;
087: this .value = value;
088: this .valid = true;
089: }
090:
091: Entry<V> entry() {
092: return entry;
093: }
094:
095: public V value() {
096: return value;
097: }
098:
099: /** Delete the element corresponding to this handle
100: * from the queue. Takes constant time.
101: */
102: public boolean remove() {
103: synchronized (lock) {
104: if (!valid) {
105: return false;
106: }
107:
108: valid = false;
109:
110: entry.next.prev = entry.prev;
111: entry.prev.next = entry.next;
112: count--;
113: }
114:
115: entry.prev = null;
116: entry.next = null;
117: entry.handle = null;
118: entry = null;
119: valid = false;
120: return true;
121: }
122: }
123:
124: public int size() {
125: synchronized (lock) {
126: return count;
127: }
128: }
129:
130: /** Add a new element to the tail of the queue.
131: * Returns a handle for the element in the queue.
132: */
133: public Handle<V> offer(V arg) {
134: if (arg == null)
135: throw new IllegalArgumentException(
136: "Argument cannot be null");
137:
138: Entry<V> entry = new Entry<V>(arg);
139:
140: synchronized (lock) {
141: entry.next = head;
142: entry.prev = head.prev;
143: head.prev.next = entry;
144: head.prev = entry;
145: count++;
146: }
147:
148: return entry.handle();
149: }
150:
151: /** Return an element from the head of the queue.
152: * The element is removed from the queue.
153: */
154: public V poll() {
155: Entry<V> first = null;
156:
157: synchronized (lock) {
158: first = head.next;
159: if (first == head)
160: return null;
161: else {
162: // assert that the following expression returns true!
163: first.handle().remove();
164: }
165: }
166:
167: // Once first is removed from the queue, it is invisible to other threads,
168: // so we don't need to synchronize here.
169: first.next = null;
170: first.prev = null;
171: V value = first.handle().value();
172: return value;
173: }
174: }
|