Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c420b1d5c620
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: cf4a71ecd18f
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Feb 2, 2015

  1. [Truffle] Store the names of threads and fibers in the actual objects…

    … as well as the Java thread objects.
    chrisseaton committed Feb 2, 2015
    Copy the full SHA
    293a7d0 View commit details
  2. Copy the full SHA
    cf4a71e View commit details
1 change: 0 additions & 1 deletion spec/truffle/tags/core/fiber/resume_tags.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ public Object resume(RubyFiber fiberBeingResumed, Object[] args) {

final RubyFiber sendingFiber = getContext().getFiberManager().getCurrentFiber();

fiberBeingResumed.resume(sendingFiber, args);
fiberBeingResumed.resume(false, sendingFiber, args);

return sendingFiber.waitForResume();
}
@@ -92,7 +92,7 @@ public Object yield(Object[] args) {
throw new RaiseException(getContext().getCoreLibrary().yieldFromRootFiberError(this));
}

fiberYieldedTo.resume(yieldingFiber, args);
fiberYieldedTo.resume(true, yieldingFiber, args);

return yieldingFiber.waitForResume();
}
Original file line number Diff line number Diff line change
@@ -39,16 +39,22 @@ private interface FiberMessage {

private static class FiberResumeMessage implements FiberMessage {

private final boolean yield;
private final RubyThread thread;
private final RubyFiber sendingFiber;
private final Object arg;

public FiberResumeMessage(RubyThread thread, RubyFiber sendingFiber, Object arg) {
public FiberResumeMessage(boolean yield, RubyThread thread, RubyFiber sendingFiber, Object arg) {
this.yield = yield;
this.thread = thread;
this.sendingFiber = sendingFiber;
this.arg = arg;
}

public boolean isYield() {
return yield;
}

public RubyThread getThread() {
return thread;
}
@@ -95,21 +101,25 @@ public static class FiberExitException extends ControlFlowException {
private final FiberManager fiberManager;
private final ThreadManager threadManager;

private String name;
private boolean topLevel;
private BlockingQueue<FiberMessage> messageQueue = new ArrayBlockingQueue<>(1);
private RubyFiber lastResumedByFiber = null;
private boolean alive = true;

public RubyFiber(RubyClass rubyClass, FiberManager fiberManager, ThreadManager threadManager, boolean topLevel) {
public RubyFiber(RubyClass rubyClass, FiberManager fiberManager, ThreadManager threadManager, String name, boolean topLevel) {
super(rubyClass);
this.fiberManager = fiberManager;
this.threadManager = threadManager;
this.name = name;
this.topLevel = topLevel;
}

public void initialize(RubyProc block) {
RubyNode.notDesignedForCompilation();

name = "Ruby Fiber@" + block.getSharedMethodInfo().getSourceSection().getShortDescription();

final RubyFiber finalFiber = this;
final RubyProc finalBlock = block;

@@ -123,7 +133,7 @@ public void run() {
try {
final Object arg = finalFiber.waitForResume();
final Object result = finalBlock.rootCall(arg);
finalFiber.lastResumedByFiber.resume(finalFiber, result);
finalFiber.lastResumedByFiber.resume(true, finalFiber, result);
} catch (FiberExitException e) {
// Naturally exit the thread on catching this
} catch (ReturnException e) {
@@ -140,7 +150,7 @@ public void run() {
}

});
thread.setName("Ruby Fiber@" + block.getSharedMethodInfo().getSourceSection().getShortDescription());
thread.setName(name);
thread.start();
}

@@ -168,8 +178,10 @@ public FiberMessage block() throws InterruptedException {
threadManager.enterGlobalLock(((FiberExceptionMessage) message).getThread());
throw new RaiseException(((FiberExceptionMessage) message).getException());
} else if (message instanceof FiberResumeMessage) {
if (!((FiberResumeMessage) message).isYield()) {
lastResumedByFiber = ((FiberResumeMessage) message).getSendingFiber();
}
threadManager.enterGlobalLock(((FiberResumeMessage) message).getThread());
lastResumedByFiber = ((FiberResumeMessage) message).getSendingFiber();
return ((FiberResumeMessage) message).getArg();
} else {
throw new UnsupportedOperationException();
@@ -181,7 +193,7 @@ public FiberMessage block() throws InterruptedException {
* thread (although the queue implementation may) and doesn't wait for the message to be
* received. On entry, assumes the the GIL is held. On exit, not holding the GIL.
*/
public void resume(RubyFiber sendingFiber, Object... args) {
public void resume(boolean yield, RubyFiber sendingFiber, Object... args) {
RubyNode.notDesignedForCompilation();

// TODO CS 2-Feb-15 move this logic into the node where we can specialise?
@@ -198,7 +210,7 @@ public void resume(RubyFiber sendingFiber, Object... args) {

final RubyThread runningThread = threadManager.leaveGlobalLock();

messageQueue.add(new FiberResumeMessage(runningThread, sendingFiber, arg));
messageQueue.add(new FiberResumeMessage(yield, runningThread, sendingFiber, arg));
}

public void shutdown() {
@@ -221,11 +233,15 @@ public boolean isTopLevel() {
return topLevel;
}

public String getName() {
return name;
}

public static class FiberAllocator implements Allocator {

@Override
public RubyBasicObject allocate(RubyContext context, RubyClass rubyClass, RubyNode currentNode) {
return new RubyFiber(rubyClass, context.getFiberManager(), context.getThreadManager(), false);
return new RubyFiber(rubyClass, context.getFiberManager(), context.getThreadManager(), null, false);
}

}
Original file line number Diff line number Diff line change
@@ -30,6 +30,8 @@ public class RubyThread extends RubyBasicObject {

private final ThreadManager manager;

private String name;

private final CountDownLatch finished = new CountDownLatch(1);

private volatile Thread thread;
@@ -67,6 +69,8 @@ public void initialize(final RubyContext context, final RubyNode currentNode, Ru
final RubyThread finalThread = this;
final Runnable finalRunnable = runnable;

name = "Ruby Thread@" + name;

thread = new Thread(new Runnable() {
@Override
public void run() {
@@ -95,7 +99,7 @@ public void run() {
}

});
thread.setName("Ruby Thread@" + name);
thread.setName(name);
thread.setDaemon(true);
thread.start();
}
@@ -144,6 +148,10 @@ public RubyException getException() {
public void shutdown() {
}

public String getName() {
return name;
}

public static class ThreadAllocator implements Allocator {

@Override
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public class FiberManager {
private final Set<RubyFiber> runningFibers = Collections.newSetFromMap(new ConcurrentHashMap<RubyFiber, Boolean>());

public FiberManager(RubyContext context) {
rootFiber = new RubyFiber(context.getCoreLibrary().getFiberClass(), this, context.getThreadManager(), true);
rootFiber = new RubyFiber(context.getCoreLibrary().getFiberClass(), this, context.getThreadManager(), "root", true);
currentFiber = rootFiber;
}