Skip to content

Commit

Permalink
Cache Fiber's last value.
Browse files Browse the repository at this point in the history
In the Rubinius implementation, every Fiber is actually a separate
pthread that is coordinated by the owning Thread object. Passing values
from and to the Fiber is mediated by the Thread.

Behind both a Thread and a Fiber is a data structure for various
information that supports the functioning of the Thread or Fiber. When
the underlying pthread function terminates, it sets sets that structure
to a "zombie" status so that its resources can eventually be released.
In doing so, it removes the reference to the Thread object associated
with the Fiber, so we need to cache the last value in the Fiber itself.
  • Loading branch information
brixen committed Mar 17, 2018
1 parent d08bd4e commit b83c673
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion machine/class/fiber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace rubinius {

Object* Fiber::return_value(STATE) {
if(vm()->thread()->nil_p()) {
return cNil;
return value();
} else if(vm()->thread_state()->raise_reason() == cNone) {
return state->vm()->thread()->fiber_value();
} else if(canceled_p()) {
Expand Down Expand Up @@ -196,8 +196,10 @@ namespace rubinius {
vm->set_call_frame(nullptr);

if(value) {
vm->fiber()->value(state, value);
vm->thread()->fiber_value(state, value);
} else {
vm->fiber()->value(state, value);
vm->thread()->fiber_value(state, cNil);
}

Expand Down
4 changes: 3 additions & 1 deletion machine/class/fiber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace rubinius {
attr_accessor(fiber_id, Fixnum);
attr_accessor(source, String);
attr_accessor(thread, Thread);
attr_accessor(value, Object);

private:
attr_field(start_time, uint64_t);
Expand All @@ -65,8 +66,9 @@ namespace rubinius {
obj->fiber_id(Fixnum::from(++Fiber::fiber_ids_));
obj->source(nil<String>());
obj->thread(nil<Thread>());
obj->value(cNil);
obj->start_time(get_current_time());
obj->vm(NULL);
obj->vm(nullptr);
obj->invoke_context(state->vm());
obj->restart_context(state->vm());
obj->status(eCreated);
Expand Down

0 comments on commit b83c673

Please sign in to comment.