thread_safe 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +24 -0
  3. data/README.md +19 -5
  4. data/Rakefile +13 -6
  5. data/examples/bench_cache.rb +1 -1
  6. data/ext/org/jruby/ext/thread_safe/JRubyCacheBackendLibrary.java +54 -15
  7. data/ext/org/jruby/ext/thread_safe/jsr166e/ConcurrentHashMap.java +28 -0
  8. data/ext/org/jruby/ext/thread_safe/jsr166e/ConcurrentHashMapV8.java +19 -10
  9. data/ext/org/jruby/ext/thread_safe/jsr166e/LongAdder.java +1 -2
  10. data/ext/org/jruby/ext/thread_safe/jsr166e/Striped64.java +1 -1
  11. data/ext/org/jruby/ext/thread_safe/jsr166e/nounsafe/ConcurrentHashMapV8.java +3788 -0
  12. data/ext/org/jruby/ext/thread_safe/jsr166e/nounsafe/LongAdder.java +204 -0
  13. data/ext/org/jruby/ext/thread_safe/jsr166e/nounsafe/Striped64.java +291 -0
  14. data/ext/org/jruby/ext/thread_safe/jsr166y/ThreadLocalRandom.java +1 -1
  15. data/ext/thread_safe/JrubyCacheBackendService.java +2 -2
  16. data/lib/thread_safe.rb +1 -1
  17. data/lib/thread_safe/atomic_reference_cache_backend.rb +1 -1
  18. data/lib/thread_safe/cache.rb +6 -3
  19. data/lib/thread_safe/mri_cache_backend.rb +2 -2
  20. data/lib/thread_safe/non_concurrent_cache_backend.rb +1 -1
  21. data/lib/thread_safe/synchronized_cache_backend.rb +1 -1
  22. data/lib/thread_safe/synchronized_delegator.rb +36 -19
  23. data/lib/thread_safe/util.rb +1 -1
  24. data/lib/thread_safe/util/adder.rb +1 -1
  25. data/lib/thread_safe/util/atomic_reference.rb +1 -1
  26. data/lib/thread_safe/util/cheap_lockable.rb +1 -1
  27. data/lib/thread_safe/util/power_of_two_tuple.rb +1 -1
  28. data/lib/thread_safe/util/striped64.rb +1 -1
  29. data/lib/thread_safe/util/volatile.rb +1 -1
  30. data/lib/thread_safe/util/volatile_tuple.rb +1 -1
  31. data/lib/thread_safe/util/xor_shift_random.rb +1 -1
  32. data/lib/thread_safe/version.rb +1 -1
  33. data/test/src/thread_safe/SecurityManager.java +21 -0
  34. data/test/test_array.rb +1 -1
  35. data/test/test_cache.rb +27 -10
  36. data/test/test_cache_loops.rb +377 -376
  37. data/test/test_hash.rb +1 -2
  38. data/test/test_helper.rb +33 -3
  39. data/test/test_synchronized_delegator.rb +67 -17
  40. data/thread_safe.gemspec +6 -3
  41. metadata +36 -10
@@ -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 org.jruby.ext.thread_safe.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 org.jruby.ext.thread_safe.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
+ }