55463
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package jdk.vm.ci.hotspot;
|
|
24 |
|
|
25 |
import java.util.ArrayList;
|
|
26 |
import java.util.List;
|
|
27 |
import java.util.Objects;
|
|
28 |
|
|
29 |
import jdk.vm.ci.services.Services;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* A mechanism for limiting the lifetime of a foreign object reference encapsulated in a
|
|
33 |
* {@link HotSpotObjectConstant}.
|
|
34 |
*
|
|
35 |
* A {@link HotSpotObjectConstant} allocated in a {@linkplain #openLocalScope local} scope will have
|
|
36 |
* its reference to any foreign object cleared when the scope {@linkplain #close() closes}. This
|
|
37 |
* allows the foreign memory manager to reclaim the foreign object (once there are no other strong
|
|
38 |
* references to it).
|
|
39 |
*
|
|
40 |
* {@link HotSpotObjectConstantScope}s have no impact on {@link HotSpotObjectConstant}s that do not
|
|
41 |
* encapsulate a foreign object reference.
|
|
42 |
*
|
|
43 |
* The object returned by {@link #enterGlobalScope()} or {@link #openLocalScope(Object)} should
|
|
44 |
* always be used in a try-with-resources statement. Failure to close a scope will almost certainly
|
|
45 |
* result in foreign objects being leaked.
|
|
46 |
*/
|
|
47 |
public final class HotSpotObjectConstantScope implements AutoCloseable {
|
|
48 |
static final ThreadLocal<HotSpotObjectConstantScope> CURRENT = new ThreadLocal<>();
|
|
49 |
|
|
50 |
private final HotSpotObjectConstantScope parent;
|
|
51 |
private List<IndirectHotSpotObjectConstantImpl> foreignObjects;
|
|
52 |
|
|
53 |
/**
|
|
54 |
* An object whose {@link Object#toString()} value describes a non-global scope. This is
|
|
55 |
* {@code null} iff this is a global scope.
|
|
56 |
*/
|
|
57 |
final Object localScopeDescription;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Opens a local scope that upon closing, will release foreign object references encapsulated by
|
|
61 |
* {@link HotSpotObjectConstant}s created in the scope.
|
|
62 |
*
|
|
63 |
* @param description an non-null object whose {@link Object#toString()} value describes the
|
|
64 |
* scope being opened
|
|
65 |
* @return {@code null} if the current runtime does not support remote object references
|
|
66 |
*/
|
|
67 |
public static HotSpotObjectConstantScope openLocalScope(Object description) {
|
|
68 |
return Services.IS_IN_NATIVE_IMAGE ? new HotSpotObjectConstantScope(Objects.requireNonNull(description)) : null;
|
|
69 |
}
|
|
70 |
|
|
71 |
/**
|
|
72 |
* Enters the global scope. This is useful to escape a local scope for execution that will
|
|
73 |
* create foreign object references that need to outlive the local scope.
|
|
74 |
*
|
|
75 |
* Foreign object references encapsulated by {@link HotSpotObjectConstant}s created in the
|
|
76 |
* global scope are only subject to reclamation once the {@link HotSpotObjectConstant} wrapper
|
|
77 |
* dies.
|
|
78 |
*
|
|
79 |
* @return {@code null} if the current runtime does not support remote object references or if
|
|
80 |
* this thread is currently in the global scope
|
|
81 |
*/
|
|
82 |
public static HotSpotObjectConstantScope enterGlobalScope() {
|
|
83 |
return Services.IS_IN_NATIVE_IMAGE && CURRENT.get() != null ? new HotSpotObjectConstantScope(null) : null;
|
|
84 |
}
|
|
85 |
|
|
86 |
private HotSpotObjectConstantScope(Object localScopeDescription) {
|
|
87 |
this.parent = CURRENT.get();
|
|
88 |
CURRENT.set(this);
|
|
89 |
this.localScopeDescription = localScopeDescription;
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Determines if this scope is global.
|
|
94 |
*/
|
|
95 |
boolean isGlobal() {
|
|
96 |
return localScopeDescription == null;
|
|
97 |
}
|
|
98 |
|
|
99 |
void add(IndirectHotSpotObjectConstantImpl obj) {
|
|
100 |
assert !isGlobal();
|
|
101 |
if (foreignObjects == null) {
|
|
102 |
foreignObjects = new ArrayList<>();
|
|
103 |
}
|
|
104 |
foreignObjects.add(obj);
|
|
105 |
}
|
|
106 |
|
|
107 |
@VMEntryPoint
|
|
108 |
@Override
|
|
109 |
public void close() {
|
|
110 |
if (CURRENT.get() != this) {
|
|
111 |
throw new IllegalStateException("Cannot close non-active scope");
|
|
112 |
}
|
|
113 |
if (foreignObjects != null) {
|
|
114 |
for (IndirectHotSpotObjectConstantImpl obj : foreignObjects) {
|
|
115 |
obj.clear(localScopeDescription);
|
|
116 |
}
|
|
117 |
foreignObjects = null;
|
|
118 |
}
|
|
119 |
CURRENT.set(parent);
|
|
120 |
}
|
|
121 |
}
|