1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/*
File: WaitFreeQueue.java
Originally written by Doug Lea and released into the public domain.
This may be used for any purposes whatsoever without acknowledgment.
Thanks for the assistance and support of Sun Microsystems Labs,
and everyone contributing, testing, and using this code.
History:
Date Who What
16Jun1998 dl Create public version
5Aug1998 dl replaced int counters with longs
17nov2001 dl Simplify given Bill Pugh's observation
that counted pointers are unnecessary.
*/
package EDU.oswego.cs.dl.util.concurrent;
/**
* A wait-free linked list based queue implementation.
* <p>
*
* While this class conforms to the full Channel interface, only the
* <code>put</code> and <code>poll</code> methods are useful in most
* applications. Because the queue does not support blocking
* operations, <code>take</code> relies on spin-loops, which can be
* extremely wasteful. <p>
*
* This class is adapted from the algorithm described in <a
* href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple,
* Fast, and Practical Non-Blocking and Blocking Concurrent Queue
* Algorithms</a> by Maged M. Michael and Michael L. Scott. This
* implementation is not strictly wait-free since it relies on locking
* for basic atomicity and visibility requirements. Locks can impose
* unbounded waits, although this should not be a major practical
* concern here since each lock is held for the duration of only a few
* statements. (However, the overhead of using so many locks can make
* it less attractive than other Channel implementations on JVMs where
* locking operations are very slow.) <p>
*
* @see BoundedLinkedQueue
* @see LinkedQueue
*
* <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
**/
public class WaitFreeQueue implements Channel {
/*
This is a straightforward adaptation of Michael & Scott
algorithm, with CAS's simulated via per-field locks,
and without version numbers for pointers since, under
Java Garbage Collection, you can never see the "wrong"
node with the same address as the one you think you have.
*/
/** List nodes for Queue **/
protected final static class Node {
protected final Object value;
protected volatile Node next;
/** Make a new node with indicated item, and null link **/
protected Node(Object x) { value = x; }
/** Simulate a CAS operation for 'next' field **/
protected synchronized boolean CASNext(Node oldNext, Node newNext) {
if (next == oldNext) {
next = newNext;
return true;
}
else
return false;
}
}
/** Head of list is always a dummy node **/
protected volatile Node head = new Node(null);
/** Pointer to last node on list **/
protected volatile Node tail = head;
/** Lock for simulating CAS for tail field **/
protected final Object tailLock = new Object();
/** Simulate CAS for head field, using 'this' lock **/
protected synchronized boolean CASHead(Node oldHead, Node newHead) {
if (head == oldHead) {
head = newHead;
return true;
}
else
return false;
}
/** Simulate CAS for tail field **/
protected boolean CASTail(Node oldTail, Node newTail) {
synchronized(tailLock) {
if (tail == oldTail) {
tail = newTail;
return true;
}
else
return false;
}
}
public void put(Object x) throws InterruptedException {
if (x == null) throw new IllegalArgumentException();
if (Thread.interrupted()) throw new InterruptedException();
Node n = new Node(x);
for(;;) {
Node t = tail;
// Try to link new node to end of list.
if (t.CASNext(null, n)) {
// Must now change tail field.
// This CAS might fail, but if so, it will be fixed by others.
CASTail(t, n);
return;
}
// If cannot link, help out a previous failed attempt to move tail
CASTail(t, t.next);
}
}
public boolean offer(Object x, long msecs) throws InterruptedException {
put(x);
return true;
}
/** Main dequeue algorithm, called by poll, take. **/
protected Object extract() throws InterruptedException {
for (;;) {
Node h = head;
Node first = h.next;
if (first == null)
return null;
Object result = first.value;
if (CASHead(h, first))
return result;
}
}
public Object peek() {
Node first = head.next;
if (first == null)
return null;
// Note: This synch unnecessary after JSR-133.
// It exists only to guarantee visibility of returned object,
// No other synch is needed, but "old" memory model requires one.
synchronized(this) {
return first.value;
}
}
/**
* Spin until poll returns a non-null value.
* You probably don't want to call this method.
* A Thread.sleep(0) is performed on each iteration
* as a heuristic to reduce contention. If you would
* rather use, for example, an exponential backoff,
* you could manually set this up using poll.
**/
public Object take() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
for(;;) {
Object x = extract();
if (x != null)
return x;
else
Thread.sleep(0);
}
}
/**
* Spin until poll returns a non-null value or time elapses.
* if msecs is positive, a Thread.sleep(0) is performed on each iteration
* as a heuristic to reduce contention.
**/
public Object poll(long msecs) throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
if (msecs <= 0)
return extract();
long startTime = System.currentTimeMillis();
for(;;) {
Object x = extract();
if (x != null)
return x;
else if (System.currentTimeMillis() - startTime >= msecs)
return null;
else
Thread.sleep(0);
}
}
}
|