[cvs] / jsr166 / src / main / java / util / concurrent / LinkedTransferQueue.java Repository:
ViewVC logotype

Diff of /jsr166/src/main/java/util/concurrent/LinkedTransferQueue.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3, Sun Jul 26 17:48:58 2009 UTC revision 1.4, Wed Jul 29 02:35:47 2009 UTC
# Line 12  Line 12 
12  import java.util.NoSuchElementException;  import java.util.NoSuchElementException;
13  import java.util.concurrent.locks.LockSupport;  import java.util.concurrent.locks.LockSupport;
14  import java.util.concurrent.atomic.AtomicReference;  import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;  
15    
16  /**  /**
17   * An unbounded {@linkplain TransferQueue} based on linked nodes.   * An unbounded {@linkplain TransferQueue} based on linked nodes.
# Line 113  Line 112 
112              this.isData = isData;              this.isData = isData;
113          }          }
114    
115          @SuppressWarnings("rawtypes")          // Unsafe mechanics
116          static final AtomicReferenceFieldUpdater<Node, Node>  
117              nextUpdater = AtomicReferenceFieldUpdater.newUpdater          private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();
118              (Node.class, Node.class, "next");          private static final long nextOffset =
119                objectFieldOffset(UNSAFE, "next", Node.class);
120    
121          final boolean casNext(Node<E> cmp, Node<E> val) {          final boolean casNext(Node<E> cmp, Node<E> val) {
122              return nextUpdater.compareAndSet(this, cmp, val);              return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
123          }          }
124    
125          final void clearNext() {          final void clearNext() {
126              nextUpdater.lazySet(this, this);              UNSAFE.putOrderedObject(this, nextOffset, this);
127          }          }
128    
129          private static final long serialVersionUID = -3375979862319811754L;          private static final long serialVersionUID = -3375979862319811754L;
# Line 440  Line 440 
440          addAll(c);          addAll(c);
441      }      }
442    
443        /**
444         * @throws InterruptedException {@inheritDoc}
445         * @throws NullPointerException {@inheritDoc}
446         */
447      public void put(E e) throws InterruptedException {      public void put(E e) throws InterruptedException {
448          if (e == null) throw new NullPointerException();          if (e == null) throw new NullPointerException();
449          if (Thread.interrupted()) throw new InterruptedException();          if (Thread.interrupted()) throw new InterruptedException();
450          xfer(e, NOWAIT, 0);          xfer(e, NOWAIT, 0);
451      }      }
452    
453        /**
454         * @throws InterruptedException {@inheritDoc}
455         * @throws NullPointerException {@inheritDoc}
456         */
457      public boolean offer(E e, long timeout, TimeUnit unit)      public boolean offer(E e, long timeout, TimeUnit unit)
458          throws InterruptedException {          throws InterruptedException {
459          if (e == null) throw new NullPointerException();          if (e == null) throw new NullPointerException();
# Line 454  Line 462 
462          return true;          return true;
463      }      }
464    
465        /**
466         * @throws NullPointerException {@inheritDoc}
467         */
468      public boolean offer(E e) {      public boolean offer(E e) {
469          if (e == null) throw new NullPointerException();          if (e == null) throw new NullPointerException();
470          xfer(e, NOWAIT, 0);          xfer(e, NOWAIT, 0);
471          return true;          return true;
472      }      }
473    
474        /**
475         * @throws NullPointerException {@inheritDoc}
476         */
477      public boolean add(E e) {      public boolean add(E e) {
478          if (e == null) throw new NullPointerException();          if (e == null) throw new NullPointerException();
479          xfer(e, NOWAIT, 0);          xfer(e, NOWAIT, 0);
480          return true;          return true;
481      }      }
482    
483        /**
484         * @throws InterruptedException {@inheritDoc}
485         * @throws NullPointerException {@inheritDoc}
486         */
487      public void transfer(E e) throws InterruptedException {      public void transfer(E e) throws InterruptedException {
488          if (e == null) throw new NullPointerException();          if (e == null) throw new NullPointerException();
489          if (xfer(e, WAIT, 0) == null) {          if (xfer(e, WAIT, 0) == null) {
# Line 474  Line 492 
492          }          }
493      }      }
494    
495        /**
496         * @throws InterruptedException {@inheritDoc}
497         * @throws NullPointerException {@inheritDoc}
498         */
499      public boolean tryTransfer(E e, long timeout, TimeUnit unit)      public boolean tryTransfer(E e, long timeout, TimeUnit unit)
500          throws InterruptedException {          throws InterruptedException {
501          if (e == null) throw new NullPointerException();          if (e == null) throw new NullPointerException();
# Line 484  Line 506 
506          throw new InterruptedException();          throw new InterruptedException();
507      }      }
508    
509        /**
510         * @throws NullPointerException {@inheritDoc}
511         */
512      public boolean tryTransfer(E e) {      public boolean tryTransfer(E e) {
513          if (e == null) throw new NullPointerException();          if (e == null) throw new NullPointerException();
514          return fulfill(e) != null;          return fulfill(e) != null;
515      }      }
516    
517        /**
518         * @throws InterruptedException {@inheritDoc}
519         */
520      public E take() throws InterruptedException {      public E take() throws InterruptedException {
521          Object e = xfer(null, WAIT, 0);          Object e = xfer(null, WAIT, 0);
522          if (e != null)          if (e != null)
# Line 497  Line 525 
525          throw new InterruptedException();          throw new InterruptedException();
526      }      }
527    
528        /**
529         * @throws InterruptedException {@inheritDoc}
530         */
531      public E poll(long timeout, TimeUnit unit) throws InterruptedException {      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
532          Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));          Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
533          if (e != null || !Thread.interrupted())          if (e != null || !Thread.interrupted())
# Line 508  Line 539 
539          return fulfill(null);          return fulfill(null);
540      }      }
541    
542        /**
543         * @throws NullPointerException     {@inheritDoc}
544         * @throws IllegalArgumentException {@inheritDoc}
545         */
546      public int drainTo(Collection<? super E> c) {      public int drainTo(Collection<? super E> c) {
547          if (c == null)          if (c == null)
548              throw new NullPointerException();              throw new NullPointerException();
# Line 522  Line 557 
557          return n;          return n;
558      }      }
559    
560        /**
561         * @throws NullPointerException     {@inheritDoc}
562         * @throws IllegalArgumentException {@inheritDoc}
563         */
564      public int drainTo(Collection<? super E> c, int maxElements) {      public int drainTo(Collection<? super E> c, int maxElements) {
565          if (c == null)          if (c == null)
566              throw new NullPointerException();              throw new NullPointerException();
# Line 801  Line 840 
840    
841      private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();      private static final sun.misc.Unsafe UNSAFE = sun.misc.Unsafe.getUnsafe();
842      private static final long headOffset =      private static final long headOffset =
843          objectFieldOffset("head", LinkedTransferQueue.class);          objectFieldOffset(UNSAFE, "head", LinkedTransferQueue.class);
844      private static final long tailOffset =      private static final long tailOffset =
845          objectFieldOffset("tail", LinkedTransferQueue.class);          objectFieldOffset(UNSAFE, "tail", LinkedTransferQueue.class);
846      private static final long cleanMeOffset =      private static final long cleanMeOffset =
847          objectFieldOffset("cleanMe", LinkedTransferQueue.class);          objectFieldOffset(UNSAFE, "cleanMe", LinkedTransferQueue.class);
848    
849    
850      private static long objectFieldOffset(String field, Class<?> klazz) {      static long objectFieldOffset(sun.misc.Unsafe UNSAFE,
851                                      String field, Class<?> klazz) {
852          try {          try {
853              return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));              return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
854          } catch (NoSuchFieldException e) {          } catch (NoSuchFieldException e) {

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8