-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
1 parent
3792e0f
commit 73aa13c
Showing
9 changed files
with
132 additions
and
4 deletions.
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
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
48 changes: 48 additions & 0 deletions
48
core/src/main/java/org/jruby/truffle/nodes/globals/GetFromThreadLocalNode.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
|
||
} |
51 changes: 51 additions & 0 deletions
51
core/src/main/java/org/jruby/truffle/nodes/globals/WrapInThreadLocalNode.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
This file was deleted.
Oops, something went wrong.
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.