Skip to content

Commit

Permalink
Retype these cache fields to provide better type information.
Browse files Browse the repository at this point in the history
I do not know if a JVM can do this, but knowing that the fields
on ThreadContext are final, and you're always accessing e.g.
context.tru, and context.tru is always truthy, there may be cases
that can fold away. Maybe. In any case, this found a bug in
RubyGlobal where an object that could never be nil was being
compared to nil.
  • Loading branch information
headius committed Mar 1, 2018
1 parent 9b50958 commit 75ac955
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
10 changes: 3 additions & 7 deletions core/src/main/java/org/jruby/RubyGlobal.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,13 +511,9 @@ private IRubyObject case_aware_op_aset(ThreadContext context, IRubyObject key, f
if (updateRealENV) {
final POSIX posix = context.runtime.getPosix();
final String keyAsJava = keyAsStr.asJavaString();
// libc (un)setenv is not reentrant, so we need to synchronize across the entire JVM (JRUBY-5933)
if (valueAsStr == context.nil) {
synchronized (Object.class) { posix.unsetenv(keyAsJava); }
} else {
final String valueAsJava = valueAsStr.asJavaString();
synchronized (Object.class) { posix.setenv(keyAsJava, valueAsJava, 1); }
}
final String valueAsJava = valueAsStr.asJavaString();
// libc setenv is not reentrant, so we need to synchronize across the entire JVM (JRUBY-5933)
synchronized (ProcessBuilder.class) { posix.setenv(keyAsJava, valueAsJava, 1); }
}

return super.op_aset(context, keyAsStr, valueAsStr);
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/org/jruby/runtime/ThreadContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.jruby.RubyContinuation.Continuation;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.RubyNil;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.RubyThread;
Expand Down Expand Up @@ -92,9 +93,9 @@ public static ThreadContext newContext(Ruby runtime) {

// runtime, nil, and runtimeCache cached here for speed of access from any thread
public final Ruby runtime;
public final IRubyObject nil;
public final RubyBoolean tru;
public final RubyBoolean fals;
public final RubyNil nil;
public final RubyBoolean.True tru;
public final RubyBoolean.False fals;
public final RuntimeCache runtimeCache;

// Is this thread currently with in a function trace?
Expand Down Expand Up @@ -194,9 +195,9 @@ public SecureRandom getSecureRandom() {
*/
private ThreadContext(Ruby runtime) {
this.runtime = runtime;
this.nil = runtime.getNil();
this.tru = runtime.getTrue();
this.fals = runtime.getFalse();
this.nil = (RubyNil) runtime.getNil();
this.tru = (RubyBoolean.True) runtime.getTrue();
this.fals = (RubyBoolean.False) runtime.getFalse();
this.currentBlockType = Block.Type.NORMAL;
this.savedExcInLambda = null;

Expand Down

0 comments on commit 75ac955

Please sign in to comment.