/
WrapInThreadLocalNode.java
51 lines (39 loc) · 1.46 KB
/
WrapInThreadLocalNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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;
}
}