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.28 by jsr166, Sun Jul 26 17:33:37 2009 UTC vs.
Revision 1.32 by jsr166, Wed Jul 29 02:19:56 2009 UTC

# Line 14 | Line 14 | import java.util.Iterator;
14   import java.util.NoSuchElementException;
15   import java.util.concurrent.locks.LockSupport;
16   import java.util.concurrent.atomic.AtomicReference;
17 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
17  
18   /**
19   * An unbounded {@linkplain TransferQueue} based on linked nodes.
# Line 115 | Line 114 | public class LinkedTransferQueue<E> exte
114              this.isData = isData;
115          }
116  
117 <        @SuppressWarnings("rawtypes")
118 <        static final AtomicReferenceFieldUpdater<Node, Node>
119 <            nextUpdater = AtomicReferenceFieldUpdater.newUpdater
120 <            (Node.class, Node.class, "next");
117 >        // Unsafe mechanics
118 >
119 >        private static final sun.misc.Unsafe UNSAFE = getUnsafe();
120 >        private static final long nextOffset =
121 >            objectFieldOffset(UNSAFE, "next", Node.class);
122  
123          final boolean casNext(Node<E> cmp, Node<E> val) {
124 <            return nextUpdater.compareAndSet(this, cmp, val);
124 >            return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
125          }
126  
127          final void clearNext() {
128 <            nextUpdater.lazySet(this, this);
128 >            UNSAFE.putOrderedObject(this, nextOffset, this);
129 >        }
130 >
131 >        /**
132 >         * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
133 >         * Replace with a simple call to Unsafe.getUnsafe when integrating
134 >         * into a jdk.
135 >         *
136 >         * @return a sun.misc.Unsafe
137 >         */
138 >        private static sun.misc.Unsafe getUnsafe() {
139 >            try {
140 >                return sun.misc.Unsafe.getUnsafe();
141 >            } catch (SecurityException se) {
142 >                try {
143 >                    return java.security.AccessController.doPrivileged
144 >                        (new java.security
145 >                         .PrivilegedExceptionAction<sun.misc.Unsafe>() {
146 >                            public sun.misc.Unsafe run() throws Exception {
147 >                                java.lang.reflect.Field f = sun.misc
148 >                                    .Unsafe.class.getDeclaredField("theUnsafe");
149 >                                f.setAccessible(true);
150 >                                return (sun.misc.Unsafe) f.get(null);
151 >                            }});
152 >                } catch (java.security.PrivilegedActionException e) {
153 >                    throw new RuntimeException("Could not initialize intrinsics",
154 >                                               e.getCause());
155 >                }
156 >            }
157          }
158  
159          private static final long serialVersionUID = -3375979862319811754L;
# Line 442 | Line 470 | public class LinkedTransferQueue<E> exte
470          addAll(c);
471      }
472  
473 +    /**
474 +     * @throws InterruptedException {@inheritDoc}
475 +     * @throws NullPointerException {@inheritDoc}
476 +     */
477      public void put(E e) throws InterruptedException {
478          if (e == null) throw new NullPointerException();
479          if (Thread.interrupted()) throw new InterruptedException();
480          xfer(e, NOWAIT, 0);
481      }
482  
483 +    /**
484 +     * @throws InterruptedException {@inheritDoc}
485 +     * @throws NullPointerException {@inheritDoc}
486 +     */
487      public boolean offer(E e, long timeout, TimeUnit unit)
488          throws InterruptedException {
489          if (e == null) throw new NullPointerException();
# Line 456 | Line 492 | public class LinkedTransferQueue<E> exte
492          return true;
493      }
494  
495 +    /**
496 +     * @throws NullPointerException {@inheritDoc}
497 +     */
498      public boolean offer(E e) {
499          if (e == null) throw new NullPointerException();
500          xfer(e, NOWAIT, 0);
501          return true;
502      }
503  
504 +    /**
505 +     * @throws NullPointerException {@inheritDoc}
506 +     */
507      public boolean add(E e) {
508          if (e == null) throw new NullPointerException();
509          xfer(e, NOWAIT, 0);
510          return true;
511      }
512  
513 +    /**
514 +     * @throws InterruptedException {@inheritDoc}
515 +     * @throws NullPointerException {@inheritDoc}
516 +     */
517      public void transfer(E e) throws InterruptedException {
518          if (e == null) throw new NullPointerException();
519          if (xfer(e, WAIT, 0) == null) {
# Line 476 | Line 522 | public class LinkedTransferQueue<E> exte
522          }
523      }
524  
525 +    /**
526 +     * @throws InterruptedException {@inheritDoc}
527 +     * @throws NullPointerException {@inheritDoc}
528 +     */
529      public boolean tryTransfer(E e, long timeout, TimeUnit unit)
530          throws InterruptedException {
531          if (e == null) throw new NullPointerException();
# Line 486 | Line 536 | public class LinkedTransferQueue<E> exte
536          throw new InterruptedException();
537      }
538  
539 +    /**
540 +     * @throws NullPointerException {@inheritDoc}
541 +     */
542      public boolean tryTransfer(E e) {
543          if (e == null) throw new NullPointerException();
544          return fulfill(e) != null;
545      }
546  
547 +    /**
548 +     * @throws InterruptedException {@inheritDoc}
549 +     */
550      public E take() throws InterruptedException {
551          Object e = xfer(null, WAIT, 0);
552          if (e != null)
# Line 499 | Line 555 | public class LinkedTransferQueue<E> exte
555          throw new InterruptedException();
556      }
557  
558 +    /**
559 +     * @throws InterruptedException {@inheritDoc}
560 +     */
561      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
562          Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
563          if (e != null || !Thread.interrupted())
# Line 510 | Line 569 | public class LinkedTransferQueue<E> exte
569          return fulfill(null);
570      }
571  
572 +    /**
573 +     * @throws NullPointerException     {@inheritDoc}
574 +     * @throws IllegalArgumentException {@inheritDoc}
575 +     */
576      public int drainTo(Collection<? super E> c) {
577          if (c == null)
578              throw new NullPointerException();
# Line 524 | Line 587 | public class LinkedTransferQueue<E> exte
587          return n;
588      }
589  
590 +    /**
591 +     * @throws NullPointerException     {@inheritDoc}
592 +     * @throws IllegalArgumentException {@inheritDoc}
593 +     */
594      public int drainTo(Collection<? super E> c, int maxElements) {
595          if (c == null)
596              throw new NullPointerException();
# Line 803 | Line 870 | public class LinkedTransferQueue<E> exte
870  
871      private static final sun.misc.Unsafe UNSAFE = getUnsafe();
872      private static final long headOffset =
873 <        objectFieldOffset("head", LinkedTransferQueue.class);
873 >        objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class);
874      private static final long tailOffset =
875 <        objectFieldOffset("tail", LinkedTransferQueue.class);
875 >        objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class);
876      private static final long cleanMeOffset =
877 <        objectFieldOffset("cleanMe", LinkedTransferQueue.class);
877 >        objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class);
878 >
879  
880 <    private static long objectFieldOffset(String field, Class<?> klazz) {
880 >    static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
881 >                                  String field, Class<?> klazz) {
882          try {
883              return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
884          } catch (NoSuchFieldException e) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines