Skip to content

Commit

Permalink
[Truffle] Fix OnceNode to ensure it's really executed only once.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 7, 2016
1 parent a71b014 commit 40ae3b6
Showing 1 changed file with 13 additions and 7 deletions.
Expand Up @@ -9,32 +9,38 @@
*/
package org.jruby.truffle.language.control;

import org.jruby.truffle.language.RubyNode;

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.utilities.AssumedValue;
import org.jruby.truffle.language.RubyNode;

/**
* Executes a child node just once, and uses the same value each subsequent time the node is exeuted.
* Executes a child node just once, and uses the same value each subsequent time the node is
* executed.
*/
public class OnceNode extends RubyNode {

@Child private RubyNode child;

private final AssumedValue<Object> valueMemo = new AssumedValue<>(OnceNode.class.getName(), null);
@CompilationFinal private volatile Object cachedValue;

public OnceNode(RubyNode child) {
this.child = child;
}

@Override
public Object execute(VirtualFrame frame) {
Object value = valueMemo.get();
Object value = cachedValue;

if (value == null) {
CompilerDirectives.transferToInterpreterAndInvalidate();
value = child.execute(frame);
valueMemo.set(value);
synchronized (this) {
if (cachedValue == null) {
value = cachedValue = child.execute(frame);
assert value != null;
}
}
}

return value;
Expand Down

0 comments on commit 40ae3b6

Please sign in to comment.