Skip to content

Commit

Permalink
Some fixes for Fiber.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Mar 12, 2016
1 parent 2aa0283 commit 09a0ed3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
54 changes: 27 additions & 27 deletions machine/builtin/fiber.cpp
Expand Up @@ -53,21 +53,23 @@ namespace rubinius {
void Fiber::start_on_stack() {
#ifdef RBX_FIBER_ENABLED
VM* vm = VM::current();
State state(vm);
State state_obj(vm), *state = &state_obj;

Fiber* fib = Fiber::current(&state);
Fiber* fib = Fiber::current(state);

// Reset the current fiber again to reset the stack limits so
// we can properly detect stack overflows
vm->set_current_fiber(fib);

Array* result = nil<Array>();
Object* obj = fib->starter()->send(&state, NULL, state.globals().sym_call.get(), fib->value(), cNil, false);
Object* obj = fib->starter()->send(state, NULL, G(sym_call), fib->value(), cNil, false);

// GC has run! Don't use stack vars!

fib = Fiber::current(&state);
fib = Fiber::current(state);
fib->status_ = Fiber::eDead;
fib->dead_ = cTrue;
fib->set_call_frame(0);

Fiber* dest = fib->prev();

Expand All @@ -80,19 +82,20 @@ namespace rubinius {
// of returning, so we can deal with it in the same way
// as *args from #yield, #resume, and #transfer
if(obj) {
result = Array::create(&state, 1);
result->set(&state, 0, obj);
result = Array::create(state, 1);
result->set(state, 0, obj);
} else {
if(state.vm()->thread_state()->raise_reason() == cException) {
dest->exception(&state, state.vm()->thread_state()->current_exception());
if(state->vm()->thread_state()->raise_reason() == cException) {
dest->exception(state, state->vm()->thread_state()->current_exception());
}
}

dest->run();
dest->value(&state, result);
state.vm()->set_current_fiber(dest);
dest->run(state);
dest->value(state, result);

dest->data_->switch_and_orphan(state, fib->data_);

dest->data_->switch_and_orphan(&state, fib->data_);
// TODO: CallFrame: return from this function

assert(0 && "fatal start_on_stack error");
#else
Expand Down Expand Up @@ -123,15 +126,15 @@ namespace rubinius {
}

if(status_ == Fiber::eDead || data_->dead_p()) {
Exception::fiber_error(state, "dead fiber called");
Exception::raise_fiber_error(state, "dead fiber called");
}

if(!prev_->nil_p()) {
Exception::fiber_error(state, "double resume");
Exception::raise_fiber_error(state, "double resume");
}

if(data_->thread() && data_->thread() != state->vm()) {
Exception::fiber_error(state, "cross thread fiber resuming is illegal");
Exception::raise_fiber_error(state, "cross thread fiber resuming is illegal");
}

Array* val = args.as_array(state);
Expand All @@ -140,10 +143,9 @@ namespace rubinius {
Fiber* cur = Fiber::current(state);
prev(state, cur);

cur->sleep(calling_environment);
cur->sleep(state->vm()->call_frame());

run();
state->vm()->set_current_fiber(this);
run(state);

data_->switch_to(state, cur->data_);

Expand Down Expand Up @@ -181,11 +183,11 @@ namespace rubinius {
}

if(status_ == Fiber::eDead || data_->dead_p()) {
Exception::fiber_error(state, "dead fiber called");
Exception::raise_fiber_error(state, "dead fiber called");
}

if(data_->thread() && data_->thread() != state->vm()) {
Exception::fiber_error(state, "cross thread fiber resuming is illegal");
Exception::raise_fiber_error(state, "cross thread fiber resuming is illegal");
}

Array* val = args.as_array(state);
Expand All @@ -197,10 +199,9 @@ namespace rubinius {

prev(state, root);

cur->sleep(calling_environment);
cur->sleep(state->vm()->call_frame());

run();
state->vm()->set_current_fiber(this);
run(state);

data_->switch_to(state, cur->data_);

Expand Down Expand Up @@ -239,18 +240,17 @@ namespace rubinius {
assert(cur != dest_fib);

if(cur->root_) {
Exception::fiber_error(state, "can't yield from root fiber");
Exception::raise_fiber_error(state, "can't yield from root fiber");
}

cur->prev(state, nil<Fiber>());

Array* val = args.as_array(state);
dest_fib->value(state, val);

cur->sleep(calling_environment);
cur->sleep(state->vm()->call_frame());

dest_fib->run();
state->vm()->set_current_fiber(dest_fib);
dest_fib->run(state);

dest_fib->data_->switch_to(state, cur->data_);

Expand Down
19 changes: 14 additions & 5 deletions machine/builtin/fiber.hpp
Expand Up @@ -50,14 +50,23 @@ namespace rubinius {
return data_->call_frame();
}

void sleep(CallFrame* cf) {
if(cf && !data_) rubinius::bug("bad fiber");
data_->set_call_frame(cf);
void set_call_frame(CallFrame* call_frame) {
if(data_) {
data_->set_call_frame(call_frame);
}
}

void sleep(CallFrame* call_frame) {
if(call_frame && !data_) rubinius::bug("bad fiber");
data_->set_call_frame(call_frame);
status_ = eSleeping;
}

void run() {
if(data_) data_->set_call_frame(0);
void run(STATE) {
state->vm()->set_current_fiber(this);
if(data_) {
state->vm()->set_call_frame(data_->call_frame());
}
status_ = eRunning;
}

Expand Down
4 changes: 2 additions & 2 deletions machine/fiber_data.hpp
Expand Up @@ -103,8 +103,8 @@ namespace rubinius {
return call_frame_;
}

void set_call_frame(CallFrame* cf) {
call_frame_ = cf;
void set_call_frame(CallFrame* call_frame) {
call_frame_ = call_frame;
}

VM* thread() const {
Expand Down

0 comments on commit 09a0ed3

Please sign in to comment.