mrb_fiber_yield() is available now; you have to link mruby-fiber mrbgem to use the function; there's no function available to create new fiber from C (countapart of Lua's lua_newthread), but that's because you cannot create a new fiber from C due to mruby C API design limitation. define your method to create fibers in Ruby; close #1269

This commit is contained in:
Yukihiro "Matz" Matsumoto
2014-03-01 00:48:08 +09:00
parent 1f134d723c
commit 2c7204fc93
2 changed files with 26 additions and 15 deletions
+3
View File
@@ -374,6 +374,9 @@ mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id);
mrb_bool mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid);
mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c);
/* fiber functions (you need to link mruby-fiber mrbgem to use) */
mrb_value mrb_fiber_yield(mrb_state *mrb, int argc, mrb_value *argv);
/* memory pool implementation */
typedef struct mrb_pool mrb_pool;
struct mrb_pool* mrb_pool_open(mrb_state*);
+23 -15
View File
@@ -205,6 +205,28 @@ fiber_alive_p(mrb_state *mrb, mrb_value self)
return mrb_bool_value(c->status != MRB_FIBER_TERMINATED);
}
mrb_value
mrb_fiber_yield(mrb_state *mrb, int len, mrb_value *a)
{
struct mrb_context *c = mrb->c;
mrb_callinfo *ci;
for (ci = c->ci; ci >= c->cibase; ci--) {
if (ci->acc < 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "can't cross C function boundary");
}
}
if (!c->prev) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "can't yield from root fiber");
}
c->prev->status = MRB_FIBER_RUNNING;
mrb->c = c->prev;
c->prev = NULL;
MARK_CONTEXT_MODIFY(mrb->c);
return fiber_result(mrb, a, len);
}
/*
* call-seq:
* Fiber.yield(args, ...) -> obj
@@ -218,25 +240,11 @@ fiber_alive_p(mrb_state *mrb, mrb_value self)
static mrb_value
fiber_yield(mrb_state *mrb, mrb_value self)
{
struct mrb_context *c = mrb->c;
mrb_callinfo *ci;
mrb_value *a;
int len;
for (ci = c->ci; ci >= c->cibase; ci--) {
if (ci->acc < 0) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "can't cross C function boundary");
}
}
if (!c->prev) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "can't yield from root fiber");
}
mrb_get_args(mrb, "*", &a, &len);
c->prev->status = MRB_FIBER_RUNNING;
mrb->c = c->prev;
c->prev = NULL;
MARK_CONTEXT_MODIFY(mrb->c);
return fiber_result(mrb, a, len);
return mrb_fiber_yield(mrb, len, a);
}
/*