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: 0be4796b08f0
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 567ae1d5c9e5
Choose a head ref
  • 6 commits
  • 8 files changed
  • 1 contributor

Commits on Jul 25, 2015

  1. Copy the full SHA
    0f56704 View commit details
  2. Copy the full SHA
    8088803 View commit details
  3. Copy the full SHA
    b04d9ee View commit details
  4. Copy the full SHA
    c314aba View commit details
  5. Copy the full SHA
    5d7f254 View commit details
  6. Copy the full SHA
    567ae1d View commit details
27 changes: 10 additions & 17 deletions core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
@@ -148,8 +148,8 @@ public class RubyThread extends RubyObject implements ExecutionContext {
private final SleepTask2 sleepTask = new SleepTask2();

private static final boolean DEBUG = false;
private static final int RUBY_MIN_THREAD_PRIORITY = -3;
private static final int RUBY_MAX_THREAD_PRIORITY = 3;
public static final int RUBY_MIN_THREAD_PRIORITY = -3;
public static final int RUBY_MAX_THREAD_PRIORITY = 3;

/** Thread statuses */
public static enum Status {
@@ -1106,26 +1106,19 @@ public IRubyObject priority_set(IRubyObject priority) {
return RubyFixnum.newFixnum(getRuntime(), iPriority);
}

/* helper methods to translate java thread priority (1-10) to
* Ruby thread priority (-3 to 3) using a quadratic polynoimal ant its
* inverse
/* 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 int javaPriorityToRubyPriority(int javaPriority) {
double d; // intermediate value
d = 1.5 * Math.sqrt(8.0*javaPriority + 41) - 13.5;
public static int javaPriorityToRubyPriority(int javaPriority) {
double d = 1.5 * Math.sqrt(8.0 * javaPriority + 41) - 13.5;
return Math.round((float) d);
}

private int rubyPriorityToJavaPriority(int rubyPriority) {
double d;
if (rubyPriority < RUBY_MIN_THREAD_PRIORITY) {
rubyPriority = RUBY_MIN_THREAD_PRIORITY;
} else if (rubyPriority > RUBY_MAX_THREAD_PRIORITY) {
rubyPriority = RUBY_MAX_THREAD_PRIORITY;
}
d = Math.pow(rubyPriority, 2.0)/18.0 + 1.5 * rubyPriority + 5;

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

7 changes: 7 additions & 0 deletions spec/ruby/core/thread/priority_spec.rb
Original file line number Diff line number Diff line change
@@ -52,4 +52,11 @@
lambda{ @thread.priority = Object.new }.should raise_error(TypeError)
end
end

it "has no effect when the thread has died" do
priority = @thread.priority
@thread.join
@thread.priority = 3
@thread.priority.should == priority
end
end
5 changes: 0 additions & 5 deletions spec/truffle/tags/core/thread/priority_tags.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -269,6 +269,11 @@ public static RubyThread.ThreadFields getFields(RubyThread thread) {
return thread.fields;
}

public static RubyThread.ThreadFields getFields(RubyBasicObject thread) {
assert RubyGuards.isRubyThread(thread);
return ((RubyThread) thread).fields;
}

public enum InterruptMode {
IMMEDIATE, ON_BLOCKING, NEVER
}
Original file line number Diff line number Diff line change
@@ -9,18 +9,20 @@
*/
package org.jruby.truffle.nodes.rubinius;

import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;
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;

import org.jruby.truffle.nodes.core.ThreadNodes;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyException;
import org.jruby.truffle.runtime.core.RubyThread;
import org.jruby.truffle.runtime.subsystems.SafepointAction;

import java.util.Locale;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.source.SourceSection;

/**
* Rubinius primitives associated with the Ruby {@code Thread} class.
@@ -51,4 +53,46 @@ public void run(RubyBasicObject currentThread, Node currentNode) {

}

@RubiniusPrimitive(name = "thread_get_priority")
public static abstract class ThreadGetPriorityPrimitiveNode extends RubiniusPrimitiveNode {
public ThreadGetPriorityPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization(guards = "isRubyThread(thread)")
public int getPriority(RubyBasicObject thread) {
final Thread javaThread = ThreadNodes.getFields(thread).thread;
if (javaThread != null) {
int javaPriority = javaThread.getPriority();
return javaPriorityToRubyPriority(javaPriority);
} else {
return ThreadNodes.getFields(thread).priority;
}
}
}

@RubiniusPrimitive(name = "thread_set_priority")
public static abstract class ThreadSetPriorityPrimitiveNode extends RubiniusPrimitiveNode {
public ThreadSetPriorityPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization(guards = "isRubyThread(thread)")
public int getPriority(RubyBasicObject thread, int rubyPriority) {
if (rubyPriority < RUBY_MIN_THREAD_PRIORITY) {
rubyPriority = RUBY_MIN_THREAD_PRIORITY;
} else if (rubyPriority > RUBY_MAX_THREAD_PRIORITY) {
rubyPriority = RUBY_MAX_THREAD_PRIORITY;
}

int javaPriority = rubyPriorityToJavaPriority(rubyPriority);
final Thread javaThread = ThreadNodes.getFields(thread).thread;
if (javaThread != null) {
javaThread.setPriority(javaPriority);
ThreadNodes.getFields(thread).priority = rubyPriority;
}
return rubyPriority;
}
}

}
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ public static class ThreadFields {
public final List<Lock> ownedLocks = new ArrayList<>(); // Always accessed by the same underlying Java thread.

public boolean abortOnException = false;
public volatile int priority = 0;

public ThreadNodes.InterruptMode interruptMode = ThreadNodes.InterruptMode.IMMEDIATE;

Original file line number Diff line number Diff line change
@@ -35,13 +35,6 @@

class Thread

def raise_prim(exc)
Rubinius.primitive :thread_raise
Kernel.raise PrimitiveFailure, "Thread#raise primitive failed"
end

private :raise_prim

def raise(exc=undefined, msg=nil, trace=nil)
return self unless alive?

17 changes: 17 additions & 0 deletions truffle/src/main/ruby/core/rubinius/bootstrap/thread.rb
Original file line number Diff line number Diff line change
@@ -33,6 +33,23 @@ def self.stop
nil
end

def raise_prim(exc)
Rubinius.primitive :thread_raise
Kernel.raise PrimitiveFailure, "Thread#raise primitive failed"
end
private :raise_prim

def priority
Rubinius.primitive :thread_get_priority
Kernel.raise ThreadError, "Thread#priority primitive failed"
end

def priority=(val)
Rubinius.primitive :thread_set_priority
Kernel.raise TypeError, "priority must be a Fixnum" unless val.kind_of? Fixnum
Kernel.raise ThreadError, "Thread#priority= primitive failed"
end

def self.exit
Thread.current.kill
end