-
Notifications
You must be signed in to change notification settings - Fork 419
Synchronization layer #273
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
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d66f616
Add EngineDetector and fix extension_helper path
pitr-ch 2625863
Add synchronization layer
pitr-ch 93a4fc2
Migrate Event to SynchronizedObject
pitr-ch d4a21ea
Fix ns_wait_until
pitr-ch 2e2a464
Support 1.9.3 Ruby
pitr-ch 41daaf6
Fix rounding error
pitr-ch 07ee51f
Add basic rbx support
pitr-ch 06fc777
Clarify return values
pitr-ch 52fdb3e
Add better Rbx support
pitr-ch 3a8aedc
Migrate CountDownLatch to SynchronizedObject
pitr-ch 662ba4a
Migrate CyclicBarrier to SynchronizedObject
pitr-ch e78b4a5
Make Thread#status work
pitr-ch 18d56fa
Improve documentation and unify default parameters
pitr-ch 524592a
Add java implementation of JavaSynchronizedObject
pitr-ch 44b3863
Cleanup the load order in synchronized object
pitr-ch 759c025
Cleanup names
pitr-ch 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
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
106 changes: 106 additions & 0 deletions
106
ext/com/concurrent_ruby/ext/SynchronizationLibrary.java
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,106 @@ | ||
package com.concurrent_ruby.ext; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import org.jruby.Ruby; | ||
import org.jruby.RubyClass; | ||
import org.jruby.RubyModule; | ||
import org.jruby.RubyObject; | ||
import org.jruby.anno.JRubyClass; | ||
import org.jruby.anno.JRubyMethod; | ||
import org.jruby.runtime.ObjectAllocator; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.jruby.runtime.load.Library; | ||
import org.jruby.runtime.Block; | ||
import org.jruby.RubyBoolean; | ||
import org.jruby.RubyNil; | ||
import org.jruby.runtime.ThreadContext; | ||
|
||
public class SynchronizationLibrary implements Library { | ||
|
||
public void load(Ruby runtime, boolean wrap) throws IOException { | ||
RubyModule synchronizationModule = runtime. | ||
defineModule("Concurrent"). | ||
defineModuleUnder("Synchronization"); | ||
RubyClass parentClass = synchronizationModule.getClass("AbstractObject"); | ||
|
||
if (parentClass == null) | ||
throw runtime.newRuntimeError("Concurrent::Synchronization::AbstractObject is missing"); | ||
|
||
RubyClass synchronizedObjectJavaClass = | ||
synchronizationModule.defineClassUnder("JavaObject", parentClass, JRUBYREFERENCE_ALLOCATOR); | ||
|
||
synchronizedObjectJavaClass.defineAnnotatedMethods(JavaObject.class); | ||
} | ||
|
||
private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() { | ||
public IRubyObject allocate(Ruby runtime, RubyClass klazz) { | ||
return new JavaObject(runtime, klazz); | ||
} | ||
}; | ||
|
||
@JRubyClass(name = "JavaObject", parent = "AbstractObject") | ||
public static class JavaObject extends RubyObject { | ||
|
||
public JavaObject(Ruby runtime, RubyClass metaClass) { | ||
super(runtime, metaClass); | ||
} | ||
|
||
@JRubyMethod | ||
public IRubyObject initialize(ThreadContext context) { | ||
return context.nil; | ||
} | ||
|
||
@JRubyMethod(name = "synchronize") | ||
public IRubyObject rubySynchronize(ThreadContext context, Block block) { | ||
synchronized (this) { | ||
return block.yield(context, null); | ||
} | ||
} | ||
|
||
@JRubyMethod(name = "ns_wait", optional = 1) | ||
public IRubyObject nsWait(ThreadContext context, IRubyObject[] args) { | ||
Ruby runtime = context.runtime; | ||
if (args.length > 1) { | ||
throw runtime.newArgumentError(args.length, 1); | ||
} | ||
Double timeout = null; | ||
if (args.length > 0 && !args[0].isNil()) { | ||
timeout = args[0].convertToFloat().getDoubleValue(); | ||
if (timeout < 0) { | ||
throw runtime.newArgumentError("time interval must be positive"); | ||
} | ||
} | ||
if (Thread.interrupted()) { | ||
throw runtime.newConcurrencyError("thread interrupted"); | ||
} | ||
boolean success = false; | ||
try { | ||
success = context.getThread().wait_timeout(this, timeout); | ||
} catch (InterruptedException ie) { | ||
throw runtime.newConcurrencyError(ie.getLocalizedMessage()); | ||
} finally { | ||
// An interrupt or timeout may have caused us to miss | ||
// a notify that we consumed, so do another notify in | ||
// case someone else is available to pick it up. | ||
if (!success) { | ||
this.notify(); | ||
} | ||
} | ||
return this; | ||
} | ||
|
||
@JRubyMethod(name = "ns_signal") | ||
public IRubyObject nsSignal(ThreadContext context) { | ||
notify(); | ||
return this; | ||
} | ||
|
||
@JRubyMethod(name = "ns_broadcast") | ||
public IRubyObject nsBroadcast(ThreadContext context) { | ||
notifyAll(); | ||
return this; | ||
} | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -3,16 +3,17 @@ | |
# user that they should use the new implementation instead. | ||
|
||
if defined?(Atomic) | ||
warn <<-RUBY | ||
warn <<-TXT | ||
[ATOMIC] Detected an `Atomic` class, which may indicate a dependency | ||
on the ruby-atomic gem. That gem has been deprecated and merged into | ||
the concurrent-ruby gem. Please use the Concurrent::Atomic class for | ||
atomic references and not the Atomic class. | ||
RUBY | ||
TXT | ||
end | ||
##################################################################### | ||
|
||
require_relative '../extension_helper' | ||
require 'concurrent/native_extensions' | ||
require 'concurrent/utility/engine' | ||
require 'concurrent/atomic_reference/concurrent_update_error' | ||
require 'concurrent/atomic_reference/mutex_atomic' | ||
|
||
|
@@ -21,7 +22,7 @@ | |
if /[^0fF]/ =~ ENV['FORCE_ATOMIC_FALLBACK'] | ||
ruby_engine = 'mutex_atomic' | ||
else | ||
ruby_engine = defined?(RUBY_ENGINE)? RUBY_ENGINE : 'ruby' | ||
ruby_engine = Concurrent.ruby_engine | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I really like this |
||
end | ||
|
||
require "concurrent/atomic_reference/#{ruby_engine}" | ||
|
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
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
Oops, something went wrong.
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.
We should consider removing this entirely for 0.9. since this message was introduced in 0.8. We definitely do not want it in the 1.0 release.