Fiber* Fiber::current(STATE) { #ifdef RBX_FIBER_ENABLED Fiber* fib = state->vm()->current_fiber.get(); // Lazily allocate a root fiber. if(fib->nil_p()) { fib = state->memory()->new_object<Fiber>(state, G(fiber)); fib->root(true); fib->status(Fiber::eRunning); fib->data(state->vm()->new_fiber_data(true, fib->stack_size()->to_native())); fib->data()->set_call_frame(state->vm()->call_frame()); state->memory()->native_finalizer(state, fib, (memory::FinalizerFunction)&Fiber::finalize); state->vm()->current_fiber.set(fib); state->vm()->root_fiber.set(fib); } return fib; #else Exception::raise_not_implemented_error(state, "Fibers not supported on this platform"); #endif }
Fiber* Fiber::create(STATE, Object* self, Object* stack_size, Object* block) { Fiber* fiber = state->memory()->new_object_pinned<Fiber>(state, as<Class>(self)); fiber->block(state, block); std::ostringstream name; name << "fiber." << fiber->fiber_id()->to_native(); { BlockPhase blocking(state); fiber->vm(state->vm()->thread_nexus()->new_vm(&state->shared(), name.str().c_str())); } fiber->vm()->set_kind(memory::ManagedThread::eFiber); fiber->vm()->set_suspending(); if(Fixnum* size = try_as<Fixnum>(stack_size)) { fiber->vm()->validate_stack_size(state, size->to_native()); fiber->stack_size(state, size); } fiber->vm()->set_thread(state->vm()->thread()); fiber->vm()->set_fiber(fiber); state->memory()->native_finalizer(state, fiber, (memory::FinalizerFunction)&Fiber::finalize); if(state->shared().config.machine_fiber_log_lifetime.value) { const std::regex& filter = state->shared().config.machine_fiber_log_filter(); if(CallFrame* call_frame = state->vm()->get_filtered_frame(state, filter)) { std::ostringstream source; source << call_frame->file(state)->cpp_str(state).c_str() << ":" << call_frame->line(state); logger::write("fiber: new: %s, %d, %s", fiber->thread_name()->c_str(state), fiber->fiber_id()->to_native(), source.str().c_str()); fiber->source(state, String::create(state, source.str().c_str())); } else { logger::write("fiber: new: %s, %d", fiber->thread_name()->c_str(state), fiber->fiber_id()->to_native()); } } state->vm()->metrics().system.fibers_created++; return fiber; }
/* This creates the pseudo-Fiber for the Thread. This provides a uniform * means of expressing things like Fiber.current. */ Fiber* Fiber::create(STATE, VM* vm) { Fiber* fiber = state->memory()->new_object_pinned<Fiber>(state, G(fiber)); vm->set_fiber(fiber); vm->set_running(); fiber->vm(vm); fiber->pid(vm->thread()->pid()); fiber->stack_size(vm->thread()->stack_size()); fiber->thread_name(state, String::create(state, vm->name().c_str())); fiber->fiber_id(Fixnum::from(0)); fiber->status(eRunning); fiber->invoke_context(vm); return fiber; }
Fiber* Fiber::create(STATE, Object* self, Object* stack_size, Object* callable) { #ifdef RBX_FIBER_ENABLED Fiber* fib = state->memory()->new_object<Fiber>(state, as<Class>(self)); fib->starter(state, callable); if(Fixnum* size = try_as<Fixnum>(stack_size)) { state->vm()->validate_stack_size(state, size->to_native()); fib->stack_size(state, size); } state->vm()->metrics().system.fibers_created++; state->memory()->native_finalizer(state, fib, (memory::FinalizerFunction)&Fiber::finalize); return fib; #else Exception::raise_not_implemented_error(state, "Fibers not supported on this platform"); #endif }