Mercurial > hg > truffle
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/CyclicAssumption.java @ 19706:dc7189b2bb5f
CyclicAssumption: Use an AtomicReference to ensure invalidation.
author | Benoit Daloze <benoit.daloze@jku.at> |
---|---|
date | Mon, 09 Mar 2015 14:22:15 +0100 |
parents | f28ea693056f |
children |
comparison
equal
deleted
inserted
replaced
19705:2f5d4df2af90 | 19706:dc7189b2bb5f |
---|---|
22 * or visit www.oracle.com if you need additional information or have any | 22 * or visit www.oracle.com if you need additional information or have any |
23 * questions. | 23 * questions. |
24 */ | 24 */ |
25 package com.oracle.truffle.api.utilities; | 25 package com.oracle.truffle.api.utilities; |
26 | 26 |
27 import java.util.concurrent.atomic.*; | |
28 | |
27 import com.oracle.truffle.api.*; | 29 import com.oracle.truffle.api.*; |
28 | 30 |
29 /** | 31 /** |
30 * Holds an {@link Assumption}, and knows how to recreate it with the same properties on | 32 * Holds an {@link Assumption}, and knows how to recreate it with the same properties on |
31 * invalidation. Used so that mutability is isolated in this class, and all other classes that need | 33 * invalidation. Used so that mutability is isolated in this class, and all other classes that need |
34 * same way that you would with any other assumption. | 36 * same way that you would with any other assumption. |
35 */ | 37 */ |
36 public class CyclicAssumption { | 38 public class CyclicAssumption { |
37 | 39 |
38 private final String name; | 40 private final String name; |
39 private Assumption assumption; | 41 private final AtomicReference<Assumption> assumption; |
40 | 42 |
41 public CyclicAssumption(String name) { | 43 public CyclicAssumption(String name) { |
42 this.name = name; | 44 this.name = name; |
43 invalidate(); | 45 this.assumption = new AtomicReference<>(Truffle.getRuntime().createAssumption(name)); |
44 } | 46 } |
45 | 47 |
46 public void invalidate() { | 48 public void invalidate() { |
47 if (assumption != null) { | 49 Assumption newAssumption = Truffle.getRuntime().createAssumption(name); |
48 assumption.invalidate(); | 50 Assumption oldAssumption = assumption.getAndSet(newAssumption); |
49 } | 51 oldAssumption.invalidate(); |
50 | |
51 assumption = Truffle.getRuntime().createAssumption(name); | |
52 } | 52 } |
53 | 53 |
54 public Assumption getAssumption() { | 54 public Assumption getAssumption() { |
55 return assumption; | 55 return assumption.get(); |
56 } | 56 } |
57 | 57 |
58 } | 58 } |