concurrent-ruby 1.0.5 → 1.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +42 -0
  3. data/Gemfile +39 -0
  4. data/{LICENSE.txt → LICENSE.md} +2 -0
  5. data/README.md +203 -105
  6. data/Rakefile +278 -0
  7. data/ext/concurrent-ruby/ConcurrentRubyService.java +17 -0
  8. data/ext/concurrent-ruby/com/concurrent_ruby/ext/AtomicReferenceLibrary.java +175 -0
  9. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JRubyMapBackendLibrary.java +248 -0
  10. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicBooleanLibrary.java +93 -0
  11. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +113 -0
  12. data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +159 -0
  13. data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +304 -0
  14. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMap.java +31 -0
  15. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMapV8.java +3863 -0
  16. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/LongAdder.java +203 -0
  17. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/Striped64.java +342 -0
  18. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/ConcurrentHashMapV8.java +3800 -0
  19. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/LongAdder.java +204 -0
  20. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/Striped64.java +291 -0
  21. data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166y/ThreadLocalRandom.java +199 -0
  22. data/lib/concurrent-ruby.rb +1 -0
  23. data/lib/concurrent.rb +24 -20
  24. data/lib/concurrent/agent.rb +7 -7
  25. data/lib/concurrent/array.rb +59 -32
  26. data/lib/concurrent/async.rb +4 -4
  27. data/lib/concurrent/atom.rb +9 -9
  28. data/lib/concurrent/atomic/atomic_boolean.rb +24 -20
  29. data/lib/concurrent/atomic/atomic_fixnum.rb +27 -23
  30. data/lib/concurrent/atomic/atomic_markable_reference.rb +164 -0
  31. data/lib/concurrent/atomic/atomic_reference.rb +176 -33
  32. data/lib/concurrent/atomic/count_down_latch.rb +6 -6
  33. data/lib/concurrent/atomic/cyclic_barrier.rb +1 -1
  34. data/lib/concurrent/atomic/event.rb +1 -1
  35. data/lib/concurrent/atomic/java_count_down_latch.rb +6 -5
  36. data/lib/concurrent/atomic/mutex_count_down_latch.rb +1 -0
  37. data/lib/concurrent/atomic/read_write_lock.rb +2 -1
  38. data/lib/concurrent/atomic/reentrant_read_write_lock.rb +3 -1
  39. data/lib/concurrent/atomic/semaphore.rb +8 -8
  40. data/lib/concurrent/atomic/thread_local_var.rb +7 -7
  41. data/lib/concurrent/atomic_reference/mutex_atomic.rb +3 -8
  42. data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +1 -1
  43. data/lib/concurrent/atomics.rb +0 -43
  44. data/lib/concurrent/collection/lock_free_stack.rb +127 -0
  45. data/lib/concurrent/collection/map/atomic_reference_map_backend.rb +3 -3
  46. data/lib/concurrent/collection/map/non_concurrent_map_backend.rb +1 -2
  47. data/lib/concurrent/collection/non_concurrent_priority_queue.rb +29 -29
  48. data/lib/concurrent/concern/dereferenceable.rb +1 -1
  49. data/lib/concurrent/concern/logging.rb +6 -1
  50. data/lib/concurrent/concern/observable.rb +7 -7
  51. data/lib/concurrent/concurrent_ruby.jar +0 -0
  52. data/lib/concurrent/configuration.rb +1 -6
  53. data/lib/concurrent/constants.rb +1 -1
  54. data/lib/concurrent/dataflow.rb +2 -1
  55. data/lib/concurrent/delay.rb +9 -7
  56. data/lib/concurrent/exchanger.rb +13 -21
  57. data/lib/concurrent/executor/abstract_executor_service.rb +2 -2
  58. data/lib/concurrent/executor/cached_thread_pool.rb +1 -1
  59. data/lib/concurrent/executor/executor_service.rb +15 -15
  60. data/lib/concurrent/executor/fixed_thread_pool.rb +18 -18
  61. data/lib/concurrent/executor/java_thread_pool_executor.rb +10 -7
  62. data/lib/concurrent/executor/single_thread_executor.rb +2 -2
  63. data/lib/concurrent/executor/thread_pool_executor.rb +6 -6
  64. data/lib/concurrent/executor/timer_set.rb +1 -1
  65. data/lib/concurrent/future.rb +4 -1
  66. data/lib/concurrent/hash.rb +53 -30
  67. data/lib/concurrent/ivar.rb +5 -6
  68. data/lib/concurrent/map.rb +20 -25
  69. data/lib/concurrent/maybe.rb +1 -1
  70. data/lib/concurrent/mutable_struct.rb +15 -14
  71. data/lib/concurrent/mvar.rb +2 -2
  72. data/lib/concurrent/promise.rb +53 -21
  73. data/lib/concurrent/promises.rb +1938 -0
  74. data/lib/concurrent/re_include.rb +58 -0
  75. data/lib/concurrent/set.rb +66 -0
  76. data/lib/concurrent/settable_struct.rb +1 -0
  77. data/lib/concurrent/synchronization.rb +4 -5
  78. data/lib/concurrent/synchronization/abstract_lockable_object.rb +5 -5
  79. data/lib/concurrent/synchronization/abstract_struct.rb +6 -4
  80. data/lib/concurrent/synchronization/lockable_object.rb +6 -6
  81. data/lib/concurrent/synchronization/{mri_lockable_object.rb → mutex_lockable_object.rb} +19 -14
  82. data/lib/concurrent/synchronization/object.rb +8 -4
  83. data/lib/concurrent/synchronization/truffleruby_object.rb +46 -0
  84. data/lib/concurrent/synchronization/volatile.rb +11 -9
  85. data/lib/concurrent/thread_safe/util/data_structures.rb +55 -0
  86. data/lib/concurrent/thread_safe/util/striped64.rb +9 -4
  87. data/lib/concurrent/timer_task.rb +5 -2
  88. data/lib/concurrent/tuple.rb +1 -1
  89. data/lib/concurrent/tvar.rb +2 -2
  90. data/lib/concurrent/utility/at_exit.rb +1 -1
  91. data/lib/concurrent/utility/engine.rb +2 -2
  92. data/lib/concurrent/utility/monotonic_time.rb +3 -3
  93. data/lib/concurrent/utility/native_extension_loader.rb +31 -33
  94. data/lib/concurrent/utility/processor_counter.rb +0 -2
  95. data/lib/concurrent/version.rb +2 -2
  96. metadata +35 -21
  97. data/lib/concurrent/atomic_reference/concurrent_update_error.rb +0 -8
  98. data/lib/concurrent/atomic_reference/direct_update.rb +0 -81
  99. data/lib/concurrent/atomic_reference/jruby+truffle.rb +0 -2
  100. data/lib/concurrent/atomic_reference/jruby.rb +0 -16
  101. data/lib/concurrent/atomic_reference/rbx.rb +0 -22
  102. data/lib/concurrent/atomic_reference/ruby.rb +0 -32
  103. data/lib/concurrent/edge.rb +0 -26
  104. data/lib/concurrent/lazy_register.rb +0 -81
  105. data/lib/concurrent/synchronization/truffle_lockable_object.rb +0 -9
  106. data/lib/concurrent/synchronization/truffle_object.rb +0 -31
  107. data/lib/concurrent/thread_safe/util/array_hash_rbx.rb +0 -30
