ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jdk7/java/util/concurrent/Exchanger.java
Revision: 1.3
Committed: Sun Jan 18 20:17:32 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +1 -0 lines
Log Message:
exactly one blank line before and after package statements

File Contents

# Content
1 /*
2 * Written by Doug Lea, Bill Scherer, and Michael Scott with
3 * assistance from members of JCP JSR-166 Expert Group and released to
4 * the public domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/
6 */
7
8 package java.util.concurrent;
9
10 import java.util.concurrent.atomic.AtomicInteger;
11 import java.util.concurrent.atomic.AtomicReference;
12 import java.util.concurrent.locks.LockSupport;
13
14 /**
15 * A synchronization point at which threads can pair and swap elements
16 * within pairs. Each thread presents some object on entry to the
17 * {@link #exchange exchange} method, matches with a partner thread,
18 * and receives its partner's object on return. An Exchanger may be
19 * viewed as a bidirectional form of a {@link SynchronousQueue}.
20 * Exchangers may be useful in applications such as genetic algorithms
21 * and pipeline designs.
22 *
23 * <p><b>Sample Usage:</b>
24 * Here are the highlights of a class that uses an {@code Exchanger}
25 * to swap buffers between threads so that the thread filling the
26 * buffer gets a freshly emptied one when it needs it, handing off the
27 * filled one to the thread emptying the buffer.
28 * <pre> {@code
29 * class FillAndEmpty {
30 * Exchanger<DataBuffer> exchanger = new Exchanger<DataBuffer>();
31 * DataBuffer initialEmptyBuffer = ... a made-up type
32 * DataBuffer initialFullBuffer = ...
33 *
34 * class FillingLoop implements Runnable {
35 * public void run() {
36 * DataBuffer currentBuffer = initialEmptyBuffer;
37 * try {
38 * while (currentBuffer != null) {
39 * addToBuffer(currentBuffer);
40 * if (currentBuffer.isFull())
41 * currentBuffer = exchanger.exchange(currentBuffer);
42 * }
43 * } catch (InterruptedException ex) { ... handle ... }
44 * }
45 * }
46 *
47 * class EmptyingLoop implements Runnable {
48 * public void run() {
49 * DataBuffer currentBuffer = initialFullBuffer;
50 * try {
51 * while (currentBuffer != null) {
52 * takeFromBuffer(currentBuffer);
53 * if (currentBuffer.isEmpty())
54 * currentBuffer = exchanger.exchange(currentBuffer);
55 * }
56 * } catch (InterruptedException ex) { ... handle ...}
57 * }
58 * }
59 *
60 * void start() {
61 * new Thread(new FillingLoop()).start();
62 * new Thread(new EmptyingLoop()).start();
63 * }
64 * }}</pre>
65 *
66 * <p>Memory consistency effects: For each pair of threads that
67 * successfully exchange objects via an {@code Exchanger}, actions
68 * prior to the {@code exchange()} in each thread
69 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
70 * those subsequent to a return from the corresponding {@code exchange()}
71 * in the other thread.
72 *
73 * @since 1.5
74 * @author Doug Lea and Bill Scherer and Michael Scott
75 * @param <V> The type of objects that may be exchanged
76 */
77 public class Exchanger<V> {
78
79 /*
80 * Overview: The core algorithm is, for an exchange "slot",
81 * and a participant (caller) with an item:
82 *
83 * for (;;) {
84 * if (slot is empty) { // offer
85 * place item in a Node;
86 * if (can CAS slot from empty to node) {
87 * wait for release;
88 * return matching item in node;
89 * }
90 * }
91 * else if (can CAS slot from node to empty) { // release
92 * get the item in node;
93 * set matching item in node;
94 * release waiting thread;
95 * }
96 * // else retry on CAS failure
97 * }
98 *
99 * This is among the simplest forms of a "dual data structure" --
100 * see Scott and Scherer's DISC 04 paper and
101 * http://www.cs.rochester.edu/research/synchronization/pseudocode/duals.html
102 *
103 * This works great in principle. But in practice, like many
104 * algorithms centered on atomic updates to a single location, it
105 * scales horribly when there are more than a few participants
106 * using the same Exchanger. So the implementation instead uses a
107 * form of elimination arena, that spreads out this contention by
108 * arranging that some threads typically use different slots,
109 * while still ensuring that eventually, any two parties will be
110 * able to exchange items. That is, we cannot completely partition
111 * across threads, but instead give threads arena indices that
112 * will on average grow under contention and shrink under lack of
113 * contention. We approach this by defining the Nodes that we need
114 * anyway as ThreadLocals, and include in them per-thread index
115 * and related bookkeeping state. (We can safely reuse per-thread
116 * nodes rather than creating them fresh each time because slots
117 * alternate between pointing to a node vs null, so cannot
118 * encounter ABA problems. However, we do need some care in
119 * resetting them between uses.)
120 *
121 * Implementing an effective arena requires allocating a bunch of
122 * space, so we only do so upon detecting contention (except on
123 * uniprocessors, where they wouldn't help, so aren't used).
124 * Otherwise, exchanges use the single-slot slotExchange method.
125 * On contention, not only must the slots be in different
126 * locations, but the locations must not encounter memory
127 * contention due to being on the same cache line (or more
128 * generally, the same coherence unit). Because, as of this
129 * writing, there is no way to determine cacheline size, we define
130 * a value that is enough for common platforms. Additionally,
131 * extra care elsewhere is taken to avoid other false/unintended
132 * sharing and to enhance locality, including adding padding to
133 * Nodes, embedding "bound" as an Exchanger field, and reworking
134 * some park/unpark mechanics compared to LockSupport versions.
135 *
136 * The arena starts out with only one used slot. We expand the
137 * effective arena size by tracking collisions; i.e., failed CASes
138 * while trying to exchange. By nature of the above algorithm, the
139 * only kinds of collision that reliably indicate contention are
140 * when two attempted releases collide -- one of two attempted
141 * offers can legitimately fail to CAS without indicating
142 * contention by more than one other thread. (Note: it is possible
143 * but not worthwhile to more precisely detect contention by
144 * reading slot values after CAS failures.) When a thread has
145 * collided at each slot within the current arena bound, it tries
146 * to expand the arena size by one. We track collisions within
147 * bounds by using a version (sequence) number on the "bound"
148 * field, and conservatively reset collision counts when a
149 * participant notices that bound has been updated (in either
150 * direction).
151 *
152 * The effective arena size is reduced (when there is more than
153 * one slot) by giving up on waiting after a while and trying to
154 * decrement the arena size on expiration. The value of "a while"
155 * is an empirical matter. We implement by piggybacking on the
156 * use of spin->yield->block that is essential for reasonable
157 * waiting performance anyway -- in a busy exchanger, offers are
158 * usually almost immediately released, in which case context
159 * switching on multiprocessors is extremely slow/wasteful. Arena
160 * waits just omit the blocking part, and instead cancel. The spin
161 * count is empirically chosen to be a value that avoids blocking
162 * 99% of the time under maximum sustained exchange rates on a
163 * range of test machines. Spins and yields entail some limited
164 * randomness (using a cheap xorshift) to avoid regular patterns
165 * that can induce unproductive grow/shrink cycles. (Using a
166 * pseudorandom also helps regularize spin cycle duration by
167 * making branches unpredictable.) Also, during an offer, a
168 * waiter can "know" that it will be released when its slot has
169 * changed, but cannot yet proceed until match is set. In the
170 * mean time it cannot cancel the offer, so instead spins/yields.
171 * Note: It is possible to avoid this secondary check by changing
172 * the linearization point to be a CAS of the match field (as done
173 * in one case in the Scott & Scherer DISC paper), which also
174 * increases asynchrony a bit, at the expense of poorer collision
175 * detection and inability to always reuse per-thread nodes. So
176 * the current scheme is typically a better tradeoff.
177 *
178 * On collisions, indices traverse the arena cyclically in reverse
179 * order, restarting at the maximum index (which will tend to be
180 * sparsest) when bounds change. (On expirations, indices instead
181 * are halved until reaching 0.) It is possible (and has been
182 * tried) to use randomized, prime-value-stepped, or double-hash
183 * style traversal instead of simple cyclic traversal to reduce
184 * bunching. But empirically, whatever benefits these may have
185 * don't overcome their added overhead: We are managing operations
186 * that occur very quickly unless there is sustained contention,
187 * so simpler/faster control policies work better than more
188 * accurate but slower ones.
189 *
190 * Because we use expiration for arena size control, we cannot
191 * throw TimeoutExceptions in the timed version of the public
192 * exchange method until the arena size has shrunken to zero (or
193 * the arena isn't enabled). This may delay response to timeout
194 * but is still within spec.
195 *
196 * Essentially all of the implementation is in methods
197 * slotExchange and arenaExchange. These have similar overall
198 * structure, but differ in too many details to combine. The
199 * slotExchange method uses the single Exchanger field "slot"
200 * rather than arena array elements. However, it still needs
201 * minimal collision detection to trigger arena construction.
202 * (The messiest part is making sure interrupt status and
203 * InterruptedExceptions come out right during transitions when
204 * both methods may be called. This is done by using null return
205 * as a sentinel to recheck interrupt status.)
206 *
207 * As is too common in this sort of code, methods are monolithic
208 * because most of the logic relies on reads of fields that are
209 * maintained as local variables so can't be nicely factored --
210 * mainly, here, bulky spin->yield->block/cancel code), and
211 * heavily dependent on intrinsics (Unsafe) to use inlined
212 * embedded CAS and related memory access operations (that tend
213 * not to be as readily inlined by dynamic compilers when they are
214 * hidden behind other methods that would more nicely name and
215 * encapsulate the intended effects). This includes the use of
216 * putOrderedX to clear fields of the per-thread Nodes between
217 * uses. Note that field Node.item is not declared as volatile
218 * even though it is read by releasing threads, because they only
219 * do so after CAS operations that must precede access, and all
220 * uses by the owning thread are otherwise acceptably ordered by
221 * other operations. (Because the actual points of atomicity are
222 * slot CASes, it would also be legal for the write to Node.match
223 * in a release to be weaker than a full volatile write. However,
224 * this is not done because it could allow further postponement of
225 * the write, delaying progress.)
226 */
227
228 /**
229 * The byte distance (as a shift value) between any two used slots
230 * in the arena. 1 << ASHIFT should be at least cacheline size.
231 */
232 private static final int ASHIFT = 7;
233
234 /**
235 * The maximum supported arena index. The maximum allocatable
236 * arena size is MMASK + 1. Must be a power of two minus one, less
237 * than (1<<(31-ASHIFT)). The cap of 255 (0xff) more than suffices
238 * for the expected scaling limits of the main algorithms.
239 */
240 private static final int MMASK = 0xff;
241
242 /**
243 * Unit for sequence/version bits of bound field. Each successful
244 * change to the bound also adds SEQ.
245 */
246 private static final int SEQ = MMASK + 1;
247
248 /** The number of CPUs, for sizing and spin control */
249 private static final int NCPU = Runtime.getRuntime().availableProcessors();
250
251 /**
252 * The maximum slot index of the arena: The number of slots that
253 * can in principle hold all threads without contention, or at
254 * most the maximum indexable value.
255 */
256 static final int FULL = (NCPU >= (MMASK << 1)) ? MMASK : NCPU >>> 1;
257
258 /**
259 * The bound for spins while waiting for a match. The actual
260 * number of iterations will on average be about twice this value
261 * due to randomization. Note: Spinning is disabled when NCPU==1.
262 */
263 private static final int SPINS = 1 << 10;
264
265 /**
266 * Value representing null arguments/returns from public
267 * methods. Needed because the API originally didn't disallow null
268 * arguments, which it should have.
269 */
270 private static final Object NULL_ITEM = new Object();
271
272 /**
273 * Sentinel value returned by internal exchange methods upon
274 * timeout, to avoid need for separate timed versions of these
275 * methods.
276 */
277 private static final Object TIMED_OUT = new Object();
278
279 /**
280 * Nodes hold partially exchanged data, plus other per-thread
281 * bookkeeping.
282 */
283 static final class Node {
284 int index; // Arena index
285 int bound; // Last recorded value of Exchanger.bound
286 int collides; // Number of CAS failures at current bound
287 int hash; // Pseudo-random for spins
288 Object item; // This thread's current item
289 volatile Object match; // Item provided by releasing thread
290 volatile Thread parked; // Set to this thread when parked, else null
291
292 // Padding to ameliorate unfortunate memory placements
293 Object p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, pa, pb, pc, pd, pe, pf;
294 Object q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, qa, qb, qc, qd, qe, qf;
295 }
296
297 /** The corresponding thread local class */
298 static final class Participant extends ThreadLocal<Node> {
299 public Node initialValue() { return new Node(); }
300 }
301
302 /**
303 * Per-thread state
304 */
305 private final Participant participant;
306
307 /**
308 * Elimination array; null until enabled (within slotExchange).
309 * Element accesses use emulation of volatile gets and CAS.
310 */
311 private volatile Node[] arena;
312
313 /**
314 * Slot used until contention detected.
315 */
316 private volatile Node slot;
317
318 /**
319 * The index of the largest valid arena position, OR'ed with SEQ
320 * number in high bits, incremented on each update. The initial
321 * update from 0 to SEQ is used to ensure that the arena array is
322 * constructed only once.
323 */
324 private volatile int bound;
325
326 /**
327 * Exchange function when arenas enabled. See above for explanation.
328 *
329 * @param item the (non-null) item to exchange
330 * @param timed true if the wait is timed
331 * @param ns if timed, the maximum wait time, else 0L
332 * @return the other thread's item; or null if interrupted; or
333 * TIMED_OUT if timed and timed out
334 */
335 private final Object arenaExchange(Object item, boolean timed, long ns) {
336 Node[] a = arena;
337 Node p = participant.get();
338 for (int i = p.index;;) { // access slot at i
339 int b, m, c; long j; // j is raw array offset
340 Node q = (Node)U.getObjectVolatile(a, j = (i << ASHIFT) + ABASE);
341 if (q != null && U.compareAndSwapObject(a, j, q, null)) {
342 Object v = q.item; // release
343 q.match = item;
344 Thread w = q.parked;
345 if (w != null)
346 U.unpark(w);
347 return v;
348 }
349 else if (i <= (m = (b = bound) & MMASK) && q == null) {
350 p.item = item; // offer
351 if (U.compareAndSwapObject(a, j, null, p)) {
352 long end = (timed && m == 0) ? System.nanoTime() + ns : 0L;
353 Thread t = Thread.currentThread(); // wait
354 for (int h = p.hash, spins = SPINS;;) {
355 Object v = p.match;
356 if (v != null) {
357 U.putOrderedObject(p, MATCH, null);
358 p.item = null; // clear for next use
359 p.hash = h;
360 return v;
361 }
362 else if (spins > 0) {
363 h ^= h << 1; h ^= h >>> 3; h ^= h << 10; // xorshift
364 if (h == 0) // initialize hash
365 h = SPINS | (int)t.getId();
366 else if (h < 0 && // approx 50% true
367 (--spins & ((SPINS >>> 1) - 1)) == 0)
368 Thread.yield(); // two yields per wait
369 }
370 else if (U.getObjectVolatile(a, j) != p)
371 spins = SPINS; // releaser hasn't set match yet
372 else if (!t.isInterrupted() && m == 0 &&
373 (!timed ||
374 (ns = end - System.nanoTime()) > 0L)) {
375 U.putObject(t, BLOCKER, this); // emulate LockSupport
376 p.parked = t; // minimize window
377 if (U.getObjectVolatile(a, j) == p)
378 U.park(false, ns);
379 p.parked = null;
380 U.putObject(t, BLOCKER, null);
381 }
382 else if (U.getObjectVolatile(a, j) == p &&
383 U.compareAndSwapObject(a, j, p, null)) {
384 if (m != 0) // try to shrink
385 U.compareAndSwapInt(this, BOUND, b, b + SEQ - 1);
386 p.item = null;
387 p.hash = h;
388 i = p.index >>>= 1; // descend
389 if (Thread.interrupted())
390 return null;
391 if (timed && m == 0 && ns <= 0L)
392 return TIMED_OUT;
393 break; // expired; restart
394 }
395 }
396 }
397 else
398 p.item = null; // clear offer
399 }
400 else {
401 if (p.bound != b) { // stale; reset
402 p.bound = b;
403 p.collides = 0;
404 i = (i != m || m == 0) ? m : m - 1;
405 }
406 else if ((c = p.collides) < m || m == FULL ||
407 !U.compareAndSwapInt(this, BOUND, b, b + SEQ + 1)) {
408 p.collides = c + 1;
409 i = (i == 0) ? m : i - 1; // cyclically traverse
410 }
411 else
412 i = m + 1; // grow
413 p.index = i;
414 }
415 }
416 }
417
418 /**
419 * Exchange function used until arenas enabled. See above for explanation.
420 *
421 * @param item the item to exchange
422 * @param timed true if the wait is timed
423 * @param ns if timed, the maximum wait time, else 0L
424 * @return the other thread's item; or null if either the arena
425 * was enabled or the thread was interrupted before completion; or
426 * TIMED_OUT if timed and timed out
427 */
428 private final Object slotExchange(Object item, boolean timed, long ns) {
429 Node p = participant.get();
430 Thread t = Thread.currentThread();
431 if (t.isInterrupted()) // preserve interrupt status so caller can recheck
432 return null;
433
434 for (Node q;;) {
435 if ((q = slot) != null) {
436 if (U.compareAndSwapObject(this, SLOT, q, null)) {
437 Object v = q.item;
438 q.match = item;
439 Thread w = q.parked;
440 if (w != null)
441 U.unpark(w);
442 return v;
443 }
444 // create arena on contention, but continue until slot null
445 if (NCPU > 1 && bound == 0 &&
446 U.compareAndSwapInt(this, BOUND, 0, SEQ))
447 arena = new Node[(FULL + 2) << ASHIFT];
448 }
449 else if (arena != null)
450 return null; // caller must reroute to arenaExchange
451 else {
452 p.item = item;
453 if (U.compareAndSwapObject(this, SLOT, null, p))
454 break;
455 p.item = null;
456 }
457 }
458
459 // await release
460 int h = p.hash;
461 long end = timed ? System.nanoTime() + ns : 0L;
462 int spins = (NCPU > 1) ? SPINS : 1;
463 Object v;
464 while ((v = p.match) == null) {
465 if (spins > 0) {
466 h ^= h << 1; h ^= h >>> 3; h ^= h << 10;
467 if (h == 0)
468 h = SPINS | (int)t.getId();
469 else if (h < 0 && (--spins & ((SPINS >>> 1) - 1)) == 0)
470 Thread.yield();
471 }
472 else if (slot != p)
473 spins = SPINS;
474 else if (!t.isInterrupted() && arena == null &&
475 (!timed || (ns = end - System.nanoTime()) > 0L)) {
476 U.putObject(t, BLOCKER, this);
477 p.parked = t;
478 if (slot == p)
479 U.park(false, ns);
480 p.parked = null;
481 U.putObject(t, BLOCKER, null);
482 }
483 else if (U.compareAndSwapObject(this, SLOT, p, null)) {
484 v = timed && ns <= 0L && !t.isInterrupted() ? TIMED_OUT : null;
485 break;
486 }
487 }
488 U.putOrderedObject(p, MATCH, null);
489 p.item = null;
490 p.hash = h;
491 return v;
492 }
493
494 /**
495 * Creates a new Exchanger.
496 */
497 public Exchanger() {
498 participant = new Participant();
499 }
500
501 /**
502 * Waits for another thread to arrive at this exchange point (unless
503 * the current thread is {@linkplain Thread#interrupt interrupted}),
504 * and then transfers the given object to it, receiving its object
505 * in return.
506 *
507 * <p>If another thread is already waiting at the exchange point then
508 * it is resumed for thread scheduling purposes and receives the object
509 * passed in by the current thread. The current thread returns immediately,
510 * receiving the object passed to the exchange by that other thread.
511 *
512 * <p>If no other thread is already waiting at the exchange then the
513 * current thread is disabled for thread scheduling purposes and lies
514 * dormant until one of two things happens:
515 * <ul>
516 * <li>Some other thread enters the exchange; or
517 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
518 * the current thread.
519 * </ul>
520 * <p>If the current thread:
521 * <ul>
522 * <li>has its interrupted status set on entry to this method; or
523 * <li>is {@linkplain Thread#interrupt interrupted} while waiting
524 * for the exchange,
525 * </ul>
526 * then {@link InterruptedException} is thrown and the current thread's
527 * interrupted status is cleared.
528 *
529 * @param x the object to exchange
530 * @return the object provided by the other thread
531 * @throws InterruptedException if the current thread was
532 * interrupted while waiting
533 */
534 @SuppressWarnings("unchecked")
535 public V exchange(V x) throws InterruptedException {
536 Object v;
537 Object item = (x == null) ? NULL_ITEM : x; // translate null args
538 if ((arena != null ||
539 (v = slotExchange(item, false, 0L)) == null) &&
540 ((Thread.interrupted() || // disambiguates null return
541 (v = arenaExchange(item, false, 0L)) == null)))
542 throw new InterruptedException();
543 return (v == NULL_ITEM) ? null : (V)v;
544 }
545
546 /**
547 * Waits for another thread to arrive at this exchange point (unless
548 * the current thread is {@linkplain Thread#interrupt interrupted} or
549 * the specified waiting time elapses), and then transfers the given
550 * object to it, receiving its object in return.
551 *
552 * <p>If another thread is already waiting at the exchange point then
553 * it is resumed for thread scheduling purposes and receives the object
554 * passed in by the current thread. The current thread returns immediately,
555 * receiving the object passed to the exchange by that other thread.
556 *
557 * <p>If no other thread is already waiting at the exchange then the
558 * current thread is disabled for thread scheduling purposes and lies
559 * dormant until one of three things happens:
560 * <ul>
561 * <li>Some other thread enters the exchange; or
562 * <li>Some other thread {@linkplain Thread#interrupt interrupts}
563 * the current thread; or
564 * <li>The specified waiting time elapses.
565 * </ul>
566 * <p>If the current thread:
567 * <ul>
568 * <li>has its interrupted status set on entry to this method; or
569 * <li>is {@linkplain Thread#interrupt interrupted} while waiting
570 * for the exchange,
571 * </ul>
572 * then {@link InterruptedException} is thrown and the current thread's
573 * interrupted status is cleared.
574 *
575 * <p>If the specified waiting time elapses then {@link
576 * TimeoutException} is thrown. If the time is less than or equal
577 * to zero, the method will not wait at all.
578 *
579 * @param x the object to exchange
580 * @param timeout the maximum time to wait
581 * @param unit the time unit of the {@code timeout} argument
582 * @return the object provided by the other thread
583 * @throws InterruptedException if the current thread was
584 * interrupted while waiting
585 * @throws TimeoutException if the specified waiting time elapses
586 * before another thread enters the exchange
587 */
588 @SuppressWarnings("unchecked")
589 public V exchange(V x, long timeout, TimeUnit unit)
590 throws InterruptedException, TimeoutException {
591 Object v;
592 Object item = (x == null) ? NULL_ITEM : x;
593 long ns = unit.toNanos(timeout);
594 if ((arena != null ||
595 (v = slotExchange(item, true, ns)) == null) &&
596 ((Thread.interrupted() ||
597 (v = arenaExchange(item, true, ns)) == null)))
598 throw new InterruptedException();
599 if (v == TIMED_OUT)
600 throw new TimeoutException();
601 return (v == NULL_ITEM) ? null : (V)v;
602 }
603
604 // Unsafe mechanics
605 private static final sun.misc.Unsafe U;
606 private static final long BOUND;
607 private static final long SLOT;
608 private static final long MATCH;
609 private static final long BLOCKER;
610 private static final int ABASE;
611 static {
612 int s;
613 try {
614 U = sun.misc.Unsafe.getUnsafe();
615 Class<?> ek = Exchanger.class;
616 Class<?> nk = Node.class;
617 Class<?> ak = Node[].class;
618 Class<?> tk = Thread.class;
619 BOUND = U.objectFieldOffset
620 (ek.getDeclaredField("bound"));
621 SLOT = U.objectFieldOffset
622 (ek.getDeclaredField("slot"));
623 MATCH = U.objectFieldOffset
624 (nk.getDeclaredField("match"));
625 BLOCKER = U.objectFieldOffset
626 (tk.getDeclaredField("parkBlocker"));
627 s = U.arrayIndexScale(ak);
628 // ABASE absorbs padding in front of element 0
629 ABASE = U.arrayBaseOffset(ak) + (1 << ASHIFT);
630
631 } catch (Exception e) {
632 throw new Error(e);
633 }
634 if ((s & (s-1)) != 0 || s > (1 << ASHIFT))
635 throw new Error("Unsupported array scale");
636 }
637
638 }