src/jdk/internal/dynalink/DynamicLinkerFactory.java

Thu, 31 Aug 2017 15:30:47 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:30:47 +0800
changeset 952
6d5471a497fb
parent 494
3c13fba4d727
parent 0
b1a7da25b547
child 1205
4112748288bb
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 /*
aoqi@0 27 * This file is available under and governed by the GNU General Public
aoqi@0 28 * License version 2 only, as published by the Free Software Foundation.
aoqi@0 29 * However, the following notice accompanied the original version of this
aoqi@0 30 * file, and Oracle licenses the original version of this file under the BSD
aoqi@0 31 * license:
aoqi@0 32 */
aoqi@0 33 /*
aoqi@0 34 Copyright 2009-2013 Attila Szegedi
aoqi@0 35
aoqi@0 36 Licensed under both the Apache License, Version 2.0 (the "Apache License")
aoqi@0 37 and the BSD License (the "BSD License"), with licensee being free to
aoqi@0 38 choose either of the two at their discretion.
aoqi@0 39
aoqi@0 40 You may not use this file except in compliance with either the Apache
aoqi@0 41 License or the BSD License.
aoqi@0 42
aoqi@0 43 If you choose to use this file in compliance with the Apache License, the
aoqi@0 44 following notice applies to you:
aoqi@0 45
aoqi@0 46 You may obtain a copy of the Apache License at
aoqi@0 47
aoqi@0 48 http://www.apache.org/licenses/LICENSE-2.0
aoqi@0 49
aoqi@0 50 Unless required by applicable law or agreed to in writing, software
aoqi@0 51 distributed under the License is distributed on an "AS IS" BASIS,
aoqi@0 52 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
aoqi@0 53 implied. See the License for the specific language governing
aoqi@0 54 permissions and limitations under the License.
aoqi@0 55
aoqi@0 56 If you choose to use this file in compliance with the BSD License, the
aoqi@0 57 following notice applies to you:
aoqi@0 58
aoqi@0 59 Redistribution and use in source and binary forms, with or without
aoqi@0 60 modification, are permitted provided that the following conditions are
aoqi@0 61 met:
aoqi@0 62 * Redistributions of source code must retain the above copyright
aoqi@0 63 notice, this list of conditions and the following disclaimer.
aoqi@0 64 * Redistributions in binary form must reproduce the above copyright
aoqi@0 65 notice, this list of conditions and the following disclaimer in the
aoqi@0 66 documentation and/or other materials provided with the distribution.
aoqi@0 67 * Neither the name of the copyright holder nor the names of
aoqi@0 68 contributors may be used to endorse or promote products derived from
aoqi@0 69 this software without specific prior written permission.
aoqi@0 70
aoqi@0 71 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
aoqi@0 72 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
aoqi@0 73 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
aoqi@0 74 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
aoqi@0 75 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
aoqi@0 76 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
aoqi@0 77 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
aoqi@0 78 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
aoqi@0 79 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
aoqi@0 80 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
aoqi@0 81 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
aoqi@0 82 */
aoqi@0 83
aoqi@0 84 package jdk.internal.dynalink;
aoqi@0 85
aoqi@0 86 import java.lang.invoke.MutableCallSite;
aoqi@0 87 import java.security.AccessController;
aoqi@0 88 import java.security.PrivilegedAction;
aoqi@0 89 import java.util.ArrayList;
aoqi@0 90 import java.util.Arrays;
aoqi@0 91 import java.util.Collections;
aoqi@0 92 import java.util.HashSet;
aoqi@0 93 import java.util.LinkedList;
aoqi@0 94 import java.util.List;
aoqi@0 95 import java.util.Set;
aoqi@0 96 import jdk.internal.dynalink.beans.BeansLinker;
aoqi@0 97 import jdk.internal.dynalink.linker.GuardingDynamicLinker;
aoqi@0 98 import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
aoqi@0 99 import jdk.internal.dynalink.linker.LinkRequest;
aoqi@0 100 import jdk.internal.dynalink.support.AutoDiscovery;
aoqi@0 101 import jdk.internal.dynalink.support.BottomGuardingDynamicLinker;
aoqi@0 102 import jdk.internal.dynalink.support.ClassLoaderGetterContextProvider;
aoqi@0 103 import jdk.internal.dynalink.support.CompositeGuardingDynamicLinker;
aoqi@0 104 import jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker;
aoqi@0 105 import jdk.internal.dynalink.support.LinkerServicesImpl;
aoqi@0 106 import jdk.internal.dynalink.support.TypeConverterFactory;
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * A factory class for creating {@link DynamicLinker}s. The most usual dynamic linker is a linker that is a composition
aoqi@0 110 * of all {@link GuardingDynamicLinker}s known and pre-created by the caller as well as any
aoqi@0 111 * {@link AutoDiscovery automatically discovered} guarding linkers and the standard fallback {@link BeansLinker}. See
aoqi@0 112 * {@link DynamicLinker} documentation for tips on how to use this class.
aoqi@0 113 *
aoqi@0 114 * @author Attila Szegedi
aoqi@0 115 */
aoqi@0 116 public class DynamicLinkerFactory {
aoqi@0 117
aoqi@0 118 /**
aoqi@0 119 * Default value for {@link #setUnstableRelinkThreshold(int) unstable relink threshold}.
aoqi@0 120 */
aoqi@0 121 public static final int DEFAULT_UNSTABLE_RELINK_THRESHOLD = 8;
aoqi@0 122
aoqi@0 123 private boolean classLoaderExplicitlySet = false;
aoqi@0 124 private ClassLoader classLoader;
aoqi@0 125
aoqi@0 126 private List<? extends GuardingDynamicLinker> prioritizedLinkers;
aoqi@0 127 private List<? extends GuardingDynamicLinker> fallbackLinkers;
aoqi@0 128 private int runtimeContextArgCount = 0;
aoqi@0 129 private boolean syncOnRelink = false;
aoqi@0 130 private int unstableRelinkThreshold = DEFAULT_UNSTABLE_RELINK_THRESHOLD;
aoqi@0 131
aoqi@0 132 /**
aoqi@0 133 * Sets the class loader for automatic discovery of available linkers. If not set explicitly, then the thread
aoqi@0 134 * context class loader at the time of {@link #createLinker()} invocation will be used.
aoqi@0 135 *
aoqi@0 136 * @param classLoader the class loader used for the autodiscovery of available linkers.
aoqi@0 137 */
aoqi@0 138 public void setClassLoader(ClassLoader classLoader) {
aoqi@0 139 this.classLoader = classLoader;
aoqi@0 140 classLoaderExplicitlySet = true;
aoqi@0 141 }
aoqi@0 142
aoqi@0 143 /**
aoqi@0 144 * Sets the prioritized linkers. Language runtimes using this framework will usually precreate at least the linker
aoqi@0 145 * for their own language. These linkers will be consulted first in the resulting dynamic linker, before any
aoqi@0 146 * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the prioritized
aoqi@0 147 * linkers, it will be ignored and the explicit prioritized instance will be used.
aoqi@0 148 *
aoqi@0 149 * @param prioritizedLinkers the list of prioritized linkers. Null can be passed to indicate no prioritized linkers
aoqi@0 150 * (this is also the default value).
aoqi@0 151 */
aoqi@0 152 public void setPrioritizedLinkers(List<? extends GuardingDynamicLinker> prioritizedLinkers) {
aoqi@0 153 this.prioritizedLinkers =
aoqi@0 154 prioritizedLinkers == null ? null : new ArrayList<>(prioritizedLinkers);
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 /**
aoqi@0 158 * Sets the prioritized linkers. Language runtimes using this framework will usually precreate at least the linker
aoqi@0 159 * for their own language. These linkers will be consulted first in the resulting dynamic linker, before any
aoqi@0 160 * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the prioritized
aoqi@0 161 * linkers, it will be ignored and the explicit prioritized instance will be used.
aoqi@0 162 *
aoqi@0 163 * @param prioritizedLinkers a list of prioritized linkers.
aoqi@0 164 */
aoqi@0 165 public void setPrioritizedLinkers(GuardingDynamicLinker... prioritizedLinkers) {
aoqi@0 166 setPrioritizedLinkers(Arrays.asList(prioritizedLinkers));
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 /**
aoqi@0 170 * Sets a single prioritized linker. Identical to calling {@link #setPrioritizedLinkers(List)} with a single-element
aoqi@0 171 * list.
aoqi@0 172 *
aoqi@0 173 * @param prioritizedLinker the single prioritized linker. Must not be null.
aoqi@0 174 * @throws IllegalArgumentException if null is passed.
aoqi@0 175 */
aoqi@0 176 public void setPrioritizedLinker(GuardingDynamicLinker prioritizedLinker) {
aoqi@0 177 if(prioritizedLinker == null) {
aoqi@0 178 throw new IllegalArgumentException("prioritizedLinker == null");
aoqi@0 179 }
aoqi@0 180 this.prioritizedLinkers = Collections.singletonList(prioritizedLinker);
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 /**
aoqi@0 184 * Sets the fallback linkers. These linkers will be consulted last in the resulting composite linker, after any
aoqi@0 185 * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the fallback
aoqi@0 186 * linkers, it will be ignored and the explicit fallback instance will be used.
aoqi@0 187 *
aoqi@0 188 * @param fallbackLinkers the list of fallback linkers. Can be empty to indicate the caller wishes to set no
aoqi@0 189 * fallback linkers.
aoqi@0 190 */
aoqi@0 191 public void setFallbackLinkers(List<? extends GuardingDynamicLinker> fallbackLinkers) {
aoqi@0 192 this.fallbackLinkers = fallbackLinkers == null ? null : new ArrayList<>(fallbackLinkers);
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 /**
aoqi@0 196 * Sets the fallback linkers. These linkers will be consulted last in the resulting composite linker, after any
aoqi@0 197 * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the fallback
aoqi@0 198 * linkers, it will be ignored and the explicit fallback instance will be used.
aoqi@0 199 *
aoqi@0 200 * @param fallbackLinkers the list of fallback linkers. Can be empty to indicate the caller wishes to set no
aoqi@0 201 * fallback linkers. If it is left as null, the standard fallback {@link BeansLinker} will be used.
aoqi@0 202 */
aoqi@0 203 public void setFallbackLinkers(GuardingDynamicLinker... fallbackLinkers) {
aoqi@0 204 setFallbackLinkers(Arrays.asList(fallbackLinkers));
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 /**
aoqi@0 208 * Sets the number of arguments in the call sites that represent the stack context of the language runtime creating
aoqi@0 209 * the linker. If the language runtime uses no context information passed on stack, then it should be zero
aoqi@0 210 * (the default value). If it is set to nonzero value, then every dynamic call site emitted by this runtime must
aoqi@0 211 * have the argument list of the form: {@code (this, contextArg1[, contextArg2[, ...]], normalArgs)}. It is
aoqi@0 212 * advisable to only pass one context-specific argument, though, of an easily recognizable, runtime specific type
aoqi@0 213 * encapsulating the runtime thread local state.
aoqi@0 214 *
aoqi@0 215 * @param runtimeContextArgCount the number of language runtime context arguments in call sites.
aoqi@0 216 */
aoqi@0 217 public void setRuntimeContextArgCount(int runtimeContextArgCount) {
aoqi@0 218 if(runtimeContextArgCount < 0) {
aoqi@0 219 throw new IllegalArgumentException("runtimeContextArgCount < 0");
aoqi@0 220 }
aoqi@0 221 this.runtimeContextArgCount = runtimeContextArgCount;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 /**
aoqi@0 225 * Sets whether the linker created by this factory will invoke {@link MutableCallSite#syncAll(MutableCallSite[])}
aoqi@0 226 * after a call site is relinked. Defaults to false. You probably want to set it to true if your runtime supports
aoqi@0 227 * multithreaded execution of dynamically linked code.
aoqi@0 228 * @param syncOnRelink true for invoking sync on relink, false otherwise.
aoqi@0 229 */
aoqi@0 230 public void setSyncOnRelink(boolean syncOnRelink) {
aoqi@0 231 this.syncOnRelink = syncOnRelink;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 /**
aoqi@0 235 * Sets the unstable relink threshold; the number of times a call site is relinked after which it will be
aoqi@0 236 * considered unstable, and subsequent link requests for it will indicate this.
aoqi@0 237 * @param unstableRelinkThreshold the new threshold. Must not be less than zero. The value of zero means that
aoqi@0 238 * call sites will never be considered unstable.
aoqi@0 239 * @see LinkRequest#isCallSiteUnstable()
aoqi@0 240 */
aoqi@0 241 public void setUnstableRelinkThreshold(int unstableRelinkThreshold) {
aoqi@0 242 if(unstableRelinkThreshold < 0) {
aoqi@0 243 throw new IllegalArgumentException("unstableRelinkThreshold < 0");
aoqi@0 244 }
aoqi@0 245 this.unstableRelinkThreshold = unstableRelinkThreshold;
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 /**
aoqi@0 249 * Creates a new dynamic linker consisting of all the prioritized, autodiscovered, and fallback linkers.
aoqi@0 250 *
aoqi@0 251 * @return the new dynamic Linker
aoqi@0 252 */
aoqi@0 253 public DynamicLinker createLinker() {
aoqi@0 254 // Treat nulls appropriately
aoqi@0 255 if(prioritizedLinkers == null) {
aoqi@0 256 prioritizedLinkers = Collections.emptyList();
aoqi@0 257 }
aoqi@0 258 if(fallbackLinkers == null) {
aoqi@0 259 fallbackLinkers = Collections.singletonList(new BeansLinker());
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 // Gather classes of all precreated (prioritized and fallback) linkers.
aoqi@0 263 // We'll filter out any discovered linkers of the same class.
aoqi@0 264 final Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses =
aoqi@0 265 new HashSet<>();
aoqi@0 266 addClasses(knownLinkerClasses, prioritizedLinkers);
aoqi@0 267 addClasses(knownLinkerClasses, fallbackLinkers);
aoqi@0 268
aoqi@0 269 final ClassLoader effectiveClassLoader = classLoaderExplicitlySet ? classLoader : getThreadContextClassLoader();
aoqi@0 270 final List<GuardingDynamicLinker> discovered = AutoDiscovery.loadLinkers(effectiveClassLoader);
aoqi@0 271 // Now, concatenate ...
aoqi@0 272 final List<GuardingDynamicLinker> linkers =
aoqi@0 273 new ArrayList<>(prioritizedLinkers.size() + discovered.size()
aoqi@0 274 + fallbackLinkers.size());
aoqi@0 275 // ... prioritized linkers, ...
aoqi@0 276 linkers.addAll(prioritizedLinkers);
aoqi@0 277 // ... filtered discovered linkers, ...
aoqi@0 278 for(GuardingDynamicLinker linker: discovered) {
aoqi@0 279 if(!knownLinkerClasses.contains(linker.getClass())) {
aoqi@0 280 linkers.add(linker);
aoqi@0 281 }
aoqi@0 282 }
aoqi@0 283 // ... and finally fallback linkers.
aoqi@0 284 linkers.addAll(fallbackLinkers);
aoqi@0 285 final List<GuardingDynamicLinker> optimized = CompositeTypeBasedGuardingDynamicLinker.optimize(linkers);
aoqi@0 286 final GuardingDynamicLinker composite;
aoqi@0 287 switch(linkers.size()) {
aoqi@0 288 case 0: {
aoqi@0 289 composite = BottomGuardingDynamicLinker.INSTANCE;
aoqi@0 290 break;
aoqi@0 291 }
aoqi@0 292 case 1: {
aoqi@0 293 composite = optimized.get(0);
aoqi@0 294 break;
aoqi@0 295 }
aoqi@0 296 default: {
aoqi@0 297 composite = new CompositeGuardingDynamicLinker(optimized);
aoqi@0 298 break;
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 final List<GuardingTypeConverterFactory> typeConverters = new LinkedList<>();
aoqi@0 303 for(GuardingDynamicLinker linker: linkers) {
aoqi@0 304 if(linker instanceof GuardingTypeConverterFactory) {
aoqi@0 305 typeConverters.add((GuardingTypeConverterFactory)linker);
aoqi@0 306 }
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 return new DynamicLinker(new LinkerServicesImpl(new TypeConverterFactory(typeConverters), composite),
aoqi@0 310 runtimeContextArgCount, syncOnRelink, unstableRelinkThreshold);
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 private static ClassLoader getThreadContextClassLoader() {
aoqi@0 314 return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
aoqi@0 315 @Override
aoqi@0 316 public ClassLoader run() {
aoqi@0 317 return Thread.currentThread().getContextClassLoader();
aoqi@0 318 }
aoqi@0 319 }, ClassLoaderGetterContextProvider.GET_CLASS_LOADER_CONTEXT);
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 private static void addClasses(Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses,
aoqi@0 323 List<? extends GuardingDynamicLinker> linkers) {
aoqi@0 324 for(GuardingDynamicLinker linker: linkers) {
aoqi@0 325 knownLinkerClasses.add(linker.getClass());
aoqi@0 326 }
aoqi@0 327 }
aoqi@0 328 }

mercurial