concurrent-ruby 1.0.5 → 1.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +155 -0
  3. data/Gemfile +37 -0
  4. data/LICENSE.txt +18 -18
  5. data/README.md +260 -103
  6. data/Rakefile +329 -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 +189 -0
  13. data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +307 -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 → concurrent-ruby/concurrent}/agent.rb +7 -7
  23. data/lib/concurrent-ruby/concurrent/array.rb +66 -0
  24. data/lib/{concurrent → concurrent-ruby/concurrent}/async.rb +28 -24
  25. data/lib/{concurrent → concurrent-ruby/concurrent}/atom.rb +10 -10
  26. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_boolean.rb +26 -22
  27. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/atomic_fixnum.rb +27 -23
  28. data/lib/concurrent-ruby/concurrent/atomic/atomic_markable_reference.rb +164 -0
  29. data/lib/concurrent-ruby/concurrent/atomic/atomic_reference.rb +205 -0
  30. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/count_down_latch.rb +7 -7
  31. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/cyclic_barrier.rb +1 -1
  32. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/event.rb +3 -3
  33. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/java_count_down_latch.rb +9 -6
  34. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_boolean.rb +2 -0
  35. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_count_down_latch.rb +1 -0
  36. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_semaphore.rb +18 -2
  37. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/read_write_lock.rb +2 -1
  38. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/reentrant_read_write_lock.rb +7 -7
  39. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/ruby_thread_local_var.rb +60 -40
  40. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/semaphore.rb +34 -13
  41. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/thread_local_var.rb +8 -8
  42. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/mutex_atomic.rb +3 -8
  43. data/lib/{concurrent → concurrent-ruby/concurrent}/atomic_reference/numeric_cas_wrapper.rb +1 -1
  44. data/lib/concurrent-ruby/concurrent/atomics.rb +10 -0
  45. data/lib/concurrent-ruby/concurrent/collection/lock_free_stack.rb +158 -0
  46. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/atomic_reference_map_backend.rb +3 -3
  47. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/mri_map_backend.rb +1 -1
  48. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/non_concurrent_map_backend.rb +1 -2
  49. data/lib/concurrent-ruby/concurrent/collection/map/truffleruby_map_backend.rb +14 -0
  50. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/non_concurrent_priority_queue.rb +30 -30
  51. data/lib/{concurrent → concurrent-ruby/concurrent}/collection/ruby_non_concurrent_priority_queue.rb +11 -1
  52. data/lib/{concurrent → concurrent-ruby/concurrent}/concern/dereferenceable.rb +3 -3
  53. data/lib/{concurrent → concurrent-ruby/concurrent}/concern/logging.rb +6 -1
  54. data/lib/{concurrent → concurrent-ruby/concurrent}/concern/observable.rb +7 -7
  55. data/lib/concurrent-ruby/concurrent/concurrent_ruby.jar +0 -0
  56. data/lib/{concurrent → concurrent-ruby/concurrent}/configuration.rb +15 -15
  57. data/lib/{concurrent → concurrent-ruby/concurrent}/constants.rb +1 -1
  58. data/lib/{concurrent → concurrent-ruby/concurrent}/dataflow.rb +2 -1
  59. data/lib/{concurrent → concurrent-ruby/concurrent}/delay.rb +9 -7
  60. data/lib/{concurrent → concurrent-ruby/concurrent}/exchanger.rb +21 -25
  61. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/abstract_executor_service.rb +35 -38
  62. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/cached_thread_pool.rb +5 -5
  63. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/executor_service.rb +17 -17
  64. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/fixed_thread_pool.rb +47 -33
  65. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_executor_service.rb +20 -17
  66. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_single_thread_executor.rb +4 -3
  67. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/java_thread_pool_executor.rb +29 -9
  68. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_executor_service.rb +10 -6
  69. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_single_thread_executor.rb +0 -1
  70. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/ruby_thread_pool_executor.rb +46 -42
  71. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/safe_task_executor.rb +5 -5
  72. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/simple_executor_service.rb +1 -1
  73. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/single_thread_executor.rb +3 -2
  74. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/thread_pool_executor.rb +7 -6
  75. data/lib/{concurrent → concurrent-ruby/concurrent}/executor/timer_set.rb +14 -17
  76. data/lib/{concurrent → concurrent-ruby/concurrent}/future.rb +4 -1
  77. data/lib/concurrent-ruby/concurrent/hash.rb +59 -0
  78. data/lib/{concurrent → concurrent-ruby/concurrent}/immutable_struct.rb +9 -1
  79. data/lib/{concurrent → concurrent-ruby/concurrent}/ivar.rb +5 -6
  80. data/lib/concurrent-ruby/concurrent/map.rb +346 -0
  81. data/lib/{concurrent → concurrent-ruby/concurrent}/maybe.rb +1 -1
  82. data/lib/{concurrent → concurrent-ruby/concurrent}/mutable_struct.rb +27 -16
  83. data/lib/{concurrent → concurrent-ruby/concurrent}/mvar.rb +2 -2
  84. data/lib/{concurrent → concurrent-ruby/concurrent}/promise.rb +54 -21
  85. data/lib/concurrent-ruby/concurrent/promises.rb +2167 -0
  86. data/lib/concurrent-ruby/concurrent/re_include.rb +58 -0
  87. data/lib/{concurrent → concurrent-ruby/concurrent}/scheduled_task.rb +29 -16
  88. data/lib/concurrent-ruby/concurrent/set.rb +74 -0
  89. data/lib/{concurrent → concurrent-ruby/concurrent}/settable_struct.rb +12 -1
  90. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_lockable_object.rb +5 -5
  91. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_struct.rb +18 -4
  92. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/condition.rb +2 -0
  93. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/jruby_object.rb +1 -0
  94. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lock.rb +2 -0
  95. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/lockable_object.rb +8 -10
  96. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/mri_object.rb +1 -0
  97. data/lib/concurrent-ruby/concurrent/synchronization/mutex_lockable_object.rb +88 -0
  98. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/object.rb +53 -23
  99. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/rbx_lockable_object.rb +6 -0
  100. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/rbx_object.rb +1 -0
  101. data/lib/concurrent-ruby/concurrent/synchronization/truffleruby_object.rb +47 -0
  102. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/volatile.rb +11 -9
  103. data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization.rb +4 -5
  104. data/lib/concurrent-ruby/concurrent/thread_safe/util/data_structures.rb +88 -0
  105. data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/striped64.rb +9 -4
  106. data/lib/{concurrent → concurrent-ruby/concurrent}/timer_task.rb +15 -35
  107. data/lib/{concurrent → concurrent-ruby/concurrent}/tuple.rb +1 -1
  108. data/lib/{concurrent → concurrent-ruby/concurrent}/tvar.rb +21 -58
  109. data/lib/{concurrent → concurrent-ruby/concurrent}/utility/engine.rb +4 -4
  110. data/lib/concurrent-ruby/concurrent/utility/monotonic_time.rb +90 -0
  111. data/lib/concurrent-ruby/concurrent/utility/native_extension_loader.rb +79 -0
  112. data/lib/{concurrent → concurrent-ruby/concurrent}/utility/processor_counter.rb +5 -35
  113. data/lib/concurrent-ruby/concurrent/version.rb +3 -0
  114. data/lib/concurrent-ruby/concurrent-ruby.rb +5 -0
  115. data/lib/{concurrent.rb → concurrent-ruby/concurrent.rb} +24 -20
  116. metadata +149 -134
  117. data/lib/concurrent/array.rb +0 -39
  118. data/lib/concurrent/atomic/atomic_reference.rb +0 -51
  119. data/lib/concurrent/atomic_reference/concurrent_update_error.rb +0 -8
  120. data/lib/concurrent/atomic_reference/direct_update.rb +0 -81
  121. data/lib/concurrent/atomic_reference/jruby+truffle.rb +0 -2
  122. data/lib/concurrent/atomic_reference/jruby.rb +0 -16
  123. data/lib/concurrent/atomic_reference/rbx.rb +0 -22
  124. data/lib/concurrent/atomic_reference/ruby.rb +0 -32
  125. data/lib/concurrent/atomics.rb +0 -53
  126. data/lib/concurrent/edge.rb +0 -26
  127. data/lib/concurrent/hash.rb +0 -36
  128. data/lib/concurrent/lazy_register.rb +0 -81
  129. data/lib/concurrent/map.rb +0 -240
  130. data/lib/concurrent/synchronization/mri_lockable_object.rb +0 -71
  131. data/lib/concurrent/synchronization/truffle_lockable_object.rb +0 -9
  132. data/lib/concurrent/synchronization/truffle_object.rb +0 -31
  133. data/lib/concurrent/thread_safe/util/array_hash_rbx.rb +0 -30
  134. data/lib/concurrent/utility/at_exit.rb +0 -97
  135. data/lib/concurrent/utility/monotonic_time.rb +0 -58
  136. data/lib/concurrent/utility/native_extension_loader.rb +0 -73
  137. data/lib/concurrent/version.rb +0 -4
  138. /data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/abstract_thread_local_var.rb +0 -0
  139. /data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/java_thread_local_var.rb +0 -0
  140. /data/lib/{concurrent → concurrent-ruby/concurrent}/atomic/mutex_atomic_fixnum.rb +0 -0
  141. /data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_notify_observer_set.rb +0 -0
  142. /data/lib/{concurrent → concurrent-ruby/concurrent}/collection/copy_on_write_observer_set.rb +0 -0
  143. /data/lib/{concurrent → concurrent-ruby/concurrent}/collection/java_non_concurrent_priority_queue.rb +0 -0
  144. /data/lib/{concurrent → concurrent-ruby/concurrent}/collection/map/synchronized_map_backend.rb +0 -0
  145. /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/deprecation.rb +0 -0
  146. /data/lib/{concurrent → concurrent-ruby/concurrent}/concern/obligation.rb +0 -0
  147. /data/lib/{concurrent → concurrent-ruby/concurrent}/errors.rb +0 -0
  148. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/immediate_executor.rb +0 -0
  149. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/indirect_immediate_executor.rb +0 -0
  150. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serial_executor_service.rb +0 -0
  151. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution.rb +0 -0
  152. /data/lib/{concurrent → concurrent-ruby/concurrent}/executor/serialized_execution_delegator.rb +0 -0
  153. /data/lib/{concurrent → concurrent-ruby/concurrent}/executors.rb +0 -0
  154. /data/lib/{concurrent → concurrent-ruby/concurrent}/options.rb +0 -0
  155. /data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/abstract_object.rb +0 -0
  156. /data/lib/{concurrent → concurrent-ruby/concurrent}/synchronization/jruby_lockable_object.rb +0 -0
  157. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/synchronized_delegator.rb +0 -0
  158. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/adder.rb +0 -0
  159. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/cheap_lockable.rb +0 -0
  160. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/power_of_two_tuple.rb +0 -0
  161. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/volatile.rb +0 -0
  162. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util/xor_shift_random.rb +0 -0
  163. /data/lib/{concurrent → concurrent-ruby/concurrent}/thread_safe/util.rb +0 -0
  164. /data/lib/{concurrent → concurrent-ruby/concurrent}/utility/native_integer.rb +0 -0
