concurrent-ruby 1.0.5 → 1.1.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +44 -0
- data/Gemfile +39 -0
- data/{LICENSE.txt → LICENSE.md} +2 -0
- data/README.md +203 -105
- data/Rakefile +279 -0
- data/ext/concurrent-ruby/ConcurrentRubyService.java +17 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/AtomicReferenceLibrary.java +175 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JRubyMapBackendLibrary.java +248 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicBooleanLibrary.java +93 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaAtomicFixnumLibrary.java +113 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/JavaSemaphoreLibrary.java +159 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/SynchronizationLibrary.java +304 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMap.java +31 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/ConcurrentHashMapV8.java +3863 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/LongAdder.java +203 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/Striped64.java +342 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/ConcurrentHashMapV8.java +3800 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/LongAdder.java +204 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166e/nounsafe/Striped64.java +291 -0
- data/ext/concurrent-ruby/com/concurrent_ruby/ext/jsr166y/ThreadLocalRandom.java +199 -0
- data/lib/concurrent-ruby.rb +1 -0
- data/lib/concurrent.rb +24 -20
- data/lib/concurrent/agent.rb +7 -7
- data/lib/concurrent/array.rb +59 -32
- data/lib/concurrent/async.rb +4 -4
- data/lib/concurrent/atom.rb +9 -9
- data/lib/concurrent/atomic/atomic_boolean.rb +24 -20
- data/lib/concurrent/atomic/atomic_fixnum.rb +27 -23
- data/lib/concurrent/atomic/atomic_markable_reference.rb +164 -0
- data/lib/concurrent/atomic/atomic_reference.rb +176 -33
- data/lib/concurrent/atomic/count_down_latch.rb +6 -6
- data/lib/concurrent/atomic/cyclic_barrier.rb +1 -1
- data/lib/concurrent/atomic/event.rb +1 -1
- data/lib/concurrent/atomic/java_count_down_latch.rb +6 -5
- data/lib/concurrent/atomic/mutex_count_down_latch.rb +1 -0
- data/lib/concurrent/atomic/read_write_lock.rb +2 -1
- data/lib/concurrent/atomic/reentrant_read_write_lock.rb +3 -1
- data/lib/concurrent/atomic/semaphore.rb +8 -8
- data/lib/concurrent/atomic/thread_local_var.rb +7 -7
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +3 -8
- data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +1 -1
- data/lib/concurrent/atomics.rb +0 -43
- data/lib/concurrent/collection/lock_free_stack.rb +158 -0
- data/lib/concurrent/collection/map/atomic_reference_map_backend.rb +3 -3
- data/lib/concurrent/collection/map/non_concurrent_map_backend.rb +1 -2
- data/lib/concurrent/collection/non_concurrent_priority_queue.rb +29 -29
- data/lib/concurrent/concern/dereferenceable.rb +1 -1
- data/lib/concurrent/concern/logging.rb +6 -1
- data/lib/concurrent/concern/observable.rb +7 -7
- data/lib/concurrent/concurrent_ruby.jar +0 -0
- data/lib/concurrent/configuration.rb +1 -6
- data/lib/concurrent/constants.rb +1 -1
- data/lib/concurrent/dataflow.rb +2 -1
- data/lib/concurrent/delay.rb +9 -7
- data/lib/concurrent/exchanger.rb +13 -21
- data/lib/concurrent/executor/abstract_executor_service.rb +2 -2
- data/lib/concurrent/executor/cached_thread_pool.rb +1 -1
- data/lib/concurrent/executor/executor_service.rb +15 -15
- data/lib/concurrent/executor/fixed_thread_pool.rb +18 -18
- data/lib/concurrent/executor/java_thread_pool_executor.rb +10 -7
- data/lib/concurrent/executor/single_thread_executor.rb +2 -2
- data/lib/concurrent/executor/thread_pool_executor.rb +6 -6
- data/lib/concurrent/executor/timer_set.rb +1 -1
- data/lib/concurrent/future.rb +4 -1
- data/lib/concurrent/hash.rb +53 -30
- data/lib/concurrent/ivar.rb +5 -6
- data/lib/concurrent/map.rb +20 -25
- data/lib/concurrent/maybe.rb +1 -1
- data/lib/concurrent/mutable_struct.rb +15 -14
- data/lib/concurrent/mvar.rb +2 -2
- data/lib/concurrent/promise.rb +53 -21
- data/lib/concurrent/promises.rb +1936 -0
- data/lib/concurrent/re_include.rb +58 -0
- data/lib/concurrent/set.rb +66 -0
- data/lib/concurrent/settable_struct.rb +1 -0
- data/lib/concurrent/synchronization.rb +4 -5
- data/lib/concurrent/synchronization/abstract_lockable_object.rb +5 -5
- data/lib/concurrent/synchronization/abstract_struct.rb +6 -4
- data/lib/concurrent/synchronization/lockable_object.rb +6 -6
- data/lib/concurrent/synchronization/{mri_lockable_object.rb → mutex_lockable_object.rb} +19 -14
- data/lib/concurrent/synchronization/object.rb +8 -4
- data/lib/concurrent/synchronization/truffleruby_object.rb +46 -0
- data/lib/concurrent/synchronization/volatile.rb +11 -9
- data/lib/concurrent/thread_safe/util/data_structures.rb +55 -0
- data/lib/concurrent/thread_safe/util/striped64.rb +9 -4
- data/lib/concurrent/timer_task.rb +5 -2
- data/lib/concurrent/tuple.rb +1 -1
- data/lib/concurrent/tvar.rb +2 -2
- data/lib/concurrent/utility/at_exit.rb +1 -1
- data/lib/concurrent/utility/engine.rb +2 -2
- data/lib/concurrent/utility/monotonic_time.rb +3 -3
- data/lib/concurrent/utility/native_extension_loader.rb +31 -33
- data/lib/concurrent/utility/processor_counter.rb +0 -2
- data/lib/concurrent/version.rb +2 -2
- metadata +35 -21
- data/lib/concurrent/atomic_reference/concurrent_update_error.rb +0 -8
- data/lib/concurrent/atomic_reference/direct_update.rb +0 -81
- data/lib/concurrent/atomic_reference/jruby+truffle.rb +0 -2
- data/lib/concurrent/atomic_reference/jruby.rb +0 -16
- data/lib/concurrent/atomic_reference/rbx.rb +0 -22
- data/lib/concurrent/atomic_reference/ruby.rb +0 -32
- data/lib/concurrent/edge.rb +0 -26
- data/lib/concurrent/lazy_register.rb +0 -81
- data/lib/concurrent/synchronization/truffle_lockable_object.rb +0 -9
- data/lib/concurrent/synchronization/truffle_object.rb +0 -31
- data/lib/concurrent/thread_safe/util/array_hash_rbx.rb +0 -30
@@ -0,0 +1,248 @@
|
|
1
|
+
package com.concurrent_ruby.ext;
|
2
|
+
|
3
|
+
import org.jruby.*;
|
4
|
+
import org.jruby.anno.JRubyClass;
|
5
|
+
import org.jruby.anno.JRubyMethod;
|
6
|
+
import com.concurrent_ruby.ext.jsr166e.ConcurrentHashMap;
|
7
|
+
import com.concurrent_ruby.ext.jsr166e.ConcurrentHashMapV8;
|
8
|
+
import com.concurrent_ruby.ext.jsr166e.nounsafe.*;
|
9
|
+
import org.jruby.runtime.Block;
|
10
|
+
import org.jruby.runtime.ObjectAllocator;
|
11
|
+
import org.jruby.runtime.ThreadContext;
|
12
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
13
|
+
import org.jruby.runtime.load.Library;
|
14
|
+
|
15
|
+
import java.io.IOException;
|
16
|
+
import java.util.Map;
|
17
|
+
|
18
|
+
import static org.jruby.runtime.Visibility.PRIVATE;
|
19
|
+
|
20
|
+
/**
|
21
|
+
* Native Java implementation to avoid the JI overhead.
|
22
|
+
*
|
23
|
+
* @author thedarkone
|
24
|
+
*/
|
25
|
+
public class JRubyMapBackendLibrary implements Library {
|
26
|
+
public void load(Ruby runtime, boolean wrap) throws IOException {
|
27
|
+
|
28
|
+
RubyModule concurrentMod = runtime.defineModule("Concurrent");
|
29
|
+
RubyModule thread_safeMod = concurrentMod.defineModuleUnder("Collection");
|
30
|
+
RubyClass jrubyRefClass = thread_safeMod.defineClassUnder("JRubyMapBackend", runtime.getObject(), BACKEND_ALLOCATOR);
|
31
|
+
jrubyRefClass.setAllocator(BACKEND_ALLOCATOR);
|
32
|
+
jrubyRefClass.defineAnnotatedMethods(JRubyMapBackend.class);
|
33
|
+
}
|
34
|
+
|
35
|
+
private static final ObjectAllocator BACKEND_ALLOCATOR = new ObjectAllocator() {
|
36
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
|
37
|
+
return new JRubyMapBackend(runtime, klazz);
|
38
|
+
}
|
39
|
+
};
|
40
|
+
|
41
|
+
@JRubyClass(name="JRubyMapBackend", parent="Object")
|
42
|
+
public static class JRubyMapBackend extends RubyObject {
|
43
|
+
// Defaults used by the CHM
|
44
|
+
static final int DEFAULT_INITIAL_CAPACITY = 16;
|
45
|
+
static final float DEFAULT_LOAD_FACTOR = 0.75f;
|
46
|
+
|
47
|
+
public static final boolean CAN_USE_UNSAFE_CHM = canUseUnsafeCHM();
|
48
|
+
|
49
|
+
private ConcurrentHashMap<IRubyObject, IRubyObject> map;
|
50
|
+
|
51
|
+
private static ConcurrentHashMap<IRubyObject, IRubyObject> newCHM(int initialCapacity, float loadFactor) {
|
52
|
+
if (CAN_USE_UNSAFE_CHM) {
|
53
|
+
return new ConcurrentHashMapV8<IRubyObject, IRubyObject>(initialCapacity, loadFactor);
|
54
|
+
} else {
|
55
|
+
return new com.concurrent_ruby.ext.jsr166e.nounsafe.ConcurrentHashMapV8<IRubyObject, IRubyObject>(initialCapacity, loadFactor);
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
private static ConcurrentHashMap<IRubyObject, IRubyObject> newCHM() {
|
60
|
+
return newCHM(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
|
61
|
+
}
|
62
|
+
|
63
|
+
private static boolean canUseUnsafeCHM() {
|
64
|
+
try {
|
65
|
+
new com.concurrent_ruby.ext.jsr166e.ConcurrentHashMapV8(); // force class load and initialization
|
66
|
+
return true;
|
67
|
+
} catch (Throwable t) { // ensuring we really do catch everything
|
68
|
+
// Doug's Unsafe setup errors always have this "Could not ini.." message
|
69
|
+
if (isCausedBySecurityException(t)) {
|
70
|
+
return false;
|
71
|
+
}
|
72
|
+
throw (t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t));
|
73
|
+
}
|
74
|
+
}
|
75
|
+
|
76
|
+
private static boolean isCausedBySecurityException(Throwable t) {
|
77
|
+
while (t != null) {
|
78
|
+
if ((t.getMessage() != null && t.getMessage().contains("Could not initialize intrinsics")) || t instanceof SecurityException) {
|
79
|
+
return true;
|
80
|
+
}
|
81
|
+
t = t.getCause();
|
82
|
+
}
|
83
|
+
return false;
|
84
|
+
}
|
85
|
+
|
86
|
+
public JRubyMapBackend(Ruby runtime, RubyClass klass) {
|
87
|
+
super(runtime, klass);
|
88
|
+
}
|
89
|
+
|
90
|
+
@JRubyMethod
|
91
|
+
public IRubyObject initialize(ThreadContext context) {
|
92
|
+
map = newCHM();
|
93
|
+
return context.getRuntime().getNil();
|
94
|
+
}
|
95
|
+
|
96
|
+
@JRubyMethod
|
97
|
+
public IRubyObject initialize(ThreadContext context, IRubyObject options) {
|
98
|
+
map = toCHM(context, options);
|
99
|
+
return context.getRuntime().getNil();
|
100
|
+
}
|
101
|
+
|
102
|
+
private ConcurrentHashMap<IRubyObject, IRubyObject> toCHM(ThreadContext context, IRubyObject options) {
|
103
|
+
Ruby runtime = context.getRuntime();
|
104
|
+
if (!options.isNil() && options.respondsTo("[]")) {
|
105
|
+
IRubyObject rInitialCapacity = options.callMethod(context, "[]", runtime.newSymbol("initial_capacity"));
|
106
|
+
IRubyObject rLoadFactor = options.callMethod(context, "[]", runtime.newSymbol("load_factor"));
|
107
|
+
int initialCapacity = !rInitialCapacity.isNil() ? RubyNumeric.num2int(rInitialCapacity.convertToInteger()) : DEFAULT_INITIAL_CAPACITY;
|
108
|
+
float loadFactor = !rLoadFactor.isNil() ? (float)RubyNumeric.num2dbl(rLoadFactor.convertToFloat()) : DEFAULT_LOAD_FACTOR;
|
109
|
+
return newCHM(initialCapacity, loadFactor);
|
110
|
+
} else {
|
111
|
+
return newCHM();
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
@JRubyMethod(name = "[]", required = 1)
|
116
|
+
public IRubyObject op_aref(ThreadContext context, IRubyObject key) {
|
117
|
+
IRubyObject value;
|
118
|
+
return ((value = map.get(key)) == null) ? context.getRuntime().getNil() : value;
|
119
|
+
}
|
120
|
+
|
121
|
+
@JRubyMethod(name = {"[]="}, required = 2)
|
122
|
+
public IRubyObject op_aset(IRubyObject key, IRubyObject value) {
|
123
|
+
map.put(key, value);
|
124
|
+
return value;
|
125
|
+
}
|
126
|
+
|
127
|
+
@JRubyMethod
|
128
|
+
public IRubyObject put_if_absent(IRubyObject key, IRubyObject value) {
|
129
|
+
IRubyObject result = map.putIfAbsent(key, value);
|
130
|
+
return result == null ? getRuntime().getNil() : result;
|
131
|
+
}
|
132
|
+
|
133
|
+
@JRubyMethod
|
134
|
+
public IRubyObject compute_if_absent(final ThreadContext context, final IRubyObject key, final Block block) {
|
135
|
+
return map.computeIfAbsent(key, new ConcurrentHashMap.Fun<IRubyObject, IRubyObject>() {
|
136
|
+
@Override
|
137
|
+
public IRubyObject apply(IRubyObject key) {
|
138
|
+
return block.yieldSpecific(context);
|
139
|
+
}
|
140
|
+
});
|
141
|
+
}
|
142
|
+
|
143
|
+
@JRubyMethod
|
144
|
+
public IRubyObject compute_if_present(final ThreadContext context, final IRubyObject key, final Block block) {
|
145
|
+
IRubyObject result = map.computeIfPresent(key, new ConcurrentHashMap.BiFun<IRubyObject, IRubyObject, IRubyObject>() {
|
146
|
+
@Override
|
147
|
+
public IRubyObject apply(IRubyObject key, IRubyObject oldValue) {
|
148
|
+
IRubyObject result = block.yieldSpecific(context, oldValue == null ? context.getRuntime().getNil() : oldValue);
|
149
|
+
return result.isNil() ? null : result;
|
150
|
+
}
|
151
|
+
});
|
152
|
+
return result == null ? context.getRuntime().getNil() : result;
|
153
|
+
}
|
154
|
+
|
155
|
+
@JRubyMethod
|
156
|
+
public IRubyObject compute(final ThreadContext context, final IRubyObject key, final Block block) {
|
157
|
+
IRubyObject result = map.compute(key, new ConcurrentHashMap.BiFun<IRubyObject, IRubyObject, IRubyObject>() {
|
158
|
+
@Override
|
159
|
+
public IRubyObject apply(IRubyObject key, IRubyObject oldValue) {
|
160
|
+
IRubyObject result = block.yieldSpecific(context, oldValue == null ? context.getRuntime().getNil() : oldValue);
|
161
|
+
return result.isNil() ? null : result;
|
162
|
+
}
|
163
|
+
});
|
164
|
+
return result == null ? context.getRuntime().getNil() : result;
|
165
|
+
}
|
166
|
+
|
167
|
+
@JRubyMethod
|
168
|
+
public IRubyObject merge_pair(final ThreadContext context, final IRubyObject key, final IRubyObject value, final Block block) {
|
169
|
+
IRubyObject result = map.merge(key, value, new ConcurrentHashMap.BiFun<IRubyObject, IRubyObject, IRubyObject>() {
|
170
|
+
@Override
|
171
|
+
public IRubyObject apply(IRubyObject oldValue, IRubyObject newValue) {
|
172
|
+
IRubyObject result = block.yieldSpecific(context, oldValue == null ? context.getRuntime().getNil() : oldValue);
|
173
|
+
return result.isNil() ? null : result;
|
174
|
+
}
|
175
|
+
});
|
176
|
+
return result == null ? context.getRuntime().getNil() : result;
|
177
|
+
}
|
178
|
+
|
179
|
+
@JRubyMethod
|
180
|
+
public RubyBoolean replace_pair(IRubyObject key, IRubyObject oldValue, IRubyObject newValue) {
|
181
|
+
return getRuntime().newBoolean(map.replace(key, oldValue, newValue));
|
182
|
+
}
|
183
|
+
|
184
|
+
@JRubyMethod(name = "key?", required = 1)
|
185
|
+
public RubyBoolean has_key_p(IRubyObject key) {
|
186
|
+
return map.containsKey(key) ? getRuntime().getTrue() : getRuntime().getFalse();
|
187
|
+
}
|
188
|
+
|
189
|
+
@JRubyMethod
|
190
|
+
public IRubyObject key(IRubyObject value) {
|
191
|
+
final IRubyObject key = map.findKey(value);
|
192
|
+
return key == null ? getRuntime().getNil() : key;
|
193
|
+
}
|
194
|
+
|
195
|
+
@JRubyMethod
|
196
|
+
public IRubyObject replace_if_exists(IRubyObject key, IRubyObject value) {
|
197
|
+
IRubyObject result = map.replace(key, value);
|
198
|
+
return result == null ? getRuntime().getNil() : result;
|
199
|
+
}
|
200
|
+
|
201
|
+
@JRubyMethod
|
202
|
+
public IRubyObject get_and_set(IRubyObject key, IRubyObject value) {
|
203
|
+
IRubyObject result = map.put(key, value);
|
204
|
+
return result == null ? getRuntime().getNil() : result;
|
205
|
+
}
|
206
|
+
|
207
|
+
@JRubyMethod
|
208
|
+
public IRubyObject delete(IRubyObject key) {
|
209
|
+
IRubyObject result = map.remove(key);
|
210
|
+
return result == null ? getRuntime().getNil() : result;
|
211
|
+
}
|
212
|
+
|
213
|
+
@JRubyMethod
|
214
|
+
public RubyBoolean delete_pair(IRubyObject key, IRubyObject value) {
|
215
|
+
return getRuntime().newBoolean(map.remove(key, value));
|
216
|
+
}
|
217
|
+
|
218
|
+
@JRubyMethod
|
219
|
+
public IRubyObject clear() {
|
220
|
+
map.clear();
|
221
|
+
return this;
|
222
|
+
}
|
223
|
+
|
224
|
+
@JRubyMethod
|
225
|
+
public IRubyObject each_pair(ThreadContext context, Block block) {
|
226
|
+
for (Map.Entry<IRubyObject,IRubyObject> entry : map.entrySet()) {
|
227
|
+
block.yieldSpecific(context, entry.getKey(), entry.getValue());
|
228
|
+
}
|
229
|
+
return this;
|
230
|
+
}
|
231
|
+
|
232
|
+
@JRubyMethod
|
233
|
+
public RubyFixnum size(ThreadContext context) {
|
234
|
+
return context.getRuntime().newFixnum(map.size());
|
235
|
+
}
|
236
|
+
|
237
|
+
@JRubyMethod
|
238
|
+
public IRubyObject get_or_default(IRubyObject key, IRubyObject defaultValue) {
|
239
|
+
return map.getValueOrDefault(key, defaultValue);
|
240
|
+
}
|
241
|
+
|
242
|
+
@JRubyMethod(visibility = PRIVATE)
|
243
|
+
public JRubyMapBackend initialize_copy(ThreadContext context, IRubyObject other) {
|
244
|
+
map = newCHM();
|
245
|
+
return this;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
}
|
@@ -0,0 +1,93 @@
|
|
1
|
+
package com.concurrent_ruby.ext;
|
2
|
+
|
3
|
+
import org.jruby.Ruby;
|
4
|
+
import org.jruby.RubyBoolean;
|
5
|
+
import org.jruby.RubyClass;
|
6
|
+
import org.jruby.RubyModule;
|
7
|
+
import org.jruby.RubyNil;
|
8
|
+
import org.jruby.RubyObject;
|
9
|
+
import org.jruby.anno.JRubyClass;
|
10
|
+
import org.jruby.anno.JRubyMethod;
|
11
|
+
import org.jruby.runtime.ObjectAllocator;
|
12
|
+
import org.jruby.runtime.ThreadContext;
|
13
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
14
|
+
import org.jruby.runtime.load.Library;
|
15
|
+
|
16
|
+
import java.io.IOException;
|
17
|
+
import java.util.concurrent.atomic.AtomicBoolean;
|
18
|
+
|
19
|
+
public class JavaAtomicBooleanLibrary implements Library {
|
20
|
+
|
21
|
+
public void load(Ruby runtime, boolean wrap) throws IOException {
|
22
|
+
RubyModule concurrentMod = runtime.defineModule("Concurrent");
|
23
|
+
RubyClass atomicCls = concurrentMod.defineClassUnder("JavaAtomicBoolean", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR);
|
24
|
+
atomicCls.defineAnnotatedMethods(JavaAtomicBoolean.class);
|
25
|
+
}
|
26
|
+
|
27
|
+
private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() {
|
28
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
|
29
|
+
return new JavaAtomicBoolean(runtime, klazz);
|
30
|
+
}
|
31
|
+
};
|
32
|
+
|
33
|
+
@JRubyClass(name = "JavaAtomicBoolean", parent = "Object")
|
34
|
+
public static class JavaAtomicBoolean extends RubyObject {
|
35
|
+
|
36
|
+
private AtomicBoolean atomicBoolean;
|
37
|
+
|
38
|
+
public JavaAtomicBoolean(Ruby runtime, RubyClass metaClass) {
|
39
|
+
super(runtime, metaClass);
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod
|
43
|
+
public IRubyObject initialize(ThreadContext context, IRubyObject value) {
|
44
|
+
atomicBoolean = new AtomicBoolean(convertRubyBooleanToJavaBoolean(value));
|
45
|
+
return context.nil;
|
46
|
+
}
|
47
|
+
|
48
|
+
@JRubyMethod
|
49
|
+
public IRubyObject initialize(ThreadContext context) {
|
50
|
+
atomicBoolean = new AtomicBoolean();
|
51
|
+
return context.nil;
|
52
|
+
}
|
53
|
+
|
54
|
+
@JRubyMethod(name = "value")
|
55
|
+
public IRubyObject value() {
|
56
|
+
return getRuntime().newBoolean(atomicBoolean.get());
|
57
|
+
}
|
58
|
+
|
59
|
+
@JRubyMethod(name = "true?")
|
60
|
+
public IRubyObject isAtomicTrue() {
|
61
|
+
return getRuntime().newBoolean(atomicBoolean.get());
|
62
|
+
}
|
63
|
+
|
64
|
+
@JRubyMethod(name = "false?")
|
65
|
+
public IRubyObject isAtomicFalse() {
|
66
|
+
return getRuntime().newBoolean((atomicBoolean.get() == false));
|
67
|
+
}
|
68
|
+
|
69
|
+
@JRubyMethod(name = "value=")
|
70
|
+
public IRubyObject setAtomic(ThreadContext context, IRubyObject newValue) {
|
71
|
+
atomicBoolean.set(convertRubyBooleanToJavaBoolean(newValue));
|
72
|
+
return context.nil;
|
73
|
+
}
|
74
|
+
|
75
|
+
@JRubyMethod(name = "make_true")
|
76
|
+
public IRubyObject makeTrue() {
|
77
|
+
return getRuntime().newBoolean(atomicBoolean.compareAndSet(false, true));
|
78
|
+
}
|
79
|
+
|
80
|
+
@JRubyMethod(name = "make_false")
|
81
|
+
public IRubyObject makeFalse() {
|
82
|
+
return getRuntime().newBoolean(atomicBoolean.compareAndSet(true, false));
|
83
|
+
}
|
84
|
+
|
85
|
+
private boolean convertRubyBooleanToJavaBoolean(IRubyObject newValue) {
|
86
|
+
if (newValue instanceof RubyBoolean.False || newValue instanceof RubyNil) {
|
87
|
+
return false;
|
88
|
+
} else {
|
89
|
+
return true;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
}
|
93
|
+
}
|
@@ -0,0 +1,113 @@
|
|
1
|
+
package com.concurrent_ruby.ext;
|
2
|
+
|
3
|
+
import java.io.IOException;
|
4
|
+
import java.util.concurrent.atomic.AtomicLong;
|
5
|
+
import org.jruby.Ruby;
|
6
|
+
import org.jruby.RubyClass;
|
7
|
+
import org.jruby.RubyFixnum;
|
8
|
+
import org.jruby.RubyModule;
|
9
|
+
import org.jruby.RubyObject;
|
10
|
+
import org.jruby.anno.JRubyClass;
|
11
|
+
import org.jruby.anno.JRubyMethod;
|
12
|
+
import org.jruby.runtime.ObjectAllocator;
|
13
|
+
import org.jruby.runtime.ThreadContext;
|
14
|
+
import org.jruby.runtime.builtin.IRubyObject;
|
15
|
+
import org.jruby.runtime.load.Library;
|
16
|
+
import org.jruby.runtime.Block;
|
17
|
+
|
18
|
+
public class JavaAtomicFixnumLibrary implements Library {
|
19
|
+
|
20
|
+
public void load(Ruby runtime, boolean wrap) throws IOException {
|
21
|
+
RubyModule concurrentMod = runtime.defineModule("Concurrent");
|
22
|
+
RubyClass atomicCls = concurrentMod.defineClassUnder("JavaAtomicFixnum", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR);
|
23
|
+
|
24
|
+
atomicCls.defineAnnotatedMethods(JavaAtomicFixnum.class);
|
25
|
+
}
|
26
|
+
|
27
|
+
private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() {
|
28
|
+
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
|
29
|
+
return new JavaAtomicFixnum(runtime, klazz);
|
30
|
+
}
|
31
|
+
};
|
32
|
+
|
33
|
+
@JRubyClass(name = "JavaAtomicFixnum", parent = "Object")
|
34
|
+
public static class JavaAtomicFixnum extends RubyObject {
|
35
|
+
|
36
|
+
private AtomicLong atomicLong;
|
37
|
+
|
38
|
+
public JavaAtomicFixnum(Ruby runtime, RubyClass metaClass) {
|
39
|
+
super(runtime, metaClass);
|
40
|
+
}
|
41
|
+
|
42
|
+
@JRubyMethod
|
43
|
+
public IRubyObject initialize(ThreadContext context) {
|
44
|
+
this.atomicLong = new AtomicLong(0);
|
45
|
+
return context.nil;
|
46
|
+
}
|
47
|
+
|
48
|
+
@JRubyMethod
|
49
|
+
public IRubyObject initialize(ThreadContext context, IRubyObject value) {
|
50
|
+
this.atomicLong = new AtomicLong(rubyFixnumToLong(value));
|
51
|
+
return context.nil;
|
52
|
+
}
|
53
|
+
|
54
|
+
@JRubyMethod(name = "value")
|
55
|
+
public IRubyObject getValue() {
|
56
|
+
return getRuntime().newFixnum(atomicLong.get());
|
57
|
+
}
|
58
|
+
|
59
|
+
@JRubyMethod(name = "value=")
|
60
|
+
public IRubyObject setValue(ThreadContext context, IRubyObject newValue) {
|
61
|
+
atomicLong.set(rubyFixnumToLong(newValue));
|
62
|
+
return context.nil;
|
63
|
+
}
|
64
|
+
|
65
|
+
@JRubyMethod(name = {"increment", "up"})
|
66
|
+
public IRubyObject increment() {
|
67
|
+
return getRuntime().newFixnum(atomicLong.incrementAndGet());
|
68
|
+
}
|
69
|
+
|
70
|
+
@JRubyMethod(name = {"increment", "up"})
|
71
|
+
public IRubyObject increment(IRubyObject value) {
|
72
|
+
long delta = rubyFixnumToLong(value);
|
73
|
+
return getRuntime().newFixnum(atomicLong.addAndGet(delta));
|
74
|
+
}
|
75
|
+
|
76
|
+
@JRubyMethod(name = {"decrement", "down"})
|
77
|
+
public IRubyObject decrement() {
|
78
|
+
return getRuntime().newFixnum(atomicLong.decrementAndGet());
|
79
|
+
}
|
80
|
+
|
81
|
+
@JRubyMethod(name = {"decrement", "down"})
|
82
|
+
public IRubyObject decrement(IRubyObject value) {
|
83
|
+
long delta = rubyFixnumToLong(value);
|
84
|
+
return getRuntime().newFixnum(atomicLong.addAndGet(-delta));
|
85
|
+
}
|
86
|
+
|
87
|
+
@JRubyMethod(name = "compare_and_set")
|
88
|
+
public IRubyObject compareAndSet(ThreadContext context, IRubyObject expect, IRubyObject update) {
|
89
|
+
return getRuntime().newBoolean(atomicLong.compareAndSet(rubyFixnumToLong(expect), rubyFixnumToLong(update)));
|
90
|
+
}
|
91
|
+
|
92
|
+
@JRubyMethod
|
93
|
+
public IRubyObject update(ThreadContext context, Block block) {
|
94
|
+
for (;;) {
|
95
|
+
long _oldValue = atomicLong.get();
|
96
|
+
IRubyObject oldValue = getRuntime().newFixnum(_oldValue);
|
97
|
+
IRubyObject newValue = block.yield(context, oldValue);
|
98
|
+
if (atomicLong.compareAndSet(_oldValue, rubyFixnumToLong(newValue))) {
|
99
|
+
return newValue;
|
100
|
+
}
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
private long rubyFixnumToLong(IRubyObject value) {
|
105
|
+
if (value instanceof RubyFixnum) {
|
106
|
+
RubyFixnum fixNum = (RubyFixnum) value;
|
107
|
+
return fixNum.getLongValue();
|
108
|
+
} else {
|
109
|
+
throw getRuntime().newArgumentError("value must be a Fixnum");
|
110
|
+
}
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|