Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tracing/dtrace/Activation.java
38918 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tracing.dtrace;2627import java.lang.ref.WeakReference;28import java.lang.ref.ReferenceQueue;29import java.security.Permission;30import java.util.HashSet;3132class Activation {33private SystemResource resource;34private int referenceCount;3536Activation(String moduleName, DTraceProvider[] providers) {37SecurityManager security = System.getSecurityManager();38if (security != null) {39Permission perm =40new RuntimePermission("com.sun.tracing.dtrace.createProvider");41security.checkPermission(perm);42}43referenceCount = providers.length;44for (DTraceProvider p : providers) {45p.setActivation(this);46}47resource = new SystemResource(48this, JVM.activate(moduleName, providers));49}5051void disposeProvider(DTraceProvider p) {52if (--referenceCount == 0) {53resource.dispose();54}55}56}5758/**59* The native resource part of an Activation.60*61* This holds the native handle.62*63* If the user loses a reference to a set of Providers without disposing them,64* and GC determines the Activation is unreachable, then the next65* activation or flush call will automatically dispose the unreachable objects66*67* The SystemResource instances are creating during activation, and68* unattached during disposal. When created, they always have a69* strong reference to them via the {@code resources} static member. Explicit70* {@code dispose} calls will unregister the native resource and remove71* references to the SystemResource object. Absent an explicit dispose,72* when their associated Activation object becomes garbage, the SystemResource73* object will be enqueued on the reference queue and disposed at the74* next call to {@code flush}.75*/76class SystemResource extends WeakReference<Activation> {7778private long handle;7980private static ReferenceQueue<Activation> referenceQueue =81referenceQueue = new ReferenceQueue<Activation>();82static HashSet<SystemResource> resources = new HashSet<SystemResource>();8384SystemResource(Activation activation, long handle) {85super(activation, referenceQueue);86this.handle = handle;87flush();88resources.add(this);89}9091void dispose() {92JVM.dispose(handle);93resources.remove(this);94handle = 0;95}9697static void flush() {98SystemResource resource = null;99while ((resource = (SystemResource)referenceQueue.poll()) != null) {100if (resource.handle != 0) {101resource.dispose();102}103}104}105}106107108109