void Fiber::Info::mark(Object* obj, ObjectMark& mark) { auto_mark(obj, mark); Fiber* fib = (Fiber*)obj; if(CallFrame* cf = fib->call_frame()) { mark.gc->walk_call_frame(cf); } }
Object* Fiber::resume(STATE, Arguments& args, CallFrame* calling_environment) { #ifdef RBX_FIBER_ENABLED if(!data_) { data_ = state->vm()->new_fiber_data(); } if(status_ == Fiber::eDead || data_->dead_p()) { Exception::fiber_error(state, "dead fiber called"); } if(!prev_->nil_p()) { Exception::fiber_error(state, "double resume"); } if(data_->thread() && data_->thread() != state->vm()) { Exception::fiber_error(state, "cross thread fiber resuming is illegal"); } Array* val = args.as_array(state); value(state, val); Fiber* cur = Fiber::current(state); prev(state, cur); cur->sleep(calling_environment); run(); state->vm()->set_current_fiber(this); data_->switch_to(state, cur->data_); // Back here when someone yields back to us! // Beware here, because the GC has probably run so GC pointers on the C++ stack // can't be accessed. cur = Fiber::current(state); state->set_call_frame(cur->call_frame()); if(!cur->exception()->nil_p()) { state->raise_exception(cur->exception()); cur->exception(state, nil<Exception>()); return 0; } Array* ret = cur->value(); if(ret->nil_p()) return cNil; switch(ret->size()) { case 0: return cNil; case 1: return ret->get(state, 0); default: return ret; } #else return Primitives::failure(); #endif }
Object* Fiber::s_yield(STATE, Arguments& args, CallFrame* calling_environment) { #ifdef RBX_FIBER_ENABLED Fiber* cur = Fiber::current(state); Fiber* dest_fib = cur->prev(); assert(cur != dest_fib); if(cur->root_) { Exception::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); dest_fib->run(); state->vm()->set_current_fiber(dest_fib); dest_fib->data_->switch_to(state, cur->data_); // Back here when someone yields back to us! // Beware here, because the GC has probably run so GC pointers on the C++ stack // can't be accessed. cur = Fiber::current(state); state->set_call_frame(cur->call_frame()); Array* ret = cur->value(); if(ret->nil_p()) return cNil; switch(ret->size()) { case 0: return cNil; case 1: return ret->get(state, 0); default: return ret; } #else return Primitives::failure(); #endif }