< prev index next >

src/java.base/share/classes/java/lang/ref/ReferenceQueue.java

Print this page
@@ -1,7 +1,7 @@
  /*
-  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
+  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.  Oracle designates this

@@ -23,10 +23,13 @@
   * questions.
   */
  
  package java.lang.ref;
  
+ import java.util.concurrent.TimeUnit;
+ import java.util.concurrent.locks.Condition;
+ import java.util.concurrent.locks.ReentrantLock;
  import java.util.function.Consumer;
  import jdk.internal.misc.VM;
  
  /**
   * Reference queues, to which registered reference objects are appended by the

@@ -35,56 +38,75 @@
   * @author   Mark Reinhold
   * @since    1.2
   */
  
  public class ReferenceQueue<T> {
- 
-     /**
-      * Constructs a new reference-object queue.
-      */
-     public ReferenceQueue() { }
- 
      private static class Null extends ReferenceQueue<Object> {
+         public Null() { super(0); }
+         
+         @Override 
          boolean enqueue(Reference<?> r) {
              return false;
          }
      }
  
      static final ReferenceQueue<Object> NULL = new Null();
      static final ReferenceQueue<Object> ENQUEUED = new Null();
  
-     private static class Lock { };
-     private final Lock lock = new Lock();
      private volatile Reference<? extends T> head;
      private long queueLength = 0;
  
-     boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
-         synchronized (lock) {
-             // Check that since getting the lock this reference hasn't already been
-             // enqueued (and even then removed)
-             ReferenceQueue<?> queue = r.queue;
-             if ((queue == NULL) || (queue == ENQUEUED)) {
-                 return false;
-             }
-             assert queue == this;
-             // Self-loop end, so if a FinalReference it remains inactive.
-             r.next = (head == null) ? r : head;
-             head = r;
-             queueLength++;
-             // Update r.queue *after* adding to list, to avoid race
-             // with concurrent enqueued checks and fast-path poll().
-             // Volatiles ensure ordering.
-             r.queue = ENQUEUED;
-             if (r instanceof FinalReference) {
-                 VM.addFinalRefCount(1);
-             }
-             lock.notifyAll();
-             return true;
+     private final ReentrantLock lock;
+     private final Condition notEmpty;
+ 
+     void signal() { notEmpty.signalAll(); }
+     void await() throws InterruptedException { notEmpty.await(); }
+     void await(long timeoutMillis) throws InterruptedException { 
+         notEmpty.await(timeoutMillis, TimeUnit.MILLISECONDS);
+     }
+ 
+     /**
+      * Constructs a new reference-object queue.
+      */
+     public ReferenceQueue() {
+         this.lock = new ReentrantLock();
+         this.notEmpty = lock.newCondition();
+     }
+ 
+     ReferenceQueue(int dummy) {
+         this.lock = null;
+         this.notEmpty = null;
+     }
+ 
+     final boolean enqueue0(Reference<? extends T> r) { // must hold lock
+         // Check that since getting the lock this reference hasn't already been
+         // enqueued (and even then removed)
+         ReferenceQueue<?> queue = r.queue;
+         if ((queue == NULL) || (queue == ENQUEUED)) {
+             return false;
          }
+         assert queue == this;
+         // Self-loop end, so if a FinalReference it remains inactive.
+         r.next = (head == null) ? r : head;
+         head = r;
+         queueLength++;
+         // Update r.queue *after* adding to list, to avoid race
+         // with concurrent enqueued checks and fast-path poll().
+         // Volatiles ensure ordering.
+         r.queue = ENQUEUED;
+         if (r instanceof FinalReference) {
+             VM.addFinalRefCount(1);
+         }
+         signal();
+         return true;
      }
  
-     private Reference<? extends T> reallyPoll() {       /* Must hold lock */
+     final boolean headIsNull() {
+         return head == null;
+     }
+     
+     final Reference<? extends T> poll0() { // must hold lock
          Reference<? extends T> r = head;
          if (r != null) {
              r.queue = NULL;
              // Update r.queue *before* removing from list, to avoid
              // race with concurrent enqueued checks and fast-path

@@ -103,23 +125,60 @@
              return r;
          }
          return null;
      }
  
+     final Reference<? extends T> remove0(long timeout) 
+             throws IllegalArgumentException, InterruptedException { // must hold lock
+         Reference<? extends T> r = poll0();
+         if (r != null) return r;
+         long start = System.nanoTime();
+         for (;;) {
+             await(timeout);
+             r = poll0();
+             if (r != null) return r;
+ 
+             long end = System.nanoTime();
+             timeout -= (end - start) / 1000_000;
+             if (timeout <= 0) return null;
+             start = end;
+         }
+     }
+ 
+     final Reference<? extends T> remove0() throws InterruptedException { // must hold lock
+         for (;;) {
+             var r = poll0();
+             if (r != null) return r;
+             await();
+         }
+     }
+ 
+     boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
+         lock.lock();
+         try {
+             return enqueue0(r);
+         } finally {
+             lock.unlock();
+         }
+     }
+ 
      /**
       * Polls this queue to see if a reference object is available.  If one is
       * available without further delay then it is removed from the queue and
       * returned.  Otherwise this method immediately returns {@code null}.
       *
       * @return  A reference object, if one was immediately available,
       *          otherwise {@code null}
       */
      public Reference<? extends T> poll() {
-         if (head == null)
+         if (headIsNull())
              return null;
-         synchronized (lock) {
-             return reallyPoll();
+         lock.lock();
+         try {
+             return poll0();
+         } finally {
+             lock.unlock();
          }
      }
  
      /**
       * Removes the next reference object in this queue, blocking until either

@@ -139,31 +198,20 @@
       *          If the value of the timeout argument is negative
       *
       * @throws  InterruptedException
       *          If the timeout wait is interrupted
       */
-     public Reference<? extends T> remove(long timeout)
-         throws IllegalArgumentException, InterruptedException
-     {
-         if (timeout < 0) {
-             throw new IllegalArgumentException("Negative timeout value");
-         }
-         synchronized (lock) {
-             Reference<? extends T> r = reallyPoll();
-             if (r != null) return r;
-             long start = (timeout == 0) ? 0 : System.nanoTime();
-             for (;;) {
-                 lock.wait(timeout);
-                 r = reallyPoll();
-                 if (r != null) return r;
-                 if (timeout != 0) {
-                     long end = System.nanoTime();
-                     timeout -= (end - start) / 1000_000;
-                     if (timeout <= 0) return null;
-                     start = end;
-                 }
-             }
+     public Reference<? extends T> remove(long timeout) 
+         throws IllegalArgumentException, InterruptedException {
+         if (timeout < 0) throw new IllegalArgumentException("Negative timeout value");
+         if (timeout == 0) return remove();
+ 
+         lock.lock();
+         try {
+             return remove0(timeout);
+         } finally {
+             lock.unlock();
          }
      }
  
      /**
       * Removes the next reference object in this queue, blocking until one

@@ -171,11 +219,16 @@
       *
       * @return A reference object, blocking until one becomes available
       * @throws  InterruptedException  If the wait is interrupted
       */
      public Reference<? extends T> remove() throws InterruptedException {
-         return remove(0);
+         lock.lock();
+         try {
+             return remove0();
+         } finally {
+             lock.unlock();
+         }
      }
  
      /**
       * Iterate queue and invoke given action with each Reference.
       * Suitable for diagnostic purposes.
< prev index next >