src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.options/src/org/graalvm/compiler/options/ModifiableOptionValues.java
author iveresov
Fri, 02 Feb 2018 17:28:17 -0800
changeset 48861 47f19ff9903c
parent 47216 71c04702a3d5
child 49873 26ebfe8ce852
permissions -rw-r--r--
8194819: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46371
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     1
/*
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     4
 *
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     8
 *
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    13
 * accompanied this code).
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    14
 *
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    18
 *
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    21
 * questions.
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    22
 */
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    23
package org.graalvm.compiler.options;
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    24
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    25
import java.util.concurrent.atomic.AtomicReference;
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    26
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    27
import org.graalvm.collections.EconomicMap;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    28
import org.graalvm.collections.Equivalence;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    29
import org.graalvm.collections.UnmodifiableEconomicMap;
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    30
import org.graalvm.collections.UnmodifiableMapCursor;
46371
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    31
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    32
/**
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    33
 * A context for obtaining values for {@link OptionKey}s that allows for key/value pairs to be
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    34
 * updated. Updates have atomic copy-on-write semantics which means a thread may see an old value
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    35
 * when reading but writers will never loose updates.
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    36
 */
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    37
public class ModifiableOptionValues extends OptionValues {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    38
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    39
    private final AtomicReference<UnmodifiableEconomicMap<OptionKey<?>, Object>> v = new AtomicReference<>();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    40
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    41
    private static final EconomicMap<OptionKey<?>, Object> EMPTY_MAP = newOptionMap();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    42
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    43
    public ModifiableOptionValues(UnmodifiableEconomicMap<OptionKey<?>, Object> values) {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    44
        super(EMPTY_MAP);
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    45
        EconomicMap<OptionKey<?>, Object> map = newOptionMap();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    46
        initMap(map, values);
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    47
        v.set(map);
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    48
    }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    49
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    50
    /**
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    51
     * Value that can be used in {@link #update(UnmodifiableEconomicMap)} and
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    52
     * {@link #update(OptionKey, Object)} to remove an explicitly set value for a key such that
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    53
     * {@link OptionKey#hasBeenSet(OptionValues)} will return {@code false} for the key.
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    54
     */
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    55
    public static final Object UNSET_KEY = new Object();
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    56
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    57
    /**
46371
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    58
     * Updates this object with the given key/value pair.
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    59
     *
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    60
     * @see #UNSET_KEY
46371
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    61
     */
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    62
    public void update(OptionKey<?> key, Object value) {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    63
        UnmodifiableEconomicMap<OptionKey<?>, Object> expect;
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    64
        EconomicMap<OptionKey<?>, Object> newMap;
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    65
        do {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    66
            expect = v.get();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    67
            newMap = EconomicMap.create(Equivalence.IDENTITY, expect);
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    68
            if (value == UNSET_KEY) {
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    69
                newMap.removeKey(key);
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    70
            } else {
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    71
                key.update(newMap, value);
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    72
                // Need to do the null encoding here as `key.update()` doesn't do it
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    73
                newMap.put(key, encodeNull(value));
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    74
            }
46371
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    75
        } while (!v.compareAndSet(expect, newMap));
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    76
    }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    77
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    78
    /**
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    79
     * Updates this object with the key/value pairs in {@code values}.
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    80
     *
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    81
     * @see #UNSET_KEY
46371
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    82
     */
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    83
    public void update(UnmodifiableEconomicMap<OptionKey<?>, Object> values) {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    84
        if (values.isEmpty()) {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    85
            return;
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    86
        }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    87
        UnmodifiableEconomicMap<OptionKey<?>, Object> expect;
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    88
        EconomicMap<OptionKey<?>, Object> newMap;
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    89
        do {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    90
            expect = v.get();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    91
            newMap = EconomicMap.create(Equivalence.IDENTITY, expect);
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    92
            UnmodifiableMapCursor<OptionKey<?>, Object> cursor = values.getEntries();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    93
            while (cursor.advance()) {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    94
                OptionKey<?> key = cursor.getKey();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
    95
                Object value = cursor.getValue();
46762
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    96
                if (value == UNSET_KEY) {
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    97
                    newMap.removeKey(key);
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    98
                } else {
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
    99
                    key.update(newMap, value);
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
   100
                    // Need to do the null encoding here as `key.update()` doesn't do it
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
   101
                    newMap.put(key, encodeNull(value));
f7defa99f173 8185829: Update Graal
dlong
parents: 46371
diff changeset
   102
                }
46371
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   103
            }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   104
        } while (!v.compareAndSet(expect, newMap));
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   105
    }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   106
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   107
    @Override
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   108
    protected <T> T get(OptionKey<T> key) {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   109
        return OptionValues.get(v.get(), key);
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   110
    }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   111
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   112
    @Override
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   113
    protected boolean containsKey(OptionKey<?> key) {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   114
        return v.get().containsKey(key);
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   115
    }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   116
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   117
    @Override
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   118
    public UnmodifiableEconomicMap<OptionKey<?>, Object> getMap() {
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   119
        return v.get();
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   120
    }
0337d0617e7b 8178088: Update Graal
iveresov
parents:
diff changeset
   121
}