|
| 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.RubyBoolean; |
| 7 | +import org.jruby.RubyClass; |
| 8 | +import org.jruby.RubyFixnum; |
| 9 | +import org.jruby.RubyModule; |
| 10 | +import org.jruby.RubyObject; |
| 11 | +import org.jruby.anno.JRubyClass; |
| 12 | +import org.jruby.anno.JRubyMethod; |
| 13 | +import org.jruby.runtime.ObjectAllocator; |
| 14 | +import org.jruby.runtime.ThreadContext; |
| 15 | +import org.jruby.runtime.builtin.IRubyObject; |
| 16 | +import org.jruby.runtime.load.Library; |
| 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 | + |
| 28 | + private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() { |
| 29 | + public IRubyObject allocate(Ruby runtime, RubyClass klazz) { |
| 30 | + return new JavaAtomicFixnum(runtime, klazz); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + @JRubyClass(name = "JavaAtomicFixnum", parent = "Object") |
| 35 | + public static class JavaAtomicFixnum extends RubyObject { |
| 36 | + |
| 37 | + private AtomicLong atomicLong; |
| 38 | + private ThreadContext context; |
| 39 | + |
| 40 | + public JavaAtomicFixnum(Ruby runtime, RubyClass metaClass) { |
| 41 | + super(runtime, metaClass); |
| 42 | + } |
| 43 | + |
| 44 | + @JRubyMethod |
| 45 | + public IRubyObject initialize(ThreadContext context) { |
| 46 | + this.atomicLong = new AtomicLong(0); |
| 47 | + this.context = context; |
| 48 | + return context.nil; |
| 49 | + } |
| 50 | + |
| 51 | + @JRubyMethod |
| 52 | + public IRubyObject initialize(ThreadContext context, IRubyObject value) { |
| 53 | + this.atomicLong = new AtomicLong(rubyFixnumToLong(value)); |
| 54 | + this.context = context; |
| 55 | + return context.nil; |
| 56 | + } |
| 57 | + |
| 58 | + @JRubyMethod(name = "value") |
| 59 | + public IRubyObject getValue() { |
| 60 | + return new RubyFixnum(getRuntime(), atomicLong.get()); |
| 61 | + } |
| 62 | + |
| 63 | + @JRubyMethod(name = "value=") |
| 64 | + public IRubyObject setValue(IRubyObject newValue) { |
| 65 | + atomicLong.set(rubyFixnumToLong(newValue)); |
| 66 | + return context.nil; |
| 67 | + } |
| 68 | + |
| 69 | + @JRubyMethod(name = {"increment", "up"}) |
| 70 | + public IRubyObject increment() { |
| 71 | + return new RubyFixnum(getRuntime(), atomicLong.incrementAndGet()); |
| 72 | + } |
| 73 | + |
| 74 | + @JRubyMethod(name = {"decrement", "down"}) |
| 75 | + public IRubyObject decrement() { |
| 76 | + return new RubyFixnum(getRuntime(), atomicLong.decrementAndGet()); |
| 77 | + } |
| 78 | + |
| 79 | + @JRubyMethod(name = "compare_and_set") |
| 80 | + public IRubyObject compareAndSet(IRubyObject expect, IRubyObject update) { |
| 81 | + return RubyBoolean.newBoolean(getRuntime(), atomicLong.compareAndSet(rubyFixnumToLong(expect), rubyFixnumToLong(update))); |
| 82 | + } |
| 83 | + |
| 84 | + private long rubyFixnumToLong(IRubyObject value) { |
| 85 | + if (value instanceof RubyFixnum) { |
| 86 | + RubyFixnum fixNum = (RubyFixnum) value; |
| 87 | + return fixNum.getLongValue(); |
| 88 | + } else { |
| 89 | + throw getRuntime().newArgumentError("initial value must be a Fixnum"); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
0 commit comments