|
| 1 | +package com.concurrent_ruby.ext; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.concurrent.Semaphore; |
| 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.RubyNumeric; |
| 11 | +import org.jruby.RubyObject; |
| 12 | +import org.jruby.anno.JRubyClass; |
| 13 | +import org.jruby.anno.JRubyMethod; |
| 14 | +import org.jruby.runtime.ObjectAllocator; |
| 15 | +import org.jruby.runtime.ThreadContext; |
| 16 | +import org.jruby.runtime.builtin.IRubyObject; |
| 17 | + |
| 18 | +public class JavaSemaphoreLibrary { |
| 19 | + |
| 20 | + public void load(Ruby runtime, boolean wrap) throws IOException { |
| 21 | + RubyModule concurrentMod = runtime.defineModule("Concurrent"); |
| 22 | + RubyClass atomicCls = concurrentMod.defineClassUnder("JavaSemaphore", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR); |
| 23 | + |
| 24 | + atomicCls.defineAnnotatedMethods(JavaSemaphore.class); |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() { |
| 29 | + public IRubyObject allocate(Ruby runtime, RubyClass klazz) { |
| 30 | + return new JavaSemaphore(runtime, klazz); |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + @JRubyClass(name = "JavaSemaphore", parent = "Object") |
| 35 | + public static class JavaSemaphore extends RubyObject { |
| 36 | + |
| 37 | + private JRubySemaphore semaphore; |
| 38 | + |
| 39 | + public JavaSemaphore(Ruby runtime, RubyClass metaClass) { |
| 40 | + super(runtime, metaClass); |
| 41 | + } |
| 42 | + |
| 43 | + @JRubyMethod |
| 44 | + public IRubyObject initialize(ThreadContext context, IRubyObject value) { |
| 45 | + this.semaphore = new JRubySemaphore(rubyFixnumToInt(value, "count")); |
| 46 | + return context.nil; |
| 47 | + } |
| 48 | + |
| 49 | + @JRubyMethod |
| 50 | + public IRubyObject acquire(ThreadContext context, IRubyObject value) throws InterruptedException { |
| 51 | + this.semaphore.acquire(rubyFixnumToInt(value, "permits")); |
| 52 | + return context.nil; |
| 53 | + } |
| 54 | + |
| 55 | + @JRubyMethod(name = "available_permits") |
| 56 | + public IRubyObject availablePermits(ThreadContext context) { |
| 57 | + return new RubyFixnum(getRuntime(), this.semaphore.availablePermits()); |
| 58 | + } |
| 59 | + |
| 60 | + @JRubyMethod(name = "drain_permits") |
| 61 | + public IRubyObject drainPermits(ThreadContext context) { |
| 62 | + return new RubyFixnum(getRuntime(), this.semaphore.drainPermits()); |
| 63 | + } |
| 64 | + |
| 65 | + @JRubyMethod |
| 66 | + public IRubyObject acquire(ThreadContext context) throws InterruptedException { |
| 67 | + this.semaphore.acquire(1); |
| 68 | + return context.nil; |
| 69 | + } |
| 70 | + |
| 71 | + @JRubyMethod(name = "try_acquire") |
| 72 | + public IRubyObject tryAcquire(ThreadContext context) throws InterruptedException { |
| 73 | + return RubyBoolean.newBoolean(getRuntime(), semaphore.tryAcquire(1)); |
| 74 | + } |
| 75 | + |
| 76 | + @JRubyMethod(name = "try_acquire") |
| 77 | + public IRubyObject tryAcquire(ThreadContext context, IRubyObject permits) throws InterruptedException { |
| 78 | + return RubyBoolean.newBoolean(getRuntime(), semaphore.tryAcquire(rubyFixnumToInt(permits, "permits"))); |
| 79 | + } |
| 80 | + |
| 81 | + @JRubyMethod(name = "try_acquire") |
| 82 | + public IRubyObject tryAcquire(ThreadContext context, IRubyObject permits, IRubyObject timeout) throws InterruptedException { |
| 83 | + return RubyBoolean.newBoolean(getRuntime(), |
| 84 | + semaphore.tryAcquire( |
| 85 | + rubyFixnumToInt(permits, "permits"), |
| 86 | + rubyNumericToLong(timeout, "timeout"), |
| 87 | + java.util.concurrent.TimeUnit.SECONDS) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + @JRubyMethod |
| 92 | + public IRubyObject release(ThreadContext context) { |
| 93 | + this.semaphore.release(1); |
| 94 | + return RubyBoolean.newBoolean(getRuntime(), true); |
| 95 | + } |
| 96 | + |
| 97 | + @JRubyMethod |
| 98 | + public IRubyObject release(ThreadContext context, IRubyObject value) { |
| 99 | + this.semaphore.release(rubyFixnumToInt(value, "permits")); |
| 100 | + return RubyBoolean.newBoolean(getRuntime(), true); |
| 101 | + } |
| 102 | + |
| 103 | + @JRubyMethod(name = "reduce_permits") |
| 104 | + public IRubyObject reducePermits(ThreadContext context, IRubyObject reduction) throws InterruptedException { |
| 105 | + this.semaphore.publicReducePermits(rubyFixnumToInt(reduction, "reduction")); |
| 106 | + return context.nil; |
| 107 | + } |
| 108 | + |
| 109 | + private int rubyFixnumToInt(IRubyObject value, String paramName) { |
| 110 | + if (value instanceof RubyFixnum && ((RubyFixnum) value).getLongValue() > 0) { |
| 111 | + RubyFixnum fixNum = (RubyFixnum) value; |
| 112 | + return (int) fixNum.getLongValue(); |
| 113 | + } else { |
| 114 | + throw getRuntime().newArgumentError(paramName + " must be in integer greater than zero"); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private long rubyNumericToLong(IRubyObject value, String paramName) { |
| 119 | + if (value instanceof RubyNumeric && ((RubyNumeric) value).getDoubleValue() > 0) { |
| 120 | + RubyNumeric fixNum = (RubyNumeric) value; |
| 121 | + return fixNum.getLongValue(); |
| 122 | + } else { |
| 123 | + throw getRuntime().newArgumentError(paramName + " must be in float greater than zero"); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + class JRubySemaphore extends Semaphore { |
| 128 | + |
| 129 | + public JRubySemaphore(int permits) { |
| 130 | + super(permits); |
| 131 | + } |
| 132 | + |
| 133 | + public JRubySemaphore(int permits, boolean value) { |
| 134 | + super(permits, value); |
| 135 | + } |
| 136 | + |
| 137 | + public void publicReducePermits(int i) { |
| 138 | + reducePermits(i); |
| 139 | + } |
| 140 | + |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
0 commit comments