[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.96, Tue Apr 19 22:55:30 2016 UTC revision 1.97, Thu Jun 2 13:16:27 2016 UTC
# Line 6  Line 6 
6    
7  package java.util.concurrent;  package java.util.concurrent;
8    
9    import java.lang.invoke.MethodHandles;
10    import java.lang.invoke.VarHandle;
11  import java.util.AbstractQueue;  import java.util.AbstractQueue;
12  import java.util.Arrays;  import java.util.Arrays;
13  import java.util.Collection;  import java.util.Collection;
# Line 415  Line 417 
417    
418      /**      /**
419       * Queue nodes. Uses Object, not E, for items to allow forgetting       * Queue nodes. Uses Object, not E, for items to allow forgetting
420       * them after use.  Relies heavily on Unsafe mechanics to minimize       * them after use.  Relies heavily on VarHandles to minimize
421       * unnecessary ordering constraints: Writes that are intrinsically       * unnecessary ordering constraints: Writes that are intrinsically
422       * ordered wrt other accesses or CASes use simple relaxed forms.       * ordered wrt other accesses or CASes use simple relaxed forms.
423       */       */
# Line 427  Line 429 
429    
430          // CAS methods for fields          // CAS methods for fields
431          final boolean casNext(Node cmp, Node val) {          final boolean casNext(Node cmp, Node val) {
432              return U.compareAndSwapObject(this, NEXT, cmp, val);              return NEXT.compareAndSet(this, cmp, val);
433          }          }
434    
435          final boolean casItem(Object cmp, Object val) {          final boolean casItem(Object cmp, Object val) {
436              // assert cmp == null || cmp.getClass() != Node.class;              // assert cmp == null || cmp.getClass() != Node.class;
437              return U.compareAndSwapObject(this, ITEM, cmp, val);              return ITEM.compareAndSet(this, cmp, val);
438          }          }
439    
440          /**          /**
# Line 440  Line 442 
442           * only be seen after publication via casNext.           * only be seen after publication via casNext.
443           */           */
444          Node(Object item, boolean isData) {          Node(Object item, boolean isData) {
445              U.putObject(this, ITEM, item); // relaxed write              ITEM.set(this, item); // relaxed write
446              this.isData = isData;              this.isData = isData;
447          }          }
448    
# Line 449  Line 451 
451           * only after CASing head field, so uses relaxed write.           * only after CASing head field, so uses relaxed write.
452           */           */
453          final void forgetNext() {          final void forgetNext() {
454              U.putObject(this, NEXT, this);              NEXT.set(this, this);
455          }          }
456    
457          /**          /**
# Line 462  Line 464 
464           * else we don't care).           * else we don't care).
465           */           */
466          final void forgetContents() {          final void forgetContents() {
467              U.putObject(this, ITEM, this);              ITEM.set(this, this);
468              U.putObject(this, WAITER, null);              WAITER.set(this, null);
469          }          }
470    
471          /**          /**
# Line 508  Line 510 
510    
511          private static final long serialVersionUID = -3375979862319811754L;          private static final long serialVersionUID = -3375979862319811754L;
512    
513          // Unsafe mechanics          // VarHandle mechanics
514          private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();          private static final VarHandle ITEM;
515          private static final long ITEM;          private static final VarHandle NEXT;
516          private static final long NEXT;          private static final VarHandle WAITER;
         private static final long WAITER;  
517          static {          static {
518              try {              try {
519                  ITEM = U.objectFieldOffset                  MethodHandles.Lookup l = MethodHandles.lookup();
520                      (Node.class.getDeclaredField("item"));                  ITEM = l.findVarHandle(Node.class, "item", Object.class);
521                  NEXT = U.objectFieldOffset                  NEXT = l.findVarHandle(Node.class, "next", Node.class);
522                      (Node.class.getDeclaredField("next"));                  WAITER = l.findVarHandle(Node.class, "waiter", Thread.class);
                 WAITER = U.objectFieldOffset  
                     (Node.class.getDeclaredField("waiter"));  
523              } catch (ReflectiveOperationException e) {              } catch (ReflectiveOperationException e) {
524                  throw new Error(e);                  throw new Error(e);
525              }              }
# Line 538  Line 537 
537    
538      // CAS methods for fields      // CAS methods for fields
539      private boolean casTail(Node cmp, Node val) {      private boolean casTail(Node cmp, Node val) {
540          return U.compareAndSwapObject(this, TAIL, cmp, val);          return TAIL.compareAndSet(this, cmp, val);
541      }      }
542    
543      private boolean casHead(Node cmp, Node val) {      private boolean casHead(Node cmp, Node val) {
544          return U.compareAndSwapObject(this, HEAD, cmp, val);          return HEAD.compareAndSet(this, cmp, val);
545      }      }
546    
547      private boolean casSweepVotes(int cmp, int val) {      private boolean casSweepVotes(int cmp, int val) {
548          return U.compareAndSwapInt(this, SWEEPVOTES, cmp, val);          return SWEEPVOTES.compareAndSet(this, cmp, val);
549      }      }
550    
551      /*      /*
# Line 1533  Line 1532 
1532          }          }
1533      }      }
1534    
1535      // Unsafe mechanics      // VarHandle mechanics
1536        private static final VarHandle HEAD;
1537      private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();      private static final VarHandle TAIL;
1538      private static final long HEAD;      private static final VarHandle SWEEPVOTES;
     private static final long TAIL;  
     private static final long SWEEPVOTES;  
1539      static {      static {
1540          try {          try {
1541              HEAD = U.objectFieldOffset              MethodHandles.Lookup l = MethodHandles.lookup();
1542                  (LinkedTransferQueue.class.getDeclaredField("head"));              HEAD = l.findVarHandle(LinkedTransferQueue.class, "head",
1543              TAIL = U.objectFieldOffset                                     Node.class);
1544                  (LinkedTransferQueue.class.getDeclaredField("tail"));              TAIL = l.findVarHandle(LinkedTransferQueue.class, "tail",
1545              SWEEPVOTES = U.objectFieldOffset                                     Node.class);
1546                  (LinkedTransferQueue.class.getDeclaredField("sweepVotes"));              SWEEPVOTES = l.findVarHandle(LinkedTransferQueue.class, "sweepVotes",
1547                                             int.class);
1548          } catch (ReflectiveOperationException e) {          } catch (ReflectiveOperationException e) {
1549              throw new Error(e);              throw new Error(e);
1550          }          }

Legend:
Removed from v.1.96  
changed lines
  Added in v.1.97

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8