1 /**********************************************************************
6 created at: Thu May 23 09:03:43 2007
8 Copyright (C) 2007 Koichi Sasada
10 **********************************************************************/
12 #include "ruby/ruby.h"
15 #include "eval_intern.h"
17 #define CAPTURE_JUST_VALID_VM_STACK 1
20 CONTINUATION_CONTEXT = 0,
22 ROOT_FIBER_CONTEXT = 2
25 typedef struct rb_context_struct {
26 enum context_type type;
31 #ifdef CAPTURE_JUST_VALID_VM_STACK
32 int vm_stack_slen; /* length of stack (head of th->stack) */
33 int vm_stack_clen; /* length of control frames (tail of th->stack) */
36 VALUE *machine_stack_src;
38 VALUE *machine_register_stack;
39 VALUE *machine_register_stack_src;
40 int machine_register_stack_size;
42 rb_thread_t saved_thread;
44 int machine_stack_size;
53 typedef struct rb_fiber_struct {
56 enum fiber_status status;
57 struct rb_fiber_struct *prev_fiber;
58 struct rb_fiber_struct *next_fiber;
61 static VALUE rb_cContinuation;
62 static VALUE rb_cFiber;
63 static VALUE rb_eFiberError;
65 #define GetContPtr(obj, ptr) \
66 Data_Get_Struct(obj, rb_context_t, ptr)
68 #define GetFiberPtr(obj, ptr) do {\
69 ptr = (rb_fiber_t*)DATA_PTR(obj);\
70 if (!ptr) rb_raise(rb_eFiberError, "uninitialized fiber");\
73 NOINLINE(static VALUE cont_capture(volatile int *stat));
75 void rb_thread_mark(rb_thread_t *th);
80 RUBY_MARK_ENTER("cont");
82 rb_context_t *cont = ptr;
83 rb_gc_mark(cont->value);
84 rb_thread_mark(&cont->saved_thread);
87 #ifdef CAPTURE_JUST_VALID_VM_STACK
88 rb_gc_mark_locations(cont->vm_stack,
89 cont->vm_stack + cont->vm_stack_slen + cont->vm_stack_clen);
91 rb_gc_mark_localtion(cont->vm_stack,
92 cont->vm_stack, cont->saved_thread.stack_size);
96 if (cont->machine_stack) {
97 rb_gc_mark_locations(cont->machine_stack,
98 cont->machine_stack + cont->machine_stack_size);
101 if (cont->machine_register_stack) {
102 rb_gc_mark_locations(cont->machine_register_stack,
103 cont->machine_register_stack + cont->machine_register_stack_size);
107 RUBY_MARK_LEAVE("cont");
113 RUBY_FREE_ENTER("cont");
115 rb_context_t *cont = ptr;
116 RUBY_FREE_UNLESS_NULL(cont->saved_thread.stack); fflush(stdout);
117 RUBY_FREE_UNLESS_NULL(cont->machine_stack);
119 RUBY_FREE_UNLESS_NULL(cont->machine_register_stack);
121 RUBY_FREE_UNLESS_NULL(cont->vm_stack);
123 /* free rb_cont_t or rb_fiber_t */
126 RUBY_FREE_LEAVE("cont");
130 fiber_mark(void *ptr)
132 RUBY_MARK_ENTER("cont");
134 rb_fiber_t *fib = ptr;
135 rb_gc_mark(fib->prev);
136 cont_mark(&fib->cont);
138 RUBY_MARK_LEAVE("cont");
142 fiber_link_join(rb_fiber_t *fib)
144 VALUE current_fibval = rb_fiber_current();
145 rb_fiber_t *current_fib;
146 GetFiberPtr(current_fibval, current_fib);
148 /* join fiber link */
149 fib->next_fiber = current_fib->next_fiber;
150 fib->prev_fiber = current_fib;
151 current_fib->next_fiber->prev_fiber = fib;
152 current_fib->next_fiber = fib;
156 fiber_link_remove(rb_fiber_t *fib)
158 fib->prev_fiber->next_fiber = fib->next_fiber;
159 fib->next_fiber->prev_fiber = fib->prev_fiber;
163 fiber_free(void *ptr)
165 RUBY_FREE_ENTER("fiber");
167 rb_fiber_t *fib = ptr;
169 if (fib->cont.type != ROOT_FIBER_CONTEXT) {
170 st_free_table(fib->cont.saved_thread.local_storage);
172 fiber_link_remove(fib);
174 cont_free(&fib->cont);
176 RUBY_FREE_LEAVE("fiber");
180 cont_save_machine_stack(rb_thread_t *th, rb_context_t *cont)
183 rb_thread_t *sth = &cont->saved_thread;
185 SET_MACHINE_STACK_END(&th->machine_stack_end);
187 th->machine_register_stack_end = rb_ia64_bsp();
190 if (th->machine_stack_start > th->machine_stack_end) {
191 size = cont->machine_stack_size = th->machine_stack_start - th->machine_stack_end;
192 cont->machine_stack_src = th->machine_stack_end;
195 size = cont->machine_stack_size = th->machine_stack_end - th->machine_stack_start;
196 cont->machine_stack_src = th->machine_stack_start;
199 if (cont->machine_stack) {
200 REALLOC_N(cont->machine_stack, VALUE, size);
203 cont->machine_stack = ALLOC_N(VALUE, size);
206 FLUSH_REGISTER_WINDOWS;
207 MEMCPY(cont->machine_stack, cont->machine_stack_src, VALUE, size);
211 size = cont->machine_register_stack_size = th->machine_register_stack_end - th->machine_register_stack_start;
212 cont->machine_register_stack_src = th->machine_register_stack_start;
213 if (cont->machine_register_stack) {
214 REALLOC_N(cont->machine_register_stack, VALUE, size);
217 cont->machine_register_stack = ALLOC_N(VALUE, size);
220 MEMCPY(cont->machine_register_stack, cont->machine_register_stack_src, VALUE, size);
223 sth->machine_stack_start = sth->machine_stack_end = 0;
225 sth->machine_register_stack_start = sth->machine_register_stack_end = 0;
230 cont_init(rb_context_t *cont)
232 rb_thread_t *th = GET_THREAD();
234 /* save thread context */
235 cont->saved_thread = *th;
238 static rb_context_t *
239 cont_new(VALUE klass)
242 volatile VALUE contval;
244 contval = Data_Make_Struct(klass, rb_context_t, cont_mark, cont_free, cont);
245 cont->self = contval;
250 void vm_stack_to_heap(rb_thread_t *th);
253 cont_capture(volatile int *stat)
256 rb_thread_t *th = GET_THREAD(), *sth;
257 volatile VALUE contval;
259 vm_stack_to_heap(th);
260 cont = cont_new(rb_cContinuation);
261 contval = cont->self;
262 sth = &cont->saved_thread;
264 #ifdef CAPTURE_JUST_VALID_VM_STACK
265 cont->vm_stack_slen = th->cfp->sp + th->mark_stack_len - th->stack;
266 cont->vm_stack_clen = th->stack + th->stack_size - (VALUE*)th->cfp;
267 cont->vm_stack = ALLOC_N(VALUE, cont->vm_stack_slen + cont->vm_stack_clen);
268 MEMCPY(cont->vm_stack, th->stack, VALUE, cont->vm_stack_slen);
269 MEMCPY(cont->vm_stack + cont->vm_stack_slen, (VALUE*)th->cfp, VALUE, cont->vm_stack_clen);
271 cont->vm_stack = ALLOC_N(VALUE, th->stack_size);
272 MEMCPY(cont->vm_stack, th->stack, VALUE, th->stack_size);
276 cont_save_machine_stack(th, cont);
278 if (ruby_setjmp(cont->jmpbuf)) {
292 NORETURN(static void cont_restore_1(rb_context_t *));
295 cont_restore_1(rb_context_t *cont)
297 rb_thread_t *th = GET_THREAD(), *sth = &cont->saved_thread;
299 /* restore thread context */
300 if (cont->type == CONTINUATION_CONTEXT) {
304 th->fiber = sth->fiber;
305 fib = th->fiber ? th->fiber : th->root_fiber;
309 GetContPtr(fib, fcont);
310 th->stack_size = fcont->saved_thread.stack_size;
311 th->stack = fcont->saved_thread.stack;
313 #ifdef CAPTURE_JUST_VALID_VM_STACK
314 MEMCPY(th->stack, cont->vm_stack, VALUE, cont->vm_stack_slen);
315 MEMCPY(th->stack + sth->stack_size - cont->vm_stack_clen,
316 cont->vm_stack + cont->vm_stack_slen, VALUE, cont->vm_stack_clen);
318 MEMCPY(th->stack, cont->vm_stack, VALUE, sth->stack_size);
323 th->stack = sth->stack;
324 th->stack_size = sth->stack_size;
325 th->local_storage = sth->local_storage;
326 th->fiber = cont->self;
330 th->safe_level = sth->safe_level;
331 th->raised_flag = sth->raised_flag;
332 th->state = sth->state;
333 th->status = sth->status;
335 th->trap_tag = sth->trap_tag;
336 th->errinfo = sth->errinfo;
337 th->first_proc = sth->first_proc;
339 /* restore machine stack */
342 /* workaround for x64 SEH */
345 ((_JUMP_BUFFER*)(&cont->jmpbuf))->Frame =
346 ((_JUMP_BUFFER*)(&buf))->Frame;
349 if (cont->machine_stack_src) {
350 FLUSH_REGISTER_WINDOWS;
351 MEMCPY(cont->machine_stack_src, cont->machine_stack,
352 VALUE, cont->machine_stack_size);
356 if (cont->machine_register_stack_src) {
357 MEMCPY(cont->machine_register_stack_src, cont->machine_register_stack,
358 VALUE, cont->machine_register_stack_size);
362 ruby_longjmp(cont->jmpbuf, 1);
365 NORETURN(NOINLINE(static void cont_restore_0(rb_context_t *, VALUE *)));
368 #define C(a) rse_##a##0, rse_##a##1, rse_##a##2, rse_##a##3, rse_##a##4
369 #define E(a) rse_##a##0= rse_##a##1= rse_##a##2= rse_##a##3= rse_##a##4
370 static volatile int C(a), C(b), C(c), C(d), C(e);
371 static volatile int C(f), C(g), C(h), C(i), C(j);
372 static volatile int C(k), C(l), C(m), C(n), C(o);
373 static volatile int C(p), C(q), C(r), C(s), C(t);
374 int rb_dummy_false = 0;
375 NORETURN(NOINLINE(static void register_stack_extend(rb_context_t *, VALUE *)));
377 register_stack_extend(rb_context_t *cont, VALUE *curr_bsp)
379 if (rb_dummy_false) {
380 /* use registers as much as possible */
381 E(a) = E(b) = E(c) = E(d) = E(e) =
382 E(f) = E(g) = E(h) = E(i) = E(j) =
383 E(k) = E(l) = E(m) = E(n) = E(o) =
384 E(p) = E(q) = E(r) = E(s) = E(t) = 0;
385 E(a) = E(b) = E(c) = E(d) = E(e) =
386 E(f) = E(g) = E(h) = E(i) = E(j) =
387 E(k) = E(l) = E(m) = E(n) = E(o) =
388 E(p) = E(q) = E(r) = E(s) = E(t) = 0;
390 if (curr_bsp < cont->machine_register_stack_src+cont->machine_register_stack_size) {
391 register_stack_extend(cont, (VALUE*)rb_ia64_bsp());
393 cont_restore_1(cont);
400 cont_restore_0(rb_context_t *cont, VALUE *addr_in_prev_frame)
402 if (cont->machine_stack_src) {
403 #define STACK_PAD_SIZE 1024
404 VALUE space[STACK_PAD_SIZE];
406 #if STACK_GROW_DIRECTION < 0 /* downward */
407 if (addr_in_prev_frame > cont->machine_stack_src) {
408 cont_restore_0(cont, &space[0]);
410 #elif STACK_GROW_DIRECTION > 0 /* upward */
411 if (addr_in_prev_frame < cont->machine_stack_src + cont->machine_stack_size) {
412 cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
415 if (addr_in_prev_frame > &space[0]) {
416 /* Stack grows downward */
417 if (addr_in_prev_frame > cont->machine_stack_src) {
418 cont_restore_0(cont, &space[0]);
422 /* Stack grows upward */
423 if (addr_in_prev_frame < cont->machine_stack_src + cont->machine_stack_size) {
424 cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
430 register_stack_extend(cont, (VALUE*)rb_ia64_bsp());
432 cont_restore_1(cont);
437 * Document-class: Continuation
439 * Continuation objects are generated by
440 * <code>Kernel#callcc</code>. They hold a return address and execution
441 * context, allowing a nonlocal return to the end of the
442 * <code>callcc</code> block from anywhere within a program.
443 * Continuations are somewhat analogous to a structured version of C's
444 * <code>setjmp/longjmp</code> (although they contain more state, so
445 * you might consider them closer to threads).
449 * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
451 * puts(message = arr.shift)
452 * $cc.call unless message =~ /Max/
461 * This (somewhat contrived) example allows the inner loop to abandon
467 * for j in i*5...(i+1)*5
468 * cont.call() if j == 17
485 * callcc {|cont| block } => obj
487 * Generates a <code>Continuation</code> object, which it passes to the
488 * associated block. Performing a <em>cont</em><code>.call</code> will
489 * cause the <code>callcc</code> to return (as will falling through the
490 * end of the block). The value returned by the <code>callcc</code> is
491 * the value of the block, or the value passed to
492 * <em>cont</em><code>.call</code>. See class <code>Continuation</code>
493 * for more details. Also see <code>Kernel::throw</code> for
494 * an alternative mechanism for unwinding a call stack.
498 rb_callcc(VALUE self)
501 volatile VALUE val = cont_capture(&called);
507 return rb_yield(val);
512 make_passing_arg(int argc, VALUE *argv)
520 return rb_ary_new4(argc, argv);
526 * cont.call(args, ...)
529 * Invokes the continuation. The program continues from the end of the
530 * <code>callcc</code> block. If no arguments are given, the original
531 * <code>callcc</code> returns <code>nil</code>. If one argument is
532 * given, <code>callcc</code> returns it. Otherwise, an array
533 * containing <i>args</i> is returned.
535 * callcc {|cont| cont.call } #=> nil
536 * callcc {|cont| cont.call 1 } #=> 1
537 * callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
541 rb_cont_call(int argc, VALUE *argv, VALUE contval)
544 rb_thread_t *th = GET_THREAD();
545 GetContPtr(contval, cont);
547 if (cont->saved_thread.self != th->self) {
548 rb_raise(rb_eRuntimeError, "continuation called across threads");
550 if (cont->saved_thread.trap_tag != th->trap_tag) {
551 rb_raise(rb_eRuntimeError, "continuation called across trap");
553 if (cont->saved_thread.fiber) {
555 GetContPtr(cont->saved_thread.fiber, fcont);
557 if (th->fiber != cont->saved_thread.fiber) {
558 rb_raise(rb_eRuntimeError, "continuation called across fiber");
563 cont->value = make_passing_arg(argc, argv);
565 cont_restore_0(cont, &contval);
566 return Qnil; /* unreachable */
574 * Document-class: Fiber
576 * Fibers are primitives for implementing light weight cooperative
577 * concurrency in Ruby. Basically they are a means of creating code blocks
578 * that can be paused and resumed, much like threads. The main difference
579 * is that they are never preempted and that the scheduling must be done by
580 * the programmer and not the VM.
582 * As opposed to other stackless light weight concurrency models, each fiber
583 * comes with a small 4KB stack. This enables the fiber to be paused from deeply
584 * nested function calls within the fiber block.
586 * When a fiber is created it will not run automatically. Rather it must be
587 * be explicitly asked to run using the <code>Fiber#resume</code> method.
588 * The code running inside the fiber can give up control by calling
589 * <code>Fiber.yield</code> in which case it yields control back to caller
590 * (the caller of the <code>Fiber#resume</code>).
592 * Upon yielding or termination the Fiber returns the value of the last
593 * executed expression
597 * fiber = Fiber.new do
610 * FiberError: dead fiber called
612 * The <code>Fiber#resume</code> method accepts an arbitary number of
613 * parameters, if it is the first call to <code>resume</code> then they
614 * will be passed as block arguments. Otherwise they will be the return
615 * value of the call to <code>Fiber.yield</code>
619 * fiber = Fiber.new do |first|
620 * second = Fiber.yield first + 2
623 * puts fiber.resume 10
624 * puts fiber.resume 14
625 * puts fiber.resume 18
631 * FiberError: dead fiber called
635 #define FIBER_VM_STACK_SIZE (4 * 1024)
638 fiber_alloc(VALUE klass)
640 return Data_Wrap_Struct(klass, fiber_mark, fiber_free, 0);
644 fiber_t_alloc(VALUE fibval)
646 rb_fiber_t *fib = ALLOC(rb_fiber_t);
648 memset(fib, 0, sizeof(rb_fiber_t));
649 fib->cont.self = fibval;
650 fib->cont.type = FIBER_CONTEXT;
651 cont_init(&fib->cont);
653 fib->status = CREATED;
655 DATA_PTR(fibval) = fib;
661 fiber_init(VALUE fibval, VALUE proc)
663 rb_fiber_t *fib = fiber_t_alloc(fibval);
664 rb_context_t *cont = &fib->cont;
665 rb_thread_t *th = &cont->saved_thread;
667 fiber_link_join(fib);
669 /* initialize cont */
673 th->stack_size = FIBER_VM_STACK_SIZE;
674 th->stack = ALLOC_N(VALUE, th->stack_size);
676 th->cfp = (void *)(th->stack + th->stack_size);
679 th->cfp->sp = th->stack + 1;
681 th->cfp->lfp = th->stack;
683 th->cfp->dfp = th->stack;
684 th->cfp->self = Qnil;
688 th->cfp->block_iseq = 0;
690 th->local_storage = st_init_numtable();
692 th->first_proc = proc;
694 MEMCPY(&cont->jmpbuf, &th->root_jmpbuf, rb_jmpbuf_t, 1);
700 rb_fiber_init(VALUE fibval)
702 return fiber_init(fibval, rb_block_proc());
706 rb_fiber_new(VALUE (*func)(ANYARGS), VALUE obj)
708 return fiber_init(fiber_alloc(rb_cFiber), rb_proc_new(func, obj));
715 VALUE curr = rb_fiber_current();
716 GetFiberPtr(curr, fib);
718 if (fib->prev == Qnil) {
719 rb_thread_t *th = GET_THREAD();
721 if (th->root_fiber != curr) {
722 return th->root_fiber;
725 rb_raise(rb_eFiberError, "can't yield from root fiber");
729 VALUE prev = fib->prev;
735 VALUE rb_fiber_transfer(VALUE fib, int argc, VALUE *argv);
738 rb_fiber_terminate(rb_fiber_t *fib)
740 VALUE value = fib->cont.value;
741 fib->status = TERMINATED;
742 rb_fiber_transfer(return_fiber(), 1, &value);
748 rb_thread_t *th = GET_THREAD();
754 GetFiberPtr(th->fiber, fib);
758 if ((state = EXEC_TAG()) == 0) {
761 GetProcPtr(cont->saved_thread.first_proc, proc);
763 argv = (argc = cont->argc) > 1 ? RARRAY_PTR(args) : &args;
766 th->local_lfp = proc->block.lfp;
767 th->local_svar = Qnil;
769 fib->status = RUNNING;
770 cont->value = vm_invoke_proc(th, proc, proc->block.self, argc, argv, 0);
776 th->thrown_errinfo = th->errinfo;
780 vm_make_jump_tag_but_local_jump(state, th->errinfo);
782 RUBY_VM_SET_INTERRUPT(th);
785 rb_fiber_terminate(fib);
786 rb_bug("rb_fiber_start: unreachable");
790 root_fiber_alloc(rb_thread_t *th)
794 /* no need to allocate vm stack */
795 fib = fiber_t_alloc(fiber_alloc(rb_cFiber));
796 fib->cont.type = ROOT_FIBER_CONTEXT;
797 fib->prev_fiber = fib->next_fiber = fib;
805 rb_thread_t *th = GET_THREAD();
806 if (th->fiber == 0) {
808 rb_fiber_t *fib = root_fiber_alloc(th);
809 th->root_fiber = th->fiber = fib->cont.self;
815 fiber_store(rb_fiber_t *next_fib)
817 rb_thread_t *th = GET_THREAD();
821 GetFiberPtr(th->fiber, fib);
822 fib->cont.saved_thread = *th;
825 /* create current fiber */
826 fib = root_fiber_alloc(th);
827 th->root_fiber = th->fiber = fib->cont.self;
830 cont_save_machine_stack(th, &fib->cont);
832 if (ruby_setjmp(fib->cont.jmpbuf)) {
834 GetFiberPtr(th->fiber, fib);
835 return fib->cont.value;
843 fiber_switch(VALUE fibval, int argc, VALUE *argv, int is_resume)
848 rb_thread_t *th = GET_THREAD();
850 GetFiberPtr(fibval, fib);
853 if (cont->saved_thread.self != th->self) {
854 rb_raise(rb_eFiberError, "fiber called across threads");
856 else if (cont->saved_thread.trap_tag != th->trap_tag) {
857 rb_raise(rb_eFiberError, "fiber called across trap");
859 else if (fib->status == TERMINATED) {
860 rb_raise(rb_eFiberError, "dead fiber called");
864 fib->prev = rb_fiber_current();
868 cont->value = make_passing_arg(argc, argv);
870 if ((value = fiber_store(fib)) == Qundef) {
871 cont_restore_0(&fib->cont, &value);
872 rb_bug("rb_fiber_resume: unreachable");
875 RUBY_VM_CHECK_INTS();
881 rb_fiber_transfer(VALUE fib, int argc, VALUE *argv)
883 return fiber_switch(fib, argc, argv, 0);
887 rb_fiber_resume(VALUE fibval, int argc, VALUE *argv)
890 GetFiberPtr(fibval, fib);
892 if (fib->prev != Qnil) {
893 rb_raise(rb_eFiberError, "double resume");
896 return fiber_switch(fibval, argc, argv, 1);
900 rb_fiber_yield(int argc, VALUE *argv)
902 return rb_fiber_transfer(return_fiber(), argc, argv);
907 * fiber.alive? -> true or false
909 * Returns true if the fiber can still be resumed (or transferred to).
910 * After finishing execution of the fiber block this method will always
914 rb_fiber_alive_p(VALUE fibval)
917 GetFiberPtr(fibval, fib);
918 return fib->status != TERMINATED;
923 * fiber.resume(args, ...) -> obj
925 * Resumes the fiber from the point at which the last <code>Fiber.yield</code>
926 * was called, or starts running it if it is the first call to
927 * <code>resume</code>. Arguments passed to resume will be the value of
928 * the <code>Fiber.yield</code> expression or will be passed as block
929 * parameters to the fiber's block if this is the first <code>resume</code>.
931 * Alternatively, when resume is called it evaluates to the arguments passed
932 * to the next <code>Fiber.yield</code> statement inside the fiber's block
933 * or to the block value if it runs to completion without any
934 * <code>Fiber.yield</code>
937 rb_fiber_m_resume(int argc, VALUE *argv, VALUE fib)
939 return rb_fiber_resume(fib, argc, argv);
944 * fiber.transfer(args, ...) -> obj
946 * Transfer control to another fiber, resuming it from where it last
947 * stopped or starting it if it was not resumed before. The calling
948 * fiber will be suspended much like in a call to <code>Fiber.yield</code>.
950 * The fiber which recieves the transfer call is treats it much like
951 * a resume call. Arguments passed to transfer are treated like those
954 * You cannot resume a fiber that transferred control to another one.
955 * This will cause a double resume error. You need to transfer control
956 * back to this fiber before it can yield and resume.
959 rb_fiber_m_transfer(int argc, VALUE *argv, VALUE fib)
961 return rb_fiber_transfer(fib, argc, argv);
966 * Fiber.yield(args, ...) -> obj
968 * Yields control back to the context that resumed the fiber, passing
969 * along any arguments that were passed to it. The fiber will resume
970 * processing at this point when <code>resume</code> is called next.
971 * Any arguments passed to the next <code>resume</code> will be the
972 * value that this <code>Fiber.yield</code> expression evaluates to.
975 rb_fiber_s_yield(int argc, VALUE *argv, VALUE klass)
977 return rb_fiber_yield(argc, argv);
982 * Fiber.current() -> fiber
984 * Returns the current fiber. You need to <code>require 'fiber'</code>
985 * before using this method. If you are not running in the context of
986 * a fiber this method will return the root fiber.
989 rb_fiber_s_current(VALUE klass)
991 return rb_fiber_current();
997 rb_cFiber = rb_define_class("Fiber", rb_cObject);
998 rb_define_alloc_func(rb_cFiber, fiber_alloc);
999 rb_eFiberError = rb_define_class("FiberError", rb_eStandardError);
1000 rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
1001 rb_define_method(rb_cFiber, "initialize", rb_fiber_init, 0);
1002 rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
1006 Init_Continuation_body(void)
1008 rb_cContinuation = rb_define_class("Continuation", rb_cObject);
1009 rb_undef_alloc_func(rb_cContinuation);
1010 rb_undef_method(CLASS_OF(rb_cContinuation), "new");
1011 rb_define_method(rb_cContinuation, "call", rb_cont_call, -1);
1012 rb_define_method(rb_cContinuation, "[]", rb_cont_call, -1);
1013 rb_define_global_function("callcc", rb_callcc, 0);
1017 Init_Fiber_as_Coroutine(void)
1019 rb_define_method(rb_cFiber, "transfer", rb_fiber_m_transfer, -1);
1020 rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0);
1021 rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0);