src/jdk/internal/dynalink/DynamicLinkerFactory.java

changeset 0
b1a7da25b547
child 952
6d5471a497fb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/jdk/internal/dynalink/DynamicLinkerFactory.java	Wed Apr 27 01:36:41 2016 +0800
     1.3 @@ -0,0 +1,328 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * This file is available under and governed by the GNU General Public
    1.31 + * License version 2 only, as published by the Free Software Foundation.
    1.32 + * However, the following notice accompanied the original version of this
    1.33 + * file, and Oracle licenses the original version of this file under the BSD
    1.34 + * license:
    1.35 + */
    1.36 +/*
    1.37 +   Copyright 2009-2013 Attila Szegedi
    1.38 +
    1.39 +   Licensed under both the Apache License, Version 2.0 (the "Apache License")
    1.40 +   and the BSD License (the "BSD License"), with licensee being free to
    1.41 +   choose either of the two at their discretion.
    1.42 +
    1.43 +   You may not use this file except in compliance with either the Apache
    1.44 +   License or the BSD License.
    1.45 +
    1.46 +   If you choose to use this file in compliance with the Apache License, the
    1.47 +   following notice applies to you:
    1.48 +
    1.49 +       You may obtain a copy of the Apache License at
    1.50 +
    1.51 +           http://www.apache.org/licenses/LICENSE-2.0
    1.52 +
    1.53 +       Unless required by applicable law or agreed to in writing, software
    1.54 +       distributed under the License is distributed on an "AS IS" BASIS,
    1.55 +       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    1.56 +       implied. See the License for the specific language governing
    1.57 +       permissions and limitations under the License.
    1.58 +
    1.59 +   If you choose to use this file in compliance with the BSD License, the
    1.60 +   following notice applies to you:
    1.61 +
    1.62 +       Redistribution and use in source and binary forms, with or without
    1.63 +       modification, are permitted provided that the following conditions are
    1.64 +       met:
    1.65 +       * Redistributions of source code must retain the above copyright
    1.66 +         notice, this list of conditions and the following disclaimer.
    1.67 +       * Redistributions in binary form must reproduce the above copyright
    1.68 +         notice, this list of conditions and the following disclaimer in the
    1.69 +         documentation and/or other materials provided with the distribution.
    1.70 +       * Neither the name of the copyright holder nor the names of
    1.71 +         contributors may be used to endorse or promote products derived from
    1.72 +         this software without specific prior written permission.
    1.73 +
    1.74 +       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
    1.75 +       IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    1.76 +       TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    1.77 +       PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
    1.78 +       BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.79 +       CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.80 +       SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
    1.81 +       BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    1.82 +       WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    1.83 +       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    1.84 +       ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.85 +*/
    1.86 +
    1.87 +package jdk.internal.dynalink;
    1.88 +
    1.89 +import java.lang.invoke.MutableCallSite;
    1.90 +import java.security.AccessController;
    1.91 +import java.security.PrivilegedAction;
    1.92 +import java.util.ArrayList;
    1.93 +import java.util.Arrays;
    1.94 +import java.util.Collections;
    1.95 +import java.util.HashSet;
    1.96 +import java.util.LinkedList;
    1.97 +import java.util.List;
    1.98 +import java.util.Set;
    1.99 +import jdk.internal.dynalink.beans.BeansLinker;
   1.100 +import jdk.internal.dynalink.linker.GuardingDynamicLinker;
   1.101 +import jdk.internal.dynalink.linker.GuardingTypeConverterFactory;
   1.102 +import jdk.internal.dynalink.linker.LinkRequest;
   1.103 +import jdk.internal.dynalink.support.AutoDiscovery;
   1.104 +import jdk.internal.dynalink.support.BottomGuardingDynamicLinker;
   1.105 +import jdk.internal.dynalink.support.ClassLoaderGetterContextProvider;
   1.106 +import jdk.internal.dynalink.support.CompositeGuardingDynamicLinker;
   1.107 +import jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker;
   1.108 +import jdk.internal.dynalink.support.LinkerServicesImpl;
   1.109 +import jdk.internal.dynalink.support.TypeConverterFactory;
   1.110 +
   1.111 +/**
   1.112 + * A factory class for creating {@link DynamicLinker}s. The most usual dynamic linker is a linker that is a composition
   1.113 + * of all {@link GuardingDynamicLinker}s known and pre-created by the caller as well as any
   1.114 + * {@link AutoDiscovery automatically discovered} guarding linkers and the standard fallback {@link BeansLinker}. See
   1.115 + * {@link DynamicLinker} documentation for tips on how to use this class.
   1.116 + *
   1.117 + * @author Attila Szegedi
   1.118 + */
   1.119 +public class DynamicLinkerFactory {
   1.120 +
   1.121 +    /**
   1.122 +     * Default value for {@link #setUnstableRelinkThreshold(int) unstable relink threshold}.
   1.123 +     */
   1.124 +    public static final int DEFAULT_UNSTABLE_RELINK_THRESHOLD = 8;
   1.125 +
   1.126 +    private boolean classLoaderExplicitlySet = false;
   1.127 +    private ClassLoader classLoader;
   1.128 +
   1.129 +    private List<? extends GuardingDynamicLinker> prioritizedLinkers;
   1.130 +    private List<? extends GuardingDynamicLinker> fallbackLinkers;
   1.131 +    private int runtimeContextArgCount = 0;
   1.132 +    private boolean syncOnRelink = false;
   1.133 +    private int unstableRelinkThreshold = DEFAULT_UNSTABLE_RELINK_THRESHOLD;
   1.134 +
   1.135 +    /**
   1.136 +     * Sets the class loader for automatic discovery of available linkers. If not set explicitly, then the thread
   1.137 +     * context class loader at the time of {@link #createLinker()} invocation will be used.
   1.138 +     *
   1.139 +     * @param classLoader the class loader used for the autodiscovery of available linkers.
   1.140 +     */
   1.141 +    public void setClassLoader(ClassLoader classLoader) {
   1.142 +        this.classLoader = classLoader;
   1.143 +        classLoaderExplicitlySet = true;
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Sets the prioritized linkers. Language runtimes using this framework will usually precreate at least the linker
   1.148 +     * for their own language. These linkers will be consulted first in the resulting dynamic linker, before any
   1.149 +     * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the prioritized
   1.150 +     * linkers, it will be ignored and the explicit prioritized instance will be used.
   1.151 +     *
   1.152 +     * @param prioritizedLinkers the list of prioritized linkers. Null can be passed to indicate no prioritized linkers
   1.153 +     * (this is also the default value).
   1.154 +     */
   1.155 +    public void setPrioritizedLinkers(List<? extends GuardingDynamicLinker> prioritizedLinkers) {
   1.156 +        this.prioritizedLinkers =
   1.157 +                prioritizedLinkers == null ? null : new ArrayList<>(prioritizedLinkers);
   1.158 +    }
   1.159 +
   1.160 +    /**
   1.161 +     * Sets the prioritized linkers. Language runtimes using this framework will usually precreate at least the linker
   1.162 +     * for their own language. These linkers will be consulted first in the resulting dynamic linker, before any
   1.163 +     * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the prioritized
   1.164 +     * linkers, it will be ignored and the explicit prioritized instance will be used.
   1.165 +     *
   1.166 +     * @param prioritizedLinkers a list of prioritized linkers.
   1.167 +     */
   1.168 +    public void setPrioritizedLinkers(GuardingDynamicLinker... prioritizedLinkers) {
   1.169 +        setPrioritizedLinkers(Arrays.asList(prioritizedLinkers));
   1.170 +    }
   1.171 +
   1.172 +    /**
   1.173 +     * Sets a single prioritized linker. Identical to calling {@link #setPrioritizedLinkers(List)} with a single-element
   1.174 +     * list.
   1.175 +     *
   1.176 +     * @param prioritizedLinker the single prioritized linker. Must not be null.
   1.177 +     * @throws IllegalArgumentException if null is passed.
   1.178 +     */
   1.179 +    public void setPrioritizedLinker(GuardingDynamicLinker prioritizedLinker) {
   1.180 +        if(prioritizedLinker == null) {
   1.181 +            throw new IllegalArgumentException("prioritizedLinker == null");
   1.182 +        }
   1.183 +        this.prioritizedLinkers = Collections.singletonList(prioritizedLinker);
   1.184 +    }
   1.185 +
   1.186 +    /**
   1.187 +     * Sets the fallback linkers. These linkers will be consulted last in the resulting composite linker, after any
   1.188 +     * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the fallback
   1.189 +     * linkers, it will be ignored and the explicit fallback instance will be used.
   1.190 +     *
   1.191 +     * @param fallbackLinkers the list of fallback linkers. Can be empty to indicate the caller wishes to set no
   1.192 +     * fallback linkers.
   1.193 +     */
   1.194 +    public void setFallbackLinkers(List<? extends GuardingDynamicLinker> fallbackLinkers) {
   1.195 +        this.fallbackLinkers = fallbackLinkers == null ? null : new ArrayList<>(fallbackLinkers);
   1.196 +    }
   1.197 +
   1.198 +    /**
   1.199 +     * Sets the fallback linkers. These linkers will be consulted last in the resulting composite linker, after any
   1.200 +     * autodiscovered linkers. If the framework also autodiscovers a linker of the same class as one of the fallback
   1.201 +     * linkers, it will be ignored and the explicit fallback instance will be used.
   1.202 +     *
   1.203 +     * @param fallbackLinkers the list of fallback linkers. Can be empty to indicate the caller wishes to set no
   1.204 +     * fallback linkers. If it is left as null, the standard fallback {@link BeansLinker} will be used.
   1.205 +     */
   1.206 +    public void setFallbackLinkers(GuardingDynamicLinker... fallbackLinkers) {
   1.207 +        setFallbackLinkers(Arrays.asList(fallbackLinkers));
   1.208 +    }
   1.209 +
   1.210 +    /**
   1.211 +     * Sets the number of arguments in the call sites that represent the stack context of the language runtime creating
   1.212 +     * the linker. If the language runtime uses no context information passed on stack, then it should be zero
   1.213 +     * (the default value). If it is set to nonzero value, then every dynamic call site emitted by this runtime must
   1.214 +     * have the argument list of the form: {@code (this, contextArg1[, contextArg2[, ...]], normalArgs)}. It is
   1.215 +     * advisable to only pass one context-specific argument, though, of an easily recognizable, runtime specific type
   1.216 +     * encapsulating the runtime thread local state.
   1.217 +     *
   1.218 +     * @param runtimeContextArgCount the number of language runtime context arguments in call sites.
   1.219 +     */
   1.220 +    public void setRuntimeContextArgCount(int runtimeContextArgCount) {
   1.221 +        if(runtimeContextArgCount < 0) {
   1.222 +            throw new IllegalArgumentException("runtimeContextArgCount < 0");
   1.223 +        }
   1.224 +        this.runtimeContextArgCount = runtimeContextArgCount;
   1.225 +    }
   1.226 +
   1.227 +    /**
   1.228 +     * Sets whether the linker created by this factory will invoke {@link MutableCallSite#syncAll(MutableCallSite[])}
   1.229 +     * after a call site is relinked. Defaults to false. You probably want to set it to true if your runtime supports
   1.230 +     * multithreaded execution of dynamically linked code.
   1.231 +     * @param syncOnRelink true for invoking sync on relink, false otherwise.
   1.232 +     */
   1.233 +    public void setSyncOnRelink(boolean syncOnRelink) {
   1.234 +        this.syncOnRelink = syncOnRelink;
   1.235 +    }
   1.236 +
   1.237 +    /**
   1.238 +     * Sets the unstable relink threshold; the number of times a call site is relinked after which it will be
   1.239 +     * considered unstable, and subsequent link requests for it will indicate this.
   1.240 +     * @param unstableRelinkThreshold the new threshold. Must not be less than zero. The value of zero means that
   1.241 +     * call sites will never be considered unstable.
   1.242 +     * @see LinkRequest#isCallSiteUnstable()
   1.243 +     */
   1.244 +    public void setUnstableRelinkThreshold(int unstableRelinkThreshold) {
   1.245 +        if(unstableRelinkThreshold < 0) {
   1.246 +            throw new IllegalArgumentException("unstableRelinkThreshold < 0");
   1.247 +        }
   1.248 +        this.unstableRelinkThreshold = unstableRelinkThreshold;
   1.249 +    }
   1.250 +
   1.251 +    /**
   1.252 +     * Creates a new dynamic linker consisting of all the prioritized, autodiscovered, and fallback linkers.
   1.253 +     *
   1.254 +     * @return the new dynamic Linker
   1.255 +     */
   1.256 +    public DynamicLinker createLinker() {
   1.257 +        // Treat nulls appropriately
   1.258 +        if(prioritizedLinkers == null) {
   1.259 +            prioritizedLinkers = Collections.emptyList();
   1.260 +        }
   1.261 +        if(fallbackLinkers == null) {
   1.262 +            fallbackLinkers = Collections.singletonList(new BeansLinker());
   1.263 +        }
   1.264 +
   1.265 +        // Gather classes of all precreated (prioritized and fallback) linkers.
   1.266 +        // We'll filter out any discovered linkers of the same class.
   1.267 +        final Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses =
   1.268 +                new HashSet<>();
   1.269 +        addClasses(knownLinkerClasses, prioritizedLinkers);
   1.270 +        addClasses(knownLinkerClasses, fallbackLinkers);
   1.271 +
   1.272 +        final ClassLoader effectiveClassLoader = classLoaderExplicitlySet ? classLoader : getThreadContextClassLoader();
   1.273 +        final List<GuardingDynamicLinker> discovered = AutoDiscovery.loadLinkers(effectiveClassLoader);
   1.274 +        // Now, concatenate ...
   1.275 +        final List<GuardingDynamicLinker> linkers =
   1.276 +                new ArrayList<>(prioritizedLinkers.size() + discovered.size()
   1.277 +                        + fallbackLinkers.size());
   1.278 +        // ... prioritized linkers, ...
   1.279 +        linkers.addAll(prioritizedLinkers);
   1.280 +        // ... filtered discovered linkers, ...
   1.281 +        for(GuardingDynamicLinker linker: discovered) {
   1.282 +            if(!knownLinkerClasses.contains(linker.getClass())) {
   1.283 +                linkers.add(linker);
   1.284 +            }
   1.285 +        }
   1.286 +        // ... and finally fallback linkers.
   1.287 +        linkers.addAll(fallbackLinkers);
   1.288 +        final List<GuardingDynamicLinker> optimized = CompositeTypeBasedGuardingDynamicLinker.optimize(linkers);
   1.289 +        final GuardingDynamicLinker composite;
   1.290 +        switch(linkers.size()) {
   1.291 +            case 0: {
   1.292 +                composite = BottomGuardingDynamicLinker.INSTANCE;
   1.293 +                break;
   1.294 +            }
   1.295 +            case 1: {
   1.296 +                composite = optimized.get(0);
   1.297 +                break;
   1.298 +            }
   1.299 +            default: {
   1.300 +                composite = new CompositeGuardingDynamicLinker(optimized);
   1.301 +                break;
   1.302 +            }
   1.303 +        }
   1.304 +
   1.305 +        final List<GuardingTypeConverterFactory> typeConverters = new LinkedList<>();
   1.306 +        for(GuardingDynamicLinker linker: linkers) {
   1.307 +            if(linker instanceof GuardingTypeConverterFactory) {
   1.308 +                typeConverters.add((GuardingTypeConverterFactory)linker);
   1.309 +            }
   1.310 +        }
   1.311 +
   1.312 +        return new DynamicLinker(new LinkerServicesImpl(new TypeConverterFactory(typeConverters), composite),
   1.313 +                runtimeContextArgCount, syncOnRelink, unstableRelinkThreshold);
   1.314 +    }
   1.315 +
   1.316 +    private static ClassLoader getThreadContextClassLoader() {
   1.317 +        return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
   1.318 +            @Override
   1.319 +            public ClassLoader run() {
   1.320 +                return Thread.currentThread().getContextClassLoader();
   1.321 +            }
   1.322 +        }, ClassLoaderGetterContextProvider.GET_CLASS_LOADER_CONTEXT);
   1.323 +    }
   1.324 +
   1.325 +    private static void addClasses(Set<Class<? extends GuardingDynamicLinker>> knownLinkerClasses,
   1.326 +            List<? extends GuardingDynamicLinker> linkers) {
   1.327 +        for(GuardingDynamicLinker linker: linkers) {
   1.328 +            knownLinkerClasses.add(linker.getClass());
   1.329 +        }
   1.330 +    }
   1.331 +}

mercurial