< prev index next >

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

Print this page
rev 50093 : [mq]: pp2_work
   1 /*
   2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  49         }
  50     }
  51 
  52     static ReferenceQueue<Object> NULL = new Null<>();
  53     static ReferenceQueue<Object> ENQUEUED = new Null<>();
  54 
  55     private static class Lock { };
  56     private Lock lock = new Lock();
  57     private volatile Reference<? extends T> head;
  58     private long queueLength = 0;
  59 
  60     boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
  61         synchronized (lock) {
  62             // Check that since getting the lock this reference hasn't already been
  63             // enqueued (and even then removed)
  64             ReferenceQueue<?> queue = r.queue;
  65             if ((queue == NULL) || (queue == ENQUEUED)) {
  66                 return false;
  67             }
  68             assert queue == this;

  69             r.next = (head == null) ? r : head;
  70             head = r;
  71             queueLength++;
  72             // Update r.queue *after* adding to list, to avoid race
  73             // with concurrent enqueued checks and fast-path poll().
  74             // Volatiles ensure ordering.
  75             r.queue = ENQUEUED;
  76             if (r instanceof FinalReference) {
  77                 VM.addFinalRefCount(1);
  78             }
  79             lock.notifyAll();
  80             return true;
  81         }
  82     }
  83 
  84     private Reference<? extends T> reallyPoll() {       /* Must hold lock */
  85         Reference<? extends T> r = head;
  86         if (r != null) {
  87             r.queue = NULL;
  88             // Update r.queue *before* removing from list, to avoid
  89             // race with concurrent enqueued checks and fast-path
  90             // poll().  Volatiles ensure ordering.
  91             @SuppressWarnings("unchecked")
  92             Reference<? extends T> rn = r.next;

  93             head = (rn == r) ? null : rn;


  94             r.next = r;
  95             queueLength--;
  96             if (r instanceof FinalReference) {
  97                 VM.addFinalRefCount(-1);
  98             }
  99             return r;
 100         }
 101         return null;
 102     }
 103 
 104     /**
 105      * Polls this queue to see if a reference object is available.  If one is
 106      * available without further delay then it is removed from the queue and
 107      * returned.  Otherwise this method immediately returns {@code null}.
 108      *
 109      * @return  A reference object, if one was immediately available,
 110      *          otherwise {@code null}
 111      */
 112     public Reference<? extends T> poll() {
 113         if (head == null)


   1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  49         }
  50     }
  51 
  52     static ReferenceQueue<Object> NULL = new Null<>();
  53     static ReferenceQueue<Object> ENQUEUED = new Null<>();
  54 
  55     private static class Lock { };
  56     private Lock lock = new Lock();
  57     private volatile Reference<? extends T> head;
  58     private long queueLength = 0;
  59 
  60     boolean enqueue(Reference<? extends T> r) { /* Called only by Reference class */
  61         synchronized (lock) {
  62             // Check that since getting the lock this reference hasn't already been
  63             // enqueued (and even then removed)
  64             ReferenceQueue<?> queue = r.queue;
  65             if ((queue == NULL) || (queue == ENQUEUED)) {
  66                 return false;
  67             }
  68             assert queue == this;
  69             // Self-loop end, so if a FinalReference it remains inactive.
  70             r.next = (head == null) ? r : head;
  71             head = r;
  72             queueLength++;
  73             // Update r.queue *after* adding to list, to avoid race
  74             // with concurrent enqueued checks and fast-path poll().
  75             // Volatiles ensure ordering.
  76             r.queue = ENQUEUED;
  77             if (r instanceof FinalReference) {
  78                 VM.addFinalRefCount(1);
  79             }
  80             lock.notifyAll();
  81             return true;
  82         }
  83     }
  84 
  85     private Reference<? extends T> reallyPoll() {       /* Must hold lock */
  86         Reference<? extends T> r = head;
  87         if (r != null) {
  88             r.queue = NULL;
  89             // Update r.queue *before* removing from list, to avoid
  90             // race with concurrent enqueued checks and fast-path
  91             // poll().  Volatiles ensure ordering.
  92             @SuppressWarnings("unchecked")
  93             Reference<? extends T> rn = r.next;
  94             // Handle self-looped next as end of list designator.
  95             head = (rn == r) ? null : rn;
  96             // Self-loop next rather than setting to null, so if a
  97             // FinalReference it remains inactive.
  98             r.next = r;
  99             queueLength--;
 100             if (r instanceof FinalReference) {
 101                 VM.addFinalRefCount(-1);
 102             }
 103             return r;
 104         }
 105         return null;
 106     }
 107 
 108     /**
 109      * Polls this queue to see if a reference object is available.  If one is
 110      * available without further delay then it is removed from the queue and
 111      * returned.  Otherwise this method immediately returns {@code null}.
 112      *
 113      * @return  A reference object, if one was immediately available,
 114      *          otherwise {@code null}
 115      */
 116     public Reference<? extends T> poll() {
 117         if (head == null)


< prev index next >