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: 4cefae4f538c
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4ef46af9e541
Choose a head ref
  • 4 commits
  • 9 files changed
  • 1 contributor

Commits on Dec 15, 2015

  1. Copy the full SHA
    f7088b9 View commit details
  2. Move Queue impl used by Fiber to FiberQueue.

    Queue impl is changing, so I'm isolating Fiber's use of it to a
    separate class to avoid volatility in Fiber logic.
    headius committed Dec 15, 2015
    Copy the full SHA
    e892863 View commit details
  3. Reimplement Queue and SizedQueue like MRI and add #close.

    MRI implements Queue and SizedQueue differently than we had been:
    
    * Implicitly locking around all operations, due to the GIL.
    * Notification of just those waiting to push for SizedQueue.
    
    In addition, the new #close method wakes up all threads waiting
    to pop or push, which was not easily done by wrapping JDK's
    LinkedBlockingQueue.
    
    This new impl is somewhat more in line with MRI, with regards to
    locking and notification. This locking will likely introduce some
    overhead compared to the old implementation, but the use of a
    simple ArrayList may balance that out somewhat. All tests in 2.3's
    thread/test_queue.rb pass now except for one relating to a
    a peculiarity in MRI's implementation: if you remove marshal_dump
    it falls back on Struct marshaling, since in MRI both Queue and
    SizedQueue are Structs. Ours are not Struct and I do not see a
    good reason to make them be so.
    headius committed Dec 15, 2015
    Copy the full SHA
    2531143 View commit details
  4. Copy the full SHA
    4ef46af View commit details
