|
9 | 9 | package scala.concurrent.impl;
|
10 | 10 |
|
11 | 11 |
|
12 |
| -import scala.concurrent.util.Unsafe; |
13 |
| -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; |
| 12 | +import java.util.concurrent.atomic.AtomicReference; |
14 | 13 |
|
15 | 14 |
|
16 | 15 |
|
17 |
| -abstract class AbstractPromise { |
18 |
| - private volatile Object _ref; |
19 |
| - |
20 |
| - final static long _refoffset; |
21 |
| - /* |
22 |
| - * In a runtime with a security manager installed, or a non-Oracle runtime, |
23 |
| - * the Unsafe class may be unavailable. In this case, _refoffset will be |
24 |
| - * set to -1. |
25 |
| - * |
26 |
| - * We assume that a valid field offset will be non-negative, and likely a |
27 |
| - * multiple of 4. |
28 |
| - */ |
29 |
| - static { |
30 |
| - long refoffset = -1; |
31 |
| - try { |
32 |
| - refoffset = Unsafe.instance.objectFieldOffset(AbstractPromise.class.getDeclaredField("_ref")); |
33 |
| - } catch (Throwable t) { |
34 |
| - /* Unsafe object instance access failed */ |
35 |
| - } |
36 |
| - _refoffset = refoffset; |
37 |
| - } |
| 16 | +abstract class AbstractPromise extends AtomicReference<Object> { |
38 | 17 |
|
39 |
| - /* |
40 |
| - * The Hotspot compiler can identify that the value of _refoffset never |
41 |
| - * changes, and compile only the branch below that is used. When the Unsafe |
42 |
| - * object is available, the compare-and-swap operation will be inlined |
43 |
| - * directly into *this* method's caller. |
44 |
| - */ |
45 | 18 | protected final boolean updateState(Object oldState, Object newState) {
|
46 |
| - if (_refoffset == -1) { |
47 |
| - return updater.compareAndSet(this, oldState, newState); |
48 |
| - } else { |
49 |
| - return Unsafe.instance.compareAndSwapObject(this, _refoffset, oldState, newState); |
50 |
| - } |
| 19 | + return compareAndSet(oldState, newState); |
51 | 20 | }
|
52 | 21 |
|
53 | 22 | protected final Object getState() {
|
54 |
| - return _ref; |
| 23 | + return get(); |
55 | 24 | }
|
56 |
| - |
57 |
| - protected final static AtomicReferenceFieldUpdater<AbstractPromise, Object> updater = |
58 |
| - AtomicReferenceFieldUpdater.newUpdater(AbstractPromise.class, Object.class, "_ref"); |
59 | 25 | }
|
0 commit comments