Skip to content

Commit

Permalink
[Truffle] Add the GC module.
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Oct 13, 2014
1 parent a667249 commit f444f8c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 19 deletions.
Expand Up @@ -72,6 +72,7 @@ public static List<MethodDetails> getMethods() {
getMethods(methods, FixnumNodesFactory.getFactories());
getMethods(methods, FloatNodesFactory.getFactories());
getMethods(methods, HashNodesFactory.getFactories());
getMethods(methods, GCNodesFactory.getFactories());
getMethods(methods, IONodesFactory.getFactories());
getMethods(methods, KernelNodesFactory.getFactories());
getMethods(methods, MainNodesFactory.getFactories());
Expand Down
83 changes: 83 additions & 0 deletions core/src/main/java/org/jruby/truffle/nodes/core/GCNodes.java
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.nodes.core;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.runtime.ModuleOperations;
import org.jruby.truffle.runtime.NilPlaceholder;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.UndefinedPlaceholder;
import org.jruby.truffle.runtime.core.RubyBasicObject;
import org.jruby.truffle.runtime.core.RubyClass;
import org.jruby.truffle.runtime.core.RubyProc;

import java.math.BigInteger;

@CoreClass(name = "GC")
public abstract class GCNodes {

@CoreMethod(names = "start", onSingleton = true, maxArgs = 0)
public abstract static class StartNode extends CoreMethodNode {

@Child protected GarbageCollectNode gcNode;

public StartNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
gcNode = GCNodesFactory.GarbageCollectNodeFactory.create(context, sourceSection, null);
}

public StartNode(StartNode prev) {
super(prev);
this.gcNode = prev.gcNode;
}

@Specialization
public NilPlaceholder start() {
return gcNode.executeGC();
}
}

@CoreMethod(names = "garbage_collect", needsSelf = false, maxArgs = 0)
public abstract static class GarbageCollectNode extends CoreMethodNode {

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

public GarbageCollectNode(GarbageCollectNode prev) {
super(prev);
}

public abstract NilPlaceholder executeGC();

@Specialization
public NilPlaceholder garbageCollect() {
return doGC();
}

@CompilerDirectives.SlowPath
private NilPlaceholder doGC() {
notDesignedForCompilation();

getContext().outsideGlobalLock(new Runnable() {
@Override
public void run() {
System.gc();
}
});

return NilPlaceholder.INSTANCE;
}
}

}
Expand Up @@ -118,33 +118,21 @@ public RubyProc defineFinalizer(Object object, RubyProc finalizer) {
@CoreMethod(names = "garbage_collect", isModuleFunction = true, maxArgs = 0)
public abstract static class GarbageCollectNode extends CoreMethodNode {

@Child protected GCNodes.GarbageCollectNode gcNode;

public GarbageCollectNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
gcNode = GCNodesFactory.GarbageCollectNodeFactory.create(context, sourceSection, null);
}

public GarbageCollectNode(GarbageCollectNode prev) {
super(prev);
this.gcNode = prev.gcNode;
}

@Specialization
public NilPlaceholder garbageCollect() {
return doGC();
}

@CompilerDirectives.SlowPath
private NilPlaceholder doGC() {
notDesignedForCompilation();

getContext().outsideGlobalLock(new Runnable() {

@Override
public void run() {
System.gc();
}

});

return NilPlaceholder.INSTANCE;
return gcNode.executeGC();
}
}

Expand Down
Expand Up @@ -89,6 +89,7 @@ public class CoreLibrary {
@CompilerDirectives.CompilationFinal private RubyModule configModule;
@CompilerDirectives.CompilationFinal private RubyModule enumerableModule;
@CompilerDirectives.CompilationFinal private RubyModule errnoModule;
@CompilerDirectives.CompilationFinal private RubyModule gcModule;
@CompilerDirectives.CompilationFinal private RubyModule kernelModule;
@CompilerDirectives.CompilationFinal private RubyModule mathModule;
@CompilerDirectives.CompilationFinal private RubyModule objectSpaceModule;
Expand Down Expand Up @@ -153,6 +154,7 @@ public void initialize() {
fileClass = new RubyClass(null, null, ioClass, "File");
fixnumClass = new RubyClass(null, null, integerClass, "Fixnum");
floatClass = new RubyClass(null, null, numericClass, "Float");
gcModule = new RubyModule(moduleClass, null, "GC");
hashClass = new RubyHash.RubyHashClass(objectClass);
kernelModule = new RubyModule(moduleClass, null, "Kernel");
loadErrorClass = new RubyException.RubyExceptionClass(standardErrorClass, "LoadError");
Expand Down Expand Up @@ -256,6 +258,7 @@ public void initialize() {
fileClass, //
fixnumClass, //
floatClass, //
gcModule, //
hashClass, //
integerClass, //
ioClass, //
Expand Down
1 change: 0 additions & 1 deletion spec/truffle/tags/core/gc/garbage_collect_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/truffle/tags/core/gc/start_tags.txt

This file was deleted.

1 change: 1 addition & 0 deletions spec/truffle/truffle.mspec
Expand Up @@ -20,6 +20,7 @@ class MSpecScript
"spec/ruby/core/file",
"spec/ruby/core/fixnum",
"spec/ruby/core/float",
"spec/ruby/core/gc",
"spec/ruby/core/hash",
"spec/ruby/core/io",
"spec/ruby/core/kernel",
Expand Down

0 comments on commit f444f8c

Please sign in to comment.