author | alanb |
Thu, 01 Dec 2016 08:57:53 +0000 | |
changeset 42338 | a60f280f803c |
parent 28059 | e576535359cc |
permissions | -rw-r--r-- |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21330
diff
changeset
|
2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
4 |
* |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
10 |
* |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
15 |
* accompanied this code). |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
16 |
* |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
20 |
* |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
23 |
* questions. |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
24 |
*/ |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
25 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
26 |
package java.lang; |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
27 |
|
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
28 |
import java.util.WeakHashMap; |
11536 | 29 |
import java.lang.ref.WeakReference; |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
30 |
import java.util.concurrent.atomic.AtomicInteger; |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
31 |
|
11536 | 32 |
import static java.lang.ClassValue.ClassValueMap.probeHomeLocation; |
33 |
import static java.lang.ClassValue.ClassValueMap.probeBackupLocations; |
|
34 |
||
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
35 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
36 |
* Lazily associate a computed value with (potentially) every type. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
37 |
* For example, if a dynamic language needs to construct a message dispatch |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
38 |
* table for each class encountered at a message send call site, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
39 |
* it can use a {@code ClassValue} to cache information needed to |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
40 |
* perform the message send quickly, for each class encountered. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
41 |
* @author John Rose, JSR 292 EG |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
42 |
* @since 1.7 |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
43 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
44 |
public abstract class ClassValue<T> { |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
45 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
46 |
* Sole constructor. (For invocation by subclass constructors, typically |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
47 |
* implicit.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
48 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
49 |
protected ClassValue() { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
50 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
51 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
52 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
53 |
* Computes the given class's derived value for this {@code ClassValue}. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
54 |
* <p> |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
55 |
* This method will be invoked within the first thread that accesses |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
56 |
* the value with the {@link #get get} method. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
57 |
* <p> |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
58 |
* Normally, this method is invoked at most once per class, |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
59 |
* but it may be invoked again if there has been a call to |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
60 |
* {@link #remove remove}. |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
61 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
62 |
* If this method throws an exception, the corresponding call to {@code get} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
63 |
* will terminate abnormally with that exception, and no class value will be recorded. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
64 |
* |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
65 |
* @param type the type whose class value must be computed |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
66 |
* @return the newly computed value associated with this {@code ClassValue}, for the given class or interface |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
67 |
* @see #get |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
68 |
* @see #remove |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
69 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
70 |
protected abstract T computeValue(Class<?> type); |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
71 |
|
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
72 |
/** |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
73 |
* Returns the value for the given class. |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
74 |
* If no value has yet been computed, it is obtained by |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
75 |
* an invocation of the {@link #computeValue computeValue} method. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
76 |
* <p> |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
77 |
* The actual installation of the value on the class |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
78 |
* is performed atomically. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
79 |
* At that point, if several racing threads have |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
80 |
* computed values, one is chosen, and returned to |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
81 |
* all the racing threads. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
82 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
83 |
* The {@code type} parameter is typically a class, but it may be any type, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
84 |
* such as an interface, a primitive type (like {@code int.class}), or {@code void.class}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
85 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
86 |
* In the absence of {@code remove} calls, a class value has a simple |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
87 |
* state diagram: uninitialized and initialized. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
88 |
* When {@code remove} calls are made, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
89 |
* the rules for value observation are more complex. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
90 |
* See the documentation for {@link #remove remove} for more information. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
91 |
* |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
92 |
* @param type the type whose class value must be computed or retrieved |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
93 |
* @return the current value associated with this {@code ClassValue}, for the given class or interface |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
94 |
* @throws NullPointerException if the argument is null |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
95 |
* @see #remove |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
96 |
* @see #computeValue |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
97 |
*/ |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
98 |
public T get(Class<?> type) { |
11536 | 99 |
// non-racing this.hashCodeForCache : final int |
100 |
Entry<?>[] cache; |
|
101 |
Entry<T> e = probeHomeLocation(cache = getCacheCarefully(type), this); |
|
102 |
// racing e : current value <=> stale value from current cache or from stale cache |
|
103 |
// invariant: e is null or an Entry with readable Entry.version and Entry.value |
|
104 |
if (match(e)) |
|
105 |
// invariant: No false positive matches. False negatives are OK if rare. |
|
106 |
// The key fact that makes this work: if this.version == e.version, |
|
107 |
// then this thread has a right to observe (final) e.value. |
|
108 |
return e.value(); |
|
109 |
// The fast path can fail for any of these reasons: |
|
110 |
// 1. no entry has been computed yet |
|
111 |
// 2. hash code collision (before or after reduction mod cache.length) |
|
112 |
// 3. an entry has been removed (either on this type or another) |
|
113 |
// 4. the GC has somehow managed to delete e.version and clear the reference |
|
114 |
return getFromBackup(cache, type); |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
115 |
} |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
116 |
|
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
117 |
/** |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
118 |
* Removes the associated value for the given class. |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
119 |
* If this value is subsequently {@linkplain #get read} for the same class, |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
120 |
* its value will be reinitialized by invoking its {@link #computeValue computeValue} method. |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
121 |
* This may result in an additional invocation of the |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
8822
diff
changeset
|
122 |
* {@code computeValue} method for the given class. |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7054
diff
changeset
|
123 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
124 |
* In order to explain the interaction between {@code get} and {@code remove} calls, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
125 |
* we must model the state transitions of a class value to take into account |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
126 |
* the alternation between uninitialized and initialized states. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
127 |
* To do this, number these states sequentially from zero, and note that |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
128 |
* uninitialized (or removed) states are numbered with even numbers, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
129 |
* while initialized (or re-initialized) states have odd numbers. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
130 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
131 |
* When a thread {@code T} removes a class value in state {@code 2N}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
132 |
* nothing happens, since the class value is already uninitialized. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
133 |
* Otherwise, the state is advanced atomically to {@code 2N+1}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
134 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
135 |
* When a thread {@code T} queries a class value in state {@code 2N}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
136 |
* the thread first attempts to initialize the class value to state {@code 2N+1} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
137 |
* by invoking {@code computeValue} and installing the resulting value. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
138 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
139 |
* When {@code T} attempts to install the newly computed value, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
140 |
* if the state is still at {@code 2N}, the class value will be initialized |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
141 |
* with the computed value, advancing it to state {@code 2N+1}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
142 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
143 |
* Otherwise, whether the new state is even or odd, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
144 |
* {@code T} will discard the newly computed value |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
145 |
* and retry the {@code get} operation. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
146 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
147 |
* Discarding and retrying is an important proviso, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
148 |
* since otherwise {@code T} could potentially install |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
149 |
* a disastrously stale value. For example: |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
150 |
* <ul> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
151 |
* <li>{@code T} calls {@code CV.get(C)} and sees state {@code 2N} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
152 |
* <li>{@code T} quickly computes a time-dependent value {@code V0} and gets ready to install it |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
153 |
* <li>{@code T} is hit by an unlucky paging or scheduling event, and goes to sleep for a long time |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
154 |
* <li>...meanwhile, {@code T2} also calls {@code CV.get(C)} and sees state {@code 2N} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
155 |
* <li>{@code T2} quickly computes a similar time-dependent value {@code V1} and installs it on {@code CV.get(C)} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
156 |
* <li>{@code T2} (or a third thread) then calls {@code CV.remove(C)}, undoing {@code T2}'s work |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
157 |
* <li> the previous actions of {@code T2} are repeated several times |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
158 |
* <li> also, the relevant computed values change over time: {@code V1}, {@code V2}, ... |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
159 |
* <li>...meanwhile, {@code T} wakes up and attempts to install {@code V0}; <em>this must fail</em> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
160 |
* </ul> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
161 |
* We can assume in the above scenario that {@code CV.computeValue} uses locks to properly |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
162 |
* observe the time-dependent states as it computes {@code V1}, etc. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
163 |
* This does not remove the threat of a stale value, since there is a window of time |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
164 |
* between the return of {@code computeValue} in {@code T} and the installation |
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
165 |
* of the new value. No user synchronization is possible during this time. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
166 |
* |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
167 |
* @param type the type whose class value must be removed |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
7562
diff
changeset
|
168 |
* @throws NullPointerException if the argument is null |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
169 |
*/ |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
170 |
public void remove(Class<?> type) { |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
171 |
ClassValueMap map = getMap(type); |
11536 | 172 |
map.removeEntry(this); |
173 |
} |
|
174 |
||
175 |
// Possible functionality for JSR 292 MR 1 |
|
176 |
/*public*/ void put(Class<?> type, T value) { |
|
177 |
ClassValueMap map = getMap(type); |
|
178 |
map.changeEntry(this, value); |
|
179 |
} |
|
180 |
||
181 |
/// -------- |
|
182 |
/// Implementation... |
|
183 |
/// -------- |
|
184 |
||
185 |
/** Return the cache, if it exists, else a dummy empty cache. */ |
|
186 |
private static Entry<?>[] getCacheCarefully(Class<?> type) { |
|
187 |
// racing type.classValueMap{.cacheArray} : null => new Entry[X] <=> new Entry[Y] |
|
188 |
ClassValueMap map = type.classValueMap; |
|
189 |
if (map == null) return EMPTY_CACHE; |
|
190 |
Entry<?>[] cache = map.getCache(); |
|
191 |
return cache; |
|
192 |
// invariant: returned value is safe to dereference and check for an Entry |
|
193 |
} |
|
194 |
||
195 |
/** Initial, one-element, empty cache used by all Class instances. Must never be filled. */ |
|
196 |
private static final Entry<?>[] EMPTY_CACHE = { null }; |
|
197 |
||
198 |
/** |
|
199 |
* Slow tail of ClassValue.get to retry at nearby locations in the cache, |
|
200 |
* or take a slow lock and check the hash table. |
|
201 |
* Called only if the first probe was empty or a collision. |
|
202 |
* This is a separate method, so compilers can process it independently. |
|
203 |
*/ |
|
204 |
private T getFromBackup(Entry<?>[] cache, Class<?> type) { |
|
205 |
Entry<T> e = probeBackupLocations(cache, this); |
|
206 |
if (e != null) |
|
207 |
return e.value(); |
|
208 |
return getFromHashMap(type); |
|
209 |
} |
|
210 |
||
211 |
// Hack to suppress warnings on the (T) cast, which is a no-op. |
|
212 |
@SuppressWarnings("unchecked") |
|
213 |
Entry<T> castEntry(Entry<?> e) { return (Entry<T>) e; } |
|
214 |
||
215 |
/** Called when the fast path of get fails, and cache reprobe also fails. |
|
216 |
*/ |
|
217 |
private T getFromHashMap(Class<?> type) { |
|
218 |
// The fail-safe recovery is to fall back to the underlying classValueMap. |
|
219 |
ClassValueMap map = getMap(type); |
|
220 |
for (;;) { |
|
221 |
Entry<T> e = map.startEntry(this); |
|
222 |
if (!e.isPromise()) |
|
223 |
return e.value(); |
|
224 |
try { |
|
225 |
// Try to make a real entry for the promised version. |
|
226 |
e = makeEntry(e.version(), computeValue(type)); |
|
227 |
} finally { |
|
228 |
// Whether computeValue throws or returns normally, |
|
229 |
// be sure to remove the empty entry. |
|
230 |
e = map.finishEntry(this, e); |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
231 |
} |
11536 | 232 |
if (e != null) |
233 |
return e.value(); |
|
234 |
// else try again, in case a racing thread called remove (so e == null) |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
235 |
} |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
236 |
} |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
237 |
|
11536 | 238 |
/** Check that e is non-null, matches this ClassValue, and is live. */ |
239 |
boolean match(Entry<?> e) { |
|
240 |
// racing e.version : null (blank) => unique Version token => null (GC-ed version) |
|
241 |
// non-racing this.version : v1 => v2 => ... (updates are read faithfully from volatile) |
|
242 |
return (e != null && e.get() == this.version); |
|
243 |
// invariant: No false positives on version match. Null is OK for false negative. |
|
244 |
// invariant: If version matches, then e.value is readable (final set in Entry.<init>) |
|
245 |
} |
|
246 |
||
247 |
/** Internal hash code for accessing Class.classValueMap.cacheArray. */ |
|
248 |
final int hashCodeForCache = nextHashCode.getAndAdd(HASH_INCREMENT) & HASH_MASK; |
|
249 |
||
250 |
/** Value stream for hashCodeForCache. See similar structure in ThreadLocal. */ |
|
251 |
private static final AtomicInteger nextHashCode = new AtomicInteger(); |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
252 |
|
11536 | 253 |
/** Good for power-of-two tables. See similar structure in ThreadLocal. */ |
254 |
private static final int HASH_INCREMENT = 0x61c88647; |
|
255 |
||
256 |
/** Mask a hash code to be positive but not too large, to prevent wraparound. */ |
|
257 |
static final int HASH_MASK = (-1 >>> 2); |
|
258 |
||
259 |
/** |
|
260 |
* Private key for retrieval of this object from ClassValueMap. |
|
261 |
*/ |
|
262 |
static class Identity { |
|
263 |
} |
|
264 |
/** |
|
265 |
* This ClassValue's identity, expressed as an opaque object. |
|
266 |
* The main object {@code ClassValue.this} is incorrect since |
|
267 |
* subclasses may override {@code ClassValue.equals}, which |
|
268 |
* could confuse keys in the ClassValueMap. |
|
269 |
*/ |
|
270 |
final Identity identity = new Identity(); |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
271 |
|
11536 | 272 |
/** |
273 |
* Current version for retrieving this class value from the cache. |
|
274 |
* Any number of computeValue calls can be cached in association with one version. |
|
275 |
* But the version changes when a remove (on any type) is executed. |
|
276 |
* A version change invalidates all cache entries for the affected ClassValue, |
|
277 |
* by marking them as stale. Stale cache entries do not force another call |
|
278 |
* to computeValue, but they do require a synchronized visit to a backing map. |
|
279 |
* <p> |
|
280 |
* All user-visible state changes on the ClassValue take place under |
|
281 |
* a lock inside the synchronized methods of ClassValueMap. |
|
282 |
* Readers (of ClassValue.get) are notified of such state changes |
|
283 |
* when this.version is bumped to a new token. |
|
284 |
* This variable must be volatile so that an unsynchronized reader |
|
285 |
* will receive the notification without delay. |
|
286 |
* <p> |
|
287 |
* If version were not volatile, one thread T1 could persistently hold onto |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
288 |
* a stale value this.value == V1, while another thread T2 advances |
11536 | 289 |
* (under a lock) to this.value == V2. This will typically be harmless, |
290 |
* but if T1 and T2 interact causally via some other channel, such that |
|
291 |
* T1's further actions are constrained (in the JMM) to happen after |
|
292 |
* the V2 event, then T1's observation of V1 will be an error. |
|
293 |
* <p> |
|
294 |
* The practical effect of making this.version be volatile is that it cannot |
|
295 |
* be hoisted out of a loop (by an optimizing JIT) or otherwise cached. |
|
296 |
* Some machines may also require a barrier instruction to execute |
|
297 |
* before this.version. |
|
298 |
*/ |
|
299 |
private volatile Version<T> version = new Version<>(this); |
|
300 |
Version<T> version() { return version; } |
|
301 |
void bumpVersion() { version = new Version<>(this); } |
|
302 |
static class Version<T> { |
|
303 |
private final ClassValue<T> classValue; |
|
304 |
private final Entry<T> promise = new Entry<>(this); |
|
305 |
Version(ClassValue<T> classValue) { this.classValue = classValue; } |
|
306 |
ClassValue<T> classValue() { return classValue; } |
|
307 |
Entry<T> promise() { return promise; } |
|
308 |
boolean isLive() { return classValue.version() == this; } |
|
309 |
} |
|
310 |
||
311 |
/** One binding of a value to a class via a ClassValue. |
|
312 |
* States are:<ul> |
|
313 |
* <li> promise if value == Entry.this |
|
314 |
* <li> else dead if version == null |
|
315 |
* <li> else stale if version != classValue.version |
|
316 |
* <li> else live </ul> |
|
317 |
* Promises are never put into the cache; they only live in the |
|
318 |
* backing map while a computeValue call is in flight. |
|
319 |
* Once an entry goes stale, it can be reset at any time |
|
320 |
* into the dead state. |
|
321 |
*/ |
|
322 |
static class Entry<T> extends WeakReference<Version<T>> { |
|
323 |
final Object value; // usually of type T, but sometimes (Entry)this |
|
324 |
Entry(Version<T> version, T value) { |
|
325 |
super(version); |
|
326 |
this.value = value; // for a regular entry, value is of type T |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
327 |
} |
11536 | 328 |
private void assertNotPromise() { assert(!isPromise()); } |
329 |
/** For creating a promise. */ |
|
330 |
Entry(Version<T> version) { |
|
331 |
super(version); |
|
332 |
this.value = this; // for a promise, value is not of type T, but Entry! |
|
333 |
} |
|
334 |
/** Fetch the value. This entry must not be a promise. */ |
|
335 |
@SuppressWarnings("unchecked") // if !isPromise, type is T |
|
336 |
T value() { assertNotPromise(); return (T) value; } |
|
337 |
boolean isPromise() { return value == this; } |
|
338 |
Version<T> version() { return get(); } |
|
339 |
ClassValue<T> classValueOrNull() { |
|
340 |
Version<T> v = version(); |
|
341 |
return (v == null) ? null : v.classValue(); |
|
342 |
} |
|
343 |
boolean isLive() { |
|
344 |
Version<T> v = version(); |
|
345 |
if (v == null) return false; |
|
346 |
if (v.isLive()) return true; |
|
347 |
clear(); |
|
348 |
return false; |
|
349 |
} |
|
350 |
Entry<T> refreshVersion(Version<T> v2) { |
|
351 |
assertNotPromise(); |
|
352 |
@SuppressWarnings("unchecked") // if !isPromise, type is T |
|
353 |
Entry<T> e2 = new Entry<>(v2, (T) value); |
|
354 |
clear(); |
|
355 |
// value = null -- caller must drop |
|
356 |
return e2; |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
357 |
} |
11536 | 358 |
static final Entry<?> DEAD_ENTRY = new Entry<>(null, null); |
359 |
} |
|
360 |
||
361 |
/** Return the backing map associated with this type. */ |
|
362 |
private static ClassValueMap getMap(Class<?> type) { |
|
363 |
// racing type.classValueMap : null (blank) => unique ClassValueMap |
|
364 |
// if a null is observed, a map is created (lazily, synchronously, uniquely) |
|
365 |
// all further access to that map is synchronized |
|
366 |
ClassValueMap map = type.classValueMap; |
|
367 |
if (map != null) return map; |
|
368 |
return initializeMap(type); |
|
369 |
} |
|
370 |
||
371 |
private static final Object CRITICAL_SECTION = new Object(); |
|
372 |
private static ClassValueMap initializeMap(Class<?> type) { |
|
373 |
ClassValueMap map; |
|
374 |
synchronized (CRITICAL_SECTION) { // private object to avoid deadlocks |
|
375 |
// happens about once per type |
|
376 |
if ((map = type.classValueMap) == null) |
|
24496 | 377 |
type.classValueMap = map = new ClassValueMap(); |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
378 |
} |
24496 | 379 |
return map; |
380 |
} |
|
11536 | 381 |
|
382 |
static <T> Entry<T> makeEntry(Version<T> explicitVersion, T value) { |
|
383 |
// Note that explicitVersion might be different from this.version. |
|
384 |
return new Entry<>(explicitVersion, value); |
|
385 |
||
386 |
// As soon as the Entry is put into the cache, the value will be |
|
387 |
// reachable via a data race (as defined by the Java Memory Model). |
|
388 |
// This race is benign, assuming the value object itself can be |
|
389 |
// read safely by multiple threads. This is up to the user. |
|
390 |
// |
|
391 |
// The entry and version fields themselves can be safely read via |
|
392 |
// a race because they are either final or have controlled states. |
|
393 |
// If the pointer from the entry to the version is still null, |
|
394 |
// or if the version goes immediately dead and is nulled out, |
|
395 |
// the reader will take the slow path and retry under a lock. |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
396 |
} |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
397 |
|
11536 | 398 |
// The following class could also be top level and non-public: |
399 |
||
24496 | 400 |
/** A backing map for all ClassValues. |
11536 | 401 |
* Gives a fully serialized "true state" for each pair (ClassValue cv, Class type). |
402 |
* Also manages an unserialized fast-path cache. |
|
403 |
*/ |
|
404 |
static class ClassValueMap extends WeakHashMap<ClassValue.Identity, Entry<?>> { |
|
405 |
private Entry<?>[] cacheArray; |
|
406 |
private int cacheLoad, cacheLoadLimit; |
|
407 |
||
408 |
/** Number of entries initially allocated to each type when first used with any ClassValue. |
|
409 |
* It would be pointless to make this much smaller than the Class and ClassValueMap objects themselves. |
|
410 |
* Must be a power of 2. |
|
411 |
*/ |
|
412 |
private static final int INITIAL_ENTRIES = 32; |
|
413 |
||
24496 | 414 |
/** Build a backing map for ClassValues. |
11536 | 415 |
* Also, create an empty cache array and install it on the class. |
416 |
*/ |
|
24496 | 417 |
ClassValueMap() { |
11536 | 418 |
sizeCache(INITIAL_ENTRIES); |
419 |
} |
|
420 |
||
421 |
Entry<?>[] getCache() { return cacheArray; } |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
422 |
|
11536 | 423 |
/** Initiate a query. Store a promise (placeholder) if there is no value yet. */ |
424 |
synchronized |
|
425 |
<T> Entry<T> startEntry(ClassValue<T> classValue) { |
|
426 |
@SuppressWarnings("unchecked") // one map has entries for all value types <T> |
|
427 |
Entry<T> e = (Entry<T>) get(classValue.identity); |
|
428 |
Version<T> v = classValue.version(); |
|
429 |
if (e == null) { |
|
430 |
e = v.promise(); |
|
431 |
// The presence of a promise means that a value is pending for v. |
|
432 |
// Eventually, finishEntry will overwrite the promise. |
|
433 |
put(classValue.identity, e); |
|
434 |
// Note that the promise is never entered into the cache! |
|
435 |
return e; |
|
436 |
} else if (e.isPromise()) { |
|
437 |
// Somebody else has asked the same question. |
|
438 |
// Let the races begin! |
|
439 |
if (e.version() != v) { |
|
440 |
e = v.promise(); |
|
441 |
put(classValue.identity, e); |
|
442 |
} |
|
443 |
return e; |
|
444 |
} else { |
|
445 |
// there is already a completed entry here; report it |
|
446 |
if (e.version() != v) { |
|
447 |
// There is a stale but valid entry here; make it fresh again. |
|
448 |
// Once an entry is in the hash table, we don't care what its version is. |
|
449 |
e = e.refreshVersion(v); |
|
450 |
put(classValue.identity, e); |
|
451 |
} |
|
452 |
// Add to the cache, to enable the fast path, next time. |
|
453 |
checkCacheLoad(); |
|
454 |
addToCache(classValue, e); |
|
455 |
return e; |
|
456 |
} |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
457 |
} |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
458 |
|
11536 | 459 |
/** Finish a query. Overwrite a matching placeholder. Drop stale incoming values. */ |
460 |
synchronized |
|
461 |
<T> Entry<T> finishEntry(ClassValue<T> classValue, Entry<T> e) { |
|
462 |
@SuppressWarnings("unchecked") // one map has entries for all value types <T> |
|
463 |
Entry<T> e0 = (Entry<T>) get(classValue.identity); |
|
464 |
if (e == e0) { |
|
465 |
// We can get here during exception processing, unwinding from computeValue. |
|
466 |
assert(e.isPromise()); |
|
467 |
remove(classValue.identity); |
|
468 |
return null; |
|
469 |
} else if (e0 != null && e0.isPromise() && e0.version() == e.version()) { |
|
470 |
// If e0 matches the intended entry, there has not been a remove call |
|
471 |
// between the previous startEntry and now. So now overwrite e0. |
|
472 |
Version<T> v = classValue.version(); |
|
473 |
if (e.version() != v) |
|
474 |
e = e.refreshVersion(v); |
|
475 |
put(classValue.identity, e); |
|
476 |
// Add to the cache, to enable the fast path, next time. |
|
477 |
checkCacheLoad(); |
|
478 |
addToCache(classValue, e); |
|
479 |
return e; |
|
480 |
} else { |
|
481 |
// Some sort of mismatch; caller must try again. |
|
482 |
return null; |
|
483 |
} |
|
484 |
} |
|
485 |
||
486 |
/** Remove an entry. */ |
|
487 |
synchronized |
|
488 |
void removeEntry(ClassValue<?> classValue) { |
|
13419
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
489 |
Entry<?> e = remove(classValue.identity); |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
490 |
if (e == null) { |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
491 |
// Uninitialized, and no pending calls to computeValue. No change. |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
492 |
} else if (e.isPromise()) { |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
493 |
// State is uninitialized, with a pending call to finishEntry. |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
494 |
// Since remove is a no-op in such a state, keep the promise |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
495 |
// by putting it back into the map. |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
496 |
put(classValue.identity, e); |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
497 |
} else { |
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
498 |
// In an initialized state. Bump forward, and de-initialize. |
11536 | 499 |
classValue.bumpVersion(); |
13419
ef84025f710f
7153157: ClassValue.get does not return if computeValue calls remove
jrose
parents:
11536
diff
changeset
|
500 |
// Make all cache elements for this guy go stale. |
11536 | 501 |
removeStaleEntries(classValue); |
502 |
} |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
503 |
} |
11536 | 504 |
|
505 |
/** Change the value for an entry. */ |
|
506 |
synchronized |
|
507 |
<T> void changeEntry(ClassValue<T> classValue, T value) { |
|
508 |
@SuppressWarnings("unchecked") // one map has entries for all value types <T> |
|
509 |
Entry<T> e0 = (Entry<T>) get(classValue.identity); |
|
510 |
Version<T> version = classValue.version(); |
|
511 |
if (e0 != null) { |
|
512 |
if (e0.version() == version && e0.value() == value) |
|
513 |
// no value change => no version change needed |
|
514 |
return; |
|
515 |
classValue.bumpVersion(); |
|
516 |
removeStaleEntries(classValue); |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
517 |
} |
11536 | 518 |
Entry<T> e = makeEntry(version, value); |
519 |
put(classValue.identity, e); |
|
520 |
// Add to the cache, to enable the fast path, next time. |
|
521 |
checkCacheLoad(); |
|
522 |
addToCache(classValue, e); |
|
523 |
} |
|
524 |
||
525 |
/// -------- |
|
526 |
/// Cache management. |
|
527 |
/// -------- |
|
528 |
||
529 |
// Statics do not need synchronization. |
|
530 |
||
531 |
/** Load the cache entry at the given (hashed) location. */ |
|
532 |
static Entry<?> loadFromCache(Entry<?>[] cache, int i) { |
|
533 |
// non-racing cache.length : constant |
|
534 |
// racing cache[i & (mask)] : null <=> Entry |
|
535 |
return cache[i & (cache.length-1)]; |
|
536 |
// invariant: returned value is null or well-constructed (ready to match) |
|
537 |
} |
|
538 |
||
539 |
/** Look in the cache, at the home location for the given ClassValue. */ |
|
540 |
static <T> Entry<T> probeHomeLocation(Entry<?>[] cache, ClassValue<T> classValue) { |
|
541 |
return classValue.castEntry(loadFromCache(cache, classValue.hashCodeForCache)); |
|
542 |
} |
|
543 |
||
544 |
/** Given that first probe was a collision, retry at nearby locations. */ |
|
545 |
static <T> Entry<T> probeBackupLocations(Entry<?>[] cache, ClassValue<T> classValue) { |
|
546 |
if (PROBE_LIMIT <= 0) return null; |
|
547 |
// Probe the cache carefully, in a range of slots. |
|
548 |
int mask = (cache.length-1); |
|
549 |
int home = (classValue.hashCodeForCache & mask); |
|
550 |
Entry<?> e2 = cache[home]; // victim, if we find the real guy |
|
551 |
if (e2 == null) { |
|
552 |
return null; // if nobody is at home, no need to search nearby |
|
553 |
} |
|
554 |
// assume !classValue.match(e2), but do not assert, because of races |
|
555 |
int pos2 = -1; |
|
556 |
for (int i = home + 1; i < home + PROBE_LIMIT; i++) { |
|
557 |
Entry<?> e = cache[i & mask]; |
|
558 |
if (e == null) { |
|
559 |
break; // only search within non-null runs |
|
560 |
} |
|
561 |
if (classValue.match(e)) { |
|
562 |
// relocate colliding entry e2 (from cache[home]) to first empty slot |
|
563 |
cache[home] = e; |
|
564 |
if (pos2 >= 0) { |
|
565 |
cache[i & mask] = Entry.DEAD_ENTRY; |
|
566 |
} else { |
|
567 |
pos2 = i; |
|
568 |
} |
|
569 |
cache[pos2 & mask] = ((entryDislocation(cache, pos2, e2) < PROBE_LIMIT) |
|
570 |
? e2 // put e2 here if it fits |
|
571 |
: Entry.DEAD_ENTRY); |
|
572 |
return classValue.castEntry(e); |
|
573 |
} |
|
574 |
// Remember first empty slot, if any: |
|
575 |
if (!e.isLive() && pos2 < 0) pos2 = i; |
|
576 |
} |
|
577 |
return null; |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
578 |
} |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
579 |
|
11536 | 580 |
/** How far out of place is e? */ |
581 |
private static int entryDislocation(Entry<?>[] cache, int pos, Entry<?> e) { |
|
582 |
ClassValue<?> cv = e.classValueOrNull(); |
|
583 |
if (cv == null) return 0; // entry is not live! |
|
584 |
int mask = (cache.length-1); |
|
585 |
return (pos - cv.hashCodeForCache) & mask; |
|
586 |
} |
|
587 |
||
588 |
/// -------- |
|
589 |
/// Below this line all functions are private, and assume synchronized access. |
|
590 |
/// -------- |
|
591 |
||
592 |
private void sizeCache(int length) { |
|
593 |
assert((length & (length-1)) == 0); // must be power of 2 |
|
594 |
cacheLoad = 0; |
|
595 |
cacheLoadLimit = (int) ((double) length * CACHE_LOAD_LIMIT / 100); |
|
596 |
cacheArray = new Entry<?>[length]; |
|
597 |
} |
|
598 |
||
599 |
/** Make sure the cache load stays below its limit, if possible. */ |
|
600 |
private void checkCacheLoad() { |
|
601 |
if (cacheLoad >= cacheLoadLimit) { |
|
602 |
reduceCacheLoad(); |
|
603 |
} |
|
604 |
} |
|
605 |
private void reduceCacheLoad() { |
|
606 |
removeStaleEntries(); |
|
607 |
if (cacheLoad < cacheLoadLimit) |
|
608 |
return; // win |
|
609 |
Entry<?>[] oldCache = getCache(); |
|
610 |
if (oldCache.length > HASH_MASK) |
|
611 |
return; // lose |
|
612 |
sizeCache(oldCache.length * 2); |
|
613 |
for (Entry<?> e : oldCache) { |
|
614 |
if (e != null && e.isLive()) { |
|
615 |
addToCache(e); |
|
616 |
} |
|
617 |
} |
|
618 |
} |
|
619 |
||
620 |
/** Remove stale entries in the given range. |
|
621 |
* Should be executed under a Map lock. |
|
622 |
*/ |
|
623 |
private void removeStaleEntries(Entry<?>[] cache, int begin, int count) { |
|
624 |
if (PROBE_LIMIT <= 0) return; |
|
625 |
int mask = (cache.length-1); |
|
626 |
int removed = 0; |
|
627 |
for (int i = begin; i < begin + count; i++) { |
|
628 |
Entry<?> e = cache[i & mask]; |
|
629 |
if (e == null || e.isLive()) |
|
630 |
continue; // skip null and live entries |
|
631 |
Entry<?> replacement = null; |
|
632 |
if (PROBE_LIMIT > 1) { |
|
633 |
// avoid breaking up a non-null run |
|
634 |
replacement = findReplacement(cache, i); |
|
635 |
} |
|
636 |
cache[i & mask] = replacement; |
|
637 |
if (replacement == null) removed += 1; |
|
638 |
} |
|
639 |
cacheLoad = Math.max(0, cacheLoad - removed); |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
640 |
} |
11536 | 641 |
|
642 |
/** Clearing a cache slot risks disconnecting following entries |
|
643 |
* from the head of a non-null run, which would allow them |
|
644 |
* to be found via reprobes. Find an entry after cache[begin] |
|
645 |
* to plug into the hole, or return null if none is needed. |
|
646 |
*/ |
|
647 |
private Entry<?> findReplacement(Entry<?>[] cache, int home1) { |
|
648 |
Entry<?> replacement = null; |
|
649 |
int haveReplacement = -1, replacementPos = 0; |
|
650 |
int mask = (cache.length-1); |
|
651 |
for (int i2 = home1 + 1; i2 < home1 + PROBE_LIMIT; i2++) { |
|
652 |
Entry<?> e2 = cache[i2 & mask]; |
|
653 |
if (e2 == null) break; // End of non-null run. |
|
654 |
if (!e2.isLive()) continue; // Doomed anyway. |
|
655 |
int dis2 = entryDislocation(cache, i2, e2); |
|
656 |
if (dis2 == 0) continue; // e2 already optimally placed |
|
657 |
int home2 = i2 - dis2; |
|
658 |
if (home2 <= home1) { |
|
659 |
// e2 can replace entry at cache[home1] |
|
660 |
if (home2 == home1) { |
|
661 |
// Put e2 exactly where he belongs. |
|
662 |
haveReplacement = 1; |
|
663 |
replacementPos = i2; |
|
664 |
replacement = e2; |
|
665 |
} else if (haveReplacement <= 0) { |
|
666 |
haveReplacement = 0; |
|
667 |
replacementPos = i2; |
|
668 |
replacement = e2; |
|
669 |
} |
|
670 |
// And keep going, so we can favor larger dislocations. |
|
671 |
} |
|
672 |
} |
|
673 |
if (haveReplacement >= 0) { |
|
674 |
if (cache[(replacementPos+1) & mask] != null) { |
|
675 |
// Be conservative, to avoid breaking up a non-null run. |
|
676 |
cache[replacementPos & mask] = (Entry<?>) Entry.DEAD_ENTRY; |
|
677 |
} else { |
|
678 |
cache[replacementPos & mask] = null; |
|
679 |
cacheLoad -= 1; |
|
680 |
} |
|
681 |
} |
|
682 |
return replacement; |
|
683 |
} |
|
684 |
||
685 |
/** Remove stale entries in the range near classValue. */ |
|
686 |
private void removeStaleEntries(ClassValue<?> classValue) { |
|
687 |
removeStaleEntries(getCache(), classValue.hashCodeForCache, PROBE_LIMIT); |
|
688 |
} |
|
689 |
||
690 |
/** Remove all stale entries, everywhere. */ |
|
691 |
private void removeStaleEntries() { |
|
21330
7b073d91ba9e
8027062: Fix lint and doclint issues in java.lang.{ClassLoader, ClassValue, SecurityManager}
darcy
parents:
14342
diff
changeset
|
692 |
Entry<?>[] cache = getCache(); |
11536 | 693 |
removeStaleEntries(cache, 0, cache.length + PROBE_LIMIT - 1); |
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
694 |
} |
11536 | 695 |
|
696 |
/** Add the given entry to the cache, in its home location, unless it is out of date. */ |
|
697 |
private <T> void addToCache(Entry<T> e) { |
|
698 |
ClassValue<T> classValue = e.classValueOrNull(); |
|
699 |
if (classValue != null) |
|
700 |
addToCache(classValue, e); |
|
701 |
} |
|
702 |
||
703 |
/** Add the given entry to the cache, in its home location. */ |
|
704 |
private <T> void addToCache(ClassValue<T> classValue, Entry<T> e) { |
|
705 |
if (PROBE_LIMIT <= 0) return; // do not fill cache |
|
706 |
// Add e to the cache. |
|
707 |
Entry<?>[] cache = getCache(); |
|
708 |
int mask = (cache.length-1); |
|
709 |
int home = classValue.hashCodeForCache & mask; |
|
710 |
Entry<?> e2 = placeInCache(cache, home, e, false); |
|
711 |
if (e2 == null) return; // done |
|
712 |
if (PROBE_LIMIT > 1) { |
|
713 |
// try to move e2 somewhere else in his probe range |
|
714 |
int dis2 = entryDislocation(cache, home, e2); |
|
715 |
int home2 = home - dis2; |
|
716 |
for (int i2 = home2; i2 < home2 + PROBE_LIMIT; i2++) { |
|
717 |
if (placeInCache(cache, i2 & mask, e2, true) == null) { |
|
718 |
return; |
|
719 |
} |
|
720 |
} |
|
721 |
} |
|
722 |
// Note: At this point, e2 is just dropped from the cache. |
|
723 |
} |
|
724 |
||
725 |
/** Store the given entry. Update cacheLoad, and return any live victim. |
|
726 |
* 'Gently' means return self rather than dislocating a live victim. |
|
727 |
*/ |
|
728 |
private Entry<?> placeInCache(Entry<?>[] cache, int pos, Entry<?> e, boolean gently) { |
|
729 |
Entry<?> e2 = overwrittenEntry(cache[pos]); |
|
730 |
if (gently && e2 != null) { |
|
731 |
// do not overwrite a live entry |
|
732 |
return e; |
|
733 |
} else { |
|
734 |
cache[pos] = e; |
|
735 |
return e2; |
|
736 |
} |
|
737 |
} |
|
738 |
||
739 |
/** Note an entry that is about to be overwritten. |
|
740 |
* If it is not live, quietly replace it by null. |
|
741 |
* If it is an actual null, increment cacheLoad, |
|
742 |
* because the caller is going to store something |
|
743 |
* in its place. |
|
744 |
*/ |
|
745 |
private <T> Entry<T> overwrittenEntry(Entry<T> e2) { |
|
746 |
if (e2 == null) cacheLoad += 1; |
|
747 |
else if (e2.isLive()) return e2; |
|
748 |
return null; |
|
749 |
} |
|
750 |
||
751 |
/** Percent loading of cache before resize. */ |
|
752 |
private static final int CACHE_LOAD_LIMIT = 67; // 0..100 |
|
753 |
/** Maximum number of probes to attempt. */ |
|
754 |
private static final int PROBE_LIMIT = 6; // 1.. |
|
755 |
// N.B. Set PROBE_LIMIT=0 to disable all fast paths. |
|
7054
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
756 |
} |
fee2fbaf3293
6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff
changeset
|
757 |
} |