ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/LinkedTransferQueue.java
(Generate patch)

Comparing jsr166/src/jsr166y/LinkedTransferQueue.java (file contents):
Revision 1.16 by jsr166, Mon Mar 30 04:32:23 2009 UTC vs.
Revision 1.24 by jsr166, Thu Jul 23 23:23:41 2009 UTC

# Line 44 | Line 44 | import java.lang.reflect.*;
44   * @since 1.7
45   * @author Doug Lea
46   * @param <E> the type of elements held in this collection
47 *
47   */
48   public class LinkedTransferQueue<E> extends AbstractQueue<E>
49      implements TransferQueue<E>, java.io.Serializable {
# Line 81 | Line 80 | public class LinkedTransferQueue<E> exte
80       * seems not to vary with number of CPUs (beyond 2) so is just
81       * a constant.
82       */
83 <    static final int maxTimedSpins = (NCPUS < 2)? 0 : 32;
83 >    static final int maxTimedSpins = (NCPUS < 2) ? 0 : 32;
84  
85      /**
86       * The number of times to spin before blocking in untimed waits.
# Line 124 | Line 123 | public class LinkedTransferQueue<E> exte
123              nextUpdater.lazySet(this, this);
124          }
125  
126 +        private static final long serialVersionUID = -3375979862319811754L;
127      }
128  
129      /**
# Line 135 | Line 135 | public class LinkedTransferQueue<E> exte
135          // enough padding for 64bytes with 4byte refs
136          Object p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pa, pb, pc, pd, pe;
137          PaddedAtomicReference(T r) { super(r); }
138 +        private static final long serialVersionUID = 8170090609809740854L;
139      }
140  
141  
142      /** head of the queue */
143      private transient final PaddedAtomicReference<QNode> head;
144 +
145      /** tail of the queue */
146      private transient final PaddedAtomicReference<QNode> tail;
147  
# Line 166 | Line 168 | public class LinkedTransferQueue<E> exte
168       * Puts or takes an item. Used for most queue operations (except
169       * poll() and tryTransfer()). See the similar code in
170       * SynchronousQueue for detailed explanation.
171 +     *
172       * @param e the item or if null, signifies that this is a take
173       * @param mode the wait mode: NOWAIT, TIMEOUT, WAIT
174       * @param nanos timeout in nanosecs, used only if mode is TIMEOUT
# Line 202 | Line 205 | public class LinkedTransferQueue<E> exte
205                      Object x = first.get();
206                      if (x != first && first.compareAndSet(x, e)) {
207                          LockSupport.unpark(first.waiter);
208 <                        return isData? e : x;
208 >                        return isData ? e : x;
209                      }
210                  }
211              }
# Line 212 | Line 215 | public class LinkedTransferQueue<E> exte
215  
216      /**
217       * Version of xfer for poll() and tryTransfer, which
218 <     * simplifies control paths both here and in xfer
218 >     * simplifies control paths both here and in xfer.
219       */
220      private Object fulfill(Object e) {
221          boolean isData = (e != null);
# Line 240 | Line 243 | public class LinkedTransferQueue<E> exte
243                      Object x = first.get();
244                      if (x != first && first.compareAndSet(x, e)) {
245                          LockSupport.unpark(first.waiter);
246 <                        return isData? e : x;
246 >                        return isData ? e : x;
247                      }
248                  }
249              }
# Line 263 | Line 266 | public class LinkedTransferQueue<E> exte
266          if (mode == NOWAIT)
267              return null;
268  
269 <        long lastTime = (mode == TIMEOUT)? System.nanoTime() : 0;
269 >        long lastTime = (mode == TIMEOUT) ? System.nanoTime() : 0;
270          Thread w = Thread.currentThread();
271          int spins = -1; // set to desired spin count below
272          for (;;) {
# Line 272 | Line 275 | public class LinkedTransferQueue<E> exte
275              Object x = s.get();
276              if (x != e) {                 // Node was matched or cancelled
277                  advanceHead(pred, s);     // unlink if head
278 <                if (x == s) {              // was cancelled
278 >                if (x == s) {             // was cancelled
279                      clean(pred, s);
280                      return null;
281                  }
# Line 295 | Line 298 | public class LinkedTransferQueue<E> exte
298              if (spins < 0) {
299                  QNode h = head.get(); // only spin if at head
300                  spins = ((h != null && h.next == s) ?
301 <                         (mode == TIMEOUT?
301 >                         ((mode == TIMEOUT) ?
302                            maxTimedSpins : maxUntimedSpins) : 0);
303              }
304              if (spins > 0)
# Line 316 | Line 319 | public class LinkedTransferQueue<E> exte
319      }
320  
321      /**
322 <     * Returns validated tail for use in cleaning methods
322 >     * Returns validated tail for use in cleaning methods.
323       */
324      private QNode getValidatedTail() {
325          for (;;) {
# Line 339 | Line 342 | public class LinkedTransferQueue<E> exte
342  
343      /**
344       * Gets rid of cancelled node s with original predecessor pred.
345 +     *
346       * @param pred predecessor of cancelled node
347       * @param s the cancelled node
348       */
# Line 378 | Line 382 | public class LinkedTransferQueue<E> exte
382      /**
383       * Tries to unsplice the cancelled node held in cleanMe that was
384       * previously uncleanable because it was at tail.
385 +     *
386       * @return current cleanMe node (or null)
387       */
388      private QNode reclean() {
# Line 422 | Line 427 | public class LinkedTransferQueue<E> exte
427       * Creates a {@code LinkedTransferQueue}
428       * initially containing the elements of the given collection,
429       * added in traversal order of the collection's iterator.
430 +     *
431       * @param c the collection of elements to initially contain
432       * @throws NullPointerException if the specified collection or any
433       *         of its elements are null
# Line 483 | Line 489 | public class LinkedTransferQueue<E> exte
489      public E take() throws InterruptedException {
490          Object e = xfer(null, WAIT, 0);
491          if (e != null)
492 <            return (E)e;
492 >            return (E) e;
493          Thread.interrupted();
494          throw new InterruptedException();
495      }
# Line 491 | Line 497 | public class LinkedTransferQueue<E> exte
497      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
498          Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
499          if (e != null || !Thread.interrupted())
500 <            return (E)e;
500 >            return (E) e;
501          throw new InterruptedException();
502      }
503  
504      public E poll() {
505 <        return (E)fulfill(null);
505 >        return (E) fulfill(null);
506      }
507  
508      public int drainTo(Collection<? super E> c) {
# Line 530 | Line 536 | public class LinkedTransferQueue<E> exte
536      // Traversal-based methods
537  
538      /**
539 <     * Return head after performing any outstanding helping steps
539 >     * Returns head after performing any outstanding helping steps.
540       */
541      private QNode traversalHead() {
542          for (;;) {
# Line 575 | Line 581 | public class LinkedTransferQueue<E> exte
581          QNode snext;       // successor of next
582          QNode curr;        // last returned node, for remove()
583          QNode pcurr;       // predecessor of curr, for remove()
584 <        E nextItem;        // Cache of next item, once commited to in next
584 >        E nextItem;        // Cache of next item, once committed to in next
585  
586          Itr() {
587              findNext();
588          }
589  
590          /**
591 <         * Ensure next points to next valid node, or null if none.
591 >         * Ensures next points to next valid node, or null if none.
592           */
593          void findNext() {
594              for (;;) {
# Line 599 | Line 605 | public class LinkedTransferQueue<E> exte
605                  Object x = q.get();
606                  QNode s = q.next;
607                  if (x != null && q != x && q != s) {
608 <                    nextItem = (E)x;
608 >                    nextItem = (E) x;
609                      snext = s;
610                      pnext = pred;
611                      next = q;
# Line 646 | Line 652 | public class LinkedTransferQueue<E> exte
652                  if (!p.isData)
653                      return null;
654                  if (x != null)
655 <                    return (E)x;
655 >                    return (E) x;
656              }
657          }
658      }
# Line 732 | Line 738 | public class LinkedTransferQueue<E> exte
738                  if (q == pred) // restart
739                      break;
740                  Object x = q.get();
741 <                if (x != null && x != q && o.equals(x) &&
741 >                if (x != null && x != q && o.equals(x) &&
742                      q.compareAndSet(x, q)) {
743                      clean(pred, q);
744                      return true;
# Line 761 | Line 767 | public class LinkedTransferQueue<E> exte
767      /**
768       * Reconstitute the Queue instance from a stream (that is,
769       * deserialize it).
770 +     *
771       * @param s the stream
772       */
773      private void readObject(java.io.ObjectInputStream s)
# Line 768 | Line 775 | public class LinkedTransferQueue<E> exte
775          s.defaultReadObject();
776          resetHeadAndTail();
777          for (;;) {
778 <            E item = (E)s.readObject();
778 >            E item = (E) s.readObject();
779              if (item == null)
780                  break;
781              else
# Line 780 | Line 787 | public class LinkedTransferQueue<E> exte
787      // Support for resetting head/tail while deserializing
788      private void resetHeadAndTail() {
789          QNode dummy = new QNode(null, false);
790 <        _unsafe.putObjectVolatile(this, headOffset,
790 >        UNSAFE.putObjectVolatile(this, headOffset,
791                                    new PaddedAtomicReference<QNode>(dummy));
792 <        _unsafe.putObjectVolatile(this, tailOffset,
792 >        UNSAFE.putObjectVolatile(this, tailOffset,
793                                    new PaddedAtomicReference<QNode>(dummy));
794 <        _unsafe.putObjectVolatile(this, cleanMeOffset,
794 >        UNSAFE.putObjectVolatile(this, cleanMeOffset,
795                                    new PaddedAtomicReference<QNode>(null));
796      }
797  
# Line 814 | Line 821 | public class LinkedTransferQueue<E> exte
821  
822      private static long fieldOffset(String fieldName)
823              throws NoSuchFieldException {
824 <        return _unsafe.objectFieldOffset
824 >        return UNSAFE.objectFieldOffset
825              (LinkedTransferQueue.class.getDeclaredField(fieldName));
826      }
827  
828 <    private static final Unsafe _unsafe;
828 >    private static final Unsafe UNSAFE;
829      private static final long headOffset;
830      private static final long tailOffset;
831      private static final long cleanMeOffset;
832      static {
833          try {
834 <            _unsafe = getUnsafe();
834 >            UNSAFE = getUnsafe();
835              headOffset = fieldOffset("head");
836              tailOffset = fieldOffset("tail");
837              cleanMeOffset = fieldOffset("cleanMe");

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines