Skip to content

Commit

Permalink
Showing 8 changed files with 60 additions and 14 deletions.
2 changes: 0 additions & 2 deletions spec/truffle/tags/core/threadgroup/add_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/truffle/tags/core/threadgroup/default_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/truffle/tags/core/threadgroup/enclose_tags.txt

This file was deleted.

2 changes: 0 additions & 2 deletions spec/truffle/tags/core/threadgroup/enclosed_tags.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -45,7 +45,8 @@ DynamicObject createThread(
@Nullable @Volatile DynamicObject exception,
@Nullable @Volatile Object value,
AtomicBoolean wakeUp,
@Volatile int priority);
@Volatile int priority,
@Nullable DynamicObject threadGroup);

boolean isThread(ObjectType objectType);
boolean isThread(DynamicObject object);
@@ -82,4 +83,7 @@ DynamicObject createThread(
int getPriority(DynamicObject object);
void setPriority(DynamicObject object, int value);

DynamicObject getThreadGroup(DynamicObject object);
void setThreadGroup(DynamicObject object, DynamicObject value);

}
Original file line number Diff line number Diff line change
@@ -69,7 +69,8 @@ public static DynamicObject createRubyThread(RubyContext context) {
null,
null,
new AtomicBoolean(false),
0);
0,
context.getCoreLibrary().getNilObject());

Layouts.THREAD.setFiberManagerUnsafe(object, new FiberManager(context, object)); // Because it is cyclic

Original file line number Diff line number Diff line change
@@ -111,6 +111,16 @@ public DynamicObject current() {

}

@CoreMethod(names = "group")
public abstract static class GroupNode extends CoreMethodArrayArgumentsNode {

@Specialization
public DynamicObject group(DynamicObject thread) {
return Layouts.THREAD.getThreadGroup(thread);
}

}

@CoreMethod(names = { "kill", "exit", "terminate" }, unsafe = UnsafeGroup.THREADS)
public abstract static class KillNode extends CoreMethodArrayArgumentsNode {

@@ -410,6 +420,7 @@ public DynamicObject allocate(
DynamicObject rubyClass,
@Cached("create()") AllocateObjectNode allocateObjectNode,
@Cached("createReadAbortOnExceptionNode()") ReadObjectFieldNode readAbortOnException ) {
final DynamicObject currentGroup = Layouts.THREAD.getThreadGroup(getContext().getThreadManager().getCurrentThread());
final DynamicObject object = allocateObjectNode.allocate(
rubyClass,
ThreadManager.createThreadLocals(getContext()),
@@ -423,7 +434,8 @@ public DynamicObject allocate(
new AtomicReference<>(null),
new AtomicReference<>(null),
new AtomicBoolean(false),
new AtomicInteger(0));
new AtomicInteger(0),
currentGroup);

Layouts.THREAD.setFiberManagerUnsafe(object, new FiberManager(getContext(), object)); // Because it is cyclic

@@ -515,6 +527,19 @@ public int getPriority(DynamicObject thread, int rubyPriority) {
}
}

@Primitive(name = "thread_set_group")
public static abstract class ThreadSetGroupPrimitiveNode extends PrimitiveArrayArgumentsNode {
public ThreadSetGroupPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization(guards = "isRubyThread(thread)")
public DynamicObject setGroup(DynamicObject thread, DynamicObject threadGroup) {
Layouts.THREAD.setThreadGroup(thread, threadGroup);
return threadGroup;
}
}

@Primitive(name = "thread_get_fiber_locals", unsafe = UnsafeGroup.THREADS)
public static abstract class ThreadGetFiberLocalsNode extends PrimitiveArrayArgumentsNode {

31 changes: 27 additions & 4 deletions truffle/src/main/ruby/core/thread.rb
Original file line number Diff line number Diff line change
@@ -352,15 +352,38 @@ def freeze
end

class ThreadGroup
def initialize
@enclosed = false
end

attr_reader :list
def enclose
@enclosed = true
end

def initialize
@list = []
def enclosed?
@enclosed
end

def add(thread)
@list.push thread
raise ThreadError, "can't move to the frozen thread group" if self.frozen?
raise ThreadError, "can't move to the enclosed thread group" if self.enclosed?

from_tg = thread.group
return nil unless from_tg
raise ThreadError, "can't move from the frozen thread group" if from_tg.frozen?
raise ThreadError, "can't move from the enclosed thread group" if from_tg.enclosed?

Truffle.invoke_primitive :thread_set_group, thread, self
self
end

def list
Thread.list.select { |th| th.group == self }
end

Default = ThreadGroup.new

end
Truffle.invoke_primitive :thread_set_group, Thread.current, ThreadGroup::Default


0 comments on commit ebb6fb7

Please sign in to comment.