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

Commits on Nov 7, 2016

  1. Copy the full SHA
    d460517 View commit details
  2. [Truffle] Fork RubyThread.Status as ThreadStatus.

    * So we don't need to load RubyThread for an enum.
    eregon committed Nov 7, 2016
    Copy the full SHA
    05e0550 View commit details
  3. Copy the full SHA
    7f0c331 View commit details
  4. Copy the full SHA
    8960ec0 View commit details
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
import com.oracle.truffle.api.object.dsl.Layout;
import com.oracle.truffle.api.object.dsl.Nullable;
import com.oracle.truffle.api.object.dsl.Volatile;
import org.jruby.RubyThread;
import org.jruby.truffle.core.InterruptMode;
import org.jruby.truffle.core.basicobject.BasicObjectLayout;
import org.jruby.truffle.core.fiber.FiberManager;
@@ -36,7 +35,7 @@ DynamicObject createThread(
DynamicObjectFactory factory,
DynamicObject threadLocals,
@Volatile InterruptMode interruptMode, // needs to be volatile for fibers implemented by threads
@Volatile RubyThread.Status status,
@Volatile ThreadStatus status,
List<Lock> ownedLocks,
@Nullable FiberManager fiberManager,
CountDownLatch finishedLatch,
@@ -70,8 +69,8 @@ DynamicObject createThread(
Thread getThread(DynamicObject object);
void setThread(DynamicObject object, Thread value);

RubyThread.Status getStatus(DynamicObject object);
void setStatus(DynamicObject object, RubyThread.Status value);
ThreadStatus getStatus(DynamicObject object);
void setStatus(DynamicObject object, ThreadStatus value);

DynamicObject getException(DynamicObject object);
void setException(DynamicObject object, DynamicObject value);
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@
import com.oracle.truffle.api.source.SourceSection;
import jnr.posix.DefaultNativeTimeval;
import jnr.posix.Timeval;
import org.jruby.RubyThread.Status;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.InterruptMode;
@@ -56,7 +55,7 @@ public ThreadManager(RubyContext context) {
}

public static final InterruptMode DEFAULT_INTERRUPT_MODE = InterruptMode.IMMEDIATE;
public static final Status DEFAULT_STATUS = Status.RUN;
public static final ThreadStatus DEFAULT_STATUS = ThreadStatus.RUN;

public static DynamicObject createRubyThread(RubyContext context) {
final DynamicObject object = Layouts.THREAD.createThread(
@@ -162,10 +161,10 @@ public static void start(RubyContext context, DynamicObject thread) {
public static void cleanup(RubyContext context, DynamicObject thread) {
assert RubyGuards.isRubyThread(thread);

Layouts.THREAD.setStatus(thread, Status.ABORTING);
Layouts.THREAD.setStatus(thread, ThreadStatus.ABORTING);
context.getThreadManager().unregisterThread(thread);

Layouts.THREAD.setStatus(thread, Status.DEAD);
Layouts.THREAD.setStatus(thread, ThreadStatus.DEAD);
Layouts.THREAD.setThread(thread, null);
assert RubyGuards.isRubyThread(thread);
for (Lock lock : Layouts.THREAD.getOwnedLocks(thread)) {
@@ -218,13 +217,13 @@ public <T> T runUntilResult(Node currentNode, BlockingAction<T> action) {
T result = null;

do {
Layouts.THREAD.setStatus(runningThread, Status.SLEEP);
Layouts.THREAD.setStatus(runningThread, ThreadStatus.SLEEP);

try {
try {
result = action.block();
} finally {
Layouts.THREAD.setStatus(runningThread, Status.RUN);
Layouts.THREAD.setStatus(runningThread, ThreadStatus.RUN);
}
} catch (InterruptedException e) {
// We were interrupted, possibly by the SafepointManager.
Original file line number Diff line number Diff line change
@@ -6,6 +6,37 @@
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*
* 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) 2002 Jason Voegele <jason@jvoegele.com>
* Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
* Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
* Copyright (C) 2004-2005 Charles O Nutter <headius@headius.com>
* Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
*
* 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.
*/
package org.jruby.truffle.core.thread;

@@ -19,7 +50,8 @@
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.RubyThread.Status;

import org.jcodings.specific.USASCIIEncoding;
import org.jruby.runtime.Visibility;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
@@ -50,11 +82,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import static org.jruby.RubyThread.RUBY_MAX_THREAD_PRIORITY;
import static org.jruby.RubyThread.RUBY_MIN_THREAD_PRIORITY;
import static org.jruby.RubyThread.javaPriorityToRubyPriority;
import static org.jruby.RubyThread.rubyPriorityToJavaPriority;

@CoreClass("Thread")
public abstract class ThreadNodes {

@@ -63,8 +90,8 @@ public abstract static class AliveNode extends CoreMethodArrayArgumentsNode {

@Specialization
public boolean alive(DynamicObject thread) {
final Status status = Layouts.THREAD.getStatus(thread);
return status != Status.ABORTING && status != Status.DEAD;
final ThreadStatus status = Layouts.THREAD.getStatus(thread);
return status != ThreadStatus.ABORTING && status != ThreadStatus.DEAD;
}

}
@@ -312,16 +339,15 @@ public abstract static class StatusNode extends CoreMethodArrayArgumentsNode {
@Specialization
public Object status(DynamicObject self) {
// TODO: slightly hackish
final Status status = Layouts.THREAD.getStatus(self);
if (status == Status.DEAD) {
final ThreadStatus status = Layouts.THREAD.getStatus(self);
if (status == ThreadStatus.DEAD) {
if (Layouts.THREAD.getException(self) != null) {
return nil();
} else {
return false;
}
}

return createString(status.bytes);
return create7BitString(status.toString().toLowerCase(), USASCIIEncoding.INSTANCE);
}

}
@@ -331,8 +357,8 @@ public abstract static class StopNode extends CoreMethodArrayArgumentsNode {

@Specialization
public boolean stop(DynamicObject self) {
final Status status = Layouts.THREAD.getStatus(self);
return status == Status.DEAD || status == Status.SLEEP;
final ThreadStatus status = Layouts.THREAD.getStatus(self);
return status == ThreadStatus.DEAD || status == ThreadStatus.SLEEP;
}

}
@@ -354,7 +380,7 @@ public abstract static class WakeupNode extends CoreMethodArrayArgumentsNode {
@TruffleBoundary
@Specialization
public DynamicObject wakeup(final DynamicObject thread) {
if (Layouts.THREAD.getStatus(thread) == Status.DEAD) {
if (Layouts.THREAD.getStatus(thread) == ThreadStatus.DEAD) {
throw new RaiseException(coreExceptions().threadErrorKilledThread(this));
}

@@ -506,10 +532,24 @@ public int getPriority(DynamicObject thread) {
return Layouts.THREAD.getPriority(thread);
}
}

/*
* helper methods to translate Java thread priority (1-10) to Ruby thread priority (-3 to 3)
* using a quadratic polynomial ant its inverse passing by (Ruby,Java): (-3,1), (0,5) and
* (3,10) i.e., j = r^2/18 + 3*r/2 + 5 r = 3/2*sqrt(8*j + 41) - 27/2
*/
private static int javaPriorityToRubyPriority(int javaPriority) {
double d = 1.5 * Math.sqrt(8.0 * javaPriority + 41) - 13.5;
return Math.round((float) d);
}
}

@Primitive(name = "thread_set_priority", unsafe = UnsafeGroup.THREADS)
public static abstract class ThreadSetPriorityPrimitiveNode extends PrimitiveArrayArgumentsNode {

static final int RUBY_MIN_THREAD_PRIORITY = -3;
static final int RUBY_MAX_THREAD_PRIORITY = 3;

public ThreadSetPriorityPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}
@@ -530,6 +570,12 @@ public int getPriority(DynamicObject thread, int rubyPriority) {
Layouts.THREAD.setPriority(thread, rubyPriority);
return rubyPriority;
}

private static int rubyPriorityToJavaPriority(int rubyPriority) {
double d = (rubyPriority * rubyPriority) / 18.0 + 1.5 * rubyPriority + 5;
return Math.round((float) d);
}

}

@Primitive(name = "thread_set_group")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.jruby.truffle.core.thread;

public enum ThreadStatus {
RUN,
SLEEP,
ABORTING,
DEAD;
}
Original file line number Diff line number Diff line change
@@ -17,10 +17,10 @@
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.RubyThread.Status;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.InterruptMode;
import org.jruby.truffle.core.thread.ThreadStatus;

import java.util.Collections;
import java.util.Set;
@@ -122,7 +122,7 @@ private SafepointAction step(Node currentNode, boolean isDrivingThread) {
final SafepointAction deferredAction = deferred ? action : null;

try {
if (!deferred && thread != null && Layouts.THREAD.getStatus(thread) != Status.ABORTING) {
if (!deferred && thread != null && Layouts.THREAD.getStatus(thread) != ThreadStatus.ABORTING) {
action.run(thread, currentNode);
}
} finally {
22 changes: 0 additions & 22 deletions truffle/src/main/java/org/jruby/truffle/parser/Signature.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.jruby.truffle.parser;

import org.jruby.Ruby;
import org.jruby.runtime.Arity;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.truffle.parser.ast.ArgsParseNode;
import org.jruby.truffle.parser.ast.ArgumentParseNode;
import org.jruby.truffle.parser.ast.ForParseNode;
@@ -13,7 +11,6 @@
import org.jruby.truffle.parser.ast.PreExeParseNode;
import org.jruby.truffle.parser.ast.StarParseNode;
import org.jruby.truffle.parser.ast.UnnamedRestArgParseNode;
import org.jruby.util.TypeConverter;

/**
* A representation of a Ruby method signature (argument layout, min/max, keyword layout, rest args).
@@ -264,23 +261,4 @@ public static Signature decode(long l) {
public String toString() {
return "signature(pre=" + pre + ",opt=" + opt + ",post=" + post + ",rest=" + rest + ",kwargs=" + kwargs + ",kwreq=" + requiredKwargs + ",kwrest=" + restKwargs + ")";
}

public void checkArity(Ruby runtime, IRubyObject[] args) {
if (args.length < required()) {
throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + required() + ")");
}
if (rest == Rest.NONE || rest == Rest.ANON) {
// no rest, so we have a maximum
if (args.length > required() + opt()) {
if (hasKwargs() && !TypeConverter.checkHashType(runtime, args[args.length - 1]).isNil()) {
// we have kwargs and a potential kwargs hash, check with length - 1
if (args.length - 1 > required() + opt()) {
throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + (required() + opt) + ")");
}
} else {
throw runtime.newArgumentError("wrong number of arguments (" + args.length + " for " + (required() + opt) + ")");
}
}
}
}
}
2 changes: 1 addition & 1 deletion truffle/src/main/ruby/core/file.rb
Original file line number Diff line number Diff line change
@@ -828,7 +828,7 @@ def self.join(*args)
end

if value.prefix? sep
ret.gsub!(/#{SEPARATOR}+$/, '')
ret.gsub!(/#{SEPARATOR}+$/o, '')
elsif not ret.suffix? sep
ret << sep
end