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.25 by jsr166, Fri Jul 24 23:48:26 2009 UTC vs.
Revision 1.30 by jsr166, Mon Jul 27 03:22:39 2009 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166y;
8 +
9   import java.util.concurrent.*;
10 < import java.util.concurrent.locks.*;
11 < import java.util.concurrent.atomic.*;
12 < import java.util.*;
13 < import java.io.*;
10 >
11 > import java.util.AbstractQueue;
12 > import java.util.Collection;
13 > 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;
18  
19   /**
20   * An unbounded {@linkplain TransferQueue} based on linked nodes.
# Line 437 | Line 442 | public class LinkedTransferQueue<E> exte
442          addAll(c);
443      }
444  
445 +    /**
446 +     * @throws InterruptedException {@inheritDoc}
447 +     * @throws NullPointerException {@inheritDoc}
448 +     */
449      public void put(E e) throws InterruptedException {
450          if (e == null) throw new NullPointerException();
451          if (Thread.interrupted()) throw new InterruptedException();
452          xfer(e, NOWAIT, 0);
453      }
454  
455 +    /**
456 +     * @throws InterruptedException {@inheritDoc}
457 +     * @throws NullPointerException {@inheritDoc}
458 +     */
459      public boolean offer(E e, long timeout, TimeUnit unit)
460          throws InterruptedException {
461          if (e == null) throw new NullPointerException();
# Line 451 | Line 464 | public class LinkedTransferQueue<E> exte
464          return true;
465      }
466  
467 +    /**
468 +     * @throws NullPointerException {@inheritDoc}
469 +     */
470      public boolean offer(E e) {
471          if (e == null) throw new NullPointerException();
472          xfer(e, NOWAIT, 0);
473          return true;
474      }
475  
476 +    /**
477 +     * @throws NullPointerException {@inheritDoc}
478 +     */
479      public boolean add(E e) {
480          if (e == null) throw new NullPointerException();
481          xfer(e, NOWAIT, 0);
482          return true;
483      }
484  
485 +    /**
486 +     * @throws InterruptedException {@inheritDoc}
487 +     * @throws NullPointerException {@inheritDoc}
488 +     */
489      public void transfer(E e) throws InterruptedException {
490          if (e == null) throw new NullPointerException();
491          if (xfer(e, WAIT, 0) == null) {
# Line 471 | Line 494 | public class LinkedTransferQueue<E> exte
494          }
495      }
496  
497 +    /**
498 +     * @throws InterruptedException {@inheritDoc}
499 +     * @throws NullPointerException {@inheritDoc}
500 +     */
501      public boolean tryTransfer(E e, long timeout, TimeUnit unit)
502          throws InterruptedException {
503          if (e == null) throw new NullPointerException();
# Line 481 | Line 508 | public class LinkedTransferQueue<E> exte
508          throw new InterruptedException();
509      }
510  
511 +    /**
512 +     * @throws NullPointerException {@inheritDoc}
513 +     */
514      public boolean tryTransfer(E e) {
515          if (e == null) throw new NullPointerException();
516          return fulfill(e) != null;
517      }
518  
519 +    /**
520 +     * @throws InterruptedException {@inheritDoc}
521 +     */
522      public E take() throws InterruptedException {
523          Object e = xfer(null, WAIT, 0);
524          if (e != null)
# Line 494 | Line 527 | public class LinkedTransferQueue<E> exte
527          throw new InterruptedException();
528      }
529  
530 +    /**
531 +     * @throws InterruptedException {@inheritDoc}
532 +     */
533      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
534          Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
535          if (e != null || !Thread.interrupted())
# Line 505 | Line 541 | public class LinkedTransferQueue<E> exte
541          return fulfill(null);
542      }
543  
544 +    /**
545 +     * @throws NullPointerException     {@inheritDoc}
546 +     * @throws IllegalArgumentException {@inheritDoc}
547 +     */
548      public int drainTo(Collection<? super E> c) {
549          if (c == null)
550              throw new NullPointerException();
# Line 519 | Line 559 | public class LinkedTransferQueue<E> exte
559          return n;
560      }
561  
562 +    /**
563 +     * @throws NullPointerException     {@inheritDoc}
564 +     * @throws IllegalArgumentException {@inheritDoc}
565 +     */
566      public int drainTo(Collection<? super E> c, int maxElements) {
567          if (c == null)
568              throw new NullPointerException();
# Line 794 | Line 838 | public class LinkedTransferQueue<E> exte
838                                   new PaddedAtomicReference<Node<E>>(null));
839      }
840  
841 <    // Unsafe mechanics for jsr166y 3rd party package.
841 >    // Unsafe mechanics
842 >
843 >    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
844 >    private static final long headOffset =
845 >        objectFieldOffset("head", LinkedTransferQueue.class);
846 >    private static final long tailOffset =
847 >        objectFieldOffset("tail", LinkedTransferQueue.class);
848 >    private static final long cleanMeOffset =
849 >        objectFieldOffset("cleanMe", LinkedTransferQueue.class);
850 >
851 >    private static long objectFieldOffset(String field, Class<?> klazz) {
852 >        try {
853 >            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
854 >        } catch (NoSuchFieldException e) {
855 >            // Convert Exception to corresponding Error
856 >            NoSuchFieldError error = new NoSuchFieldError(field);
857 >            error.initCause(e);
858 >            throw error;
859 >        }
860 >    }
861 >
862 >    /**
863 >     * Returns a sun.misc.Unsafe.  Suitable for use in a 3rd party package.
864 >     * Replace with a simple call to Unsafe.getUnsafe when integrating
865 >     * into a jdk.
866 >     *
867 >     * @return a sun.misc.Unsafe
868 >     */
869      private static sun.misc.Unsafe getUnsafe() {
870          try {
871              return sun.misc.Unsafe.getUnsafe();
872          } catch (SecurityException se) {
873              try {
874                  return java.security.AccessController.doPrivileged
875 <                    (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
875 >                    (new java.security
876 >                     .PrivilegedExceptionAction<sun.misc.Unsafe>() {
877                          public sun.misc.Unsafe run() throws Exception {
878 <                            return getUnsafeByReflection();
878 >                            java.lang.reflect.Field f = sun.misc
879 >                                .Unsafe.class.getDeclaredField("theUnsafe");
880 >                            f.setAccessible(true);
881 >                            return (sun.misc.Unsafe) f.get(null);
882                          }});
883              } catch (java.security.PrivilegedActionException e) {
884                  throw new RuntimeException("Could not initialize intrinsics",
# Line 811 | Line 886 | public class LinkedTransferQueue<E> exte
886              }
887          }
888      }
814
815    private static sun.misc.Unsafe getUnsafeByReflection()
816            throws NoSuchFieldException, IllegalAccessException {
817        java.lang.reflect.Field f =
818            sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
819        f.setAccessible(true);
820        return (sun.misc.Unsafe) f.get(null);
821    }
822
823    private static long fieldOffset(String fieldName, Class<?> klazz) {
824        try {
825            return UNSAFE.objectFieldOffset(klazz.getDeclaredField(fieldName));
826        } catch (NoSuchFieldException e) {
827            // Convert Exception to Error
828            NoSuchFieldError error = new NoSuchFieldError(fieldName);
829            error.initCause(e);
830            throw error;
831        }
832    }
833
834    private static final sun.misc.Unsafe UNSAFE = getUnsafe();
835    static final long headOffset =
836        fieldOffset("head", LinkedTransferQueue.class);
837    static final long tailOffset =
838        fieldOffset("tail", LinkedTransferQueue.class);
839    static final long cleanMeOffset =
840        fieldOffset("cleanMe", LinkedTransferQueue.class);
841
889   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines