comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/AssumedValue.java @ 13325:f28ea693056f

New assumption utilities.
author Chris Seaton <chris.seaton@oracle.com>
date Fri, 13 Dec 2013 15:53:30 +0000
parents
children 0e7894989f37
comparison
equal deleted inserted replaced
13324:e585ac5a385d 13325:f28ea693056f
1 /*
2 * Copyright (c) 2013, 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 package com.oracle.truffle.api.utilities;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
29 import com.oracle.truffle.api.nodes.*;
30
31 /**
32 * A {@link CompilationFinal} value combined with an {@link Assumption} to notify when it changes.
33 * Note that you should be careful that modifications to this value do not cause deoptimization
34 * loops. This could be by using a value that is monotonic.
35 */
36 public class AssumedValue<T> {
37
38 @CompilationFinal private T value;
39 private final CyclicAssumption assumption;
40
41 public AssumedValue(String name, T initialValue) {
42 assumption = new CyclicAssumption(name);
43 value = initialValue;
44 }
45
46 /**
47 * Get the current value, updating it if it has been {@link #set}. The compiler may be able to
48 * make this method return a constant value, but still accommodate mutation.
49 */
50 public T get() {
51 try {
52 assumption.getAssumption().check();
53 } catch (InvalidAssumptionException e) {
54 // No need to rewrite anything - just pick up the new value
55 }
56
57 return value;
58 }
59
60 /**
61 * Set a new value, which will be picked up the next time {@link #get} is called.
62 */
63 public void set(T newValue) {
64 value = newValue;
65 assumption.invalidate();
66 }
67
68 }