2007-05-23 18:52:19 -04:00
|
|
|
/**********************************************************************
|
|
|
|
|
2009-02-22 09:23:33 -05:00
|
|
|
cont.c -
|
2007-05-23 18:52:19 -04:00
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Thu May 23 09:03:43 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Koichi Sasada
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
2007-06-09 23:06:15 -04:00
|
|
|
#include "ruby/ruby.h"
|
* internal.h: declare internal functions here.
* node.h: declare NODE dependent internal functions here.
* iseq.h: declare rb_iseq_t dependent internal functions here.
* vm_core.h: declare rb_thread_t dependent internal functions here.
* bignum.c, class.c, compile.c, complex.c, cont.c, dir.c, encoding.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c, io.c,
iseq.c, load.c, marshal.c, math.c, numeric.c, object.c, parse.y,
proc.c, process.c, range.c, rational.c, re.c, ruby.c, string.c,
thread.c, time.c, transcode.c, variable.c, vm.c,
tool/compile_prelude.rb: don't declare internal functions declared
in above headers. include above headers if required.
Note that rb_thread_mark() was declared as
void rb_thread_mark(rb_thread_t *th) in cont.c but defined as
void rb_thread_mark(void *ptr) in vm.c. Now it is declared as
the later in internal.h.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-06-17 18:43:38 -04:00
|
|
|
#include "internal.h"
|
* blockinlining.c: remove "yarv" prefix.
* array.c, numeric.c: ditto.
* insnhelper.ci, insns.def, vm_evalbody.ci: ditto.
* yarvcore.c: removed.
* yarvcore.h: renamed to core.h.
* cont.c, debug.c, error.c, process.c, signal.c : ditto.
* ext/probeprofiler/probeprofiler.c: ditto.
* id.c, id.h: added.
* inits.c: ditto.
* compile.c: rename internal functions.
* compile.h: fix debug flag.
* eval.c, object.c, vm.c: remove ruby_top_self.
use rb_vm_top_self() instead.
* eval_intern.h, eval_load: ditto.
* gc.c: rename yarv_machine_stack_mark() to
rb_gc_mark_machine_stack().
* insnhelper.h: remove unused macros.
* iseq.c: add iseq_compile() to create iseq object
from source string.
* proc.c: rename a internal function.
* template/insns.inc.tmpl: remove YARV prefix.
* thread.c:
* vm.c (rb_iseq_eval): added.
* vm.c: move some functions from yarvcore.c.
* vm_dump.c: fix to remove compiler warning.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-07-12 00:25:46 -04:00
|
|
|
#include "vm_core.h"
|
2007-05-23 18:52:19 -04:00
|
|
|
#include "gc.h"
|
|
|
|
#include "eval_intern.h"
|
|
|
|
|
2011-10-24 09:49:58 -04:00
|
|
|
#if ((defined(_WIN32) && _WIN32_WINNT >= 0x0400) || (defined(HAVE_GETCONTEXT) && defined(HAVE_SETCONTEXT))) && !defined(__NetBSD__) && !defined(__sun) && !defined(FIBER_USE_NATIVE)
|
2010-05-05 14:37:37 -04:00
|
|
|
#define FIBER_USE_NATIVE 1
|
|
|
|
|
|
|
|
/* FIBER_USE_NATIVE enables Fiber performance improvement using system
|
|
|
|
* dependent method such as make/setcontext on POSIX system or
|
|
|
|
* CreateFiber() API on Windows.
|
|
|
|
* This hack make Fiber context switch faster (x2 or more).
|
|
|
|
* However, it decrease maximum number of Fiber. For example, on the
|
|
|
|
* 32bit POSIX OS, ten or twenty thousands Fiber can be created.
|
|
|
|
*
|
|
|
|
* Details is reported in the paper "A Fast Fiber Implementation for Ruby 1.9"
|
|
|
|
* in Proc. of 51th Programming Symposium, pp.21--28 (2010) (in Japanese).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* On our experience, NetBSD doesn't support using setcontext() and pthread
|
|
|
|
* simultaneously. This is because pthread_self(), TLS and other information
|
|
|
|
* are represented by stack pointer (higher bits of stack pointer).
|
|
|
|
* TODO: check such constraint on configure.
|
|
|
|
*/
|
2011-06-06 10:06:59 -04:00
|
|
|
#elif !defined(FIBER_USE_NATIVE)
|
|
|
|
#define FIBER_USE_NATIVE 0
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
|
|
|
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <ucontext.h>
|
2010-07-27 20:38:21 -04:00
|
|
|
#endif
|
2010-07-27 20:25:53 -04:00
|
|
|
#define RB_PAGE_SIZE (pagesize)
|
|
|
|
#define RB_PAGE_MASK (~(RB_PAGE_SIZE - 1))
|
2010-05-05 14:37:37 -04:00
|
|
|
static long pagesize;
|
2011-07-08 00:09:00 -04:00
|
|
|
#define FIBER_MACHINE_STACK_ALLOCATION_SIZE (0x10000)
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
|
|
|
|
2008-11-18 11:19:37 -05:00
|
|
|
#define CAPTURE_JUST_VALID_VM_STACK 1
|
|
|
|
|
2007-11-08 20:11:49 -05:00
|
|
|
enum context_type {
|
|
|
|
CONTINUATION_CONTEXT = 0,
|
|
|
|
FIBER_CONTEXT = 1,
|
2008-07-01 04:27:58 -04:00
|
|
|
ROOT_FIBER_CONTEXT = 2
|
2007-11-08 20:11:49 -05:00
|
|
|
};
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
typedef struct rb_context_struct {
|
2008-10-22 11:12:07 -04:00
|
|
|
enum context_type type;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
VALUE self;
|
2008-11-30 22:00:48 -05:00
|
|
|
int argc;
|
2007-06-02 03:48:29 -04:00
|
|
|
VALUE value;
|
2007-05-23 18:52:19 -04:00
|
|
|
VALUE *vm_stack;
|
2008-11-18 11:19:37 -05:00
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
2009-05-31 21:11:04 -04:00
|
|
|
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) */
|
2008-11-18 11:19:37 -05:00
|
|
|
#endif
|
2007-05-23 18:52:19 -04:00
|
|
|
VALUE *machine_stack;
|
|
|
|
VALUE *machine_stack_src;
|
2007-06-14 04:35:20 -04:00
|
|
|
#ifdef __ia64
|
|
|
|
VALUE *machine_register_stack;
|
|
|
|
VALUE *machine_register_stack_src;
|
|
|
|
int machine_register_stack_size;
|
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_thread_t saved_thread;
|
|
|
|
rb_jmpbuf_t jmpbuf;
|
* compile.c, cont.c, gc.c, insns.def, iseq.c, iseq.h, process.c,
thread.c, vm.c, vm_core.h, vm_dump.c, vm_eval.c,
vm_insnhelper.c, vm_method.c, template/insns_info.inc.tmpl,
tool/instruction.rb: fixed types.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-09-21 16:58:26 -04:00
|
|
|
size_t machine_stack_size;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
} rb_context_t;
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
enum fiber_status {
|
|
|
|
CREATED,
|
|
|
|
RUNNING,
|
2008-12-08 23:25:54 -05:00
|
|
|
TERMINATED
|
2008-10-22 11:12:07 -04:00
|
|
|
};
|
|
|
|
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE && !defined(_WIN32)
|
2010-05-05 14:37:37 -04:00
|
|
|
#define MAX_MAHINE_STACK_CACHE 10
|
|
|
|
static int machine_stack_cache_index = 0;
|
|
|
|
typedef struct machine_stack_cache_struct {
|
|
|
|
void *ptr;
|
|
|
|
size_t size;
|
|
|
|
} machine_stack_cache_t;
|
|
|
|
static machine_stack_cache_t machine_stack_cache[MAX_MAHINE_STACK_CACHE];
|
|
|
|
static machine_stack_cache_t terminated_machine_stack;
|
|
|
|
#endif
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
typedef struct rb_fiber_struct {
|
|
|
|
rb_context_t cont;
|
|
|
|
VALUE prev;
|
|
|
|
enum fiber_status status;
|
|
|
|
struct rb_fiber_struct *prev_fiber;
|
|
|
|
struct rb_fiber_struct *next_fiber;
|
2011-11-08 23:26:39 -05:00
|
|
|
/* If a fiber invokes "transfer",
|
|
|
|
* then this fiber can't "resume" any more after that.
|
|
|
|
* You shouldn't mix "transfer" and "resume".
|
|
|
|
*/
|
|
|
|
int transfered;
|
|
|
|
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
void *fib_handle;
|
|
|
|
#else
|
|
|
|
ucontext_t context;
|
|
|
|
#endif
|
|
|
|
#endif
|
2008-10-22 11:12:07 -04:00
|
|
|
} rb_fiber_t;
|
|
|
|
|
2009-09-08 11:27:31 -04:00
|
|
|
static const rb_data_type_t cont_data_type, fiber_data_type;
|
2007-08-24 22:03:44 -04:00
|
|
|
static VALUE rb_cContinuation;
|
|
|
|
static VALUE rb_cFiber;
|
|
|
|
static VALUE rb_eFiberError;
|
2007-05-23 18:52:19 -04:00
|
|
|
|
|
|
|
#define GetContPtr(obj, ptr) \
|
2010-12-06 15:56:04 -05:00
|
|
|
TypedData_Get_Struct((obj), rb_context_t, &cont_data_type, (ptr))
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2008-11-28 10:23:09 -05:00
|
|
|
#define GetFiberPtr(obj, ptr) do {\
|
2010-12-06 15:56:04 -05:00
|
|
|
TypedData_Get_Struct((obj), rb_fiber_t, &fiber_data_type, (ptr)); \
|
|
|
|
if (!(ptr)) rb_raise(rb_eFiberError, "uninitialized fiber"); \
|
2008-11-28 10:23:09 -05:00
|
|
|
} while(0)
|
2008-10-22 11:12:07 -04:00
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
NOINLINE(static VALUE cont_capture(volatile int *stat));
|
|
|
|
|
2009-09-20 21:13:24 -04:00
|
|
|
#define THREAD_MUST_BE_RUNNING(th) do { \
|
2010-12-06 15:56:04 -05:00
|
|
|
if (!(th)->tag) rb_raise(rb_eThreadError, "not running thread"); \
|
2009-09-20 21:13:24 -04:00
|
|
|
} while (0)
|
2007-05-23 18:52:19 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
cont_mark(void *ptr)
|
|
|
|
{
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_MARK_ENTER("cont");
|
2007-05-23 18:52:19 -04:00
|
|
|
if (ptr) {
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_context_t *cont = ptr;
|
2007-06-02 03:48:29 -04:00
|
|
|
rb_gc_mark(cont->value);
|
2011-10-02 08:08:40 -04:00
|
|
|
rb_thread_mark(&cont->saved_thread);
|
2011-10-05 12:47:33 -04:00
|
|
|
rb_gc_mark(cont->saved_thread.self);
|
2007-05-23 18:52:19 -04:00
|
|
|
|
|
|
|
if (cont->vm_stack) {
|
2008-11-18 11:19:37 -05:00
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
2007-05-23 18:52:19 -04:00
|
|
|
rb_gc_mark_locations(cont->vm_stack,
|
2008-11-18 11:19:37 -05:00
|
|
|
cont->vm_stack + cont->vm_stack_slen + cont->vm_stack_clen);
|
2009-02-05 11:13:54 -05:00
|
|
|
#else
|
2008-11-18 11:19:37 -05:00
|
|
|
rb_gc_mark_localtion(cont->vm_stack,
|
|
|
|
cont->vm_stack, cont->saved_thread.stack_size);
|
|
|
|
#endif
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
2007-05-28 21:59:53 -04:00
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
if (cont->machine_stack) {
|
2010-05-05 14:37:37 -04:00
|
|
|
if (cont->type == CONTINUATION_CONTEXT) {
|
|
|
|
/* cont */
|
|
|
|
rb_gc_mark_locations(cont->machine_stack,
|
|
|
|
cont->machine_stack + cont->machine_stack_size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* fiber */
|
|
|
|
rb_thread_t *th;
|
|
|
|
rb_fiber_t *fib = (rb_fiber_t*)cont;
|
|
|
|
GetThreadPtr(cont->saved_thread.self, th);
|
|
|
|
if ((th->fiber != cont->self) && fib->status == RUNNING) {
|
|
|
|
rb_gc_mark_locations(cont->machine_stack,
|
|
|
|
cont->machine_stack + cont->machine_stack_size);
|
|
|
|
}
|
|
|
|
}
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
2007-06-14 04:35:20 -04:00
|
|
|
#ifdef __ia64
|
|
|
|
if (cont->machine_register_stack) {
|
|
|
|
rb_gc_mark_locations(cont->machine_register_stack,
|
|
|
|
cont->machine_register_stack + cont->machine_register_stack_size);
|
|
|
|
}
|
|
|
|
#endif
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_MARK_LEAVE("cont");
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cont_free(void *ptr)
|
|
|
|
{
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_FREE_ENTER("cont");
|
2007-05-23 18:52:19 -04:00
|
|
|
if (ptr) {
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_context_t *cont = ptr;
|
2008-10-22 11:12:07 -04:00
|
|
|
RUBY_FREE_UNLESS_NULL(cont->saved_thread.stack); fflush(stdout);
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
if (cont->type == CONTINUATION_CONTEXT) {
|
|
|
|
/* cont */
|
|
|
|
RUBY_FREE_UNLESS_NULL(cont->machine_stack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* fiber */
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (GET_THREAD()->fiber != cont->self && cont->type != ROOT_FIBER_CONTEXT) {
|
|
|
|
/* don't delete root fiber handle */
|
|
|
|
rb_fiber_t *fib = (rb_fiber_t*)cont;
|
|
|
|
if (fib->fib_handle) {
|
|
|
|
DeleteFiber(fib->fib_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* not WIN32 */
|
|
|
|
if (GET_THREAD()->fiber != cont->self) {
|
|
|
|
rb_fiber_t *fib = (rb_fiber_t*)cont;
|
|
|
|
if (fib->context.uc_stack.ss_sp) {
|
|
|
|
if (cont->type == ROOT_FIBER_CONTEXT) {
|
|
|
|
rb_bug("Illegal root fiber parameter");
|
|
|
|
}
|
|
|
|
munmap((void*)fib->context.uc_stack.ss_sp, fib->context.uc_stack.ss_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* It may reached here when finalize */
|
|
|
|
/* TODO examine whether it is a bug */
|
|
|
|
/* rb_bug("cont_free: release self"); */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#else /* not FIBER_USE_NATIVE */
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_FREE_UNLESS_NULL(cont->machine_stack);
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
2007-06-14 04:35:20 -04:00
|
|
|
#ifdef __ia64
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_FREE_UNLESS_NULL(cont->machine_register_stack);
|
2007-06-14 04:35:20 -04:00
|
|
|
#endif
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_FREE_UNLESS_NULL(cont->vm_stack);
|
2007-11-08 20:11:49 -05:00
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
/* free rb_cont_t or rb_fiber_t */
|
2007-05-23 18:52:19 -04:00
|
|
|
ruby_xfree(ptr);
|
|
|
|
}
|
2007-06-24 22:44:20 -04:00
|
|
|
RUBY_FREE_LEAVE("cont");
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
|
|
|
|
2009-09-08 11:27:31 -04:00
|
|
|
static size_t
|
2009-09-08 22:22:08 -04:00
|
|
|
cont_memsize(const void *ptr)
|
2009-09-08 11:27:31 -04:00
|
|
|
{
|
2009-09-08 22:26:06 -04:00
|
|
|
const rb_context_t *cont = ptr;
|
2009-09-08 11:27:31 -04:00
|
|
|
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) {
|
2009-09-08 22:07:50 -04:00
|
|
|
size += cont->machine_register_stack_size * sizeof(*cont->machine_register_stack);
|
2009-09-08 11:27:31 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
static void
|
|
|
|
fiber_mark(void *ptr)
|
|
|
|
{
|
|
|
|
RUBY_MARK_ENTER("cont");
|
|
|
|
if (ptr) {
|
|
|
|
rb_fiber_t *fib = ptr;
|
|
|
|
rb_gc_mark(fib->prev);
|
|
|
|
cont_mark(&fib->cont);
|
|
|
|
}
|
|
|
|
RUBY_MARK_LEAVE("cont");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fiber_link_join(rb_fiber_t *fib)
|
|
|
|
{
|
|
|
|
VALUE current_fibval = rb_fiber_current();
|
|
|
|
rb_fiber_t *current_fib;
|
|
|
|
GetFiberPtr(current_fibval, current_fib);
|
|
|
|
|
|
|
|
/* join fiber link */
|
|
|
|
fib->next_fiber = current_fib->next_fiber;
|
|
|
|
fib->prev_fiber = current_fib;
|
|
|
|
current_fib->next_fiber->prev_fiber = fib;
|
|
|
|
current_fib->next_fiber = fib;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fiber_link_remove(rb_fiber_t *fib)
|
|
|
|
{
|
|
|
|
fib->prev_fiber->next_fiber = fib->next_fiber;
|
|
|
|
fib->next_fiber->prev_fiber = fib->prev_fiber;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fiber_free(void *ptr)
|
|
|
|
{
|
|
|
|
RUBY_FREE_ENTER("fiber");
|
|
|
|
if (ptr) {
|
|
|
|
rb_fiber_t *fib = ptr;
|
2009-11-02 04:35:43 -05:00
|
|
|
if (fib->cont.type != ROOT_FIBER_CONTEXT &&
|
|
|
|
fib->cont.saved_thread.local_storage) {
|
2008-10-22 11:12:07 -04:00
|
|
|
st_free_table(fib->cont.saved_thread.local_storage);
|
|
|
|
}
|
|
|
|
fiber_link_remove(fib);
|
|
|
|
|
|
|
|
cont_free(&fib->cont);
|
|
|
|
}
|
|
|
|
RUBY_FREE_LEAVE("fiber");
|
|
|
|
}
|
|
|
|
|
2009-09-08 11:27:31 -04:00
|
|
|
static size_t
|
2009-09-08 22:22:08 -04:00
|
|
|
fiber_memsize(const void *ptr)
|
2009-09-08 11:27:31 -04:00
|
|
|
{
|
2009-09-08 22:26:06 -04:00
|
|
|
const rb_fiber_t *fib = ptr;
|
2009-09-08 11:27:31 -04:00
|
|
|
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);
|
|
|
|
}
|
2010-10-03 19:00:21 -04:00
|
|
|
return size;
|
2009-09-08 11:27:31 -04:00
|
|
|
}
|
|
|
|
|
2012-02-15 09:00:11 -05:00
|
|
|
VALUE
|
|
|
|
rb_obj_is_fiber(VALUE obj)
|
|
|
|
{
|
|
|
|
if (rb_typeddata_is_kind_of(obj, &fiber_data_type)) {
|
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
static void
|
|
|
|
cont_save_machine_stack(rb_thread_t *th, rb_context_t *cont)
|
2007-05-23 18:52:19 -04:00
|
|
|
{
|
2009-05-31 21:11:04 -04:00
|
|
|
size_t size;
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2007-07-14 03:19:59 -04:00
|
|
|
SET_MACHINE_STACK_END(&th->machine_stack_end);
|
2007-06-14 04:35:20 -04:00
|
|
|
#ifdef __ia64
|
|
|
|
th->machine_register_stack_end = rb_ia64_bsp();
|
|
|
|
#endif
|
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
if (th->machine_stack_start > th->machine_stack_end) {
|
|
|
|
size = cont->machine_stack_size = th->machine_stack_start - th->machine_stack_end;
|
|
|
|
cont->machine_stack_src = th->machine_stack_end;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
size = cont->machine_stack_size = th->machine_stack_end - th->machine_stack_start;
|
|
|
|
cont->machine_stack_src = th->machine_stack_start;
|
|
|
|
}
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
if (cont->machine_stack) {
|
|
|
|
REALLOC_N(cont->machine_stack, VALUE, size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cont->machine_stack = ALLOC_N(VALUE, size);
|
|
|
|
}
|
|
|
|
|
2007-12-23 14:03:23 -05:00
|
|
|
FLUSH_REGISTER_WINDOWS;
|
2007-05-23 18:52:19 -04:00
|
|
|
MEMCPY(cont->machine_stack, cont->machine_stack_src, VALUE, size);
|
2007-06-14 04:35:20 -04:00
|
|
|
|
|
|
|
#ifdef __ia64
|
|
|
|
rb_ia64_flushrs();
|
|
|
|
size = cont->machine_register_stack_size = th->machine_register_stack_end - th->machine_register_stack_start;
|
|
|
|
cont->machine_register_stack_src = th->machine_register_stack_start;
|
|
|
|
if (cont->machine_register_stack) {
|
|
|
|
REALLOC_N(cont->machine_register_stack, VALUE, size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cont->machine_register_stack = ALLOC_N(VALUE, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
MEMCPY(cont->machine_register_stack, cont->machine_register_stack_src, VALUE, size);
|
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2009-09-08 11:27:31 -04:00
|
|
|
static const rb_data_type_t cont_data_type = {
|
|
|
|
"continuation",
|
2010-07-18 03:31:54 -04:00
|
|
|
{cont_mark, cont_free, cont_memsize,},
|
2009-09-08 11:27:31 -04:00
|
|
|
};
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
static void
|
2011-06-14 08:57:50 -04:00
|
|
|
cont_save_thread(rb_context_t *cont, rb_thread_t *th)
|
2008-10-22 11:12:07 -04:00
|
|
|
{
|
|
|
|
/* save thread context */
|
|
|
|
cont->saved_thread = *th;
|
2011-06-14 08:57:50 -04:00
|
|
|
/* saved_thread->machine_stack_(start|end) should be NULL */
|
|
|
|
/* because it may happen GC afterward */
|
|
|
|
cont->saved_thread.machine_stack_start = 0;
|
|
|
|
cont->saved_thread.machine_stack_end = 0;
|
|
|
|
#ifdef __ia64
|
2011-07-19 03:39:54 -04:00
|
|
|
cont->saved_thread.machine_register_stack_start = 0;
|
|
|
|
cont->saved_thread.machine_register_stack_end = 0;
|
2011-06-14 08:57:50 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cont_init(rb_context_t *cont, rb_thread_t *th)
|
|
|
|
{
|
|
|
|
/* save thread context */
|
|
|
|
cont_save_thread(cont, th);
|
2009-11-02 04:35:43 -05:00
|
|
|
cont->saved_thread.local_storage = 0;
|
2008-10-22 11:12:07 -04:00
|
|
|
}
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
static rb_context_t *
|
|
|
|
cont_new(VALUE klass)
|
|
|
|
{
|
|
|
|
rb_context_t *cont;
|
|
|
|
volatile VALUE contval;
|
2009-09-20 21:13:24 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2009-09-20 21:13:24 -04:00
|
|
|
THREAD_MUST_BE_RUNNING(th);
|
2009-09-08 11:27:31 -04:00
|
|
|
contval = TypedData_Make_Struct(klass, rb_context_t, &cont_data_type, cont);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
cont->self = contval;
|
2009-09-20 21:13:24 -04:00
|
|
|
cont_init(cont, th);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
cont_capture(volatile int *stat)
|
|
|
|
{
|
2007-05-30 01:56:13 -04:00
|
|
|
rb_context_t *cont;
|
2007-07-03 00:53:05 -04:00
|
|
|
rb_thread_t *th = GET_THREAD(), *sth;
|
2007-06-14 04:35:20 -04:00
|
|
|
volatile VALUE contval;
|
2007-05-30 01:56:13 -04:00
|
|
|
|
2009-09-20 21:13:24 -04:00
|
|
|
THREAD_MUST_BE_RUNNING(th);
|
* vm.c: add a prefix "rb_" to exposed functions
vm_get_ruby_level_next_cfp(), rb_vm_make_env_object(),
vm_stack_to_heap(), vm_make_proc(), vm_invoke_proc(),
vm_get_sourceline(), vm_cref(), vm_localjump_error(),
vm_make_jump_tag_but_local_jump(), vm_jump_tag_but_local_jump().
This changes may affect only core because most of renamed functions
require a pointer of not-exposed struct such as rb_thread_t or NODE.
In short, they are core functions.
* cont.c, eval.c, eval_intern.h, load.c, proc.c, thread.c,
vm_core.h, vm_dump.c, vm_eval.c, vm_exec.c, vm_insnhelper.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-18 21:38:11 -05:00
|
|
|
rb_vm_stack_to_heap(th);
|
2007-08-24 22:03:44 -04:00
|
|
|
cont = cont_new(rb_cContinuation);
|
2007-06-14 04:35:20 -04:00
|
|
|
contval = cont->self;
|
2007-07-03 00:53:05 -04:00
|
|
|
sth = &cont->saved_thread;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2008-11-18 11:19:37 -05:00
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
|
|
|
cont->vm_stack_slen = th->cfp->sp + th->mark_stack_len - th->stack;
|
|
|
|
cont->vm_stack_clen = th->stack + th->stack_size - (VALUE*)th->cfp;
|
|
|
|
cont->vm_stack = ALLOC_N(VALUE, cont->vm_stack_slen + cont->vm_stack_clen);
|
|
|
|
MEMCPY(cont->vm_stack, th->stack, VALUE, cont->vm_stack_slen);
|
|
|
|
MEMCPY(cont->vm_stack + cont->vm_stack_slen, (VALUE*)th->cfp, VALUE, cont->vm_stack_clen);
|
2009-02-05 11:13:54 -05:00
|
|
|
#else
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
cont->vm_stack = ALLOC_N(VALUE, th->stack_size);
|
|
|
|
MEMCPY(cont->vm_stack, th->stack, VALUE, th->stack_size);
|
2008-11-18 11:19:37 -05:00
|
|
|
#endif
|
2007-07-03 00:53:05 -04:00
|
|
|
sth->stack = 0;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
|
|
|
cont_save_machine_stack(th, cont);
|
2007-05-23 18:52:19 -04:00
|
|
|
|
|
|
|
if (ruby_setjmp(cont->jmpbuf)) {
|
2011-06-22 07:00:24 -04:00
|
|
|
volatile VALUE value;
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2007-06-02 03:48:29 -04:00
|
|
|
value = cont->value;
|
2009-05-31 22:21:31 -04:00
|
|
|
if (cont->argc == -1) rb_exc_raise(value);
|
2007-06-02 03:48:29 -04:00
|
|
|
cont->value = Qnil;
|
2007-05-23 18:52:19 -04:00
|
|
|
*stat = 1;
|
2007-06-02 03:48:29 -04:00
|
|
|
return value;
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*stat = 0;
|
* bignum.c (big_rshift), compile.c (validate_label,
iseq_build_from_ary_exception), cont.c (cont_capture), dir.c
(dir_open_dir), gc.c (objspace_each_objects), io.c (pipe_open)
(rb_io_advise), parse.y (parser_compile_string)
(rb_parser_compile_file), proc.c (binding_free), process.c
(rb_proc_exec_n, rb_seteuid_core, proc_setegid, rb_setegid_core)
(p_uid_exchange, p_gid_exchange), regparse.c (strdup_with_null),
signal.c (sig_dfl), vm.c (rb_iseq_eval, rb_iseq_eval_main),
vm_insnhelper.c (vm_expandarray): suppress
unused-but-set-variable warnings.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33951 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-12-05 04:57:00 -05:00
|
|
|
return contval;
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-05-05 14:37:37 -04:00
|
|
|
cont_restore_thread(rb_context_t *cont)
|
2007-05-23 18:52:19 -04:00
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD(), *sth = &cont->saved_thread;
|
|
|
|
|
|
|
|
/* restore thread context */
|
2007-11-08 20:11:49 -05:00
|
|
|
if (cont->type == CONTINUATION_CONTEXT) {
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
/* continuation */
|
2007-08-06 12:41:17 -04:00
|
|
|
VALUE fib;
|
2007-06-06 23:48:13 -04:00
|
|
|
|
2007-05-28 21:59:53 -04:00
|
|
|
th->fiber = sth->fiber;
|
2007-08-06 12:41:17 -04:00
|
|
|
fib = th->fiber ? th->fiber : th->root_fiber;
|
2007-06-06 14:19:42 -04:00
|
|
|
|
2007-08-06 12:41:17 -04:00
|
|
|
if (fib) {
|
2009-09-08 21:51:49 -04:00
|
|
|
rb_fiber_t *fcont;
|
|
|
|
GetFiberPtr(fib, fcont);
|
|
|
|
th->stack_size = fcont->cont.saved_thread.stack_size;
|
|
|
|
th->stack = fcont->cont.saved_thread.stack;
|
2007-06-06 14:19:42 -04:00
|
|
|
}
|
2008-11-18 11:19:37 -05:00
|
|
|
#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);
|
2009-02-05 11:13:54 -05:00
|
|
|
#else
|
2007-06-06 14:19:42 -04:00
|
|
|
MEMCPY(th->stack, cont->vm_stack, VALUE, sth->stack_size);
|
2008-11-18 11:19:37 -05:00
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
2007-11-08 20:11:49 -05:00
|
|
|
else {
|
|
|
|
/* fiber */
|
|
|
|
th->stack = sth->stack;
|
|
|
|
th->stack_size = sth->stack_size;
|
|
|
|
th->local_storage = sth->local_storage;
|
|
|
|
th->fiber = cont->self;
|
|
|
|
}
|
2007-05-23 18:52:19 -04:00
|
|
|
|
|
|
|
th->cfp = sth->cfp;
|
|
|
|
th->safe_level = sth->safe_level;
|
|
|
|
th->raised_flag = sth->raised_flag;
|
|
|
|
th->state = sth->state;
|
|
|
|
th->status = sth->status;
|
|
|
|
th->tag = sth->tag;
|
2010-01-25 13:22:58 -05:00
|
|
|
th->protect_tag = sth->protect_tag;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
th->errinfo = sth->errinfo;
|
|
|
|
th->first_proc = sth->first_proc;
|
2010-05-05 14:37:37 -04:00
|
|
|
}
|
|
|
|
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
static void
|
|
|
|
fiber_set_stack_location(void)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2010-05-15 22:46:16 -04:00
|
|
|
VALUE *ptr;
|
2010-05-08 12:15:20 -04:00
|
|
|
|
|
|
|
SET_MACHINE_STACK_END(&ptr);
|
2010-10-12 09:18:21 -04:00
|
|
|
th->machine_stack_start = (void*)(((VALUE)ptr & RB_PAGE_MASK) + STACK_UPPER((void *)&ptr, 0, RB_PAGE_SIZE));
|
2010-05-05 14:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VOID CALLBACK
|
|
|
|
fiber_entry(void *arg)
|
|
|
|
{
|
|
|
|
fiber_set_stack_location();
|
|
|
|
rb_fiber_start();
|
|
|
|
}
|
2011-07-06 08:24:25 -04:00
|
|
|
#else /* _WIN32 */
|
|
|
|
|
2011-07-07 05:09:44 -04:00
|
|
|
/*
|
|
|
|
* FreeBSD require a first (i.e. addr) argument of mmap(2) is not NULL
|
|
|
|
* if MAP_STACK is passed.
|
2011-08-29 20:51:46 -04:00
|
|
|
* http://www.FreeBSD.org/cgi/query-pr.cgi?pr=158755
|
2011-07-07 05:09:44 -04:00
|
|
|
*/
|
2011-08-29 20:51:46 -04:00
|
|
|
#if defined(MAP_STACK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
|
2011-07-06 08:24:25 -04:00
|
|
|
#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON | MAP_STACK)
|
2010-05-05 14:37:37 -04:00
|
|
|
#else
|
2011-07-06 08:24:25 -04:00
|
|
|
#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON)
|
|
|
|
#endif
|
|
|
|
|
2011-07-06 08:31:53 -04:00
|
|
|
static char*
|
2010-05-05 14:37:37 -04:00
|
|
|
fiber_machine_stack_alloc(size_t size)
|
|
|
|
{
|
2011-07-06 08:31:53 -04:00
|
|
|
char *ptr;
|
2010-05-05 14:37:37 -04:00
|
|
|
|
|
|
|
if (machine_stack_cache_index > 0) {
|
|
|
|
if (machine_stack_cache[machine_stack_cache_index - 1].size == (size / sizeof(VALUE))) {
|
|
|
|
ptr = machine_stack_cache[machine_stack_cache_index - 1].ptr;
|
|
|
|
machine_stack_cache_index--;
|
|
|
|
machine_stack_cache[machine_stack_cache_index].ptr = NULL;
|
|
|
|
machine_stack_cache[machine_stack_cache_index].size = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
/* TODO handle multiple machine stack size */
|
|
|
|
rb_bug("machine_stack_cache size is not canonicalized");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-06-15 20:12:55 -04:00
|
|
|
void *page;
|
2010-05-08 12:15:20 -04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2011-07-06 08:29:09 -04:00
|
|
|
|
2011-07-06 08:24:25 -04:00
|
|
|
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
|
2011-07-06 08:22:56 -04:00
|
|
|
if (ptr == MAP_FAILED) {
|
2010-05-05 14:37:37 -04:00
|
|
|
rb_raise(rb_eFiberError, "can't alloc machine stack to fiber");
|
|
|
|
}
|
2011-07-06 08:29:09 -04:00
|
|
|
|
|
|
|
/* guard page setup */
|
2011-07-06 08:31:53 -04:00
|
|
|
page = ptr + STACK_DIR_UPPER(size - RB_PAGE_SIZE, 0);
|
2011-07-06 08:29:09 -04:00
|
|
|
if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
|
2010-05-08 12:15:20 -04:00
|
|
|
rb_raise(rb_eFiberError, "mprotect failed");
|
2010-05-05 14:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
fiber_initialize_machine_stack_context(rb_fiber_t *fib, size_t size)
|
|
|
|
{
|
|
|
|
rb_thread_t *sth = &fib->cont.saved_thread;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
fib->fib_handle = CreateFiberEx(size - 1, size, 0, fiber_entry, NULL);
|
2010-05-09 03:28:17 -04:00
|
|
|
if (!fib->fib_handle) {
|
|
|
|
/* try to release unnecessary fibers & retry to create */
|
|
|
|
rb_gc();
|
|
|
|
fib->fib_handle = CreateFiberEx(size - 1, size, 0, fiber_entry, NULL);
|
|
|
|
if (!fib->fib_handle) {
|
|
|
|
rb_raise(rb_eFiberError, "can't create fiber");
|
|
|
|
}
|
|
|
|
}
|
2011-07-06 08:29:09 -04:00
|
|
|
sth->machine_stack_maxsize = size;
|
2010-05-05 14:37:37 -04:00
|
|
|
#else /* not WIN32 */
|
|
|
|
ucontext_t *context = &fib->context;
|
2011-07-06 08:31:53 -04:00
|
|
|
char *ptr;
|
2010-05-08 12:15:20 -04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2010-05-05 14:37:37 -04:00
|
|
|
|
|
|
|
getcontext(context);
|
|
|
|
ptr = fiber_machine_stack_alloc(size);
|
|
|
|
context->uc_link = NULL;
|
2011-07-06 08:31:53 -04:00
|
|
|
context->uc_stack.ss_sp = ptr;
|
2010-05-05 14:37:37 -04:00
|
|
|
context->uc_stack.ss_size = size;
|
|
|
|
makecontext(context, rb_fiber_start, 0);
|
2011-07-06 08:31:53 -04:00
|
|
|
sth->machine_stack_start = (VALUE*)(ptr + STACK_DIR_UPPER(0, size));
|
2011-07-06 08:29:09 -04:00
|
|
|
sth->machine_stack_maxsize = size - RB_PAGE_SIZE;
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
|
|
|
#ifdef __ia64
|
|
|
|
sth->machine_register_stack_maxsize = sth->machine_stack_maxsize;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
NOINLINE(static void fiber_setcontext(rb_fiber_t *newfib, rb_fiber_t *oldfib));
|
|
|
|
|
|
|
|
static void
|
|
|
|
fiber_setcontext(rb_fiber_t *newfib, rb_fiber_t *oldfib)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD(), *sth = &newfib->cont.saved_thread;
|
|
|
|
|
|
|
|
if (newfib->status != RUNNING) {
|
2011-07-08 00:09:00 -04:00
|
|
|
fiber_initialize_machine_stack_context(newfib, FIBER_MACHINE_STACK_ALLOCATION_SIZE);
|
2010-05-05 14:37:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* restore thread context */
|
|
|
|
cont_restore_thread(&newfib->cont);
|
|
|
|
th->machine_stack_maxsize = sth->machine_stack_maxsize;
|
|
|
|
if (sth->machine_stack_end && (newfib != oldfib)) {
|
|
|
|
rb_bug("fiber_setcontext: sth->machine_stack_end has non zero value");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save oldfib's machine stack */
|
|
|
|
if (oldfib->status != TERMINATED) {
|
2010-10-12 09:18:21 -04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2010-05-06 05:13:54 -04:00
|
|
|
SET_MACHINE_STACK_END(&th->machine_stack_end);
|
2010-05-08 12:15:20 -04:00
|
|
|
if (STACK_DIR_UPPER(0, 1)) {
|
|
|
|
oldfib->cont.machine_stack_size = th->machine_stack_start - th->machine_stack_end;
|
|
|
|
oldfib->cont.machine_stack = th->machine_stack_end;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
oldfib->cont.machine_stack_size = th->machine_stack_end - th->machine_stack_start;
|
|
|
|
oldfib->cont.machine_stack = th->machine_stack_start;
|
2010-05-05 14:37:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* exchange machine_stack_start between oldfib and newfib */
|
|
|
|
oldfib->cont.saved_thread.machine_stack_start = th->machine_stack_start;
|
|
|
|
th->machine_stack_start = sth->machine_stack_start;
|
|
|
|
/* oldfib->machine_stack_end should be NULL */
|
|
|
|
oldfib->cont.saved_thread.machine_stack_end = 0;
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (!newfib->context.uc_stack.ss_sp && th->root_fiber != newfib->cont.self) {
|
|
|
|
rb_bug("non_root_fiber->context.uc_stac.ss_sp should not be NULL");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* swap machine context */
|
|
|
|
#ifdef _WIN32
|
|
|
|
SwitchToFiber(newfib->fib_handle);
|
|
|
|
#else
|
2010-05-21 14:55:50 -04:00
|
|
|
swapcontext(&oldfib->context, &newfib->context);
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NOINLINE(NORETURN(static void cont_restore_1(rb_context_t *)));
|
|
|
|
|
|
|
|
static void
|
|
|
|
cont_restore_1(rb_context_t *cont)
|
|
|
|
{
|
|
|
|
cont_restore_thread(cont);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
/* restore machine stack */
|
2007-07-08 13:19:01 -04:00
|
|
|
#ifdef _M_AMD64
|
|
|
|
{
|
|
|
|
/* workaround for x64 SEH */
|
|
|
|
jmp_buf buf;
|
|
|
|
setjmp(buf);
|
|
|
|
((_JUMP_BUFFER*)(&cont->jmpbuf))->Frame =
|
|
|
|
((_JUMP_BUFFER*)(&buf))->Frame;
|
|
|
|
}
|
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
if (cont->machine_stack_src) {
|
2007-12-23 14:03:23 -05:00
|
|
|
FLUSH_REGISTER_WINDOWS;
|
2012-01-12 00:12:03 -05:00
|
|
|
MEMCPY(cont->machine_stack_src, cont->machine_stack,
|
|
|
|
VALUE, cont->machine_stack_size);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2007-06-14 04:35:20 -04:00
|
|
|
#ifdef __ia64
|
|
|
|
if (cont->machine_register_stack_src) {
|
|
|
|
MEMCPY(cont->machine_register_stack_src, cont->machine_register_stack,
|
|
|
|
VALUE, cont->machine_register_stack_size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
ruby_longjmp(cont->jmpbuf, 1);
|
|
|
|
}
|
|
|
|
|
2007-05-27 21:25:55 -04:00
|
|
|
NORETURN(NOINLINE(static void cont_restore_0(rb_context_t *, VALUE *)));
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2007-06-14 04:35:20 -04:00
|
|
|
#ifdef __ia64
|
|
|
|
#define C(a) rse_##a##0, rse_##a##1, rse_##a##2, rse_##a##3, rse_##a##4
|
|
|
|
#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);
|
2009-01-06 05:09:54 -05:00
|
|
|
#if 0
|
|
|
|
{/* the above lines make cc-mode.el confused so much */}
|
|
|
|
#endif
|
2007-06-14 04:35:20 -04:00
|
|
|
int rb_dummy_false = 0;
|
2009-01-14 00:43:49 -05:00
|
|
|
NORETURN(NOINLINE(static void register_stack_extend(rb_context_t *, VALUE *, VALUE *)));
|
2007-06-14 04:35:20 -04:00
|
|
|
static void
|
2009-01-06 05:09:54 -05:00
|
|
|
register_stack_extend(rb_context_t *cont, VALUE *vp, VALUE *curr_bsp)
|
2007-06-14 04:35:20 -04:00
|
|
|
{
|
|
|
|
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) =
|
|
|
|
E(k) = E(l) = E(m) = E(n) = E(o) =
|
|
|
|
E(p) = E(q) = E(r) = E(s) = E(t) = 0;
|
|
|
|
E(a) = E(b) = E(c) = E(d) = E(e) =
|
|
|
|
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) {
|
2009-01-06 05:09:54 -05:00
|
|
|
register_stack_extend(cont, vp, (VALUE*)rb_ia64_bsp());
|
2007-06-14 04:35:20 -04:00
|
|
|
}
|
2009-01-06 05:09:54 -05:00
|
|
|
cont_restore_0(cont, vp);
|
2007-06-14 04:35:20 -04:00
|
|
|
}
|
|
|
|
#undef C
|
|
|
|
#undef E
|
|
|
|
#endif
|
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
static void
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
cont_restore_0(rb_context_t *cont, VALUE *addr_in_prev_frame)
|
2007-05-23 18:52:19 -04:00
|
|
|
{
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
if (cont->machine_stack_src) {
|
2009-01-16 21:11:38 -05:00
|
|
|
#ifdef HAVE_ALLOCA
|
|
|
|
#define STACK_PAD_SIZE 1
|
|
|
|
#else
|
2007-05-23 18:52:19 -04:00
|
|
|
#define STACK_PAD_SIZE 1024
|
2009-01-16 21:11:38 -05:00
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
VALUE space[STACK_PAD_SIZE];
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2009-01-06 05:09:54 -05:00
|
|
|
#if !STACK_GROW_DIRECTION
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
if (addr_in_prev_frame > &space[0]) {
|
|
|
|
/* Stack grows downward */
|
2009-01-06 05:09:54 -05:00
|
|
|
#endif
|
|
|
|
#if STACK_GROW_DIRECTION <= 0
|
2009-01-16 21:11:38 -05:00
|
|
|
volatile VALUE *const end = cont->machine_stack_src;
|
|
|
|
if (&space[0] > end) {
|
2009-01-06 05:09:54 -05:00
|
|
|
# ifdef HAVE_ALLOCA
|
2009-01-16 21:11:38 -05:00
|
|
|
volatile VALUE *sp = ALLOCA_N(VALUE, &space[0] - end);
|
2012-01-12 00:12:03 -05:00
|
|
|
space[0] = *sp;
|
2009-01-06 05:09:54 -05:00
|
|
|
# else
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
cont_restore_0(cont, &space[0]);
|
2009-01-06 05:09:54 -05:00
|
|
|
# endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
2009-01-06 05:09:54 -05:00
|
|
|
#endif
|
|
|
|
#if !STACK_GROW_DIRECTION
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
else {
|
|
|
|
/* Stack grows upward */
|
2009-01-06 05:09:54 -05:00
|
|
|
#endif
|
|
|
|
#if STACK_GROW_DIRECTION >= 0
|
2009-01-16 21:11:38 -05:00
|
|
|
volatile VALUE *const end = cont->machine_stack_src + cont->machine_stack_size;
|
|
|
|
if (&space[STACK_PAD_SIZE] < end) {
|
2009-01-06 05:09:54 -05:00
|
|
|
# ifdef HAVE_ALLOCA
|
2009-01-16 21:11:38 -05:00
|
|
|
volatile VALUE *sp = ALLOCA_N(VALUE, end - &space[STACK_PAD_SIZE]);
|
2012-01-12 00:12:03 -05:00
|
|
|
space[0] = *sp;
|
2009-01-06 05:09:54 -05:00
|
|
|
# else
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
|
2009-01-06 05:09:54 -05:00
|
|
|
# endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
2009-01-06 05:09:54 -05:00
|
|
|
#endif
|
|
|
|
#if !STACK_GROW_DIRECTION
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
cont_restore_1(cont);
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
2009-01-06 05:09:54 -05:00
|
|
|
#ifdef __ia64
|
2010-12-06 15:56:04 -05:00
|
|
|
#define cont_restore_0(cont, vp) register_stack_extend((cont), (vp), (VALUE*)rb_ia64_bsp());
|
2009-01-06 05:09:54 -05:00
|
|
|
#endif
|
2007-05-23 18:52:19 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: Continuation
|
|
|
|
*
|
2010-10-26 20:26:29 -04:00
|
|
|
* Continuation objects are generated by <code>Kernel#callcc</code>,
|
|
|
|
* after having <code>require</code>d <i>continuation</i>. They hold
|
|
|
|
* a return address and execution 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).
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* For instance:
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2010-10-26 20:26:29 -04:00
|
|
|
* require "continuation"
|
2007-05-23 18:52:19 -04:00
|
|
|
* arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
|
2010-10-26 20:26:29 -04:00
|
|
|
* callcc{|cc| $cc = cc}
|
2007-05-23 18:52:19 -04:00
|
|
|
* puts(message = arr.shift)
|
|
|
|
* $cc.call unless message =~ /Max/
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* <em>produces:</em>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* Freddie
|
|
|
|
* Herbie
|
|
|
|
* Ron
|
|
|
|
* Max
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* This (somewhat contrived) example allows the inner loop to abandon
|
|
|
|
* processing early:
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2010-10-26 20:26:29 -04:00
|
|
|
* require "continuation"
|
2007-05-23 18:52:19 -04:00
|
|
|
* 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
|
|
|
|
* }
|
2010-10-26 20:26:29 -04:00
|
|
|
* puts
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* <em>produces:</em>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* 0: 0 1 2 3 4
|
|
|
|
* 1: 5 6 7 8 9
|
|
|
|
* 2: 10 11 12 13 14
|
|
|
|
* 3: 15 16
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2010-05-17 17:07:33 -04:00
|
|
|
* callcc {|cont| block } -> obj
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2010-10-26 20:26:29 -04:00
|
|
|
* Generates a <code>Continuation</code> object, which it passes to
|
|
|
|
* the associated block. You need to <code>require
|
|
|
|
* 'continuation'</code> before using this method. 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 <em>cont</em><code>.call</code>. See
|
|
|
|
* class <code>Continuation</code> for more details. Also see
|
|
|
|
* <code>Kernel::throw</code> for an alternative mechanism for
|
|
|
|
* unwinding a call stack.
|
2007-05-23 18:52:19 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_callcc(VALUE self)
|
|
|
|
{
|
|
|
|
volatile int called;
|
|
|
|
volatile VALUE val = cont_capture(&called);
|
|
|
|
|
|
|
|
if (called) {
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return rb_yield(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
static VALUE
|
|
|
|
make_passing_arg(int argc, VALUE *argv)
|
|
|
|
{
|
|
|
|
switch(argc) {
|
|
|
|
case 0:
|
|
|
|
return Qnil;
|
|
|
|
case 1:
|
|
|
|
return argv[0];
|
|
|
|
default:
|
|
|
|
return rb_ary_new4(argc, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cont.call(args, ...)
|
|
|
|
* cont[args, ...]
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* 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.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2007-05-23 18:52:19 -04:00
|
|
|
* callcc {|cont| cont.call } #=> nil
|
|
|
|
* callcc {|cont| cont.call 1 } #=> 1
|
|
|
|
* callcc {|cont| cont.call 1, 2, 3 } #=> [1, 2, 3]
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_cont_call(int argc, VALUE *argv, VALUE contval)
|
|
|
|
{
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_context_t *cont;
|
2007-05-23 18:52:19 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
GetContPtr(contval, cont);
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
if (cont->saved_thread.self != th->self) {
|
2007-05-23 18:52:19 -04:00
|
|
|
rb_raise(rb_eRuntimeError, "continuation called across threads");
|
|
|
|
}
|
2010-01-25 13:22:58 -05:00
|
|
|
if (cont->saved_thread.protect_tag != th->protect_tag) {
|
|
|
|
rb_raise(rb_eRuntimeError, "continuation called across stack rewinding barrier");
|
|
|
|
}
|
2007-06-05 21:55:09 -04:00
|
|
|
if (cont->saved_thread.fiber) {
|
2009-09-08 21:51:49 -04:00
|
|
|
rb_fiber_t *fcont;
|
|
|
|
GetFiberPtr(cont->saved_thread.fiber, fcont);
|
2007-06-05 21:55:09 -04:00
|
|
|
|
2007-06-14 23:20:13 -04:00
|
|
|
if (th->fiber != cont->saved_thread.fiber) {
|
|
|
|
rb_raise(rb_eRuntimeError, "continuation called across fiber");
|
|
|
|
}
|
2007-06-05 21:55:09 -04:00
|
|
|
}
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2008-11-30 22:00:48 -05:00
|
|
|
cont->argc = argc;
|
2007-06-02 03:48:29 -04:00
|
|
|
cont->value = make_passing_arg(argc, argv);
|
2007-05-23 18:52:19 -04:00
|
|
|
|
2007-08-23 03:55:37 -04:00
|
|
|
cont_restore_0(cont, &contval);
|
2007-05-23 18:52:19 -04:00
|
|
|
return Qnil; /* unreachable */
|
|
|
|
}
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
/*********/
|
|
|
|
/* fiber */
|
|
|
|
/*********/
|
|
|
|
|
2008-12-26 19:25:47 -05:00
|
|
|
/*
|
|
|
|
* Document-class: Fiber
|
|
|
|
*
|
|
|
|
* Fibers are primitives for implementing light weight cooperative
|
2009-02-22 09:23:33 -05:00
|
|
|
* 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.
|
2008-12-26 19:25:47 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2009-02-22 09:23:33 -05:00
|
|
|
* 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
|
2008-12-26 19:25:47 -05:00
|
|
|
* (the caller of the <code>Fiber#resume</code>).
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
|
|
|
* Upon yielding or termination the Fiber returns the value of the last
|
2008-12-26 19:25:47 -05:00
|
|
|
* executed expression
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* For instance:
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* fiber = Fiber.new do
|
|
|
|
* Fiber.yield 1
|
|
|
|
* 2
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* puts fiber.resume
|
|
|
|
* puts fiber.resume
|
|
|
|
* puts fiber.resume
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* <em>produces</em>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
* FiberError: dead fiber called
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2009-11-03 12:46:28 -05:00
|
|
|
* The <code>Fiber#resume</code> method accepts an arbitrary number of
|
2008-12-26 19:25:47 -05:00
|
|
|
* parameters, if it is the first call to <code>resume</code> then they
|
2008-12-28 17:16:19 -05:00
|
|
|
* will be passed as block arguments. Otherwise they will be the return
|
2008-12-26 19:25:47 -05:00
|
|
|
* value of the call to <code>Fiber.yield</code>
|
|
|
|
*
|
|
|
|
* Example:
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* 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>
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* 12
|
|
|
|
* 14
|
|
|
|
* FiberError: dead fiber called
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-08-25 04:51:59 -04:00
|
|
|
#define FIBER_VM_STACK_SIZE (4 * 1024)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2009-09-08 11:27:31 -04:00
|
|
|
static const rb_data_type_t fiber_data_type = {
|
|
|
|
"fiber",
|
2010-07-18 03:31:54 -04:00
|
|
|
{fiber_mark, fiber_free, fiber_memsize,},
|
2009-09-08 11:27:31 -04:00
|
|
|
};
|
|
|
|
|
2008-11-28 10:23:09 -05:00
|
|
|
static VALUE
|
2007-11-08 20:11:49 -05:00
|
|
|
fiber_alloc(VALUE klass)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2009-09-08 11:27:31 -04:00
|
|
|
return TypedData_Wrap_Struct(klass, &fiber_data_type, 0);
|
2008-11-28 10:23:09 -05:00
|
|
|
}
|
2007-11-08 20:11:49 -05:00
|
|
|
|
2008-11-28 10:23:09 -05:00
|
|
|
static rb_fiber_t*
|
|
|
|
fiber_t_alloc(VALUE fibval)
|
|
|
|
{
|
2009-09-20 21:13:24 -04:00
|
|
|
rb_fiber_t *fib;
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2008-11-28 10:23:09 -05:00
|
|
|
|
2010-11-03 13:08:35 -04:00
|
|
|
if (DATA_PTR(fibval) != 0) {
|
|
|
|
rb_raise(rb_eRuntimeError, "cannot initialize twice");
|
|
|
|
}
|
|
|
|
|
2009-09-20 21:13:24 -04:00
|
|
|
THREAD_MUST_BE_RUNNING(th);
|
|
|
|
fib = ALLOC(rb_fiber_t);
|
2008-11-28 10:23:09 -05:00
|
|
|
memset(fib, 0, sizeof(rb_fiber_t));
|
2008-10-22 11:12:07 -04:00
|
|
|
fib->cont.self = fibval;
|
|
|
|
fib->cont.type = FIBER_CONTEXT;
|
2009-09-20 21:13:24 -04:00
|
|
|
cont_init(&fib->cont, th);
|
2008-10-22 11:12:07 -04:00
|
|
|
fib->prev = Qnil;
|
|
|
|
fib->status = CREATED;
|
2007-11-08 20:11:49 -05:00
|
|
|
|
2008-11-28 10:23:09 -05:00
|
|
|
DATA_PTR(fibval) = fib;
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
return fib;
|
2007-11-08 20:11:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-11-28 10:23:09 -05:00
|
|
|
fiber_init(VALUE fibval, VALUE proc)
|
2007-11-08 20:11:49 -05:00
|
|
|
{
|
2008-11-28 10:23:09 -05:00
|
|
|
rb_fiber_t *fib = fiber_t_alloc(fibval);
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_context_t *cont = &fib->cont;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_thread_t *th = &cont->saved_thread;
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
/* initialize cont */
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
cont->vm_stack = 0;
|
|
|
|
|
|
|
|
th->stack = 0;
|
2009-11-02 04:35:43 -05:00
|
|
|
th->stack_size = 0;
|
|
|
|
|
|
|
|
fiber_link_join(fib);
|
|
|
|
|
2007-08-25 04:51:59 -04:00
|
|
|
th->stack_size = FIBER_VM_STACK_SIZE;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
th->stack = ALLOC_N(VALUE, th->stack_size);
|
2007-06-25 14:39:23 -04:00
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
th->cfp = (void *)(th->stack + th->stack_size);
|
|
|
|
th->cfp--;
|
|
|
|
th->cfp->pc = 0;
|
|
|
|
th->cfp->sp = th->stack + 1;
|
|
|
|
th->cfp->bp = 0;
|
|
|
|
th->cfp->lfp = th->stack;
|
|
|
|
*th->cfp->lfp = 0;
|
|
|
|
th->cfp->dfp = th->stack;
|
|
|
|
th->cfp->self = Qnil;
|
2007-07-05 06:49:45 -04:00
|
|
|
th->cfp->flag = 0;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
th->cfp->iseq = 0;
|
|
|
|
th->cfp->proc = 0;
|
|
|
|
th->cfp->block_iseq = 0;
|
2010-05-05 13:51:21 -04:00
|
|
|
th->cfp->me = 0;
|
2007-06-25 14:47:03 -04:00
|
|
|
th->tag = 0;
|
2007-09-28 15:04:45 -04:00
|
|
|
th->local_storage = st_init_numtable();
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2007-08-25 23:31:20 -04:00
|
|
|
th->first_proc = proc;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2011-06-06 10:06:59 -04:00
|
|
|
#if !FIBER_USE_NATIVE
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
MEMCPY(&cont->jmpbuf, &th->root_jmpbuf, rb_jmpbuf_t, 1);
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
return fibval;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2009-09-21 07:06:32 -04:00
|
|
|
/* :nodoc: */
|
2008-11-28 10:23:09 -05:00
|
|
|
static VALUE
|
|
|
|
rb_fiber_init(VALUE fibval)
|
2007-08-25 23:31:20 -04:00
|
|
|
{
|
2008-11-28 10:23:09 -05:00
|
|
|
return fiber_init(fibval, rb_block_proc());
|
2007-08-25 23:31:20 -04:00
|
|
|
}
|
|
|
|
|
2008-11-28 10:23:09 -05:00
|
|
|
VALUE
|
|
|
|
rb_fiber_new(VALUE (*func)(ANYARGS), VALUE obj)
|
2007-08-25 23:31:20 -04:00
|
|
|
{
|
2008-11-28 10:23:09 -05:00
|
|
|
return fiber_init(fiber_alloc(rb_cFiber), rb_proc_new(func, obj));
|
2007-08-25 23:31:20 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 14:51:39 -04:00
|
|
|
static VALUE
|
|
|
|
return_fiber(void)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_t *fib;
|
2007-08-21 14:51:39 -04:00
|
|
|
VALUE curr = rb_fiber_current();
|
2012-04-17 00:55:10 -04:00
|
|
|
VALUE prev;
|
2008-10-22 11:12:07 -04:00
|
|
|
GetFiberPtr(curr, fib);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2012-04-17 00:55:10 -04:00
|
|
|
prev = fib->prev;
|
|
|
|
if (NIL_P(prev)) {
|
|
|
|
const VALUE root_fiber = GET_THREAD()->root_fiber;
|
2007-08-21 14:51:39 -04:00
|
|
|
|
2012-04-17 00:55:10 -04:00
|
|
|
if (root_fiber == curr) {
|
2007-08-21 14:51:39 -04:00
|
|
|
rb_raise(rb_eFiberError, "can't yield from root fiber");
|
|
|
|
}
|
2012-04-17 00:55:10 -04:00
|
|
|
return root_fiber;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
else {
|
2008-10-22 11:12:07 -04:00
|
|
|
fib->prev = Qnil;
|
2007-08-21 14:51:39 -04:00
|
|
|
return prev;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-21 14:51:39 -04:00
|
|
|
VALUE rb_fiber_transfer(VALUE fib, int argc, VALUE *argv);
|
|
|
|
|
|
|
|
static void
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_terminate(rb_fiber_t *fib)
|
2007-08-21 14:51:39 -04:00
|
|
|
{
|
2008-10-22 11:12:07 -04:00
|
|
|
VALUE value = fib->cont.value;
|
|
|
|
fib->status = TERMINATED;
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE && !defined(_WIN32)
|
2010-05-05 14:37:37 -04:00
|
|
|
/* Ruby must not switch to other thread until storing terminated_machine_stack */
|
|
|
|
terminated_machine_stack.ptr = fib->context.uc_stack.ss_sp;
|
|
|
|
terminated_machine_stack.size = fib->context.uc_stack.ss_size / sizeof(VALUE);
|
|
|
|
fib->context.uc_stack.ss_sp = NULL;
|
|
|
|
fib->cont.machine_stack = NULL;
|
|
|
|
fib->cont.machine_stack_size = 0;
|
|
|
|
#endif
|
2007-08-21 14:51:39 -04:00
|
|
|
rb_fiber_transfer(return_fiber(), 1, &value);
|
|
|
|
}
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
void
|
|
|
|
rb_fiber_start(void)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_t *fib;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_context_t *cont;
|
|
|
|
rb_proc_t *proc;
|
|
|
|
int state;
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
GetFiberPtr(th->fiber, fib);
|
|
|
|
cont = &fib->cont;
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
TH_PUSH_TAG(th);
|
|
|
|
if ((state = EXEC_TAG()) == 0) {
|
2008-11-30 22:00:48 -05:00
|
|
|
int argc;
|
|
|
|
VALUE *argv, args;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
GetProcPtr(cont->saved_thread.first_proc, proc);
|
2007-06-02 03:48:29 -04:00
|
|
|
args = cont->value;
|
2008-11-30 22:00:48 -05:00
|
|
|
argv = (argc = cont->argc) > 1 ? RARRAY_PTR(args) : &args;
|
2007-06-02 03:48:29 -04:00
|
|
|
cont->value = Qnil;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
th->errinfo = Qnil;
|
|
|
|
th->local_lfp = proc->block.lfp;
|
|
|
|
th->local_svar = Qnil;
|
2007-06-02 03:48:29 -04:00
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
fib->status = RUNNING;
|
* vm.c: add a prefix "rb_" to exposed functions
vm_get_ruby_level_next_cfp(), rb_vm_make_env_object(),
vm_stack_to_heap(), vm_make_proc(), vm_invoke_proc(),
vm_get_sourceline(), vm_cref(), vm_localjump_error(),
vm_make_jump_tag_but_local_jump(), vm_jump_tag_but_local_jump().
This changes may affect only core because most of renamed functions
require a pointer of not-exposed struct such as rb_thread_t or NODE.
In short, they are core functions.
* cont.c, eval.c, eval_intern.h, load.c, proc.c, thread.c,
vm_core.h, vm_dump.c, vm_eval.c, vm_exec.c, vm_insnhelper.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-18 21:38:11 -05:00
|
|
|
cont->value = rb_vm_invoke_proc(th, proc, proc->block.self, argc, argv, 0);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
TH_POP_TAG();
|
|
|
|
|
|
|
|
if (state) {
|
2009-11-03 18:19:33 -05:00
|
|
|
if (state == TAG_RAISE) {
|
2007-08-21 14:51:39 -04:00
|
|
|
th->thrown_errinfo = th->errinfo;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
th->thrown_errinfo =
|
* vm.c: add a prefix "rb_" to exposed functions
vm_get_ruby_level_next_cfp(), rb_vm_make_env_object(),
vm_stack_to_heap(), vm_make_proc(), vm_invoke_proc(),
vm_get_sourceline(), vm_cref(), vm_localjump_error(),
vm_make_jump_tag_but_local_jump(), vm_jump_tag_but_local_jump().
This changes may affect only core because most of renamed functions
require a pointer of not-exposed struct such as rb_thread_t or NODE.
In short, they are core functions.
* cont.c, eval.c, eval_intern.h, load.c, proc.c, thread.c,
vm_core.h, vm_dump.c, vm_eval.c, vm_exec.c, vm_insnhelper.c:
ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-01-18 21:38:11 -05:00
|
|
|
rb_vm_make_jump_tag_but_local_jump(state, th->errinfo);
|
2007-08-21 14:51:39 -04:00
|
|
|
}
|
2007-12-24 23:16:06 -05:00
|
|
|
RUBY_VM_SET_INTERRUPT(th);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_terminate(fib);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_bug("rb_fiber_start: unreachable");
|
|
|
|
}
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
static rb_fiber_t *
|
|
|
|
root_fiber_alloc(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
rb_fiber_t *fib;
|
|
|
|
/* no need to allocate vm stack */
|
2008-11-28 10:23:09 -05:00
|
|
|
fib = fiber_t_alloc(fiber_alloc(rb_cFiber));
|
2008-10-22 11:12:07 -04:00
|
|
|
fib->cont.type = ROOT_FIBER_CONTEXT;
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
fib->fib_handle = ConvertThreadToFiber(0);
|
|
|
|
#endif
|
|
|
|
#endif
|
2011-06-08 08:50:03 -04:00
|
|
|
fib->status = RUNNING;
|
2009-11-01 20:04:31 -05:00
|
|
|
fib->prev_fiber = fib->next_fiber = fib;
|
2008-10-22 11:12:07 -04:00
|
|
|
|
|
|
|
return fib;
|
|
|
|
}
|
|
|
|
|
2007-08-06 12:41:17 -04:00
|
|
|
VALUE
|
* cont.c (rb_fiber_current), dln.c (dln_print_undef, dln_undefined),
eval.c (rb_iterator_p, rb_need_block), load.c: (Init_load), ruby.c
(uscore_get, rb_f_chop), st.c (stat_col), signal.c
(rb_signal_buff_size, ruby_sig_finalize), thread.c
(rb_thread_sleep_forever, rb_thread_sleep_deadly, rb_thread_alone):
protoized.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21929 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-01 07:51:44 -05:00
|
|
|
rb_fiber_current(void)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2007-08-06 12:41:17 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
if (th->fiber == 0) {
|
|
|
|
/* save root */
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_t *fib = root_fiber_alloc(th);
|
|
|
|
th->root_fiber = th->fiber = fib->cont.self;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
return th->fiber;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2008-10-22 11:12:07 -04:00
|
|
|
fiber_store(rb_fiber_t *next_fib)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_t *fib;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
|
|
|
if (th->fiber) {
|
2008-10-22 11:12:07 -04:00
|
|
|
GetFiberPtr(th->fiber, fib);
|
2011-06-14 08:57:50 -04:00
|
|
|
cont_save_thread(&fib->cont, th);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* create current fiber */
|
2008-10-22 11:12:07 -04:00
|
|
|
fib = root_fiber_alloc(th);
|
|
|
|
th->root_fiber = th->fiber = fib->cont.self;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2011-06-06 10:06:59 -04:00
|
|
|
#if !FIBER_USE_NATIVE
|
2008-10-22 11:12:07 -04:00
|
|
|
cont_save_machine_stack(th, &fib->cont);
|
2011-10-13 17:16:46 -04:00
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2011-10-13 17:16:46 -04:00
|
|
|
if (FIBER_USE_NATIVE || ruby_setjmp(fib->cont.jmpbuf)) {
|
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
fiber_setcontext(next_fib, fib);
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (terminated_machine_stack.ptr) {
|
|
|
|
if (machine_stack_cache_index < MAX_MAHINE_STACK_CACHE) {
|
|
|
|
machine_stack_cache[machine_stack_cache_index].ptr = terminated_machine_stack.ptr;
|
|
|
|
machine_stack_cache[machine_stack_cache_index].size = terminated_machine_stack.size;
|
|
|
|
machine_stack_cache_index++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (terminated_machine_stack.ptr != fib->cont.machine_stack) {
|
|
|
|
munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_bug("terminated fiber resumed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
terminated_machine_stack.ptr = NULL;
|
|
|
|
terminated_machine_stack.size = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
/* restored */
|
2008-10-22 11:12:07 -04:00
|
|
|
GetFiberPtr(th->fiber, fib);
|
2009-05-31 22:21:31 -04:00
|
|
|
if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value);
|
2008-10-22 11:12:07 -04:00
|
|
|
return fib->cont.value;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
2011-06-06 10:06:59 -04:00
|
|
|
#if !FIBER_USE_NATIVE
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
else {
|
|
|
|
return Qundef;
|
|
|
|
}
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 14:51:39 -04:00
|
|
|
static inline VALUE
|
2008-10-22 11:12:07 -04:00
|
|
|
fiber_switch(VALUE fibval, int argc, VALUE *argv, int is_resume)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2007-06-02 03:48:29 -04:00
|
|
|
VALUE value;
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_t *fib;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_context_t *cont;
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
GetFiberPtr(fibval, fib);
|
|
|
|
cont = &fib->cont;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2011-11-20 16:17:57 -05:00
|
|
|
if (th->fiber == fibval) {
|
|
|
|
/* ignore fiber context switch
|
|
|
|
* because destination fiber is same as current fiber
|
|
|
|
*/
|
|
|
|
return make_passing_arg(argc, argv);
|
|
|
|
}
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
if (cont->saved_thread.self != th->self) {
|
2007-06-02 03:48:29 -04:00
|
|
|
rb_raise(rb_eFiberError, "fiber called across threads");
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
2010-01-25 13:22:58 -05:00
|
|
|
else if (cont->saved_thread.protect_tag != th->protect_tag) {
|
|
|
|
rb_raise(rb_eFiberError, "fiber called across stack rewinding barrier");
|
|
|
|
}
|
2008-10-22 11:12:07 -04:00
|
|
|
else if (fib->status == TERMINATED) {
|
2009-05-31 22:21:31 -04:00
|
|
|
value = rb_exc_new2(rb_eFiberError, "dead fiber called");
|
2010-05-09 13:32:45 -04:00
|
|
|
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;
|
|
|
|
}
|
2009-05-31 22:21:31 -04:00
|
|
|
GetFiberPtr(fibval, fib);
|
|
|
|
cont = &fib->cont;
|
|
|
|
cont->argc = -1;
|
|
|
|
cont->value = value;
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
{
|
|
|
|
VALUE oldfibval;
|
|
|
|
rb_fiber_t *oldfib;
|
|
|
|
oldfibval = rb_fiber_current();
|
|
|
|
GetFiberPtr(oldfibval, oldfib);
|
|
|
|
fiber_setcontext(fib, oldfib);
|
|
|
|
}
|
|
|
|
#else
|
2009-05-31 22:21:31 -04:00
|
|
|
cont_restore_0(cont, &value);
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 14:51:39 -04:00
|
|
|
if (is_resume) {
|
2008-10-22 11:12:07 -04:00
|
|
|
fib->prev = rb_fiber_current();
|
2007-08-21 14:51:39 -04:00
|
|
|
}
|
2007-08-25 04:51:59 -04:00
|
|
|
|
2008-11-30 22:00:48 -05:00
|
|
|
cont->argc = argc;
|
2007-06-02 03:48:29 -04:00
|
|
|
cont->value = make_passing_arg(argc, argv);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
|
2010-05-05 14:37:37 -04:00
|
|
|
value = fiber_store(fib);
|
2011-06-06 10:06:59 -04:00
|
|
|
#if !FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
if (value == Qundef) {
|
2009-05-31 22:21:31 -04:00
|
|
|
cont_restore_0(cont, &value);
|
2007-08-21 14:51:39 -04:00
|
|
|
rb_bug("rb_fiber_resume: unreachable");
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
2010-05-05 14:37:37 -04:00
|
|
|
#endif
|
2007-08-21 14:51:39 -04:00
|
|
|
RUBY_VM_CHECK_INTS();
|
|
|
|
|
2007-06-02 03:48:29 -04:00
|
|
|
return value;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 14:51:39 -04:00
|
|
|
VALUE
|
|
|
|
rb_fiber_transfer(VALUE fib, int argc, VALUE *argv)
|
2007-08-15 13:56:22 -04:00
|
|
|
{
|
2007-08-21 14:51:39 -04:00
|
|
|
return fiber_switch(fib, argc, argv, 0);
|
2007-08-15 13:56:22 -04:00
|
|
|
}
|
|
|
|
|
2007-08-21 14:51:39 -04:00
|
|
|
VALUE
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_resume(VALUE fibval, int argc, VALUE *argv)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_t *fib;
|
|
|
|
GetFiberPtr(fibval, fib);
|
2007-08-21 14:51:39 -04:00
|
|
|
|
2010-11-03 13:08:25 -04:00
|
|
|
if (fib->prev != Qnil || fib->cont.type == ROOT_FIBER_CONTEXT) {
|
2007-08-21 14:51:39 -04:00
|
|
|
rb_raise(rb_eFiberError, "double resume");
|
|
|
|
}
|
2011-11-08 23:26:39 -05:00
|
|
|
if (fib->transfered != 0) {
|
|
|
|
rb_raise(rb_eFiberError, "cannot resume transferred Fiber");
|
|
|
|
}
|
2007-08-21 14:51:39 -04:00
|
|
|
|
2008-10-22 11:12:07 -04:00
|
|
|
return fiber_switch(fibval, argc, argv, 1);
|
2007-08-21 14:51:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_fiber_yield(int argc, VALUE *argv)
|
|
|
|
{
|
2007-08-23 03:55:37 -04:00
|
|
|
return rb_fiber_transfer(return_fiber(), argc, argv);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2012-02-15 09:00:11 -05:00
|
|
|
void
|
|
|
|
rb_fiber_reset_root_local_storage(VALUE thval)
|
|
|
|
{
|
|
|
|
rb_thread_t *th;
|
|
|
|
rb_fiber_t *fib;
|
|
|
|
|
|
|
|
GetThreadPtr(thval, th);
|
|
|
|
if (th->root_fiber && th->root_fiber != th->fiber) {
|
|
|
|
GetFiberPtr(th->root_fiber, fib);
|
|
|
|
th->local_storage = fib->cont.saved_thread.local_storage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-26 19:25:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.alive? -> true or false
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2010-10-26 20:26:29 -04:00
|
|
|
* 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. You need to <code>require 'fiber'</code>
|
|
|
|
* before using this method.
|
2008-12-26 19:25:47 -05:00
|
|
|
*/
|
2007-08-06 12:41:17 -04:00
|
|
|
VALUE
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_alive_p(VALUE fibval)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2008-10-22 11:12:07 -04:00
|
|
|
rb_fiber_t *fib;
|
|
|
|
GetFiberPtr(fibval, fib);
|
2009-02-13 01:04:25 -05:00
|
|
|
return fib->status != TERMINATED ? Qtrue : Qfalse;
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2008-12-26 19:25:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.resume(args, ...) -> obj
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* Resumes the fiber from the point at which the last <code>Fiber.yield</code>
|
2009-02-22 09:23:33 -05:00
|
|
|
* was called, or starts running it if it is the first call to
|
2008-12-26 19:25:47 -05:00
|
|
|
* <code>resume</code>. Arguments passed to resume will be the value of
|
2009-02-22 09:23:33 -05:00
|
|
|
* the <code>Fiber.yield</code> expression or will be passed as block
|
2008-12-26 19:25:47 -05:00
|
|
|
* parameters to the fiber's block if this is the first <code>resume</code>.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* 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>
|
|
|
|
*/
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
static VALUE
|
2007-08-21 14:51:39 -04:00
|
|
|
rb_fiber_m_resume(int argc, VALUE *argv, VALUE fib)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2007-08-21 14:51:39 -04:00
|
|
|
return rb_fiber_resume(fib, argc, argv);
|
|
|
|
}
|
|
|
|
|
2008-12-26 19:25:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.transfer(args, ...) -> obj
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* Transfer control to another fiber, resuming it from where it last
|
2009-02-22 09:23:33 -05:00
|
|
|
* stopped or starting it if it was not resumed before. The calling
|
2010-10-26 20:26:29 -04:00
|
|
|
* fiber will be suspended much like in a call to
|
|
|
|
* <code>Fiber.yield</code>. You need to <code>require 'fiber'</code>
|
|
|
|
* before using this method.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2009-11-03 12:46:28 -05:00
|
|
|
* The fiber which receives the transfer call is treats it much like
|
2008-12-26 19:25:47 -05:00
|
|
|
* a resume call. Arguments passed to transfer are treated like those
|
|
|
|
* passed to resume.
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* 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.
|
|
|
|
*/
|
2007-08-21 14:51:39 -04:00
|
|
|
static VALUE
|
2011-11-08 23:26:39 -05:00
|
|
|
rb_fiber_m_transfer(int argc, VALUE *argv, VALUE fibval)
|
2007-08-21 14:51:39 -04:00
|
|
|
{
|
2011-11-08 23:26:39 -05:00
|
|
|
rb_fiber_t *fib;
|
|
|
|
GetFiberPtr(fibval, fib);
|
|
|
|
fib->transfered = 1;
|
|
|
|
return rb_fiber_transfer(fibval, argc, argv);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2008-12-26 19:25:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Fiber.yield(args, ...) -> obj
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* 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.
|
|
|
|
*/
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
static VALUE
|
2007-08-21 14:51:39 -04:00
|
|
|
rb_fiber_s_yield(int argc, VALUE *argv, VALUE klass)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2007-08-21 14:51:39 -04:00
|
|
|
return rb_fiber_yield(argc, argv);
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2008-12-26 19:25:47 -05:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Fiber.current() -> fiber
|
2009-02-22 09:23:33 -05:00
|
|
|
*
|
2008-12-26 19:25:47 -05:00
|
|
|
* 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.
|
|
|
|
*/
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
static VALUE
|
2007-08-21 14:51:39 -04:00
|
|
|
rb_fiber_s_current(VALUE klass)
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
{
|
2007-08-21 14:51:39 -04:00
|
|
|
return rb_fiber_current();
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
}
|
|
|
|
|
2010-05-08 00:50:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2007-05-23 18:52:19 -04:00
|
|
|
void
|
|
|
|
Init_Cont(void)
|
|
|
|
{
|
2011-06-06 10:06:59 -04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 14:37:37 -04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
SYSTEM_INFO info;
|
|
|
|
GetSystemInfo(&info);
|
|
|
|
pagesize = info.dwPageSize;
|
|
|
|
#else /* not WIN32 */
|
|
|
|
pagesize = sysconf(_SC_PAGESIZE);
|
|
|
|
#endif
|
|
|
|
SET_MACHINE_STACK_END(&th->machine_stack_end);
|
|
|
|
#endif
|
|
|
|
|
* cont.c: support Fiber. Check test/ruby/test_fiber.rb for detail.
Fiber is known as "Micro Thread", "Coroutine", and other terms.
At this time, only Fiber#pass is supported to change context.
I want to know more suitable method name/API for Fiber (... do you
know more suitable class name instead of Fiber?) as "suspend/resume",
"call", "yield", "start/kick/stop/restart", ....
* eval.c, eval_intern.h, thread.c, yarvcore.c, yarvcore.h: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-05-27 15:12:43 -04:00
|
|
|
rb_cFiber = rb_define_class("Fiber", rb_cObject);
|
2008-11-28 10:23:09 -05:00
|
|
|
rb_define_alloc_func(rb_cFiber, fiber_alloc);
|
2007-06-02 03:48:29 -04:00
|
|
|
rb_eFiberError = rb_define_class("FiberError", rb_eStandardError);
|
2007-09-26 06:26:35 -04:00
|
|
|
rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
|
2008-11-28 10:23:09 -05:00
|
|
|
rb_define_method(rb_cFiber, "initialize", rb_fiber_init, 0);
|
2007-09-26 06:26:35 -04:00
|
|
|
rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
|
2007-05-23 18:52:19 -04:00
|
|
|
}
|
|
|
|
|
2010-07-21 17:38:25 -04:00
|
|
|
#if defined __GNUC__ && __GNUC__ >= 4
|
|
|
|
#pragma GCC visibility push(default)
|
|
|
|
#endif
|
|
|
|
|
2007-08-24 22:03:44 -04:00
|
|
|
void
|
2009-01-20 02:17:28 -05:00
|
|
|
ruby_Init_Continuation_body(void)
|
2007-08-24 22:03:44 -04:00
|
|
|
{
|
|
|
|
rb_cContinuation = rb_define_class("Continuation", rb_cObject);
|
|
|
|
rb_undef_alloc_func(rb_cContinuation);
|
|
|
|
rb_undef_method(CLASS_OF(rb_cContinuation), "new");
|
|
|
|
rb_define_method(rb_cContinuation, "call", rb_cont_call, -1);
|
|
|
|
rb_define_method(rb_cContinuation, "[]", rb_cont_call, -1);
|
|
|
|
rb_define_global_function("callcc", rb_callcc, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-01-20 02:17:28 -05:00
|
|
|
ruby_Init_Fiber_as_Coroutine(void)
|
2007-08-24 22:03:44 -04:00
|
|
|
{
|
|
|
|
rb_define_method(rb_cFiber, "transfer", rb_fiber_m_transfer, -1);
|
|
|
|
rb_define_method(rb_cFiber, "alive?", rb_fiber_alive_p, 0);
|
|
|
|
rb_define_singleton_method(rb_cFiber, "current", rb_fiber_s_current, 0);
|
|
|
|
}
|
2010-07-21 17:38:25 -04:00
|
|
|
|
|
|
|
#if defined __GNUC__ && __GNUC__ >= 4
|
|
|
|
#pragma GCC visibility pop
|
|
|
|
#endif
|