11 changes: 6 additions & 5 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -1327,6 +1327,7 @@ private boolean doesReflectionWork() {
private void bootstrap() {
initCore();
initExceptions();
initLibraries();
}

private void initDefinedMessages() {
@@ -1565,11 +1566,6 @@ private void initCore() {
RubyEnumerator.defineEnumerator(this);
}

// Fiber depends on thread library, so we load it here
new ThreadLibrary().load(this, false);

new ThreadFiberLibrary().load(this, false);

TracePoint.createTracePointClass(this);
}

@@ -1650,6 +1646,11 @@ private void initExceptions() {
initErrno();
}

private void initLibraries() {
new ThreadLibrary().load(this, false);
new ThreadFiberLibrary().load(this, false);
}

private RubyClass defineClassIfAllowed(String name, RubyClass superClass) {
// TODO: should probably apply the null object pattern for a
// non-allowed class, rather than null
9 changes: 9 additions & 0 deletions core/src/main/java/org/jruby/RubyNameError.java
Original file line number Diff line number Diff line change
@@ -217,6 +217,15 @@ public IRubyObject name() {
return name;
}

@JRubyMethod
public IRubyObject receiver(ThreadContext context) {
if (name instanceof RubyNameErrorMessage) {
return ((RubyNameErrorMessage)name).object;
}

throw context.runtime.newArgumentError("no receiver is available");
}

@Override
public void copySpecialInstanceVariables(IRubyObject clone) {
super.copySpecialInstanceVariables(clone);
134 changes: 134 additions & 0 deletions core/src/main/java/org/jruby/ext/fiber/FiberQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2006 MenTaLguY <mental@rydia.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.ext.fiber;

import org.jruby.Ruby;
import org.jruby.RubyThread;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
* A RubyThread-aware BlockingQueue wrapper used by Fiber for transfering values.
*/
public class FiberQueue {
protected BlockingQueue<IRubyObject> queue;
protected final Ruby runtime;

public FiberQueue(Ruby runtime) {
this.runtime = runtime;
this.queue = new ArrayBlockingQueue<>(1, false);
}

final RubyThread.Task<FiberQueue, IRubyObject> takeTask = new RubyThread.Task<FiberQueue, IRubyObject>() {
@Override
public IRubyObject run(ThreadContext context, FiberQueue queue) throws InterruptedException {
return queue.getQueueSafe().take();
}

@Override
public void wakeup(RubyThread thread, FiberQueue data) {
thread.getNativeThread().interrupt();
}
};

final RubyThread.Task<IRubyObject[], IRubyObject> putTask = new RubyThread.Task<IRubyObject[], IRubyObject>() {
@Override
public IRubyObject run(ThreadContext context, IRubyObject[] args) throws InterruptedException {
final BlockingQueue<IRubyObject> queue = getQueueSafe();
if(args.length == 2 && args[1].isTrue() && queue.remainingCapacity() == 0) {
throw context.runtime.newThreadError("queue full");
}
queue.put(args[0]);
return context.nil;
}

@Override
public void wakeup(RubyThread thread, IRubyObject[] data) {
thread.getNativeThread().interrupt();
}
};

public IRubyObject shutdown(ThreadContext context) {
queue = null;
return context.runtime.getNil();
}

public synchronized void shutdown() {
queue = null;
}

public boolean isShutdown() {
return queue == null;
}

public BlockingQueue<IRubyObject> getQueueSafe() {
BlockingQueue<IRubyObject> queue = this.queue;
checkShutdown();
return queue;
}

public synchronized void checkShutdown() {
if (queue == null) {
throw new RaiseException(runtime, runtime.getThreadError(), "queue shut down", false);
}
}

public IRubyObject pop(ThreadContext context) {
return pop(context, true);
}

public IRubyObject pop(ThreadContext context, IRubyObject arg0) {
return pop(context, !arg0.isTrue());
}

public void push(ThreadContext context, final IRubyObject[] args) {
checkShutdown();
try {
context.getThread().executeTask(context, args, putTask);
} catch (InterruptedException ie) {
throw context.runtime.newThreadError("interrupted in FiberQueue.push");
}
}

private IRubyObject pop(ThreadContext context, boolean should_block) {
final BlockingQueue<IRubyObject> queue = getQueueSafe();
if (!should_block && queue.size() == 0) {
throw new RaiseException(context.runtime, context.runtime.getThreadError(), "queue empty", false);
}
try {
return context.getThread().executeTask(context, this, takeTask);
} catch (InterruptedException ie) {
throw context.runtime.newThreadError("interrupted in FiberQueue.pop");
}
}

}
14 changes: 6 additions & 8 deletions core/src/main/java/org/jruby/ext/fiber/ThreadFiber.java
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.JumpException;
import org.jruby.exceptions.RaiseException;
import org.jruby.ext.thread.SizedQueue;
import org.jruby.javasupport.JavaUtil;
import org.jruby.runtime.Block;
import org.jruby.runtime.ExecutionContext;
@@ -31,9 +30,8 @@ public static void initRootFiber(ThreadContext context) {
Ruby runtime = context.runtime;

ThreadFiber rootFiber = new ThreadFiber(runtime, runtime.getClass("Fiber")); // FIXME: getFiber()

assert runtime.getClass("SizedQueue") != null : "SizedQueue has not been loaded";
rootFiber.data = new FiberData(new SizedQueue(runtime, runtime.getClass("SizedQueue"), 1), null, rootFiber);

rootFiber.data = new FiberData(new FiberQueue(runtime), null, rootFiber);
rootFiber.thread = context.getThread();
context.setRootFiber(rootFiber);
}
@@ -44,7 +42,7 @@ public IRubyObject initialize(ThreadContext context, Block block) {

if (!block.isGiven()) throw runtime.newArgumentError("tried to create Proc object without block");

data = new FiberData(new SizedQueue(runtime, runtime.getClass("SizedQueue"), 1), context.getFiberCurrentThread(), this);
data = new FiberData(new FiberQueue(runtime), context.getFiberCurrentThread(), this);

FiberData currentFiberData = context.getFiber().data;

@@ -238,7 +236,7 @@ boolean alive() {
return thread != null && thread.isAlive() && !data.queue.isShutdown();
}

static RubyThread createThread(final Ruby runtime, final FiberData data, final SizedQueue queue, final Block block) {
static RubyThread createThread(final Ruby runtime, final FiberData data, final FiberQueue queue, final Block block) {
final AtomicReference<RubyThread> fiberThread = new AtomicReference();
runtime.getFiberExecutor().execute(new Runnable() {
public void run() {
@@ -336,7 +334,7 @@ public RubyThread getThread() {
}

public static class FiberData {
FiberData(SizedQueue queue, RubyThread parent, ThreadFiber fiber) {
FiberData(FiberQueue queue, RubyThread parent, ThreadFiber fiber) {
this.queue = queue;
this.parent = parent;
this.fiber = new WeakReference<ThreadFiber>(fiber);
@@ -346,7 +344,7 @@ public ThreadFiber getPrev() {
return prev;
}

final SizedQueue queue;
final FiberQueue queue;
volatile ThreadFiber prev;
final RubyThread parent;
final WeakReference<ThreadFiber> fiber;
Loading