1 /* |
|
2 * Copyright (c) 2008, 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 |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.tracing.dtrace; |
|
27 |
|
28 import java.lang.ref.WeakReference; |
|
29 import java.lang.ref.ReferenceQueue; |
|
30 import java.security.Permission; |
|
31 import java.util.HashSet; |
|
32 |
|
33 class Activation { |
|
34 private SystemResource resource; |
|
35 private int referenceCount; |
|
36 |
|
37 Activation(String moduleName, DTraceProvider[] providers) { |
|
38 SecurityManager security = System.getSecurityManager(); |
|
39 if (security != null) { |
|
40 Permission perm = |
|
41 new RuntimePermission("com.sun.tracing.dtrace.createProvider"); |
|
42 security.checkPermission(perm); |
|
43 } |
|
44 referenceCount = providers.length; |
|
45 for (DTraceProvider p : providers) { |
|
46 p.setActivation(this); |
|
47 } |
|
48 resource = new SystemResource( |
|
49 this, JVM.activate(moduleName, providers)); |
|
50 } |
|
51 |
|
52 void disposeProvider(DTraceProvider p) { |
|
53 if (--referenceCount == 0) { |
|
54 resource.dispose(); |
|
55 } |
|
56 } |
|
57 } |
|
58 |
|
59 /** |
|
60 * The native resource part of an Activation. |
|
61 * |
|
62 * This holds the native handle. |
|
63 * |
|
64 * If the user loses a reference to a set of Providers without disposing them, |
|
65 * and GC determines the Activation is unreachable, then the next |
|
66 * activation or flush call will automatically dispose the unreachable objects |
|
67 * |
|
68 * The SystemResource instances are creating during activation, and |
|
69 * unattached during disposal. When created, they always have a |
|
70 * strong reference to them via the {@code resources} static member. Explicit |
|
71 * {@code dispose} calls will unregister the native resource and remove |
|
72 * references to the SystemResource object. Absent an explicit dispose, |
|
73 * when their associated Activation object becomes garbage, the SystemResource |
|
74 * object will be enqueued on the reference queue and disposed at the |
|
75 * next call to {@code flush}. |
|
76 */ |
|
77 class SystemResource extends WeakReference<Activation> { |
|
78 |
|
79 private long handle; |
|
80 |
|
81 private static ReferenceQueue<Activation> referenceQueue = |
|
82 referenceQueue = new ReferenceQueue<Activation>(); |
|
83 static HashSet<SystemResource> resources = new HashSet<SystemResource>(); |
|
84 |
|
85 SystemResource(Activation activation, long handle) { |
|
86 super(activation, referenceQueue); |
|
87 this.handle = handle; |
|
88 flush(); |
|
89 resources.add(this); |
|
90 } |
|
91 |
|
92 void dispose() { |
|
93 JVM.dispose(handle); |
|
94 resources.remove(this); |
|
95 handle = 0; |
|
96 } |
|
97 |
|
98 static void flush() { |
|
99 SystemResource resource = null; |
|
100 while ((resource = (SystemResource)referenceQueue.poll()) != null) { |
|
101 if (resource.handle != 0) { |
|
102 resource.dispose(); |
|
103 } |
|
104 } |
|
105 } |
|
106 } |
|
107 |
|