Skip to content

Commit

Permalink
Split most remaining varargs methods in Thread
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 4, 2024
1 parent 526d764 commit d4e103f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 81 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5463,7 +5463,7 @@ public void run() {
} finally {
cleanupPOpen(tuple);

IRubyObject status = waitThread[0].join(IRubyObject.NULL_ARRAY);
IRubyObject status = waitThread[0].join(context);
context.setLastExitStatus(status);
}
}
Expand Down
156 changes: 80 additions & 76 deletions core/src/main/java/org/jruby/RubyThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,11 +605,6 @@ public static RubyThread start(IRubyObject recv, IRubyObject[] args, Block block
return startThread(recv, args, false, block);
}

@Deprecated
public static RubyThread start19(IRubyObject recv, IRubyObject[] args, Block block) {
return start(recv, args, block);
}

public static RubyThread adopt(IRubyObject recv, Thread t) {
final Ruby runtime = recv.getRuntime();
return adoptThread(runtime, runtime.getThreadService(), (RubyClass) recv, t);
Expand Down Expand Up @@ -879,26 +874,32 @@ private static int checkInterruptMask(final ThreadContext context, final IRubyOb
throw context.runtime.newArgumentError("unknown mask signature");
}

@JRubyMethod(name = "pending_interrupt?", meta = true)
public static IRubyObject pending_interrupt_p_s(ThreadContext context, IRubyObject self) {
return context.getThread().pending_interrupt_p(context);
}

@JRubyMethod(name = "pending_interrupt?", meta = true, optional = 1, checkArity = false)
public static IRubyObject pending_interrupt_p(ThreadContext context, IRubyObject self, IRubyObject[] args) {
return context.getThread().pending_interrupt_p(context, args);
public static IRubyObject pending_interrupt_p_s(ThreadContext context, IRubyObject self, IRubyObject err) {
return context.getThread().pending_interrupt_p(context, err);
}

@JRubyMethod(name = "pending_interrupt?", optional = 1, checkArity = false)
public IRubyObject pending_interrupt_p(ThreadContext context, IRubyObject[] args) {
int argc = Arity.checkArgumentCount(context, args, 0, 1);
@JRubyMethod(name = "pending_interrupt?")
public IRubyObject pending_interrupt_p(ThreadContext context) {
return RubyBoolean.newBoolean(context, pendingInterruptQueue.isEmpty());
}

@JRubyMethod(name = "pending_interrupt?")
public IRubyObject pending_interrupt_p(ThreadContext context, IRubyObject err) {
if (pendingInterruptQueue.isEmpty()) {
return context.fals;
}
if (argc == 1) {
IRubyObject err = args[0];
if (!(err instanceof RubyModule)) {
throw context.runtime.newTypeError("class or module required for rescue clause");
}
return pendingInterruptInclude((RubyModule) err) ? context.tru : context.fals;

if (!(err instanceof RubyModule)) {
throw context.runtime.newTypeError("class or module required for rescue clause");
}
return context.tru;

return pendingInterruptInclude((RubyModule) err) ? context.tru : context.fals;
}

private boolean pendingInterruptInclude(RubyModule err) {
Expand Down Expand Up @@ -1082,11 +1083,6 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject key) {
}
}

@Deprecated
public IRubyObject op_aref(IRubyObject key) {
return op_aref(getRuntime().getCurrentContext(), key);
}

@JRubyMethod(name = "[]=", required = 2)
public IRubyObject op_aset(IRubyObject key, IRubyObject value) {
checkFrozen();
Expand All @@ -1099,7 +1095,7 @@ public IRubyObject op_aset(IRubyObject key, IRubyObject value) {
return value;
}

@JRubyMethod(name = "key?", required = 1)
@JRubyMethod(name = "key?")
public RubyBoolean key_p(ThreadContext context, IRubyObject key) {
key = getSymbolKey(key);
final Map<IRubyObject, IRubyObject> locals = getFiberLocals();
Expand All @@ -1108,11 +1104,6 @@ public RubyBoolean key_p(ThreadContext context, IRubyObject key) {
}
}

@Deprecated
public RubyBoolean key_p(IRubyObject key) {
return key_p(getRuntime().getCurrentContext(), key);
}

@JRubyMethod
public RubyArray keys() {
final Map<IRubyObject, IRubyObject> locals = getFiberLocals();
Expand All @@ -1127,7 +1118,7 @@ public RubyArray keys() {
return RubyArray.newArrayMayCopy(getRuntime(), ary);
}

@JRubyMethod(name = "thread_variable?", required = 1)
@JRubyMethod(name = "thread_variable?")
public IRubyObject thread_variable_p(ThreadContext context, IRubyObject key) {
key = getSymbolKey(key);
final Map<IRubyObject, IRubyObject> locals = getThreadLocals();
Expand All @@ -1136,7 +1127,7 @@ public IRubyObject thread_variable_p(ThreadContext context, IRubyObject key) {
}
}

@JRubyMethod(name = "thread_variable_get", required = 1)
@JRubyMethod(name = "thread_variable_get")
public IRubyObject thread_variable_get(ThreadContext context, IRubyObject key) {
key = getSymbolKey(key);
final Map<IRubyObject, IRubyObject> locals = getThreadLocals();
Expand Down Expand Up @@ -1201,56 +1192,35 @@ public static RubyBoolean abort_on_exception(ThreadContext context, IRubyObject
return context.runtime.isAbortOnException() ? context.tru : context.fals;
}

@JRubyMethod(name = "abort_on_exception=", required = 1, meta = true)
@JRubyMethod(name = "abort_on_exception=", meta = true)
public static IRubyObject abort_on_exception_set(ThreadContext context, IRubyObject recv, IRubyObject value) {
context.runtime.setAbortOnException(value.isTrue());
return value;
}

@Deprecated
public RubyBoolean abort_on_exception() {
return abortOnException ? getRuntime().getTrue() : getRuntime().getFalse();
}

@Deprecated
public static RubyBoolean abort_on_exception_x(IRubyObject recv) {
Ruby runtime = recv.getRuntime();
return runtime.isAbortOnException() ? runtime.getTrue() : runtime.getFalse();
}

@Deprecated
public static IRubyObject abort_on_exception_set_x(IRubyObject recv, IRubyObject value) {
recv.getRuntime().setGlobalAbortOnExceptionEnabled(value.isTrue());
return value;
}

@JRubyMethod(name = "alive?")
public RubyBoolean alive_p() {
return isAlive() ? getRuntime().getTrue() : getRuntime().getFalse();
public RubyBoolean alive_p(ThreadContext context) {
return RubyBoolean.newBoolean(context, isAlive());
}

@Deprecated
public IRubyObject join(IRubyObject[] args) {
return join(getRuntime().getCurrentContext(), args);
}
@JRubyMethod
public IRubyObject join(ThreadContext context) {
long timeoutMillis = Long.MAX_VALUE;

@JRubyMethod(optional = 1, checkArity = false)
public IRubyObject join(ThreadContext context, IRubyObject[] args) {
int argc = Arity.checkArgumentCount(context, args, 0, 1);
return joinCommon(context, timeoutMillis);
}

Ruby runtime = context.runtime;
@JRubyMethod
public IRubyObject join(ThreadContext context, IRubyObject timeout) {
long timeoutMillis = Long.MAX_VALUE;

if (argc > 0 && !args[0].isNil()) {
if (argc > 1) {
throw runtime.newArgumentError(argc, 1);
}
if (!timeout.isNil()) {
// MRI behavior: value given in seconds; converted to Float; less
// than or equal to zero returns immediately; returns nil
timeoutMillis = (long) (1000 * RubyNumeric.num2dbl(args[0]));
timeoutMillis = (long) (1000 * RubyNumeric.num2dbl(timeout));
if (timeoutMillis <= 0) {
// TODO: not sure that we should skip calling join() altogether.
// Thread.join() has some implications for Java Memory Model, etc.
// TODO: not sure that we should skip calling join() altogether.
// Thread.join() has some implications for Java Memory Model, etc.
if (threadImpl.isAlive()) {
return context.nil;
} else {
Expand All @@ -1259,6 +1229,11 @@ public IRubyObject join(ThreadContext context, IRubyObject[] args) {
}
}

return joinCommon(context, timeoutMillis);
}

private IRubyObject joinCommon(ThreadContext context, long timeoutMillis) {
Ruby runtime = context.runtime;
if (isCurrent()) {
throw runtime.newThreadError("Target thread must not be current thread");
}
Expand Down Expand Up @@ -1316,17 +1291,12 @@ public IRubyObject join(ThreadContext context, IRubyObject[] args) {

@JRubyMethod
public IRubyObject value(ThreadContext context) {
join(context, NULL_ARRAY);
join(context);
synchronized (this) {
return finalResult;
}
}

@Deprecated
public IRubyObject value() {
return value(getRuntime().getCurrentContext());
}

@JRubyMethod
public IRubyObject group() {
final RubyThreadGroup group = this.threadGroup;
Expand Down Expand Up @@ -1504,11 +1474,6 @@ public IRubyObject raise(ThreadContext context, IRubyObject[] args, Block block)
return genericRaise(context, context.getThread(), args);
}

@Deprecated
public IRubyObject raise(IRubyObject[] args, Block block) {
return raise(getRuntime().getCurrentContext(), args, block);
}

@JRubyMethod
public IRubyObject native_thread_id(ThreadContext context) {
if (!isAlive()) return context.nil;
Expand Down Expand Up @@ -2643,4 +2608,43 @@ public void decrementBlocking() {
public boolean isBlocking() {
return blockingCount > 0;
}

@Deprecated
public IRubyObject pending_interrupt_p(ThreadContext context, IRubyObject[] args) {
switch (args.length) {
case 0:
return pending_interrupt_p(context);
case 1:
return pending_interrupt_p(context, args[0]);
default:
throw context.runtime.newArgumentError(args.length, 0, 1);
}
}

@Deprecated
public static IRubyObject pending_interrupt_p(ThreadContext context, IRubyObject self, IRubyObject[] args) {
return context.getThread().pending_interrupt_p(context, args);
}

@Deprecated
public RubyBoolean alive_p() {
return isAlive() ? getRuntime().getTrue() : getRuntime().getFalse();
}

@Deprecated
public IRubyObject value() {
return value(getRuntime().getCurrentContext());
}

@Deprecated
public IRubyObject join(ThreadContext context, IRubyObject[] args) {
switch (args.length) {
case 0:
return join(context);
case 1:
return join(context, args[0]);
default:
throw context.runtime.newArgumentError(args.length, 0, 1);
}
}
}
14 changes: 11 additions & 3 deletions core/src/main/java/org/jruby/RubyThreadGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import org.jruby.runtime.Block;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.collections.WeakHashSet;

Expand Down Expand Up @@ -66,8 +67,8 @@ public static RubyClass createThreadGroupClass(Ruby runtime) {
return threadGroupClass;
}

@JRubyMethod(name = "add", required = 1)
public IRubyObject add(IRubyObject rubyThread, Block block) {
@JRubyMethod(name = "add")
public IRubyObject add(ThreadContext context, IRubyObject rubyThread, Block block) {
if (!(rubyThread instanceof RubyThread)) throw getRuntime().newTypeError(rubyThread, getRuntime().getThread());

// synchronize on the RubyThread for threadgroup updates
Expand Down Expand Up @@ -95,7 +96,7 @@ public IRubyObject add(IRubyObject rubyThread, Block block) {
}

// we only add live threads
if (thread.alive_p().isTrue()) {
if (thread.alive_p(context).isTrue()) {
addDirectly(thread);
}

Expand Down Expand Up @@ -161,4 +162,11 @@ private RubyThreadGroup(Ruby runtime, RubyClass type) {
super(runtime, type);
}

@Deprecated
public IRubyObject add(IRubyObject rubyThread, Block block) {
if (!(rubyThread instanceof RubyThread)) throw getRuntime().newTypeError(rubyThread, getRuntime().getThread());

return add(((RubyThread) rubyThread).getContext(), rubyThread, block);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void teardown() {

try {
rth.kill();
rth.join(mainContext, IRubyObject.NULL_ARRAY);
rth.join(mainContext);
} catch (RaiseException re) {
// ignore Ruby exceptions raised out of join
}
Expand Down

0 comments on commit d4e103f

Please sign in to comment.