jdk/src/share/classes/sun/misc/Cleaner.java
author darcy
Tue, 12 Nov 2013 09:44:39 -0800
changeset 21655 55f32ae4f920
parent 5506 202f599c92aa
permissions -rw-r--r--
8028229: Fix more raw types lint warning in core libraries Reviewed-by: chegar, forax, lancea, alanb, jfranck
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.ref.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * General-purpose phantom-reference-based cleaners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p> Cleaners are a lightweight and more robust alternative to finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * They are lightweight because they are not created by the VM and thus do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * require a JNI upcall to be created, and because their cleanup code is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * invoked directly by the reference-handler thread rather than by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * finalizer thread.  They are more robust because they use phantom references,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the weakest type of reference object, thereby avoiding the nasty ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * problems inherent to finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p> A cleaner tracks a referent object and encapsulates a thunk of arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * cleanup code.  Some time after the GC detects that a cleaner's referent has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * become phantom-reachable, the reference-handler thread will run the cleaner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Cleaners may also be invoked directly; they are thread safe and ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * they run their thunks at most once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p> Cleaners are not a replacement for finalization.  They should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * only when the cleanup code is extremely simple and straightforward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Nontrivial cleaners are inadvisable since they risk blocking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * reference-handler thread and delaying further cleanup and finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class Cleaner
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 5506
diff changeset
    60
    extends PhantomReference<Object>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Dummy reference queue, needed because the PhantomReference constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // insists that we pass a queue.  Nothing will ever be placed on this queue
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // since the reference handler invokes cleaners explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    //
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 5506
diff changeset
    67
    private static final ReferenceQueue<Object> dummyQueue = new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // Doubly-linked list of live cleaners, which prevents the cleaners
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // themselves from being GC'd before their referents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    static private Cleaner first = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private Cleaner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        next = null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static synchronized Cleaner add(Cleaner cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (first != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            cl.next = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            first.prev = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        first = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static synchronized boolean remove(Cleaner cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // If already removed, do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (cl.next == cl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // Update list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (first == cl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (cl.next != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                first = cl.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                first = cl.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (cl.next != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            cl.next.prev = cl.prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (cl.prev != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            cl.prev.next = cl.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        // Indicate removal by pointing the cleaner to itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        cl.next = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        cl.prev = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private final Runnable thunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private Cleaner(Object referent, Runnable thunk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        super(referent, dummyQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this.thunk = thunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Creates a new cleaner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 5506
diff changeset
   122
     * @param  ob the referent object to be cleaned
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param  thunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *         The cleanup code to be run when the cleaner is invoked.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *         cleanup code is run directly from the reference-handler thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *         so it should be as simple and straightforward as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @return  The new cleaner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public static Cleaner create(Object ob, Runnable thunk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (thunk == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return add(new Cleaner(ob, thunk));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Runs this cleaner, if it has not been run before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void clean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (!remove(this))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            thunk.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } catch (final Throwable x) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   145
            AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   146
                    public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        if (System.err != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            new Error("Cleaner terminated abnormally", x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                .printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}