jdk/src/java.base/share/classes/java/lang/ref/Reference.java
changeset 44119 e813e246ef17
parent 40954 72ca7f63532a
child 44748 e7d6b4646d04
equal deleted inserted replaced
44118:800a6450f7d4 44119:e813e246ef17
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   138                 processPendingReferences();
   138                 processPendingReferences();
   139             }
   139             }
   140         }
   140         }
   141     }
   141     }
   142 
   142 
   143     /* Atomically get and clear (set to null) the VM's pending list.
   143     /*
       
   144      * system property to disable clearing before enqueuing.
       
   145      */
       
   146     private static final boolean disableClearBeforeEnqueue
       
   147         = Boolean.getBoolean("jdk.lang.ref.disableClearBeforeEnqueue");
       
   148 
       
   149     /*
       
   150      * Atomically get and clear (set to null) the VM's pending list.
   144      */
   151      */
   145     private static native Reference<Object> getAndClearReferencePendingList();
   152     private static native Reference<Object> getAndClearReferencePendingList();
   146 
   153 
   147     /* Test whether the VM's pending list contains any entries.
   154     /*
       
   155      * Test whether the VM's pending list contains any entries.
   148      */
   156      */
   149     private static native boolean hasReferencePendingList();
   157     private static native boolean hasReferencePendingList();
   150 
   158 
   151     /* Wait until the VM's pending list may be non-null.
   159     /*
       
   160      * Wait until the VM's pending list may be non-null.
   152      */
   161      */
   153     private static native void waitForReferencePendingList();
   162     private static native void waitForReferencePendingList();
   154 
   163 
   155     private static final Object processPendingLock = new Object();
   164     private static final Object processPendingLock = new Object();
   156     private static boolean processPendingActive = false;
   165     private static boolean processPendingActive = false;
   259      */
   268      */
   260     public void clear() {
   269     public void clear() {
   261         this.referent = null;
   270         this.referent = null;
   262     }
   271     }
   263 
   272 
   264 
       
   265     /* -- Queue operations -- */
   273     /* -- Queue operations -- */
   266 
   274 
   267     /**
   275     /**
   268      * Tells whether or not this reference object has been enqueued, either by
   276      * Tells whether or not this reference object has been enqueued, either by
   269      * the program or by the garbage collector.  If this reference object was
   277      * the program or by the garbage collector.  If this reference object was
   276     public boolean isEnqueued() {
   284     public boolean isEnqueued() {
   277         return (this.queue == ReferenceQueue.ENQUEUED);
   285         return (this.queue == ReferenceQueue.ENQUEUED);
   278     }
   286     }
   279 
   287 
   280     /**
   288     /**
   281      * Adds this reference object to the queue with which it is registered,
   289      * Clears this reference object and adds it to the queue with which
   282      * if any.
   290      * it is registered, if any.
   283      *
   291      *
   284      * <p> This method is invoked only by Java code; when the garbage collector
   292      * <p> This method is invoked only by Java code; when the garbage collector
   285      * enqueues references it does so directly, without invoking this method.
   293      * enqueues references it does so directly, without invoking this method.
   286      *
   294      *
   287      * @return   <code>true</code> if this reference object was successfully
   295      * @return   <code>true</code> if this reference object was successfully
   288      *           enqueued; <code>false</code> if it was already enqueued or if
   296      *           enqueued; <code>false</code> if it was already enqueued or if
   289      *           it was not registered with a queue when it was created
   297      *           it was not registered with a queue when it was created
   290      */
   298      */
   291     public boolean enqueue() {
   299     public boolean enqueue() {
       
   300         if (!disableClearBeforeEnqueue)
       
   301             this.referent = null;
   292         return this.queue.enqueue(this);
   302         return this.queue.enqueue(this);
   293     }
   303     }
   294 
       
   295 
   304 
   296     /* -- Constructors -- */
   305     /* -- Constructors -- */
   297 
   306 
   298     Reference(T referent) {
   307     Reference(T referent) {
   299         this(referent, null);
   308         this(referent, null);
   417     public static void reachabilityFence(Object ref) {
   426     public static void reachabilityFence(Object ref) {
   418         // Does nothing, because this method is annotated with @DontInline
   427         // Does nothing, because this method is annotated with @DontInline
   419         // HotSpot needs to retain the ref and not GC it before a call to this
   428         // HotSpot needs to retain the ref and not GC it before a call to this
   420         // method
   429         // method
   421     }
   430     }
   422 
       
   423 }
   431 }