77 * holds its object strongly while the JS wrapper is reachable and through a WeakReference
88 * after the wrapper was collected (until the object is collected too).
99 *
10- * Where StampedLock exists (API 24+) any thread may read and findOrRegister: writers are
11- * serialized by its write lock and exact reads validate against it. Without it, every
12- * mutation and every exact read must come from the thread that holds the runtime's
13- * isolate lock. Neither table allocates per entry.
10+ * When concurrent, any thread may read and findOrRegister: every access synchronizes on the
11+ * table. Otherwise every mutation and every read must come from the thread that holds the
12+ * runtime's isolate lock. Neither table allocates per entry.
13+ *
14+ * A monitor rather than java.util.concurrent locks: those live in the boot classpath but
15+ * outside the boot image, so on device they run interpreted, and an app's profile can't
16+ * compile them. Uncontended monitor enter/exit is compiled into every caller.
1417 */
1518final class ObjectTable {
1619 static final int NO_ID = -1 ;
@@ -22,114 +25,28 @@ interface IdSource {
2225 int newId ();
2326 }
2427
25- // Null where StampedLock is unavailable (API < 24): findIdOptimistic then always misses.
26- private final Stamps stamps ;
28+ private final boolean concurrent ;
2729
2830 ObjectTable () {
2931 this (false );
3032 }
3133
32- ObjectTable (boolean optimisticReads ) {
33- stamps = optimisticReads ? new Stamps () : null ;
34+ ObjectTable (boolean concurrent ) {
35+ this . concurrent = concurrent ;
3436 }
3537
3638 /**
37- * Writers are already serialized by the isolate lock; the write stamp only lets an
38- * optimistic reader detect that a write overlapped it. Kept in its own class so that
39- * runtimes where StampedLock is missing never load it.
40- */
41- private static final class Stamps {
42- private final java .util .concurrent .locks .StampedLock lock = new java .util .concurrent .locks .StampedLock ();
43-
44- long beginWrite () {
45- return lock .writeLock ();
46- }
47-
48- void endWrite (long stamp ) {
49- lock .unlockWrite (stamp );
50- }
51-
52- long beginRead () {
53- return lock .tryOptimisticRead ();
54- }
55-
56- long readLock () {
57- return lock .readLock ();
58- }
59-
60- void unlockRead (long stamp ) {
61- lock .unlockRead (stamp );
62- }
63-
64- boolean validate (long stamp ) {
65- return lock .validate (stamp );
66- }
67- }
68-
69- /**
70- * The id obj is registered under, from any thread, without the isolate lock. A miss
71- * (NO_ID) is not authoritative: it is also returned when a write overlapped the read.
72- * A hit was correct at the time of the read; the object may have been released since,
73- * which the caller must check under the isolate lock.
39+ * The id obj is registered under, from any thread, without the isolate lock; NO_ID when
40+ * the table isn't concurrent. A hit was correct at the time of the read; the object may
41+ * have been released since, which the caller must check under the isolate lock.
7442 */
7543 int findIdOptimistic (Object obj ) {
76- if (stamps == null ) {
77- return NO_ID ;
78- }
79- long stamp = stamps .beginRead ();
80- if (stamp == 0 ) {
44+ if (!concurrent ) {
8145 return NO_ID ;
8246 }
83- int id ;
84- try {
85- id = findIdUnlocked (obj );
86- } catch (RuntimeException e ) {
87- // A torn view of a resize can index past an array; the stamp check below fails then.
88- id = NO_ID ;
89- }
90- return stamps .validate (stamp ) ? id : NO_ID ;
91- }
92-
93- private int findIdUnlocked (Object obj ) {
94- int [] index = identityIndex ;
95- int [] hashes = identityIndexHashes ;
96- int mask = index .length - 1 ;
97- int hash = System .identityHashCode (obj );
98- int i = mix (hash ) & mask ;
99- for (int probes = 0 ; probes <= mask ; probes ++) {
100- int entry = index [i ];
101- if (entry == 0 ) {
102- return NO_ID ;
103- }
104- if (hashes [i ] == hash && getUnlocked (entry - 1 ) == obj ) {
105- return entry - 1 ;
106- }
107- i = (i + 1 ) & mask ;
108- }
109- return NO_ID ;
110- }
111-
112- // get() without the lock. Safe on a torn view: every array it reads is indexed within
113- // its own bounds, and a stale one only makes the caller's stamp check fail.
114- private Object getUnlocked (int id ) {
115- if (id == NO_ID ) {
116- return null ;
117- }
118- Chunk [] cs = chunks ;
119- int c = id >>> CHUNK_SHIFT ;
120- if (c >= cs .length ) {
121- return null ;
122- }
123- Chunk chunk = cs [c ];
124- if (chunk == null ) {
125- return null ;
126- }
127- Leaf leaf = chunk .leaves [(id >>> LEAF_BITS ) & CHUNK_MASK ];
128- if (leaf == null ) {
129- return null ;
47+ synchronized (this ) {
48+ return findIdLocked (obj );
13049 }
131- Object value = leaf .values [id & LEAF_MASK ];
132- return value == null ? null : deref (value );
13350 }
13451
13552 private static Object deref (Object value ) {
@@ -177,15 +94,12 @@ private static final class Chunk {
17794
17895 /** Registers obj under id, held strongly, replacing whatever id held before. */
17996 void putStrong (int id , Object obj ) {
180- if (stamps == null ) {
97+ if (! concurrent ) {
18198 putStrongLocked (id , obj );
18299 return ;
183100 }
184- long stamp = stamps .beginWrite ();
185- try {
101+ synchronized (this ) {
186102 putStrongLocked (id , obj );
187- } finally {
188- stamps .endWrite (stamp );
189103 }
190104 }
191105
@@ -217,18 +131,17 @@ private void putStrongLocked(int id, Object obj, int hash) {
217131 }
218132
219133 boolean allowsConcurrentRegistration () {
220- return stamps != null ;
134+ return concurrent ;
221135 }
222136
223137 /**
224138 * The id obj is registered under, registering it strongly under a new id from ids when
225139 * it has none; FRESH is set in the result when this call registered it. The lookup and
226- * the insert happen under one write lock, so concurrent calls agree on one id. Only
227- * when allowsConcurrentRegistration().
140+ * the insert happen under one lock, so concurrent calls agree on one id. Only when
141+ * allowsConcurrentRegistration().
228142 */
229143 long findOrRegister (Object obj , IdSource ids ) {
230- long stamp = stamps .beginWrite ();
231- try {
144+ synchronized (this ) {
232145 int hash = System .identityHashCode (obj );
233146 int id = findIdLocked (obj , hash );
234147 if (id != NO_ID ) {
@@ -237,32 +150,16 @@ long findOrRegister(Object obj, IdSource ids) {
237150 id = ids .newId ();
238151 putStrongLocked (id , obj , hash );
239152 return (id & 0xFFFFFFFFL ) | FRESH ;
240- } finally {
241- stamps .endWrite (stamp );
242153 }
243154 }
244155
245156 /** The id obj is registered under, or NO_ID. */
246157 int findId (Object obj ) {
247- if (stamps == null ) {
158+ if (! concurrent ) {
248159 return findIdLocked (obj );
249160 }
250- long stamp = stamps .beginRead ();
251- if (stamp != 0 ) {
252- try {
253- int id = findIdUnlocked (obj );
254- if (stamps .validate (stamp )) {
255- return id ;
256- }
257- } catch (RuntimeException e ) {
258- // A torn view of a resize; retried under the read lock.
259- }
260- }
261- stamp = stamps .readLock ();
262- try {
161+ synchronized (this ) {
263162 return findIdLocked (obj );
264- } finally {
265- stamps .unlockRead (stamp );
266163 }
267164 }
268165
@@ -288,25 +185,11 @@ private int findIdLocked(Object obj, int hash) {
288185
289186 /** The object registered under id; null if there is none or it was collected. */
290187 Object get (int id ) {
291- if (stamps == null ) {
188+ if (! concurrent ) {
292189 return getLocked (id );
293190 }
294- long stamp = stamps .beginRead ();
295- if (stamp != 0 ) {
296- try {
297- Object value = getUnlocked (id );
298- if (stamps .validate (stamp )) {
299- return value ;
300- }
301- } catch (RuntimeException e ) {
302- // A torn view of a resize; retried under the read lock.
303- }
304- }
305- stamp = stamps .readLock ();
306- try {
191+ synchronized (this ) {
307192 return getLocked (id );
308- } finally {
309- stamps .unlockRead (stamp );
310193 }
311194 }
312195
@@ -316,14 +199,11 @@ private Object getLocked(int id) {
316199 }
317200
318201 boolean contains (int id ) {
319- if (stamps == null ) {
202+ if (! concurrent ) {
320203 return rawLocked (id ) != null ;
321204 }
322- long stamp = stamps .readLock ();
323- try {
205+ synchronized (this ) {
324206 return rawLocked (id ) != null ;
325- } finally {
326- stamps .unlockRead (stamp );
327207 }
328208 }
329209
@@ -332,35 +212,35 @@ boolean contains(int id) {
332212 * the object is still alive; an entry whose object was already collected is removed.
333213 */
334214 boolean weakenAndCheckAlive (int id ) {
335- if (stamps == null ) {
215+ if (! concurrent ) {
336216 return weakenAndCheckAliveLocked (id );
337217 }
338- long stamp = stamps .beginWrite ();
339- try {
218+ synchronized (this ) {
340219 return weakenAndCheckAliveLocked (id );
341- } finally {
342- stamps .endWrite (stamp );
343220 }
344221 }
345222
346223 /**
347- * weakenAndCheckAlive for ids[0..count), under one write lock; overwrites the id of
348- * every object no longer alive with NO_ID.
224+ * weakenAndCheckAlive for ids[0..count), under one lock; overwrites the id of every
225+ * object no longer alive with NO_ID.
349226 */
350227 void weakenAndCheckAlive (int [] ids , int count ) {
351228 if (count == 0 ) {
352229 return ;
353230 }
354- long stamp = stamps == null ? 0 : stamps .beginWrite ();
355- try {
356- for (int i = 0 ; i < count ; i ++) {
357- if (!weakenAndCheckAliveLocked (ids [i ])) {
358- ids [i ] = NO_ID ;
359- }
360- }
361- } finally {
362- if (stamps != null ) {
363- stamps .endWrite (stamp );
231+ if (!concurrent ) {
232+ weakenAndCheckAliveAllLocked (ids , count );
233+ return ;
234+ }
235+ synchronized (this ) {
236+ weakenAndCheckAliveAllLocked (ids , count );
237+ }
238+ }
239+
240+ private void weakenAndCheckAliveAllLocked (int [] ids , int count ) {
241+ for (int i = 0 ; i < count ; i ++) {
242+ if (!weakenAndCheckAliveLocked (ids [i ])) {
243+ ids [i ] = NO_ID ;
364244 }
365245 }
366246 }
@@ -383,15 +263,12 @@ private boolean weakenAndCheckAliveLocked(int id) {
383263 }
384264
385265 void remove (int id ) {
386- if (stamps == null ) {
266+ if (! concurrent ) {
387267 removeLocked (id );
388268 return ;
389269 }
390- long stamp = stamps .beginWrite ();
391- try {
270+ synchronized (this ) {
392271 removeLocked (id );
393- } finally {
394- stamps .endWrite (stamp );
395272 }
396273 }
397274
0 commit comments