@@ -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
+ }
@@ -0,0 +1,199 @@
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.16 version
8
+
9
+ package com.concurrent_ruby.ext.jsr166y;
10
+
11
+ import java.util.Random;
12
+
13
+ /**
14
+ * A random number generator isolated to the current thread. Like the
15
+ * global {@link java.util.Random} generator used by the {@link
16
+ * java.lang.Math} class, a {@code ThreadLocalRandom} is initialized
17
+ * with an internally generated seed that may not otherwise be
18
+ * modified. When applicable, use of {@code ThreadLocalRandom} rather
19
+ * than shared {@code Random} objects in concurrent programs will
20
+ * typically encounter much less overhead and contention. Use of
21
+ * {@code ThreadLocalRandom} is particularly appropriate when multiple
22
+ * tasks (for example, each a {@link ForkJoinTask}) use random numbers
23
+ * in parallel in thread pools.
24
+ *
25
+ * <p>Usages of this class should typically be of the form:
26
+ * {@code ThreadLocalRandom.current().nextX(...)} (where
27
+ * {@code X} is {@code Int}, {@code Long}, etc).
28
+ * When all usages are of this form, it is never possible to
29
+ * accidently share a {@code ThreadLocalRandom} across multiple threads.
30
+ *
31
+ * <p>This class also provides additional commonly used bounded random
32
+ * generation methods.
33
+ *
34
+ * @since 1.7
35
+ * @author Doug Lea
36
+ */
37
+ public class ThreadLocalRandom extends Random {
38
+ // same constants as Random, but must be redeclared because private
39
+ private static final long multiplier = 0x5DEECE66DL;
40
+ private static final long addend = 0xBL;
41
+ private static final long mask = (1L << 48) - 1;
42
+
43
+ /**
44
+ * The random seed. We can't use super.seed.
45
+ */
46
+ private long rnd;
47
+
48
+ /**
49
+ * Initialization flag to permit calls to setSeed to succeed only
50
+ * while executing the Random constructor. We can't allow others
51
+ * since it would cause setting seed in one part of a program to
52
+ * unintentionally impact other usages by the thread.
53
+ */
54
+ boolean initialized;
55
+
56
+ // Padding to help avoid memory contention among seed updates in
57
+ // different TLRs in the common case that they are located near
58
+ // each other.
59
+ private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
60
+
61
+ /**
62
+ * The actual ThreadLocal
63
+ */
64
+ private static final ThreadLocal<ThreadLocalRandom> localRandom =
65
+ new ThreadLocal<ThreadLocalRandom>() {
66
+ protected ThreadLocalRandom initialValue() {
67
+ return new ThreadLocalRandom();
68
+ }
69
+ };
70
+
71
+
72
+ /**
73
+ * Constructor called only by localRandom.initialValue.
74
+ */
75
+ ThreadLocalRandom() {
76
+ super();
77
+ initialized = true;
78
+ }
79
+
80
+ /**
81
+ * Returns the current thread's {@code ThreadLocalRandom}.
82
+ *
83
+ * @return the current thread's {@code ThreadLocalRandom}
84
+ */
85
+ public static ThreadLocalRandom current() {
86
+ return localRandom.get();
87
+ }
88
+
89
+ /**
90
+ * Throws {@code UnsupportedOperationException}. Setting seeds in
91
+ * this generator is not supported.
92
+ *
93
+ * @throws UnsupportedOperationException always
94
+ */
95
+ public void setSeed(long seed) {
96
+ if (initialized)
97
+ throw new UnsupportedOperationException();
98
+ rnd = (seed ^ multiplier) & mask;
99
+ }
100
+
101
+ protected int next(int bits) {
102
+ rnd = (rnd * multiplier + addend) & mask;
103
+ return (int) (rnd >>> (48-bits));
104
+ }
105
+
106
+ /**
107
+ * Returns a pseudorandom, uniformly distributed value between the
108
+ * given least value (inclusive) and bound (exclusive).
109
+ *
110
+ * @param least the least value returned
111
+ * @param bound the upper bound (exclusive)
112
+ * @throws IllegalArgumentException if least greater than or equal
113
+ * to bound
114
+ * @return the next value
115
+ */
116
+ public int nextInt(int least, int bound) {
117
+ if (least >= bound)
118
+ throw new IllegalArgumentException();
119
+ return nextInt(bound - least) + least;
120
+ }
121
+
122
+ /**
123
+ * Returns a pseudorandom, uniformly distributed value
124
+ * between 0 (inclusive) and the specified value (exclusive).
125
+ *
126
+ * @param n the bound on the random number to be returned. Must be
127
+ * positive.
128
+ * @return the next value
129
+ * @throws IllegalArgumentException if n is not positive
130
+ */
131
+ public long nextLong(long n) {
132
+ if (n <= 0)
133
+ throw new IllegalArgumentException("n must be positive");
134
+ // Divide n by two until small enough for nextInt. On each
135
+ // iteration (at most 31 of them but usually much less),
136
+ // randomly choose both whether to include high bit in result
137
+ // (offset) and whether to continue with the lower vs upper
138
+ // half (which makes a difference only if odd).
139
+ long offset = 0;
140
+ while (n >= Integer.MAX_VALUE) {
141
+ int bits = next(2);
142
+ long half = n >>> 1;
143
+ long nextn = ((bits & 2) == 0) ? half : n - half;
144
+ if ((bits & 1) == 0)
145
+ offset += n - nextn;
146
+ n = nextn;
147
+ }
148
+ return offset + nextInt((int) n);
149
+ }
150
+
151
+ /**
152
+ * Returns a pseudorandom, uniformly distributed value between the
153
+ * given least value (inclusive) and bound (exclusive).
154
+ *
155
+ * @param least the least value returned
156
+ * @param bound the upper bound (exclusive)
157
+ * @return the next value
158
+ * @throws IllegalArgumentException if least greater than or equal
159
+ * to bound
160
+ */
161
+ public long nextLong(long least, long bound) {
162
+ if (least >= bound)
163
+ throw new IllegalArgumentException();
164
+ return nextLong(bound - least) + least;
165
+ }
166
+
167
+ /**
168
+ * Returns a pseudorandom, uniformly distributed {@code double} value
169
+ * between 0 (inclusive) and the specified value (exclusive).
170
+ *
171
+ * @param n the bound on the random number to be returned. Must be
172
+ * positive.
173
+ * @return the next value
174
+ * @throws IllegalArgumentException if n is not positive
175
+ */
176
+ public double nextDouble(double n) {
177
+ if (n <= 0)
178
+ throw new IllegalArgumentException("n must be positive");
179
+ return nextDouble() * n;
180
+ }
181
+
182
+ /**
183
+ * Returns a pseudorandom, uniformly distributed value between the
184
+ * given least value (inclusive) and bound (exclusive).
185
+ *
186
+ * @param least the least value returned
187
+ * @param bound the upper bound (exclusive)
188
+ * @return the next value
189
+ * @throws IllegalArgumentException if least greater than or equal
190
+ * to bound
191
+ */
192
+ public double nextDouble(double least, double bound) {
193
+ if (least >= bound)
194
+ throw new IllegalArgumentException();
195
+ return nextDouble() * (bound - least) + least;
196
+ }
197
+
198
+ private static final long serialVersionUID = -5851777807851030925L;
199
+ }