|
1 /* |
|
2 * Copyright (c) 2010, 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 |
|
26 package java.dyn; |
|
27 |
|
28 import java.util.WeakHashMap; |
|
29 import java.util.concurrent.atomic.AtomicInteger; |
|
30 import java.util.concurrent.atomic.AtomicReference; |
|
31 |
|
32 /** |
|
33 * Lazily associate a computed value with (potentially) every class. |
|
34 * @author John Rose, JSR 292 EG |
|
35 */ |
|
36 public abstract class ClassValue<T> { |
|
37 /** |
|
38 * Compute the given class's derived value for this {@code ClassValue}. |
|
39 * <p> |
|
40 * This method will be invoked within the first thread that accesses |
|
41 * the value with the {@link #get}. |
|
42 * <p> |
|
43 * Normally, this method is invoked at most once per class, |
|
44 * but it may be invoked again in case of subsequent invocations |
|
45 * of {@link #remove} followed by {@link #get}. |
|
46 * |
|
47 * @return the computed value for this thread-local |
|
48 */ |
|
49 protected abstract T computeValue(Class<?> type); |
|
50 |
|
51 /** |
|
52 * Creates a new class value. |
|
53 */ |
|
54 protected ClassValue() { |
|
55 } |
|
56 |
|
57 /** |
|
58 * Returns the value for the given class. |
|
59 * If no value has yet been computed, it is obtained by |
|
60 * by an invocation of the {@link #computeValue} method. |
|
61 * <p> |
|
62 * The actual installation of the value on the class |
|
63 * is performed while the class's synchronization lock |
|
64 * is held. At that point, if racing threads have |
|
65 * computed values, one is chosen, and returned to |
|
66 * all the racing threads. |
|
67 * |
|
68 * @return the current thread's value of this thread-local |
|
69 */ |
|
70 public T get(Class<?> type) { |
|
71 ClassValueMap map = getMap(type); |
|
72 if (map != null) { |
|
73 Object x = map.get(this); |
|
74 if (x != null) { |
|
75 return (T) map.unmaskNull(x); |
|
76 } |
|
77 } |
|
78 return setComputedValue(type); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Removes the associated value for the given class. |
|
83 * If this value is subsequently {@linkplain #get read} for the same class, |
|
84 * its value will be reinitialized by invoking its {@link #computeValue} method. |
|
85 * This may result in an additional invocation of the |
|
86 * {@code computeValue} method for the given class. |
|
87 */ |
|
88 public void remove(Class<?> type) { |
|
89 ClassValueMap map = getMap(type); |
|
90 if (map != null) { |
|
91 synchronized (map) { |
|
92 map.remove(this); |
|
93 } |
|
94 } |
|
95 } |
|
96 |
|
97 /// Implementation... |
|
98 |
|
99 /** The hash code for this type is based on the identity of the object, |
|
100 * and is well-dispersed for power-of-two tables. |
|
101 */ |
|
102 public final int hashCode() { return hashCode; } |
|
103 private final int hashCode = HASH_CODES.getAndAdd(0x61c88647); |
|
104 private static final AtomicInteger HASH_CODES = new AtomicInteger(); |
|
105 |
|
106 private static final AtomicInteger STORE_BARRIER = new AtomicInteger(); |
|
107 |
|
108 /** Slow path for {@link #get}. */ |
|
109 private T setComputedValue(Class<?> type) { |
|
110 ClassValueMap map = getMap(type); |
|
111 if (map == null) { |
|
112 map = initializeMap(type); |
|
113 } |
|
114 T value = computeValue(type); |
|
115 STORE_BARRIER.lazySet(0); |
|
116 // All stores pending from computeValue are completed. |
|
117 synchronized (map) { |
|
118 // Warm up the table with a null entry. |
|
119 map.preInitializeEntry(this); |
|
120 } |
|
121 // All stores pending from table expansion are completed. |
|
122 synchronized (map) { |
|
123 value = (T) map.initializeEntry(this, value); |
|
124 // One might fear a possible race condition here |
|
125 // if the code for map.put has flushed the write |
|
126 // to map.table[*] before the writes to the Map.Entry |
|
127 // are done. This is not possible, since we have |
|
128 // warmed up the table with an empty entry. |
|
129 } |
|
130 return value; |
|
131 } |
|
132 |
|
133 // Replace this map by a per-class slot. |
|
134 private static final WeakHashMap<Class<?>, ClassValueMap> ROOT |
|
135 = new WeakHashMap<Class<?>, ClassValueMap>(); |
|
136 |
|
137 private static ClassValueMap getMap(Class<?> type) { |
|
138 return ROOT.get(type); |
|
139 } |
|
140 |
|
141 private static ClassValueMap initializeMap(Class<?> type) { |
|
142 synchronized (ClassValue.class) { |
|
143 ClassValueMap map = ROOT.get(type); |
|
144 if (map == null) |
|
145 ROOT.put(type, map = new ClassValueMap()); |
|
146 return map; |
|
147 } |
|
148 } |
|
149 |
|
150 static class ClassValueMap extends WeakHashMap<ClassValue, Object> { |
|
151 /** Make sure this table contains an Entry for the given key, even if it is empty. */ |
|
152 void preInitializeEntry(ClassValue key) { |
|
153 if (!this.containsKey(key)) |
|
154 this.put(key, null); |
|
155 } |
|
156 /** Make sure this table contains a non-empty Entry for the given key. */ |
|
157 Object initializeEntry(ClassValue key, Object value) { |
|
158 Object prior = this.get(key); |
|
159 if (prior != null) { |
|
160 return unmaskNull(prior); |
|
161 } |
|
162 this.put(key, maskNull(value)); |
|
163 return value; |
|
164 } |
|
165 |
|
166 Object maskNull(Object x) { |
|
167 return x == null ? this : x; |
|
168 } |
|
169 Object unmaskNull(Object x) { |
|
170 return x == this ? null : x; |
|
171 } |
|
172 } |
|
173 } |