@@ -0,0 +1,204 @@
1
+ /*
2
+ * Written by Doug Lea with assistance from members of JCP JSR-166
3
+ * Expert Group and released to the public domain, as explained at
4
+ * http://creativecommons.org/publicdomain/zero/1.0/
5
+ */
6
+
7
+ // This is based on 1.9 version.
8
+
9
+ package com.concurrent_ruby.ext.jsr166e.nounsafe;
10
+
11
+ import java.util.concurrent.atomic.AtomicLong;
12
+ import java.io.IOException;
13
+ import java.io.Serializable;
14
+ import java.io.ObjectInputStream;
15
+
16
+ /**
17
+ * One or more variables that together maintain an initially zero
18
+ * {@code long} sum. When updates (method {@link #add}) are contended
19
+ * across threads, the set of variables may grow dynamically to reduce
20
+ * contention. Method {@link #sum} (or, equivalently, {@link
21
+ * #longValue}) returns the current total combined across the
22
+ * variables maintaining the sum.
23
+ *
24
+ * <p>This class is usually preferable to {@link AtomicLong} when
25
+ * multiple threads update a common sum that is used for purposes such
26
+ * as collecting statistics, not for fine-grained synchronization
27
+ * control. Under low update contention, the two classes have similar
28
+ * characteristics. But under high contention, expected throughput of
29
+ * this class is significantly higher, at the expense of higher space
30
+ * consumption.
31
+ *
32
+ * <p>This class extends {@link Number}, but does <em>not</em> define
33
+ * methods such as {@code hashCode} and {@code compareTo} because
34
+ * instances are expected to be mutated, and so are not useful as
35
+ * collection keys.
36
+ *
37
+ * <p><em>jsr166e note: This class is targeted to be placed in
38
+ * java.util.concurrent.atomic.</em>
39
+ *
40
+ * @since 1.8
41
+ * @author Doug Lea
42
+ */
43
+ public class LongAdder extends Striped64 implements Serializable {
44
+ private static final long serialVersionUID = 7249069246863182397L;
45
+
46
+ /**
47
+ * Version of plus for use in retryUpdate
48
+ */
49
+ final long fn(long v, long x) { return v + x; }
50
+
51
+ /**
52
+ * Creates a new adder with initial sum of zero.
53
+ */
54
+ public LongAdder() {
55
+ }
56
+
57
+ /**
58
+ * Adds the given value.
59
+ *
60
+ * @param x the value to add
61
+ */
62
+ public void add(long x) {
63
+ Cell[] as; long b, v; HashCode hc; Cell a; int n;
64
+ if ((as = cells) != null || !casBase(b = base, b + x)) {
65
+ boolean uncontended = true;
66
+ int h = (hc = threadHashCode.get()).code;
67
+ if (as == null || (n = as.length) < 1 ||
68
+ (a = as[(n - 1) & h]) == null ||
69
+ !(uncontended = a.cas(v = a.value, v + x)))
70
+ retryUpdate(x, hc, uncontended);
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Equivalent to {@code add(1)}.
76
+ */
77
+ public void increment() {
78
+ add(1L);
79
+ }
80
+
81
+ /**
82
+ * Equivalent to {@code add(-1)}.
83
+ */
84
+ public void decrement() {
85
+ add(-1L);
86
+ }
87
+
88
+ /**
89
+ * Returns the current sum. The returned value is <em>NOT</em> an
90
+ * atomic snapshot: Invocation in the absence of concurrent
91
+ * updates returns an accurate result, but concurrent updates that
92
+ * occur while the sum is being calculated might not be
93
+ * incorporated.
94
+ *
95
+ * @return the sum
96
+ */
97
+ public long sum() {
98
+ long sum = base;
99
+ Cell[] as = cells;
100
+ if (as != null) {
101
+ int n = as.length;
102
+ for (int i = 0; i < n; ++i) {
103
+ Cell a = as[i];
104
+ if (a != null)
105
+ sum += a.value;
106
+ }
107
+ }
108
+ return sum;
109
+ }
110
+
111
+ /**
112
+ * Resets variables maintaining the sum to zero. This method may
113
+ * be a useful alternative to creating a new adder, but is only
114
+ * effective if there are no concurrent updates. Because this
115
+ * method is intrinsically racy, it should only be used when it is
116
+ * known that no threads are concurrently updating.
117
+ */
118
+ public void reset() {
119
+ internalReset(0L);
120
+ }
121
+
122
+ /**
123
+ * Equivalent in effect to {@link #sum} followed by {@link
124
+ * #reset}. This method may apply for example during quiescent
125
+ * points between multithreaded computations. If there are
126
+ * updates concurrent with this method, the returned value is
127
+ * <em>not</em> guaranteed to be the final value occurring before
128
+ * the reset.
129
+ *
130
+ * @return the sum
131
+ */
132
+ public long sumThenReset() {
133
+ long sum = base;
134
+ Cell[] as = cells;
135
+ base = 0L;
136
+ if (as != null) {
137
+ int n = as.length;
138
+ for (int i = 0; i < n; ++i) {
139
+ Cell a = as[i];
140
+ if (a != null) {
141
+ sum += a.value;
142
+ a.value = 0L;
143
+ }
144
+ }
145
+ }
146
+ return sum;
147
+ }
148
+
149
+ /**
150
+ * Returns the String representation of the {@link #sum}.
151
+ * @return the String representation of the {@link #sum}
152
+ */
153
+ public String toString() {
154
+ return Long.toString(sum());
155
+ }
156
+
157
+ /**
158
+ * Equivalent to {@link #sum}.
159
+ *
160
+ * @return the sum
161
+ */
162
+ public long longValue() {
163
+ return sum();
164
+ }
165
+
166
+ /**
167
+ * Returns the {@link #sum} as an {@code int} after a narrowing
168
+ * primitive conversion.
169
+ */
170
+ public int intValue() {
171
+ return (int)sum();
172
+ }
173
+
174
+ /**
175
+ * Returns the {@link #sum} as a {@code float}
176
+ * after a widening primitive conversion.
177
+ */
178
+ public float floatValue() {
179
+ return (float)sum();
180
+ }
181
+
182
+ /**
183
+ * Returns the {@link #sum} as a {@code double} after a widening
184
+ * primitive conversion.
185
+ */
186
+ public double doubleValue() {
187
+ return (double)sum();
188
+ }
189
+
190
+ private void writeObject(java.io.ObjectOutputStream s)
191
+ throws java.io.IOException {
192
+ s.defaultWriteObject();
193
+ s.writeLong(sum());
194
+ }
195
+
196
+ private void readObject(ObjectInputStream s)
197
+ throws IOException, ClassNotFoundException {
198
+ s.defaultReadObject();
199
+ busy = 0;
200
+ cells = null;
201
+ base = s.readLong();
202
+ }
203
+
204
+ }
@@ -0,0 +1,291 @@
1
+ /*
2
+ * Written by Doug Lea with assistance from members of JCP JSR-166
3
+ * Expert Group and released to the public domain, as explained at
4
+ * http://creativecommons.org/publicdomain/zero/1.0/
5
+ */
6
+
7
+ // This is based on 1.5 version.
8
+
9
+ package com.concurrent_ruby.ext.jsr166e.nounsafe;
10
+
11
+ import java.util.Random;
12
+ import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
13
+ import java.util.concurrent.atomic.AtomicLongFieldUpdater;
14
+
15
+ /**
16
+ * A package-local class holding common representation and mechanics
17
+ * for classes supporting dynamic striping on 64bit values. The class
18
+ * extends Number so that concrete subclasses must publicly do so.
19
+ */
20
+ abstract class Striped64 extends Number {
21
+ /*
22
+ * This class maintains a lazily-initialized table of atomically
23
+ * updated variables, plus an extra "base" field. The table size
24
+ * is a power of two. Indexing uses masked per-thread hash codes.
25
+ * Nearly all declarations in this class are package-private,
26
+ * accessed directly by subclasses.
27
+ *
28
+ * Table entries are of class Cell; a variant of AtomicLong padded
29
+ * to reduce cache contention on most processors. Padding is
30
+ * overkill for most Atomics because they are usually irregularly
31
+ * scattered in memory and thus don't interfere much with each
32
+ * other. But Atomic objects residing in arrays will tend to be
33
+ * placed adjacent to each other, and so will most often share
34
+ * cache lines (with a huge negative performance impact) without
35
+ * this precaution.
36
+ *
37
+ * In part because Cells are relatively large, we avoid creating
38
+ * them until they are needed. When there is no contention, all
39
+ * updates are made to the base field. Upon first contention (a
40
+ * failed CAS on base update), the table is initialized to size 2.
41
+ * The table size is doubled upon further contention until
42
+ * reaching the nearest power of two greater than or equal to the
43
+ * number of CPUS. Table slots remain empty (null) until they are
44
+ * needed.
45
+ *
46
+ * A single spinlock ("busy") is used for initializing and
47
+ * resizing the table, as well as populating slots with new Cells.
48
+ * There is no need for a blocking lock: When the lock is not
49
+ * available, threads try other slots (or the base). During these
50
+ * retries, there is increased contention and reduced locality,
51
+ * which is still better than alternatives.
52
+ *
53
+ * Per-thread hash codes are initialized to random values.
54
+ * Contention and/or table collisions are indicated by failed
55
+ * CASes when performing an update operation (see method
56
+ * retryUpdate). Upon a collision, if the table size is less than
57
+ * the capacity, it is doubled in size unless some other thread
58
+ * holds the lock. If a hashed slot is empty, and lock is
59
+ * available, a new Cell is created. Otherwise, if the slot
60
+ * exists, a CAS is tried. Retries proceed by "double hashing",
61
+ * using a secondary hash (Marsaglia XorShift) to try to find a
62
+ * free slot.
63
+ *
64
+ * The table size is capped because, when there are more threads
65
+ * than CPUs, supposing that each thread were bound to a CPU,
66
+ * there would exist a perfect hash function mapping threads to
67
+ * slots that eliminates collisions. When we reach capacity, we
68
+ * search for this mapping by randomly varying the hash codes of
69
+ * colliding threads. Because search is random, and collisions
70
+ * only become known via CAS failures, convergence can be slow,
71
+ * and because threads are typically not bound to CPUS forever,
72
+ * may not occur at all. However, despite these limitations,
73
+ * observed contention rates are typically low in these cases.
74
+ *
75
+ * It is possible for a Cell to become unused when threads that
76
+ * once hashed to it terminate, as well as in the case where
77
+ * doubling the table causes no thread to hash to it under
78
+ * expanded mask. We do not try to detect or remove such cells,
79
+ * under the assumption that for long-running instances, observed
80
+ * contention levels will recur, so the cells will eventually be
81
+ * needed again; and for short-lived ones, it does not matter.
82
+ */
83
+
84
+ /**
85
+ * Padded variant of AtomicLong supporting only raw accesses plus CAS.
86
+ * The value field is placed between pads, hoping that the JVM doesn't
87
+ * reorder them.
88
+ *
89
+ * JVM intrinsics note: It would be possible to use a release-only
90
+ * form of CAS here, if it were provided.
91
+ */
92
+ static final class Cell {
93
+ volatile long p0, p1, p2, p3, p4, p5, p6;
94
+ volatile long value;
95
+ volatile long q0, q1, q2, q3, q4, q5, q6;
96
+
97
+ static AtomicLongFieldUpdater<Cell> VALUE_UPDATER = AtomicLongFieldUpdater.newUpdater(Cell.class, "value");
98
+
99
+ Cell(long x) { value = x; }
100
+
101
+ final boolean cas(long cmp, long val) {
102
+ return VALUE_UPDATER.compareAndSet(this, cmp, val);
103
+ }
104
+
105
+ }
106
+
107
+ /**
108
+ * Holder for the thread-local hash code. The code is initially
109
+ * random, but may be set to a different value upon collisions.
110
+ */
111
+ static final class HashCode {
112
+ static final Random rng = new Random();
113
+ int code;
114
+ HashCode() {
115
+ int h = rng.nextInt(); // Avoid zero to allow xorShift rehash
116
+ code = (h == 0) ? 1 : h;
117
+ }
118
+ }
119
+
120
+ /**
121
+ * The corresponding ThreadLocal class
122
+ */
123
+ static final class ThreadHashCode extends ThreadLocal<HashCode> {
124
+ public HashCode initialValue() { return new HashCode(); }
125
+ }
126
+
127
+ /**
128
+ * Static per-thread hash codes. Shared across all instances to
129
+ * reduce ThreadLocal pollution and because adjustments due to
130
+ * collisions in one table are likely to be appropriate for
131
+ * others.
132
+ */
133
+ static final ThreadHashCode threadHashCode = new ThreadHashCode();
134
+
135
+ /** Number of CPUS, to place bound on table size */
136
+ static final int NCPU = Runtime.getRuntime().availableProcessors();
137
+
138
+ /**
139
+ * Table of cells. When non-null, size is a power of 2.
140
+ */
141
+ transient volatile Cell[] cells;
142
+
143
+ /**
144
+ * Base value, used mainly when there is no contention, but also as
145
+ * a fallback during table initialization races. Updated via CAS.
146
+ */
147
+ transient volatile long base;
148
+
149
+ /**
150
+ * Spinlock (locked via CAS) used when resizing and/or creating Cells.
151
+ */
152
+ transient volatile int busy;
153
+
154
+ AtomicLongFieldUpdater<Striped64> BASE_UPDATER = AtomicLongFieldUpdater.newUpdater(Striped64.class, "base");
155
+ AtomicIntegerFieldUpdater<Striped64> BUSY_UPDATER = AtomicIntegerFieldUpdater.newUpdater(Striped64.class, "busy");
156
+
157
+ /**
158
+ * Package-private default constructor
159
+ */
160
+ Striped64() {
161
+ }
162
+
163
+ /**
164
+ * CASes the base field.
165
+ */
166
+ final boolean casBase(long cmp, long val) {
167
+ return BASE_UPDATER.compareAndSet(this, cmp, val);
168
+ }
169
+
170
+ /**
171
+ * CASes the busy field from 0 to 1 to acquire lock.
172
+ */
173
+ final boolean casBusy() {
174
+ return BUSY_UPDATER.compareAndSet(this, 0, 1);
175
+ }
176
+
177
+ /**
178
+ * Computes the function of current and new value. Subclasses
179
+ * should open-code this update function for most uses, but the
180
+ * virtualized form is needed within retryUpdate.
181
+ *
182
+ * @param currentValue the current value (of either base or a cell)
183
+ * @param newValue the argument from a user update call
184
+ * @return result of the update function
185
+ */
186
+ abstract long fn(long currentValue, long newValue);
187
+
188
+ /**
189
+ * Handles cases of updates involving initialization, resizing,
190
+ * creating new Cells, and/or contention. See above for
191
+ * explanation. This method suffers the usual non-modularity
192
+ * problems of optimistic retry code, relying on rechecked sets of
193
+ * reads.
194
+ *
195
+ * @param x the value
196
+ * @param hc the hash code holder
197
+ * @param wasUncontended false if CAS failed before call
198
+ */
199
+ final void retryUpdate(long x, HashCode hc, boolean wasUncontended) {
200
+ int h = hc.code;
201
+ boolean collide = false; // True if last slot nonempty
202
+ for (;;) {
203
+ Cell[] as; Cell a; int n; long v;
204
+ if ((as = cells) != null && (n = as.length) > 0) {
205
+ if ((a = as[(n - 1) & h]) == null) {
206
+ if (busy == 0) { // Try to attach new Cell
207
+ Cell r = new Cell(x); // Optimistically create
208
+ if (busy == 0 && casBusy()) {
209
+ boolean created = false;
210
+ try { // Recheck under lock
211
+ Cell[] rs; int m, j;
212
+ if ((rs = cells) != null &&
213
+ (m = rs.length) > 0 &&
214
+ rs[j = (m - 1) & h] == null) {
215
+ rs[j] = r;
216
+ created = true;
217
+ }
218
+ } finally {
219
+ busy = 0;
220
+ }
221
+ if (created)
222
+ break;
223
+ continue; // Slot is now non-empty
224
+ }
225
+ }
226
+ collide = false;
227
+ }
228
+ else if (!wasUncontended) // CAS already known to fail
229
+ wasUncontended = true; // Continue after rehash
230
+ else if (a.cas(v = a.value, fn(v, x)))
231
+ break;
232
+ else if (n >= NCPU || cells != as)
233
+ collide = false; // At max size or stale
234
+ else if (!collide)
235
+ collide = true;
236
+ else if (busy == 0 && casBusy()) {
237
+ try {
238
+ if (cells == as) { // Expand table unless stale
239
+ Cell[] rs = new Cell[n << 1];
240
+ for (int i = 0; i < n; ++i)
241
+ rs[i] = as[i];
242
+ cells = rs;
243
+ }
244
+ } finally {
245
+ busy = 0;
246
+ }
247
+ collide = false;
248
+ continue; // Retry with expanded table
249
+ }
250
+ h ^= h << 13; // Rehash
251
+ h ^= h >>> 17;
252
+ h ^= h << 5;
253
+ }
254
+ else if (busy == 0 && cells == as && casBusy()) {
255
+ boolean init = false;
256
+ try { // Initialize table
257
+ if (cells == as) {
258
+ Cell[] rs = new Cell[2];
259
+ rs[h & 1] = new Cell(x);
260
+ cells = rs;
261
+ init = true;
262
+ }
263
+ } finally {
264
+ busy = 0;
265
+ }
266
+ if (init)
267
+ break;
268
+ }
269
+ else if (casBase(v = base, fn(v, x)))
270
+ break; // Fall back on using base
271
+ }
272
+ hc.code = h; // Record index for next time
273
+ }
274
+
275
+
276
+ /**
277
+ * Sets base and all cells to the given value.
278
+ */
279
+ final void internalReset(long initialValue) {
280
+ Cell[] as = cells;
281
+ base = initialValue;
282
+ if (as != null) {
283
+ int n = as.length;
284
+ for (int i = 0; i < n; ++i) {
285
+ Cell a = as[i];
286
+ if (a != null)
287
+ a.value = initialValue;
288
+ }
289
+ }
290
+ }
291
+ }