Skip to content

Commit

Permalink
Showing 3 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -15,8 +15,10 @@
import com.oracle.truffle.api.frame.FrameInstance;
import com.oracle.truffle.api.frame.FrameInstanceVisitor;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;

import org.jruby.Ruby;
import org.jruby.RubyGC;
import org.jruby.ext.rbconfig.RbConfigLibrary;
@@ -520,4 +522,20 @@ protected static boolean canLower(long value) {

}

@CoreMethod(names = "synchronized", isModuleFunction = true, required = 1, needsBlock = true)
public abstract static class SynchronizedPrimitiveNode extends YieldingCoreMethodNode {

public SynchronizedPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

// We must not allow to synchronize on boxed primitives.
@Specialization(guards = "isRubyProc(block)")
public Object synchronize(VirtualFrame frame, RubyBasicObject self, RubyBasicObject block) {
synchronized (self) {
return yield(frame, block);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

/**
* Manages Ruby {@code Thread} objects.
@@ -33,10 +32,8 @@ public class ThreadManager {

private final RubyContext context;

private final ReentrantLock globalLock = new ReentrantLock();

private final RubyBasicObject rootThread;
private RubyBasicObject currentThread;
private final ThreadLocal<RubyBasicObject> currentThread = new ThreadLocal<RubyBasicObject>();

private final Set<RubyBasicObject> runningRubyThreads = Collections.newSetFromMap(new ConcurrentHashMap<RubyBasicObject, Boolean>());

@@ -65,8 +62,7 @@ public RubyBasicObject getRootThread() {
@TruffleBoundary
public void enterGlobalLock(RubyBasicObject thread) {
assert RubyGuards.isRubyThread(thread);
globalLock.lock();
currentThread = thread;
currentThread.set(thread);
}

/**
@@ -77,13 +73,7 @@ public void enterGlobalLock(RubyBasicObject thread) {
*/
@TruffleBoundary
public RubyBasicObject leaveGlobalLock() {
if (!globalLock.isHeldByCurrentThread()) {
throw new RuntimeException("You don't own this lock!");
}

final RubyBasicObject result = currentThread;
globalLock.unlock();
return result;
return currentThread.get();
}


@@ -128,8 +118,7 @@ public <T> T runUntilResult(BlockingActionWithoutGlobalLock<T> action) {
}

public RubyBasicObject getCurrentThread() {
assert globalLock.isHeldByCurrentThread() : "getCurrentThread() is only correct if holding the global lock";
return currentThread;
return currentThread.get();
}

public synchronized void registerThread(RubyBasicObject thread) {
6 changes: 2 additions & 4 deletions truffle/src/main/ruby/core/shims.rb
Original file line number Diff line number Diff line change
@@ -152,12 +152,10 @@ class Rubinius::ByteArray

end

# Don't apply any synchronization at the moment

module Rubinius

def self.synchronize(object)
yield
def self.synchronize(object, &block)
Truffle::Primitive.synchronized(object, &block)
end

end

0 comments on commit 155ee96

Please sign in to comment.