Skip to content

Commit 39ca9db

Browse files
committed
Initial implementation of Concurrent::AtomicBoolean in pure Java.
1 parent 5e2ae69 commit 39ca9db

File tree

3 files changed

+98
-44
lines changed

3 files changed

+98
-44
lines changed

ext/ConcurrentRubyExtService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import java.io.IOException;
2-
2+
33
import org.jruby.Ruby;
44
import org.jruby.runtime.load.BasicLibraryService;
55

66
public class ConcurrentRubyExtService implements BasicLibraryService {
77
public boolean basicLoad(final Ruby runtime) throws IOException {
88
new com.concurrent_ruby.ext.AtomicReferenceLibrary().load(runtime, false);
9+
new com.concurrent_ruby.ext.JavaAtomicBooleanLibrary().load(runtime, false);
910
return true;
1011
}
1112
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.concurrent_ruby.ext;
2+
3+
import java.io.IOException;
4+
import org.jruby.Ruby;
5+
import org.jruby.RubyClass;
6+
import org.jruby.RubyModule;
7+
import org.jruby.RubyObject;
8+
import org.jruby.anno.JRubyClass;
9+
import org.jruby.anno.JRubyMethod;
10+
import org.jruby.runtime.ObjectAllocator;
11+
import org.jruby.runtime.builtin.IRubyObject;
12+
import org.jruby.runtime.load.Library;
13+
import java.util.concurrent.atomic.AtomicBoolean;
14+
import org.jruby.RubyBoolean;
15+
import org.jruby.RubyNil;
16+
import org.jruby.runtime.ThreadContext;
17+
18+
public class JavaAtomicBooleanLibrary implements Library {
19+
20+
public void load(Ruby runtime, boolean wrap) throws IOException {
21+
RubyModule concurrentMod = runtime.defineModule("Concurrent");
22+
RubyClass atomicCls = concurrentMod.defineClassUnder("JavaAtomicBoolean", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR);
23+
atomicCls.defineAnnotatedMethods(JavaAtomicBoolean.class);
24+
}
25+
26+
private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() {
27+
public IRubyObject allocate(Ruby runtime, RubyClass klazz) {
28+
return new JavaAtomicBoolean(runtime, klazz);
29+
}
30+
};
31+
32+
@JRubyClass(name = "JavaAtomicBoolean", parent = "Object")
33+
public static class JavaAtomicBoolean extends RubyObject {
34+
35+
private AtomicBoolean atomicBoolean;
36+
private ThreadContext context;
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+
this.context = context;
46+
return context.nil;
47+
}
48+
49+
@JRubyMethod
50+
public IRubyObject initialize(ThreadContext context) {
51+
atomicBoolean = new AtomicBoolean();
52+
this.context = context;
53+
return context.nil;
54+
}
55+
56+
@JRubyMethod(name = "value")
57+
public IRubyObject value() {
58+
return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.get());
59+
}
60+
61+
@JRubyMethod(name = "true?")
62+
public IRubyObject isAtomicTrue() {
63+
return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.get());
64+
}
65+
66+
@JRubyMethod(name = "false?")
67+
public IRubyObject isAtomicFalse() {
68+
return RubyBoolean.newBoolean(getRuntime(), (atomicBoolean.get() == false));
69+
}
70+
71+
@JRubyMethod(name = "value=")
72+
public IRubyObject setAtomic(IRubyObject newValue) {
73+
atomicBoolean.set(convertRubyBooleanToJavaBoolean(newValue));
74+
return context.nil;
75+
}
76+
77+
@JRubyMethod(name = "make_true")
78+
public IRubyObject makeTrue() {
79+
return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.compareAndSet(false, true));
80+
}
81+
82+
@JRubyMethod(name = "make_false")
83+
public IRubyObject makeFalse() {
84+
return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.compareAndSet(true, false));
85+
}
86+
87+
private boolean convertRubyBooleanToJavaBoolean(IRubyObject newValue) {
88+
if (newValue instanceof RubyBoolean.False || newValue instanceof RubyNil) {
89+
return false;
90+
} else {
91+
return true;
92+
}
93+
}
94+
}
95+
}
96+

lib/concurrent/atomic/atomic_boolean.rb

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -115,49 +115,6 @@ def make_false
115115

116116
if RUBY_PLATFORM == 'java'
117117

118-
# @!macro atomic_boolean
119-
class JavaAtomicBoolean
120-
121-
# @!macro atomic_boolean_method_initialize
122-
#
123-
def initialize(initial = false)
124-
@atomic = java.util.concurrent.atomic.AtomicBoolean.new(!!initial)
125-
end
126-
127-
# @!macro atomic_boolean_method_value_get
128-
#
129-
def value
130-
@atomic.get
131-
end
132-
133-
# @!macro atomic_boolean_method_value_set
134-
#
135-
def value=(value)
136-
@atomic.set(!!value)
137-
end
138-
139-
# @!macro atomic_boolean_method_true_question
140-
def true?
141-
@atomic.get
142-
end
143-
144-
# @!macro atomic_boolean_method_false_question
145-
def false?
146-
!@atomic.get
147-
end
148-
149-
# @!macro atomic_boolean_method_make_true
150-
def make_true
151-
@atomic.compareAndSet(false, true)
152-
end
153-
154-
# @!macro atomic_boolean_method_make_false
155-
def make_false
156-
@atomic.compareAndSet(true, false)
157-
end
158-
end
159-
160-
# @!macro atomic_boolean
161118
class AtomicBoolean < JavaAtomicBoolean
162119
end
163120

0 commit comments

Comments
 (0)