Skip to content

Commit 28e735e

Browse files
matzcremno
authored andcommitted
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
1 parent 7962e8d commit 28e735e

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

include/mruby.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id);
374374
mrb_bool mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid);
375375
mrb_bool mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c);
376376

377+
/* fiber functions (you need to link mruby-fiber mrbgem to use) */
378+
mrb_value mrb_fiber_yield(mrb_state *mrb, int argc, mrb_value *argv);
379+
377380
/* memory pool implementation */
378381
typedef struct mrb_pool mrb_pool;
379382
struct mrb_pool* mrb_pool_open(mrb_state*);

mrbgems/mruby-fiber/src/fiber.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,11 @@ fiber_alive_p(mrb_state *mrb, mrb_value self)
205205
return mrb_bool_value(c->status != MRB_FIBER_TERMINATED);
206206
}
207207

208-
/*
209-
* call-seq:
210-
* Fiber.yield(args, ...) -> obj
211-
*
212-
* Yields control back to the context that resumed the fiber, passing
213-
* along any arguments that were passed to it. The fiber will resume
214-
* processing at this point when <code>resume</code> is called next.
215-
* Any arguments passed to the next <code>resume</code> will be the
216-
* value that this <code>Fiber.yield</code> expression evaluates to.
217-
*/
218-
static mrb_value
219-
fiber_yield(mrb_state *mrb, mrb_value self)
208+
mrb_value
209+
mrb_fiber_yield(mrb_state *mrb, int len, mrb_value *a)
220210
{
221211
struct mrb_context *c = mrb->c;
222212
mrb_callinfo *ci;
223-
mrb_value *a;
224-
int len;
225213

226214
for (ci = c->ci; ci >= c->cibase; ci--) {
227215
if (ci->acc < 0) {
@@ -231,14 +219,34 @@ fiber_yield(mrb_state *mrb, mrb_value self)
231219
if (!c->prev) {
232220
mrb_raise(mrb, E_ARGUMENT_ERROR, "can't yield from root fiber");
233221
}
234-
mrb_get_args(mrb, "*", &a, &len);
222+
235223
c->prev->status = MRB_FIBER_RUNNING;
236224
mrb->c = c->prev;
237225
c->prev = NULL;
238226
MARK_CONTEXT_MODIFY(mrb->c);
239227
return fiber_result(mrb, a, len);
240228
}
241229

230+
/*
231+
* call-seq:
232+
* Fiber.yield(args, ...) -> obj
233+
*
234+
* Yields control back to the context that resumed the fiber, passing
235+
* along any arguments that were passed to it. The fiber will resume
236+
* processing at this point when <code>resume</code> is called next.
237+
* Any arguments passed to the next <code>resume</code> will be the
238+
* value that this <code>Fiber.yield</code> expression evaluates to.
239+
*/
240+
static mrb_value
241+
fiber_yield(mrb_state *mrb, mrb_value self)
242+
{
243+
mrb_value *a;
244+
int len;
245+
246+
mrb_get_args(mrb, "*", &a, &len);
247+
return mrb_fiber_yield(mrb, len, a);
248+
}
249+
242250
/*
243251
* call-seq:
244252
* Fiber.current() -> fiber

0 commit comments

Comments
 (0)