Permalink
2 comments
on commit
sign in to comment.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[Truffle] Make $_ thread local - at the same time as being frame local.
We achieve this by wrapping the value in a ThreadLocal when it's assigned, giving it the default value nil for other threads.
- Loading branch information
Showing
with
132 additions
and 4 deletions.
- +5 −0 core/src/main/java/org/jruby/truffle/nodes/RubyNode.java
- +1 −0 core/src/main/java/org/jruby/truffle/nodes/RubyTypes.java
- +16 −1 core/src/main/java/org/jruby/truffle/nodes/core/BindingNodes.java
- +2 −1 core/src/main/java/org/jruby/truffle/nodes/core/KernelNodes.java
- +48 −0 core/src/main/java/org/jruby/truffle/nodes/globals/GetFromThreadLocalNode.java
- +51 −0 core/src/main/java/org/jruby/truffle/nodes/globals/WrapInThreadLocalNode.java
- +2 −0 core/src/main/java/org/jruby/truffle/nodes/globals/WriteReadOnlyGlobalNode.java
- +7 −1 core/src/main/java/org/jruby/truffle/translator/BodyTranslator.java
- +0 −1 spec/truffle/tags/language/predefined_tags.txt
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -56,6 +56,7 @@ | ||
RubyTime.class, // | ||
RubyEncodingConverter.class, // | ||
RubyBasicObject.class, // | ||
ThreadLocal.class, // | ||
Object[].class}) | ||
|
||
public class RubyTypes { | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,48 @@ | ||
package org.jruby.truffle.nodes.globals; | ||
|
||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.source.SourceSection; | ||
import org.jruby.truffle.nodes.RubyNode; | ||
import org.jruby.truffle.runtime.RubyContext; | ||
|
||
/** | ||
* If a child node produces a {@link ThreadLocal}, get the value from it. If the value is not a {@code ThreadLocal}, | ||
* return it unmodified. | ||
* | ||
* This is used in combination with nodes that read and writes from storage locations such as frames to make them | ||
* thread-local. | ||
* | ||
* Also see {@link WrapInThreadLocalNode}. | ||
*/ | ||
@NodeChild(value = "value", type = RubyNode.class) | ||
public abstract class GetFromThreadLocalNode extends RubyNode { | ||
|
||
public GetFromThreadLocalNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public GetFromThreadLocalNode(GetFromThreadLocalNode prev) { | ||
super(prev); | ||
} | ||
|
||
@Specialization | ||
public Object get(ThreadLocal threadLocal) { | ||
return threadLocal.get(); | ||
} | ||
|
||
@Specialization(guards = "!isThreadLocal") | ||
public Object get(Object value) { | ||
return value; | ||
} | ||
|
||
public static Object get(RubyContext context, Object value) { | ||
if (value instanceof ThreadLocal) { | ||
return ((ThreadLocal) value).get(); | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,51 @@ | ||
package org.jruby.truffle.nodes.globals; | ||
|
||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.source.SourceSection; | ||
import org.jruby.truffle.nodes.RubyNode; | ||
import org.jruby.truffle.runtime.RubyContext; | ||
|
||
/** | ||
* Wrap a child value in a new {@link ThreadLocal} so that a value can be stored in a location such as a frame without | ||
* making that value visible to other threads. | ||
* | ||
* This is used in combination with nodes that read and writes from storage locations such as frames to make them | ||
* thread-local. | ||
* | ||
* Also see {@link GetFromThreadLocalNode}. | ||
*/ | ||
@NodeChild(value = "value", type = RubyNode.class) | ||
public abstract class WrapInThreadLocalNode extends RubyNode { | ||
|
||
public WrapInThreadLocalNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public WrapInThreadLocalNode(WrapInThreadLocalNode prev) { | ||
super(prev); | ||
} | ||
|
||
@Specialization | ||
public ThreadLocal wrap(Object value) { | ||
return wrap(getContext(), value); | ||
} | ||
|
||
public static ThreadLocal wrap(RubyContext context, Object value) { | ||
final RubyContext finalContext = context; | ||
|
||
final ThreadLocal threadLocal = new ThreadLocal() { | ||
|
||
@Override | ||
protected Object initialValue() { | ||
return finalContext.getCoreLibrary().getNilObject(); | ||
} | ||
|
||
}; | ||
|
||
threadLocal.set(value); | ||
|
||
return threadLocal; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73aa13c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought Truffle 0.6 was going to give us the guard methods for free for declared types. Did that not land?
73aa13c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No - we've got a bug open for it, but it didn't happen yet.