Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/CyclicAssumption.java @ 22342:a1b3b7894611
CyclicAssumption: use AtomicReferenceFieldUpdater for the assumption field
author | Andreas Woess <andreas.woess@oracle.com> |
---|---|
date | Wed, 04 Nov 2015 11:03:08 +0100 |
parents | dc83cc1f94f2 |
children |
comparison
equal
deleted
inserted
replaced
22341:4df3d8fd6cfd | 22342:a1b3b7894611 |
---|---|
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.AtomicReferenceFieldUpdater; | |
28 | |
27 import com.oracle.truffle.api.Assumption; | 29 import com.oracle.truffle.api.Assumption; |
28 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; | 30 import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
29 import com.oracle.truffle.api.Truffle; | 31 import com.oracle.truffle.api.Truffle; |
30 import java.util.concurrent.atomic.AtomicReference; | |
31 | 32 |
32 /** | 33 /** |
33 * Holds an {@link Assumption}, and knows how to recreate it with the same properties on | 34 * Holds an {@link Assumption}, and knows how to recreate it with the same properties on |
34 * invalidation. Used so that mutability is isolated in this class, and all other classes that need | 35 * invalidation. Used so that mutability is isolated in this class, and all other classes that need |
35 * an assumption that may be recreated can have a final reference to an object of this class. Note | 36 * an assumption that may be recreated can have a final reference to an object of this class. Note |
37 * same way that you would with any other assumption. | 38 * same way that you would with any other assumption. |
38 */ | 39 */ |
39 public class CyclicAssumption { | 40 public class CyclicAssumption { |
40 | 41 |
41 private final String name; | 42 private final String name; |
42 private final AtomicReference<Assumption> assumption; | 43 private volatile Assumption assumption; |
44 | |
45 private static final AtomicReferenceFieldUpdater<CyclicAssumption, Assumption> ASSUMPTION_UPDATER = AtomicReferenceFieldUpdater.newUpdater(CyclicAssumption.class, Assumption.class, "assumption"); | |
43 | 46 |
44 public CyclicAssumption(String name) { | 47 public CyclicAssumption(String name) { |
45 this.name = name; | 48 this.name = name; |
46 this.assumption = new AtomicReference<>(Truffle.getRuntime().createAssumption(name)); | 49 this.assumption = Truffle.getRuntime().createAssumption(name); |
47 } | 50 } |
48 | 51 |
49 @TruffleBoundary | 52 @TruffleBoundary |
50 public void invalidate() { | 53 public void invalidate() { |
51 Assumption newAssumption = Truffle.getRuntime().createAssumption(name); | 54 Assumption newAssumption = Truffle.getRuntime().createAssumption(name); |
52 Assumption oldAssumption = assumption.getAndSet(newAssumption); | 55 Assumption oldAssumption = ASSUMPTION_UPDATER.getAndSet(this, newAssumption); |
53 oldAssumption.invalidate(); | 56 oldAssumption.invalidate(); |
54 } | 57 } |
55 | 58 |
56 public Assumption getAssumption() { | 59 public Assumption getAssumption() { |
57 return assumption.get(); | 60 return assumption; |
58 } | 61 } |
59 | 62 |
60 } | 63 } |