platform/shared/ruby/cont.c in rhodes-3.1.1 vs platform/shared/ruby/cont.c in rhodes-3.2.0.beta.1

- old
+ new

@@ -1,8 +1,8 @@ /********************************************************************** - cont.c - + cont.c - $Author: yugui $ created at: Thu May 23 09:03:43 2007 Copyright (C) 2007 Koichi Sasada @@ -27,23 +27,23 @@ VALUE self; int argc; VALUE value; VALUE *vm_stack; #ifdef CAPTURE_JUST_VALID_VM_STACK - int vm_stack_slen; /* length of stack (head of th->stack) */ - int vm_stack_clen; /* length of control frames (tail of th->stack) */ + size_t vm_stack_slen; /* length of stack (head of th->stack) */ + size_t vm_stack_clen; /* length of control frames (tail of th->stack) */ #endif VALUE *machine_stack; VALUE *machine_stack_src; #ifdef __ia64 VALUE *machine_register_stack; VALUE *machine_register_stack_src; int machine_register_stack_size; #endif rb_thread_t saved_thread; rb_jmpbuf_t jmpbuf; - int machine_stack_size; + size_t machine_stack_size; } rb_context_t; enum fiber_status { CREATED, RUNNING, @@ -56,25 +56,29 @@ enum fiber_status status; struct rb_fiber_struct *prev_fiber; struct rb_fiber_struct *next_fiber; } rb_fiber_t; +static const rb_data_type_t cont_data_type, fiber_data_type; static VALUE rb_cContinuation; static VALUE rb_cFiber; static VALUE rb_eFiberError; #define GetContPtr(obj, ptr) \ - Data_Get_Struct(obj, rb_context_t, ptr) + TypedData_Get_Struct(obj, rb_context_t, &cont_data_type, ptr) #define GetFiberPtr(obj, ptr) do {\ - ptr = (rb_fiber_t*)DATA_PTR(obj);\ - if (!ptr) rb_raise(rb_eFiberError, "uninitialized fiber");\ + TypedData_Get_Struct(obj, rb_fiber_t, &fiber_data_type, ptr); \ + if (!ptr) rb_raise(rb_eFiberError, "uninitialized fiber"); \ } while(0) NOINLINE(static VALUE cont_capture(volatile int *stat)); void rb_thread_mark(rb_thread_t *th); +#define THREAD_MUST_BE_RUNNING(th) do { \ + if (!th->tag) rb_raise(rb_eThreadError, "not running thread"); \ + } while (0) static void cont_mark(void *ptr) { RUBY_MARK_ENTER("cont"); @@ -124,10 +128,38 @@ ruby_xfree(ptr); } RUBY_FREE_LEAVE("cont"); } +static size_t +cont_memsize(const void *ptr) +{ + const rb_context_t *cont = ptr; + size_t size = 0; + if (cont) { + size = sizeof(*cont); + if (cont->vm_stack) { +#ifdef CAPTURE_JUST_VALID_VM_STACK + size_t n = (cont->vm_stack_slen + cont->vm_stack_clen); +#else + size_t n = cont->saved_thread.stack_size; +#endif + size += n * sizeof(*cont->vm_stack); + } + + if (cont->machine_stack) { + size += cont->machine_stack_size * sizeof(*cont->machine_stack); + } +#ifdef __ia64 + if (cont->machine_register_stack) { + size += cont->machine_register_stack_size * sizeof(*cont->machine_register_stack); + } +#endif + } + return size; +} + static void fiber_mark(void *ptr) { RUBY_MARK_ENTER("cont"); if (ptr) { @@ -164,24 +196,40 @@ { RUBY_FREE_ENTER("fiber"); if (ptr) { rb_fiber_t *fib = ptr; - if (fib->cont.type != ROOT_FIBER_CONTEXT) { + if (fib->cont.type != ROOT_FIBER_CONTEXT && + fib->cont.saved_thread.local_storage) { st_free_table(fib->cont.saved_thread.local_storage); } fiber_link_remove(fib); cont_free(&fib->cont); } RUBY_FREE_LEAVE("fiber"); } +static size_t +fiber_memsize(const void *ptr) +{ + const rb_fiber_t *fib = ptr; + size_t size = 0; + if (ptr) { + size = sizeof(*fib); + if (fib->cont.type != ROOT_FIBER_CONTEXT) { + size += st_memsize(fib->cont.saved_thread.local_storage); + } + size += cont_memsize(&fib->cont); + } + return size; +} + static void cont_save_machine_stack(rb_thread_t *th, rb_context_t *cont) { - int size; + size_t size; rb_thread_t *sth = &cont->saved_thread; SET_MACHINE_STACK_END(&th->machine_stack_end); #ifdef __ia64 th->machine_register_stack_end = rb_ia64_bsp(); @@ -224,28 +272,35 @@ #ifdef __ia64 sth->machine_register_stack_start = sth->machine_register_stack_end = 0; #endif } +static const rb_data_type_t cont_data_type = { + "continuation", + cont_mark, cont_free, cont_memsize, +}; + static void -cont_init(rb_context_t *cont) +cont_init(rb_context_t *cont, rb_thread_t *th) { - rb_thread_t *th = GET_THREAD(); - /* save thread context */ cont->saved_thread = *th; + cont->saved_thread.local_storage = 0; + cont->saved_thread.machine_stack_start = cont->saved_thread.machine_stack_end = 0; } static rb_context_t * cont_new(VALUE klass) { rb_context_t *cont; volatile VALUE contval; + rb_thread_t *th = GET_THREAD(); - contval = Data_Make_Struct(klass, rb_context_t, cont_mark, cont_free, cont); + THREAD_MUST_BE_RUNNING(th); + contval = TypedData_Make_Struct(klass, rb_context_t, &cont_data_type, cont); cont->self = contval; - cont_init(cont); + cont_init(cont, th); return cont; } void rb_vm_stack_to_heap(rb_thread_t *th); @@ -254,10 +309,11 @@ { rb_context_t *cont; rb_thread_t *th = GET_THREAD(), *sth; volatile VALUE contval; + THREAD_MUST_BE_RUNNING(th); rb_vm_stack_to_heap(th); cont = cont_new(rb_cContinuation); contval = cont->self; sth = &cont->saved_thread; @@ -277,10 +333,11 @@ if (ruby_setjmp(cont->jmpbuf)) { VALUE value; value = cont->value; + if (cont->argc == -1) rb_exc_raise(value); cont->value = Qnil; *stat = 1; return value; } else { @@ -303,14 +360,14 @@ th->fiber = sth->fiber; fib = th->fiber ? th->fiber : th->root_fiber; if (fib) { - rb_context_t *fcont; - GetContPtr(fib, fcont); - th->stack_size = fcont->saved_thread.stack_size; - th->stack = fcont->saved_thread.stack; + rb_fiber_t *fcont; + GetFiberPtr(fib, fcont); + th->stack_size = fcont->cont.saved_thread.stack_size; + th->stack = fcont->cont.saved_thread.stack; } #ifdef CAPTURE_JUST_VALID_VM_STACK MEMCPY(th->stack, cont->vm_stack, VALUE, cont->vm_stack_slen); MEMCPY(th->stack + sth->stack_size - cont->vm_stack_clen, cont->vm_stack + cont->vm_stack_slen, VALUE, cont->vm_stack_clen); @@ -330,11 +387,11 @@ th->safe_level = sth->safe_level; th->raised_flag = sth->raised_flag; th->state = sth->state; th->status = sth->status; th->tag = sth->tag; - th->trap_tag = sth->trap_tag; + th->protect_tag = sth->protect_tag; th->errinfo = sth->errinfo; th->first_proc = sth->first_proc; /* restore machine stack */ #ifdef _M_AMD64 @@ -369,14 +426,17 @@ #define E(a) rse_##a##0= rse_##a##1= rse_##a##2= rse_##a##3= rse_##a##4 static volatile int C(a), C(b), C(c), C(d), C(e); static volatile int C(f), C(g), C(h), C(i), C(j); static volatile int C(k), C(l), C(m), C(n), C(o); static volatile int C(p), C(q), C(r), C(s), C(t); +#if 0 +{/* the above lines make cc-mode.el confused so much */} +#endif int rb_dummy_false = 0; -NORETURN(NOINLINE(static void register_stack_extend(rb_context_t *, VALUE *))); +NORETURN(NOINLINE(static void register_stack_extend(rb_context_t *, VALUE *, VALUE *))); static void -register_stack_extend(rb_context_t *cont, VALUE *curr_bsp) +register_stack_extend(rb_context_t *cont, VALUE *vp, VALUE *curr_bsp) { if (rb_dummy_false) { /* use registers as much as possible */ E(a) = E(b) = E(c) = E(d) = E(e) = E(f) = E(g) = E(h) = E(i) = E(j) = @@ -386,54 +446,69 @@ E(f) = E(g) = E(h) = E(i) = E(j) = E(k) = E(l) = E(m) = E(n) = E(o) = E(p) = E(q) = E(r) = E(s) = E(t) = 0; } if (curr_bsp < cont->machine_register_stack_src+cont->machine_register_stack_size) { - register_stack_extend(cont, (VALUE*)rb_ia64_bsp()); + register_stack_extend(cont, vp, (VALUE*)rb_ia64_bsp()); } - cont_restore_1(cont); + cont_restore_0(cont, vp); } #undef C #undef E #endif static void cont_restore_0(rb_context_t *cont, VALUE *addr_in_prev_frame) { if (cont->machine_stack_src) { +#ifdef HAVE_ALLOCA +#define STACK_PAD_SIZE 1 +#else #define STACK_PAD_SIZE 1024 +#endif VALUE space[STACK_PAD_SIZE]; -#if STACK_GROW_DIRECTION < 0 /* downward */ - if (addr_in_prev_frame > cont->machine_stack_src) { - cont_restore_0(cont, &space[0]); - } -#elif STACK_GROW_DIRECTION > 0 /* upward */ - if (addr_in_prev_frame < cont->machine_stack_src + cont->machine_stack_size) { - cont_restore_0(cont, &space[STACK_PAD_SIZE-1]); - } -#else +#if !STACK_GROW_DIRECTION if (addr_in_prev_frame > &space[0]) { /* Stack grows downward */ - if (addr_in_prev_frame > cont->machine_stack_src) { +#endif +#if STACK_GROW_DIRECTION <= 0 + volatile VALUE *const end = cont->machine_stack_src; + if (&space[0] > end) { +# ifdef HAVE_ALLOCA + volatile VALUE *sp = ALLOCA_N(VALUE, &space[0] - end); + (void)sp; +# else cont_restore_0(cont, &space[0]); +# endif } +#endif +#if !STACK_GROW_DIRECTION } else { /* Stack grows upward */ - if (addr_in_prev_frame < cont->machine_stack_src + cont->machine_stack_size) { +#endif +#if STACK_GROW_DIRECTION >= 0 + volatile VALUE *const end = cont->machine_stack_src + cont->machine_stack_size; + if (&space[STACK_PAD_SIZE] < end) { +# ifdef HAVE_ALLOCA + volatile VALUE *sp = ALLOCA_N(VALUE, end - &space[STACK_PAD_SIZE]); + (void)sp; +# else cont_restore_0(cont, &space[STACK_PAD_SIZE-1]); +# endif } +#endif +#if !STACK_GROW_DIRECTION } #endif } -#ifdef __ia64 - register_stack_extend(cont, (VALUE*)rb_ia64_bsp()); -#else cont_restore_1(cont); -#endif } +#ifdef __ia64 +#define cont_restore_0(cont, vp) register_stack_extend(cont, vp, (VALUE*)rb_ia64_bsp()); +#endif /* * Document-class: Continuation * * Continuation objects are generated by @@ -441,51 +516,51 @@ * context, allowing a nonlocal return to the end of the * <code>callcc</code> block from anywhere within a program. * Continuations are somewhat analogous to a structured version of C's * <code>setjmp/longjmp</code> (although they contain more state, so * you might consider them closer to threads). - * + * * For instance: - * + * * arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ] * callcc{|$cc|} * puts(message = arr.shift) * $cc.call unless message =~ /Max/ - * + * * <em>produces:</em> - * + * * Freddie * Herbie * Ron * Max - * + * * This (somewhat contrived) example allows the inner loop to abandon * processing early: - * + * * callcc {|cont| * for i in 0..4 * print "\n#{i}: " * for j in i*5...(i+1)*5 * cont.call() if j == 17 * printf "%3d", j * end * end * } * print "\n" - * + * * <em>produces:</em> - * + * * 0: 0 1 2 3 4 * 1: 5 6 7 8 9 * 2: 10 11 12 13 14 * 3: 15 16 */ /* * call-seq: - * callcc {|cont| block } => obj - * + * callcc {|cont| block } -> obj + * * Generates a <code>Continuation</code> object, which it passes to the * associated block. Performing a <em>cont</em><code>.call</code> will * cause the <code>callcc</code> to return (as will falling through the * end of the block). The value returned by the <code>callcc</code> is * the value of the block, or the value passed to @@ -523,17 +598,17 @@ /* * call-seq: * cont.call(args, ...) * cont[args, ...] - * + * * Invokes the continuation. The program continues from the end of the * <code>callcc</code> block. If no arguments are given, the original * <code>callcc</code> returns <code>nil</code>. If one argument is * given, <code>callcc</code> returns it. Otherwise, an array * containing <i>args</i> is returned. - * + * * callcc {|cont| cont.call } #=> nil * callcc {|cont| cont.call 1 } #=> 1 * callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3] */ @@ -545,16 +620,16 @@ GetContPtr(contval, cont); if (cont->saved_thread.self != th->self) { rb_raise(rb_eRuntimeError, "continuation called across threads"); } - if (cont->saved_thread.trap_tag != th->trap_tag) { - rb_raise(rb_eRuntimeError, "continuation called across trap"); + if (cont->saved_thread.protect_tag != th->protect_tag) { + rb_raise(rb_eRuntimeError, "continuation called across stack rewinding barrier"); } if (cont->saved_thread.fiber) { - rb_context_t *fcont; - GetContPtr(cont->saved_thread.fiber, fcont); + rb_fiber_t *fcont; + GetFiberPtr(cont->saved_thread.fiber, fcont); if (th->fiber != cont->saved_thread.fiber) { rb_raise(rb_eRuntimeError, "continuation called across fiber"); } } @@ -572,85 +647,93 @@ /* * Document-class: Fiber * * Fibers are primitives for implementing light weight cooperative - * concurrency in Ruby. Basically they are a means of creating code blocks - * that can be paused and resumed, much like threads. The main difference - * is that they are never preempted and that the scheduling must be done by - * the programmer and not the VM. + * concurrency in Ruby. Basically they are a means of creating code blocks + * that can be paused and resumed, much like threads. The main difference + * is that they are never preempted and that the scheduling must be done by + * the programmer and not the VM. * * As opposed to other stackless light weight concurrency models, each fiber * comes with a small 4KB stack. This enables the fiber to be paused from deeply * nested function calls within the fiber block. * - * When a fiber is created it will not run automatically. Rather it must be - * be explicitly asked to run using the <code>Fiber#resume</code> method. - * The code running inside the fiber can give up control by calling - * <code>Fiber.yield</code> in which case it yields control back to caller + * When a fiber is created it will not run automatically. Rather it must be + * be explicitly asked to run using the <code>Fiber#resume</code> method. + * The code running inside the fiber can give up control by calling + * <code>Fiber.yield</code> in which case it yields control back to caller * (the caller of the <code>Fiber#resume</code>). - * - * Upon yielding or termination the Fiber returns the value of the last + * + * Upon yielding or termination the Fiber returns the value of the last * executed expression - * + * * For instance: - * + * * fiber = Fiber.new do * Fiber.yield 1 * 2 * end * * puts fiber.resume * puts fiber.resume * puts fiber.resume - * + * * <em>produces</em> - * + * * 1 * 2 * FiberError: dead fiber called - * - * The <code>Fiber#resume</code> method accepts an arbitary number of + * + * The <code>Fiber#resume</code> method accepts an arbitrary number of * parameters, if it is the first call to <code>resume</code> then they * will be passed as block arguments. Otherwise they will be the return * value of the call to <code>Fiber.yield</code> * * Example: - * + * * fiber = Fiber.new do |first| * second = Fiber.yield first + 2 * end * * puts fiber.resume 10 * puts fiber.resume 14 * puts fiber.resume 18 * * <em>produces</em> - * + * * 12 * 14 * FiberError: dead fiber called * */ #define FIBER_VM_STACK_SIZE (4 * 1024) +static const rb_data_type_t fiber_data_type = { + "fiber", + fiber_mark, fiber_free, fiber_memsize, +}; + static VALUE fiber_alloc(VALUE klass) { - return Data_Wrap_Struct(klass, fiber_mark, fiber_free, 0); + return TypedData_Wrap_Struct(klass, &fiber_data_type, 0); } static rb_fiber_t* fiber_t_alloc(VALUE fibval) { - rb_fiber_t *fib = ALLOC(rb_fiber_t); + rb_fiber_t *fib; + rb_thread_t *th = GET_THREAD(); + THREAD_MUST_BE_RUNNING(th); + fib = ALLOC(rb_fiber_t); memset(fib, 0, sizeof(rb_fiber_t)); fib->cont.self = fibval; fib->cont.type = FIBER_CONTEXT; - cont_init(&fib->cont); + cont_init(&fib->cont, th); fib->prev = Qnil; fib->status = CREATED; DATA_PTR(fibval) = fib; @@ -662,16 +745,19 @@ { rb_fiber_t *fib = fiber_t_alloc(fibval); rb_context_t *cont = &fib->cont; rb_thread_t *th = &cont->saved_thread; - fiber_link_join(fib); /* initialize cont */ cont->vm_stack = 0; th->stack = 0; + th->stack_size = 0; + + fiber_link_join(fib); + th->stack_size = FIBER_VM_STACK_SIZE; th->stack = ALLOC_N(VALUE, th->stack_size); th->cfp = (void *)(th->stack + th->stack_size); th->cfp--; @@ -684,20 +770,22 @@ th->cfp->self = Qnil; th->cfp->flag = 0; th->cfp->iseq = 0; th->cfp->proc = 0; th->cfp->block_iseq = 0; + th->cfp->me = 0; th->tag = 0; th->local_storage = st_init_numtable(); th->first_proc = proc; MEMCPY(&cont->jmpbuf, &th->root_jmpbuf, rb_jmpbuf_t, 1); return fibval; } +/* :nodoc: */ static VALUE rb_fiber_init(VALUE fibval) { return fiber_init(fibval, rb_block_proc()); } @@ -770,11 +858,11 @@ cont->value = rb_vm_invoke_proc(th, proc, proc->block.self, argc, argv, 0); } TH_POP_TAG(); if (state) { - if (TAG_RAISE) { + if (state == TAG_RAISE) { th->thrown_errinfo = th->errinfo; } else { th->thrown_errinfo = rb_vm_make_jump_tag_but_local_jump(state, th->errinfo); @@ -798,11 +886,11 @@ return fib; } VALUE -rb_fiber_current() +rb_fiber_current(void) { rb_thread_t *th = GET_THREAD(); if (th->fiber == 0) { /* save root */ rb_fiber_t *fib = root_fiber_alloc(th); @@ -830,10 +918,11 @@ cont_save_machine_stack(th, &fib->cont); if (ruby_setjmp(fib->cont.jmpbuf)) { /* restored */ GetFiberPtr(th->fiber, fib); + if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value); return fib->cont.value; } else { return Qundef; } @@ -851,26 +940,40 @@ cont = &fib->cont; if (cont->saved_thread.self != th->self) { rb_raise(rb_eFiberError, "fiber called across threads"); } - else if (cont->saved_thread.trap_tag != th->trap_tag) { - rb_raise(rb_eFiberError, "fiber called across trap"); + else if (cont->saved_thread.protect_tag != th->protect_tag) { + rb_raise(rb_eFiberError, "fiber called across stack rewinding barrier"); } else if (fib->status == TERMINATED) { - rb_raise(rb_eFiberError, "dead fiber called"); + value = rb_exc_new2(rb_eFiberError, "dead fiber called"); + if (th->fiber != fibval) { + GetFiberPtr(th->fiber, fib); + if (fib->status != TERMINATED) rb_exc_raise(value); + fibval = th->root_fiber; + } + else { + fibval = fib->prev; + if (NIL_P(fibval)) fibval = th->root_fiber; + } + GetFiberPtr(fibval, fib); + cont = &fib->cont; + cont->argc = -1; + cont->value = value; + cont_restore_0(cont, &value); } if (is_resume) { fib->prev = rb_fiber_current(); } cont->argc = argc; cont->value = make_passing_arg(argc, argv); if ((value = fiber_store(fib)) == Qundef) { - cont_restore_0(&fib->cont, &value); + cont_restore_0(cont, &value); rb_bug("rb_fiber_resume: unreachable"); } RUBY_VM_CHECK_INTS(); @@ -903,33 +1006,33 @@ } /* * call-seq: * fiber.alive? -> true or false - * + * * Returns true if the fiber can still be resumed (or transferred to). * After finishing execution of the fiber block this method will always * return false. */ VALUE rb_fiber_alive_p(VALUE fibval) { rb_fiber_t *fib; GetFiberPtr(fibval, fib); - return fib->status != TERMINATED; + return fib->status != TERMINATED ? Qtrue : Qfalse; } /* * call-seq: * fiber.resume(args, ...) -> obj - * + * * Resumes the fiber from the point at which the last <code>Fiber.yield</code> - * was called, or starts running it if it is the first call to + * was called, or starts running it if it is the first call to * <code>resume</code>. Arguments passed to resume will be the value of - * the <code>Fiber.yield</code> expression or will be passed as block + * the <code>Fiber.yield</code> expression or will be passed as block * parameters to the fiber's block if this is the first <code>resume</code>. - * + * * Alternatively, when resume is called it evaluates to the arguments passed * to the next <code>Fiber.yield</code> statement inside the fiber's block * or to the block value if it runs to completion without any * <code>Fiber.yield</code> */ @@ -940,19 +1043,19 @@ } /* * call-seq: * fiber.transfer(args, ...) -> obj - * + * * Transfer control to another fiber, resuming it from where it last - * stopped or starting it if it was not resumed before. The calling + * stopped or starting it if it was not resumed before. The calling * fiber will be suspended much like in a call to <code>Fiber.yield</code>. - * - * The fiber which recieves the transfer call is treats it much like + * + * The fiber which receives the transfer call is treats it much like * a resume call. Arguments passed to transfer are treated like those * passed to resume. - * + * * You cannot resume a fiber that transferred control to another one. * This will cause a double resume error. You need to transfer control * back to this fiber before it can yield and resume. */ static VALUE @@ -962,11 +1065,11 @@ } /* * call-seq: * Fiber.yield(args, ...) -> obj - * + * * Yields control back to the context that resumed the fiber, passing * along any arguments that were passed to it. The fiber will resume * processing at this point when <code>resume</code> is called next. * Any arguments passed to the next <code>resume</code> will be the * value that this <code>Fiber.yield</code> expression evaluates to. @@ -978,19 +1081,34 @@ } /* * call-seq: * Fiber.current() -> fiber - * + * * Returns the current fiber. You need to <code>require 'fiber'</code> * before using this method. If you are not running in the context of * a fiber this method will return the root fiber. */ static VALUE rb_fiber_s_current(VALUE klass) { return rb_fiber_current(); } + + + +/* + * Document-class: FiberError + * + * Raised when an invalid operation is attempted on a Fiber, in + * particular when attempting to call/resume a dead fiber, + * attempting to yield from the root fiber, or calling a fiber across + * threads. + * + * fiber = Fiber.new{} + * fiber.resume #=> nil + * fiber.resume #=> FiberError: dead fiber called + */ void Init_Cont(void) { rb_cFiber = rb_define_class("Fiber", rb_cObject);