Skip to content

Commit

Permalink
Showing 1 changed file with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -9,20 +9,43 @@
*/
package org.jruby.truffle.language.objects;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.profiles.ConditionProfile;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.Layouts;
import org.jruby.truffle.language.RubyNode;

public class ThreadLocalObjectNode extends RubyNode {

@CompilationFinal private DynamicObject firstThreadSeen;
@CompilationFinal private DynamicObject firstThreadSeenLocals;
private final ConditionProfile firstThreadProfile = ConditionProfile.createCountingProfile();

public ThreadLocalObjectNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Override
public Object execute(VirtualFrame frame) {
return Layouts.THREAD.getThreadLocals(getContext().getThreadManager().getCurrentThread());
if (firstThreadSeen == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
firstThreadSeen = currentThread();
firstThreadSeenLocals = Layouts.THREAD.getThreadLocals(firstThreadSeen);
}

if (firstThreadProfile.profile(currentThread() == firstThreadSeen)) {
return firstThreadSeenLocals;
} else {
return Layouts.THREAD.getThreadLocals(currentThread());
}
}

private DynamicObject currentThread() {
return getContext().getThreadManager().getCurrentThread();
}

}

2 comments on commit d349235

@eregon
Copy link
Member

@eregon eregon commented on d349235 Feb 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have a global Assumption only one Thread yet to make it a bit nicer.
But OTOH this also specializes on a given thread so that's more powerful (would be nice to use @Cached here).

Sorry, something went wrong.

@chrisseaton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want a global assumption - much better to let code paths that happen to just see one thread, no matter how many there are, specialise on their own. I've switched to using the DSL here. It's a pity I can't see a way to hoist the thread guard to be once per method. This probably has extremely limited practical benefit anyway.

Sorry, something went wrong.

Please sign in to comment.