-
Notifications
You must be signed in to change notification settings - Fork 419
Implementation of Concurrent::JavaSemaphore in pure Java. #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.concurrent_ruby.ext; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.Semaphore; | ||
import org.jruby.Ruby; | ||
import org.jruby.RubyBoolean; | ||
import org.jruby.RubyClass; | ||
import org.jruby.RubyFixnum; | ||
import org.jruby.RubyModule; | ||
import org.jruby.RubyNumeric; | ||
import org.jruby.RubyObject; | ||
import org.jruby.anno.JRubyClass; | ||
import org.jruby.anno.JRubyMethod; | ||
import org.jruby.runtime.ObjectAllocator; | ||
import org.jruby.runtime.ThreadContext; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
|
||
public class JavaSemaphoreLibrary { | ||
|
||
public void load(Ruby runtime, boolean wrap) throws IOException { | ||
RubyModule concurrentMod = runtime.defineModule("Concurrent"); | ||
RubyClass atomicCls = concurrentMod.defineClassUnder("JavaSemaphore", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR); | ||
|
||
atomicCls.defineAnnotatedMethods(JavaSemaphore.class); | ||
|
||
} | ||
|
||
private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() { | ||
public IRubyObject allocate(Ruby runtime, RubyClass klazz) { | ||
return new JavaSemaphore(runtime, klazz); | ||
} | ||
}; | ||
|
||
@JRubyClass(name = "JavaSemaphore", parent = "Object") | ||
public static class JavaSemaphore extends RubyObject { | ||
|
||
private JRubySemaphore semaphore; | ||
|
||
public JavaSemaphore(Ruby runtime, RubyClass metaClass) { | ||
super(runtime, metaClass); | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject initialize(ThreadContext context, IRubyObject value) { | ||
this.semaphore = new JRubySemaphore(rubyFixnumToInt(value, "count")); | ||
return context.nil; | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject acquire(ThreadContext context, IRubyObject value) throws InterruptedException { | ||
this.semaphore.acquire(rubyFixnumToInt(value, "permits")); | ||
return context.nil; | ||
} | ||
|
||
@JRubyMethod(name = "available_permits") | ||
public IRubyObject availablePermits(ThreadContext context) { | ||
return new RubyFixnum(getRuntime(), this.semaphore.availablePermits()); | ||
} | ||
|
||
@JRubyMethod(name = "drain_permits") | ||
public IRubyObject drainPermits(ThreadContext context) { | ||
return new RubyFixnum(getRuntime(), this.semaphore.drainPermits()); | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject acquire(ThreadContext context) throws InterruptedException { | ||
this.semaphore.acquire(1); | ||
return context.nil; | ||
} | ||
|
||
@JRubyMethod(name = "try_acquire") | ||
public IRubyObject tryAcquire(ThreadContext context) throws InterruptedException { | ||
return RubyBoolean.newBoolean(getRuntime(), semaphore.tryAcquire(1)); | ||
} | ||
|
||
@JRubyMethod(name = "try_acquire") | ||
public IRubyObject tryAcquire(ThreadContext context, IRubyObject permits) throws InterruptedException { | ||
return RubyBoolean.newBoolean(getRuntime(), semaphore.tryAcquire(rubyFixnumToInt(permits, "permits"))); | ||
} | ||
|
||
@JRubyMethod(name = "try_acquire") | ||
public IRubyObject tryAcquire(ThreadContext context, IRubyObject permits, IRubyObject timeout) throws InterruptedException { | ||
return RubyBoolean.newBoolean(getRuntime(), | ||
semaphore.tryAcquire( | ||
rubyFixnumToInt(permits, "permits"), | ||
rubyNumericToLong(timeout, "timeout"), | ||
java.util.concurrent.TimeUnit.SECONDS) | ||
); | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject release(ThreadContext context) { | ||
this.semaphore.release(1); | ||
return RubyBoolean.newBoolean(getRuntime(), true); | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject release(ThreadContext context, IRubyObject value) { | ||
this.semaphore.release(rubyFixnumToInt(value, "permits")); | ||
return RubyBoolean.newBoolean(getRuntime(), true); | ||
} | ||
|
||
@JRubyMethod(name = "reduce_permits") | ||
public IRubyObject reducePermits(ThreadContext context, IRubyObject reduction) throws InterruptedException { | ||
this.semaphore.publicReducePermits(rubyFixnumToInt(reduction, "reduction")); | ||
return context.nil; | ||
} | ||
|
||
private int rubyFixnumToInt(IRubyObject value, String paramName) { | ||
if (value instanceof RubyFixnum && ((RubyFixnum) value).getLongValue() > 0) { | ||
RubyFixnum fixNum = (RubyFixnum) value; | ||
return (int) fixNum.getLongValue(); | ||
} else { | ||
throw getRuntime().newArgumentError(paramName + " must be in integer greater than zero"); | ||
} | ||
} | ||
|
||
private long rubyNumericToLong(IRubyObject value, String paramName) { | ||
if (value instanceof RubyNumeric && ((RubyNumeric) value).getDoubleValue() > 0) { | ||
RubyNumeric fixNum = (RubyNumeric) value; | ||
return fixNum.getLongValue(); | ||
} else { | ||
throw getRuntime().newArgumentError(paramName + " must be in float greater than zero"); | ||
} | ||
} | ||
|
||
class JRubySemaphore extends Semaphore { | ||
|
||
public JRubySemaphore(int permits) { | ||
super(permits); | ||
} | ||
|
||
public JRubySemaphore(int permits, boolean value) { | ||
super(permits, value); | ||
} | ||
|
||
public void publicReducePermits(int i) { | ||
reducePermits(i); | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've missed one more thing. JRuby extensions should try to use interface provided by
Ruby
object (result ofgetRuntime()
) it may do additional steps like caching etc. see https://github.com/jruby/jruby/blob/master/core/src/main/java/org/jruby/RubyFixnum.java#L207-L212. So it's better to use thenewBoolean
andnewFixnum
methods onRuby
object.