2007-05-24 02:52:19 +04:00
|
|
|
/**********************************************************************
|
|
|
|
|
2009-02-22 17:23:33 +03:00
|
|
|
cont.c -
|
2007-05-24 02:52:19 +04:00
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Thu May 23 09:03:43 2007
|
|
|
|
|
|
|
|
Copyright (C) 2007 Koichi Sasada
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
* 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-18 02: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 08:25:46 +04:00
|
|
|
#include "vm_core.h"
|
2007-05-24 02:52:19 +04:00
|
|
|
#include "gc.h"
|
|
|
|
#include "eval_intern.h"
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
#include "mjit.h"
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2010-05-05 22:37:37 +04:00
|
|
|
/* 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).
|
|
|
|
*/
|
|
|
|
|
2018-11-20 12:59:10 +03:00
|
|
|
/*
|
2018-11-20 13:17:04 +03:00
|
|
|
Enable FIBER_USE_COROUTINE to make fiber yield/resume much faster by using native assembly implementations.
|
2018-11-20 13:17:12 +03:00
|
|
|
|
2018-11-20 12:59:14 +03:00
|
|
|
rvm install ruby-head-ioquatix-native-fiber --url https://github.com/ioquatix/ruby --branch native-fiber
|
2018-11-20 13:17:12 +03:00
|
|
|
|
2018-11-20 12:59:10 +03:00
|
|
|
# Without libcoro
|
|
|
|
koyoko% ./build/bin/ruby ./fiber_benchmark.rb 10000 1000
|
|
|
|
setup time for 10000 fibers: 0.099961
|
|
|
|
execution time for 1000 messages: 19.505909
|
|
|
|
|
|
|
|
# With libcoro
|
|
|
|
koyoko% ./build/bin/ruby ./fiber_benchmark.rb 10000 1000
|
|
|
|
setup time for 10000 fibers: 0.099268
|
|
|
|
execution time for 1000 messages: 8.491746
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef FIBER_USE_COROUTINE
|
2018-11-20 12:59:14 +03:00
|
|
|
#include FIBER_USE_COROUTINE
|
2018-11-20 12:59:10 +03:00
|
|
|
#define FIBER_USE_NATIVE 1
|
|
|
|
#endif
|
|
|
|
|
2013-10-08 05:44:45 +04:00
|
|
|
#if !defined(FIBER_USE_NATIVE)
|
|
|
|
# if defined(HAVE_GETCONTEXT) && defined(HAVE_SETCONTEXT)
|
|
|
|
# if 0
|
|
|
|
# elif defined(__NetBSD__)
|
2010-05-05 22:37:37 +04:00
|
|
|
/* 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.
|
|
|
|
*/
|
2013-10-08 05:44:45 +04:00
|
|
|
# define FIBER_USE_NATIVE 0
|
|
|
|
# elif defined(__sun)
|
|
|
|
/* On Solaris because resuming any Fiber caused SEGV, for some reason.
|
|
|
|
*/
|
|
|
|
# define FIBER_USE_NATIVE 0
|
2013-10-08 06:14:21 +04:00
|
|
|
# elif defined(__GNU__)
|
|
|
|
/* GNU/Hurd doesn't fully support getcontext, setcontext, makecontext
|
|
|
|
* and swapcontext functions. Disabling their usage till support is
|
|
|
|
* implemented. More info at
|
|
|
|
* http://darnassus.sceen.net/~hurd-web/open_issues/glibc/#getcontext
|
|
|
|
*/
|
|
|
|
# define FIBER_USE_NATIVE 0
|
2013-10-08 05:44:45 +04:00
|
|
|
# else
|
|
|
|
# define FIBER_USE_NATIVE 1
|
|
|
|
# endif
|
|
|
|
# elif defined(_WIN32)
|
2016-05-01 14:42:41 +03:00
|
|
|
# define FIBER_USE_NATIVE 1
|
2013-10-08 05:44:45 +04:00
|
|
|
# endif
|
2013-10-07 17:45:31 +04:00
|
|
|
#endif
|
2013-10-07 16:03:58 +04:00
|
|
|
#if !defined(FIBER_USE_NATIVE)
|
2011-06-06 18:06:59 +04:00
|
|
|
#define FIBER_USE_NATIVE 0
|
2010-05-05 22:37:37 +04:00
|
|
|
#endif
|
|
|
|
|
2011-06-06 18:06:59 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 22:37:37 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <ucontext.h>
|
2010-07-28 04:38:21 +04:00
|
|
|
#endif
|
2010-07-28 04:25:53 +04:00
|
|
|
#define RB_PAGE_SIZE (pagesize)
|
|
|
|
#define RB_PAGE_MASK (~(RB_PAGE_SIZE - 1))
|
2010-05-05 22:37:37 +04:00
|
|
|
static long pagesize;
|
2012-05-18 12:32:56 +04:00
|
|
|
#endif /*FIBER_USE_NATIVE*/
|
2010-05-05 22:37:37 +04:00
|
|
|
|
2008-11-18 19:19:37 +03:00
|
|
|
#define CAPTURE_JUST_VALID_VM_STACK 1
|
|
|
|
|
2007-11-09 04:11:49 +03:00
|
|
|
enum context_type {
|
|
|
|
CONTINUATION_CONTEXT = 0,
|
2018-09-12 23:49:24 +03:00
|
|
|
FIBER_CONTEXT = 1
|
2007-11-09 04:11:49 +03:00
|
|
|
};
|
|
|
|
|
2017-08-22 03:41:24 +03:00
|
|
|
struct cont_saved_vm_stack {
|
|
|
|
VALUE *ptr;
|
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
2017-10-26 11:42:44 +03:00
|
|
|
size_t slen; /* length of stack (head of ec->vm_stack) */
|
|
|
|
size_t clen; /* length of control frames (tail of ec->vm_stack) */
|
2017-08-22 03:41:24 +03: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 23:12:43 +04:00
|
|
|
typedef struct rb_context_struct {
|
2008-10-22 19:12:07 +04:00
|
|
|
enum context_type type;
|
2008-12-01 06:00:48 +03:00
|
|
|
int argc;
|
2014-08-15 04:25:34 +04:00
|
|
|
VALUE self;
|
2007-06-02 11:48:29 +04:00
|
|
|
VALUE value;
|
2017-08-22 03:41:24 +03:00
|
|
|
|
|
|
|
struct cont_saved_vm_stack saved_vm_stack;
|
|
|
|
|
2014-01-28 10:09:58 +04:00
|
|
|
struct {
|
|
|
|
VALUE *stack;
|
|
|
|
VALUE *stack_src;
|
|
|
|
size_t stack_size;
|
|
|
|
} machine;
|
2017-09-10 22:00:08 +03:00
|
|
|
rb_execution_context_t saved_ec;
|
2019-06-05 14:39:17 +03:00
|
|
|
int free_vm_stack;
|
* 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 23:12:43 +04:00
|
|
|
rb_jmpbuf_t jmpbuf;
|
2013-11-15 21:15:31 +04:00
|
|
|
rb_ensure_entry_t *ensure_array;
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
/* Pointer to MJIT info about the continuation. */
|
|
|
|
struct mjit_cont *mjit_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 23:12:43 +04:00
|
|
|
} rb_context_t;
|
|
|
|
|
2017-08-10 04:47:13 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fiber status:
|
|
|
|
* [Fiber.new] ------> FIBER_CREATED
|
|
|
|
* | [Fiber#resume]
|
|
|
|
* v
|
|
|
|
* +--> FIBER_RESUMED ----+
|
|
|
|
* [Fiber#resume] | | [Fiber.yield] |
|
|
|
|
* | v |
|
|
|
|
* +-- FIBER_SUSPENDED | [Terminate]
|
|
|
|
* |
|
|
|
|
* FIBER_TERMINATED <-+
|
|
|
|
*/
|
2008-10-22 19:12:07 +04:00
|
|
|
enum fiber_status {
|
2017-06-26 08:36:10 +03:00
|
|
|
FIBER_CREATED,
|
2017-08-10 04:47:13 +03:00
|
|
|
FIBER_RESUMED,
|
|
|
|
FIBER_SUSPENDED,
|
2017-06-26 08:36:10 +03:00
|
|
|
FIBER_TERMINATED
|
2008-10-22 19:12:07 +04:00
|
|
|
};
|
|
|
|
|
2017-08-10 04:47:13 +03:00
|
|
|
#define FIBER_CREATED_P(fib) ((fib)->status == FIBER_CREATED)
|
|
|
|
#define FIBER_RESUMED_P(fib) ((fib)->status == FIBER_RESUMED)
|
|
|
|
#define FIBER_SUSPENDED_P(fib) ((fib)->status == FIBER_SUSPENDED)
|
|
|
|
#define FIBER_TERMINATED_P(fib) ((fib)->status == FIBER_TERMINATED)
|
|
|
|
#define FIBER_RUNNABLE_P(fib) (FIBER_CREATED_P(fib) || FIBER_SUSPENDED_P(fib))
|
|
|
|
|
2018-11-20 12:59:10 +03:00
|
|
|
#if FIBER_USE_NATIVE && !defined(FIBER_USE_COROUTINE) && !defined(_WIN32)
|
2018-07-03 08:12:52 +03:00
|
|
|
static inline int
|
2018-05-08 09:53:54 +03:00
|
|
|
fiber_context_create(ucontext_t *context, void (*func)(), void *arg, void *ptr, size_t size)
|
2018-05-08 03:01:16 +03:00
|
|
|
{
|
2018-07-03 08:12:52 +03:00
|
|
|
if (getcontext(context) < 0) return -1;
|
|
|
|
/*
|
|
|
|
* getcontext() may fail by some reasons:
|
|
|
|
* 1. SELinux policy banned one of "rt_sigprocmask",
|
2018-07-03 16:25:59 +03:00
|
|
|
* "sigprocmask" or "swapcontext";
|
2018-07-03 08:12:52 +03:00
|
|
|
* 2. libseccomp (aka. syscall filter) banned one of them.
|
|
|
|
*/
|
2018-05-08 03:01:16 +03:00
|
|
|
context->uc_link = NULL;
|
|
|
|
context->uc_stack.ss_sp = ptr;
|
|
|
|
context->uc_stack.ss_size = size;
|
|
|
|
makecontext(context, func, 0);
|
2018-07-03 08:12:52 +03:00
|
|
|
return 0;
|
2018-05-08 03:01:16 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-16 04:19:22 +04:00
|
|
|
struct rb_fiber_struct {
|
2008-10-22 19:12:07 +04:00
|
|
|
rb_context_t cont;
|
2017-06-28 18:25:30 +03:00
|
|
|
VALUE first_proc;
|
2014-10-16 02:35:08 +04:00
|
|
|
struct rb_fiber_struct *prev;
|
2018-08-22 07:04:06 +03:00
|
|
|
BITFIELD(enum fiber_status, status, 2);
|
2011-11-09 08:26:39 +04:00
|
|
|
/* If a fiber invokes "transfer",
|
|
|
|
* then this fiber can't "resume" any more after that.
|
|
|
|
* You shouldn't mix "transfer" and "resume".
|
|
|
|
*/
|
2018-08-21 02:48:03 +03:00
|
|
|
unsigned int transferred : 1;
|
2011-11-09 08:26:39 +04:00
|
|
|
|
2011-06-06 18:06:59 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2018-11-20 12:59:10 +03:00
|
|
|
#if defined(FIBER_USE_COROUTINE)
|
2018-11-20 13:18:12 +03:00
|
|
|
#define FIBER_ALLOCATE_STACK
|
2018-11-20 12:59:10 +03:00
|
|
|
coroutine_context context;
|
|
|
|
void *ss_sp;
|
|
|
|
size_t ss_size;
|
|
|
|
#elif defined(_WIN32)
|
2010-05-05 22:37:37 +04:00
|
|
|
void *fib_handle;
|
|
|
|
#else
|
2018-11-20 13:18:12 +03:00
|
|
|
#define FIBER_ALLOCATE_STACK
|
2010-05-05 22:37:37 +04:00
|
|
|
ucontext_t context;
|
2014-06-07 11:11:31 +04:00
|
|
|
/* Because context.uc_stack.ss_sp and context.uc_stack.ss_size
|
|
|
|
* are not necessarily valid after makecontext() or swapcontext(),
|
|
|
|
* they are saved in these variables for later use.
|
|
|
|
*/
|
|
|
|
void *ss_sp;
|
|
|
|
size_t ss_size;
|
2010-05-05 22:37:37 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
2014-10-16 04:17:44 +04:00
|
|
|
};
|
2008-10-22 19:12:07 +04:00
|
|
|
|
2018-11-20 13:18:12 +03:00
|
|
|
#ifdef FIBER_ALLOCATE_STACK
|
|
|
|
#define MAX_MACHINE_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_MACHINE_STACK_CACHE];
|
|
|
|
static machine_stack_cache_t terminated_machine_stack;
|
|
|
|
#endif
|
|
|
|
|
2017-08-10 04:47:13 +03:00
|
|
|
static const char *
|
|
|
|
fiber_status_name(enum fiber_status s)
|
|
|
|
{
|
|
|
|
switch (s) {
|
|
|
|
case FIBER_CREATED: return "created";
|
|
|
|
case FIBER_RESUMED: return "resumed";
|
|
|
|
case FIBER_SUSPENDED: return "suspended";
|
|
|
|
case FIBER_TERMINATED: return "terminated";
|
|
|
|
}
|
|
|
|
VM_UNREACHABLE(fiber_status_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
static void
|
|
|
|
fiber_verify(const rb_fiber_t *fib)
|
|
|
|
{
|
|
|
|
#if VM_CHECK_MODE > 0
|
2017-11-06 08:41:48 +03:00
|
|
|
VM_ASSERT(fib->cont.saved_ec.fiber_ptr == fib);
|
2017-10-26 11:32:49 +03:00
|
|
|
|
|
|
|
switch (fib->status) {
|
|
|
|
case FIBER_RESUMED:
|
|
|
|
VM_ASSERT(fib->cont.saved_ec.vm_stack != NULL);
|
|
|
|
break;
|
|
|
|
case FIBER_SUSPENDED:
|
|
|
|
VM_ASSERT(fib->cont.saved_ec.vm_stack != NULL);
|
|
|
|
break;
|
|
|
|
case FIBER_CREATED:
|
|
|
|
case FIBER_TERMINATED:
|
|
|
|
/* TODO */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VM_UNREACHABLE(fiber_verify);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if VM_CHECK_MODE > 0
|
|
|
|
void
|
|
|
|
rb_ec_verify(const rb_execution_context_t *ec)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-08-10 04:47:13 +03:00
|
|
|
static void
|
2018-05-01 23:55:39 +03:00
|
|
|
fiber_status_set(rb_fiber_t *fib, enum fiber_status s)
|
2017-08-10 04:47:13 +03:00
|
|
|
{
|
2018-01-02 09:41:40 +03:00
|
|
|
if (0) fprintf(stderr, "fib: %p, status: %s -> %s\n", (void *)fib, fiber_status_name(fib->status), fiber_status_name(s));
|
2017-08-10 04:47:13 +03:00
|
|
|
VM_ASSERT(!FIBER_TERMINATED_P(fib));
|
|
|
|
VM_ASSERT(fib->status != s);
|
2017-10-26 11:32:49 +03:00
|
|
|
fiber_verify(fib);
|
2018-05-01 23:55:39 +03:00
|
|
|
fib->status = s;
|
2017-08-10 04:47:13 +03:00
|
|
|
}
|
|
|
|
|
2019-06-20 06:30:29 +03:00
|
|
|
void
|
|
|
|
rb_ec_set_vm_stack(rb_execution_context_t *ec, VALUE *stack, size_t size)
|
|
|
|
{
|
|
|
|
ec->vm_stack = stack;
|
|
|
|
ec->vm_stack_size = size;
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
static inline void
|
|
|
|
ec_switch(rb_thread_t *th, rb_fiber_t *fib)
|
|
|
|
{
|
|
|
|
rb_execution_context_t *ec = &fib->cont.saved_ec;
|
2018-07-26 11:30:10 +03:00
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
ruby_current_execution_context_ptr = th->ec = ec;
|
2018-07-26 11:30:10 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* timer-thread may set trap interrupt on previous th->ec at any time;
|
|
|
|
* ensure we do not delay (or lose) the trap interrupt handling.
|
|
|
|
*/
|
|
|
|
if (th->vm->main_thread == th && rb_signal_buff_size() > 0) {
|
|
|
|
RUBY_VM_SET_TRAP_INTERRUPT(ec);
|
|
|
|
}
|
|
|
|
|
2017-11-06 08:41:48 +03:00
|
|
|
VM_ASSERT(ec->fiber_ptr->cont.self == 0 || ec->vm_stack != NULL);
|
2017-10-26 11:32:49 +03:00
|
|
|
}
|
|
|
|
|
2009-09-08 19:27:31 +04:00
|
|
|
static const rb_data_type_t cont_data_type, fiber_data_type;
|
2007-08-25 06:03:44 +04:00
|
|
|
static VALUE rb_cContinuation;
|
|
|
|
static VALUE rb_cFiber;
|
|
|
|
static VALUE rb_eFiberError;
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2018-08-21 04:01:37 +03:00
|
|
|
static rb_context_t *
|
|
|
|
cont_ptr(VALUE obj)
|
|
|
|
{
|
|
|
|
rb_context_t *cont;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(obj, rb_context_t, &cont_data_type, cont);
|
|
|
|
|
|
|
|
return cont;
|
|
|
|
}
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2018-08-21 04:01:37 +03:00
|
|
|
static rb_fiber_t *
|
|
|
|
fiber_ptr(VALUE obj)
|
|
|
|
{
|
|
|
|
rb_fiber_t *fib;
|
|
|
|
|
|
|
|
TypedData_Get_Struct(obj, rb_fiber_t, &fiber_data_type, fib);
|
|
|
|
if (!fib) rb_raise(rb_eFiberError, "uninitialized fiber");
|
|
|
|
|
|
|
|
return fib;
|
|
|
|
}
|
2008-10-22 19:12:07 +04:00
|
|
|
|
2016-12-08 02:47:59 +03:00
|
|
|
NOINLINE(static VALUE cont_capture(volatile int *volatile stat));
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2009-09-21 05:13:24 +04:00
|
|
|
#define THREAD_MUST_BE_RUNNING(th) do { \
|
2017-10-26 11:32:49 +03:00
|
|
|
if (!(th)->ec->tag) rb_raise(rb_eThreadError, "not running thread"); \
|
2009-09-21 05:13:24 +04:00
|
|
|
} while (0)
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2017-09-10 22:00:08 +03:00
|
|
|
static VALUE
|
|
|
|
cont_thread_value(const rb_context_t *cont)
|
|
|
|
{
|
2017-10-29 15:57:04 +03:00
|
|
|
return cont->saved_ec.thread_ptr->self;
|
2017-09-10 22:00:08 +03:00
|
|
|
}
|
|
|
|
|
2019-05-31 23:25:24 +03:00
|
|
|
static void
|
|
|
|
cont_compact(void *ptr)
|
|
|
|
{
|
|
|
|
rb_context_t *cont = ptr;
|
|
|
|
|
|
|
|
cont->value = rb_gc_location(cont->value);
|
|
|
|
rb_execution_context_update(&cont->saved_ec);
|
|
|
|
}
|
|
|
|
|
2007-05-24 02:52:19 +04:00
|
|
|
static void
|
|
|
|
cont_mark(void *ptr)
|
|
|
|
{
|
2017-02-13 04:05:23 +03:00
|
|
|
rb_context_t *cont = ptr;
|
|
|
|
|
2007-06-25 06:44:20 +04:00
|
|
|
RUBY_MARK_ENTER("cont");
|
2019-05-31 23:25:24 +03:00
|
|
|
rb_gc_mark_no_pin(cont->value);
|
2016-07-28 14:02:30 +03:00
|
|
|
|
2017-09-10 22:00:08 +03:00
|
|
|
rb_execution_context_mark(&cont->saved_ec);
|
|
|
|
rb_gc_mark(cont_thread_value(cont));
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2017-08-22 03:41:24 +03:00
|
|
|
if (cont->saved_vm_stack.ptr) {
|
2008-11-18 19:19:37 +03:00
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
2017-08-22 03:41:24 +03:00
|
|
|
rb_gc_mark_locations(cont->saved_vm_stack.ptr,
|
|
|
|
cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
|
2009-02-05 19:13:54 +03:00
|
|
|
#else
|
2017-08-22 03:41:24 +03:00
|
|
|
rb_gc_mark_locations(cont->saved_vm_stack.ptr,
|
2017-09-10 22:00:08 +03:00
|
|
|
cont->saved_vm_stack.ptr, cont->saved_ec.stack_size);
|
2008-11-18 19:19:37 +03:00
|
|
|
#endif
|
2017-02-13 04:05:23 +03:00
|
|
|
}
|
2007-05-29 05:59:53 +04:00
|
|
|
|
2017-02-13 04:05:23 +03:00
|
|
|
if (cont->machine.stack) {
|
|
|
|
if (cont->type == CONTINUATION_CONTEXT) {
|
|
|
|
/* cont */
|
|
|
|
rb_gc_mark_locations(cont->machine.stack,
|
|
|
|
cont->machine.stack + cont->machine.stack_size);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* fiber */
|
2017-08-10 06:34:25 +03:00
|
|
|
const rb_fiber_t *fib = (rb_fiber_t*)cont;
|
2017-06-28 07:49:30 +03:00
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
if (!FIBER_TERMINATED_P(fib)) {
|
2014-01-28 10:09:58 +04:00
|
|
|
rb_gc_mark_locations(cont->machine.stack,
|
|
|
|
cont->machine.stack + cont->machine.stack_size);
|
2010-05-05 22:37:37 +04:00
|
|
|
}
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
2017-02-13 04:05:23 +03:00
|
|
|
}
|
|
|
|
|
2007-06-25 06:44:20 +04:00
|
|
|
RUBY_MARK_LEAVE("cont");
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
|
|
|
|
2018-09-12 23:49:24 +03:00
|
|
|
static int
|
|
|
|
fiber_is_root_p(const rb_fiber_t *fib)
|
|
|
|
{
|
|
|
|
return fib == fib->cont.saved_ec.thread_ptr->root_fiber;
|
|
|
|
}
|
|
|
|
|
2007-05-24 02:52:19 +04:00
|
|
|
static void
|
|
|
|
cont_free(void *ptr)
|
|
|
|
{
|
2017-03-17 22:59:56 +03:00
|
|
|
rb_context_t *cont = ptr;
|
|
|
|
|
2007-06-25 06:44:20 +04:00
|
|
|
RUBY_FREE_ENTER("cont");
|
2019-06-05 14:39:17 +03:00
|
|
|
|
|
|
|
if (cont->free_vm_stack) {
|
|
|
|
ruby_xfree(cont->saved_ec.vm_stack);
|
|
|
|
}
|
2017-10-26 11:32:49 +03:00
|
|
|
|
2011-06-06 18:06:59 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2017-03-17 22:59:56 +03:00
|
|
|
if (cont->type == CONTINUATION_CONTEXT) {
|
|
|
|
/* cont */
|
|
|
|
ruby_xfree(cont->ensure_array);
|
|
|
|
RUBY_FREE_UNLESS_NULL(cont->machine.stack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* fiber */
|
2018-11-20 13:13:55 +03:00
|
|
|
rb_fiber_t *fib = (rb_fiber_t*)cont;
|
2018-11-20 12:59:10 +03:00
|
|
|
#if defined(FIBER_USE_COROUTINE)
|
|
|
|
coroutine_destroy(&fib->context);
|
2019-06-05 14:39:17 +03:00
|
|
|
if (fib->ss_sp != NULL) {
|
|
|
|
if (fiber_is_root_p(fib)) {
|
|
|
|
rb_bug("Illegal root fiber parameter");
|
|
|
|
}
|
2018-11-20 13:18:04 +03:00
|
|
|
#ifdef _WIN32
|
2018-11-22 05:17:44 +03:00
|
|
|
VirtualFree((void*)fib->ss_sp, 0, MEM_RELEASE);
|
2018-11-20 13:18:04 +03:00
|
|
|
#else
|
2018-11-20 12:59:10 +03:00
|
|
|
munmap((void*)fib->ss_sp, fib->ss_size);
|
2018-11-20 13:18:04 +03:00
|
|
|
#endif
|
2018-11-22 05:17:44 +03:00
|
|
|
fib->ss_sp = NULL;
|
2018-11-20 12:59:10 +03:00
|
|
|
}
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
if (!fiber_is_root_p(fib)) {
|
2017-03-17 22:59:56 +03:00
|
|
|
/* don't delete root fiber handle */
|
|
|
|
if (fib->fib_handle) {
|
|
|
|
DeleteFiber(fib->fib_handle);
|
2010-05-05 22:37:37 +04:00
|
|
|
}
|
2017-03-17 22:59:56 +03:00
|
|
|
}
|
2010-05-05 22:37:37 +04:00
|
|
|
#else /* not WIN32 */
|
2018-09-12 23:49:24 +03:00
|
|
|
/* fib->ss_sp == NULL is possible for root fiber */
|
2017-10-26 11:32:49 +03:00
|
|
|
if (fib->ss_sp != NULL) {
|
|
|
|
munmap((void*)fib->ss_sp, fib->ss_size);
|
2010-05-05 22:37:37 +04:00
|
|
|
}
|
2017-03-17 22:59:56 +03:00
|
|
|
#endif
|
|
|
|
}
|
2010-05-05 22:37:37 +04:00
|
|
|
#else /* not FIBER_USE_NATIVE */
|
2017-03-17 22:59:56 +03:00
|
|
|
ruby_xfree(cont->ensure_array);
|
|
|
|
RUBY_FREE_UNLESS_NULL(cont->machine.stack);
|
2007-06-14 12:35:20 +04:00
|
|
|
#endif
|
2017-08-22 03:41:24 +03:00
|
|
|
RUBY_FREE_UNLESS_NULL(cont->saved_vm_stack.ptr);
|
2007-11-09 04:11:49 +03:00
|
|
|
|
2018-06-23 16:41:06 +03:00
|
|
|
if (mjit_enabled && cont->mjit_cont != NULL) {
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
mjit_cont_free(cont->mjit_cont);
|
|
|
|
}
|
2017-03-17 22:59:56 +03:00
|
|
|
/* free rb_cont_t or rb_fiber_t */
|
|
|
|
ruby_xfree(ptr);
|
2007-06-25 06:44:20 +04:00
|
|
|
RUBY_FREE_LEAVE("cont");
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
|
|
|
|
2009-09-08 19:27:31 +04:00
|
|
|
static size_t
|
2009-09-09 06:22:08 +04:00
|
|
|
cont_memsize(const void *ptr)
|
2009-09-08 19:27:31 +04:00
|
|
|
{
|
2009-09-09 06:26:06 +04:00
|
|
|
const rb_context_t *cont = ptr;
|
2009-09-08 19:27:31 +04:00
|
|
|
size_t size = 0;
|
2015-12-09 03:38:32 +03:00
|
|
|
|
|
|
|
size = sizeof(*cont);
|
2017-08-22 03:41:24 +03:00
|
|
|
if (cont->saved_vm_stack.ptr) {
|
2009-09-08 19:27:31 +04:00
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
2017-08-22 03:41:24 +03:00
|
|
|
size_t n = (cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
|
2009-09-08 19:27:31 +04:00
|
|
|
#else
|
2017-09-10 22:00:08 +03:00
|
|
|
size_t n = cont->saved_ec.vm_stack_size;
|
2009-09-08 19:27:31 +04:00
|
|
|
#endif
|
2017-08-22 03:41:24 +03:00
|
|
|
size += n * sizeof(*cont->saved_vm_stack.ptr);
|
2015-12-09 03:38:32 +03:00
|
|
|
}
|
2009-09-08 19:27:31 +04:00
|
|
|
|
2015-12-09 03:38:32 +03:00
|
|
|
if (cont->machine.stack) {
|
|
|
|
size += cont->machine.stack_size * sizeof(*cont->machine.stack);
|
|
|
|
}
|
2019-06-19 12:06:57 +03:00
|
|
|
|
2009-09-08 19:27:31 +04:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2019-05-31 23:25:24 +03:00
|
|
|
void
|
|
|
|
rb_fiber_update_self(rb_fiber_t *fib)
|
|
|
|
{
|
|
|
|
if (fib->cont.self) {
|
2019-06-11 19:16:45 +03:00
|
|
|
fib->cont.self = rb_gc_location(fib->cont.self);
|
2019-05-31 23:25:24 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-06-11 19:16:45 +03:00
|
|
|
rb_execution_context_update(&fib->cont.saved_ec);
|
2019-05-31 23:25:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 02:35:08 +04:00
|
|
|
void
|
2017-08-10 06:34:25 +03:00
|
|
|
rb_fiber_mark_self(const rb_fiber_t *fib)
|
2014-10-16 02:35:08 +04:00
|
|
|
{
|
2017-10-26 11:32:49 +03:00
|
|
|
if (fib->cont.self) {
|
2019-06-11 19:16:45 +03:00
|
|
|
rb_gc_mark_no_pin(fib->cont.self);
|
2017-10-26 11:32:49 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_execution_context_mark(&fib->cont.saved_ec);
|
|
|
|
}
|
2014-10-16 02:35:08 +04:00
|
|
|
}
|
|
|
|
|
2019-05-31 23:25:24 +03:00
|
|
|
static void
|
|
|
|
fiber_compact(void *ptr)
|
|
|
|
{
|
|
|
|
rb_fiber_t *fib = ptr;
|
|
|
|
fib->first_proc = rb_gc_location(fib->first_proc);
|
|
|
|
|
|
|
|
if (fib->prev) rb_fiber_update_self(fib->prev);
|
|
|
|
|
|
|
|
cont_compact(&fib->cont);
|
|
|
|
fiber_verify(fib);
|
|
|
|
}
|
|
|
|
|
2008-10-22 19:12:07 +04:00
|
|
|
static void
|
|
|
|
fiber_mark(void *ptr)
|
|
|
|
{
|
2017-02-13 04:05:23 +03:00
|
|
|
rb_fiber_t *fib = ptr;
|
2008-10-22 19:12:07 +04:00
|
|
|
RUBY_MARK_ENTER("cont");
|
2017-08-10 04:47:13 +03:00
|
|
|
fiber_verify(fib);
|
2019-05-31 23:25:24 +03:00
|
|
|
rb_gc_mark_no_pin(fib->first_proc);
|
2017-10-26 11:32:49 +03:00
|
|
|
if (fib->prev) rb_fiber_mark_self(fib->prev);
|
|
|
|
|
|
|
|
#if !FIBER_USE_NATIVE
|
|
|
|
if (fib->status == FIBER_TERMINATED) {
|
|
|
|
/* FIBER_TERMINATED fiber should not mark machine stack */
|
|
|
|
if (fib->cont.saved_ec.machine.stack_end != NULL) {
|
|
|
|
fib->cont.saved_ec.machine.stack_end = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-13 04:05:23 +03:00
|
|
|
cont_mark(&fib->cont);
|
2008-10-22 19:12:07 +04:00
|
|
|
RUBY_MARK_LEAVE("cont");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fiber_free(void *ptr)
|
|
|
|
{
|
2017-03-17 22:59:56 +03:00
|
|
|
rb_fiber_t *fib = ptr;
|
2008-10-22 19:12:07 +04:00
|
|
|
RUBY_FREE_ENTER("fiber");
|
2017-10-26 17:21:31 +03:00
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
if (fib->cont.saved_ec.local_storage) {
|
2017-09-10 22:00:08 +03:00
|
|
|
st_free_table(fib->cont.saved_ec.local_storage);
|
2008-10-22 19:12:07 +04:00
|
|
|
}
|
2017-03-17 22:59:56 +03:00
|
|
|
|
|
|
|
cont_free(&fib->cont);
|
2008-10-22 19:12:07 +04:00
|
|
|
RUBY_FREE_LEAVE("fiber");
|
|
|
|
}
|
|
|
|
|
2009-09-08 19:27:31 +04:00
|
|
|
static size_t
|
2009-09-09 06:22:08 +04:00
|
|
|
fiber_memsize(const void *ptr)
|
2009-09-08 19:27:31 +04:00
|
|
|
{
|
2009-09-09 06:26:06 +04:00
|
|
|
const rb_fiber_t *fib = ptr;
|
2018-09-12 23:49:19 +03:00
|
|
|
size_t size = sizeof(*fib);
|
|
|
|
const rb_execution_context_t *saved_ec = &fib->cont.saved_ec;
|
|
|
|
const rb_thread_t *th = rb_ec_thread_ptr(saved_ec);
|
2015-12-09 03:38:32 +03:00
|
|
|
|
2018-09-12 23:49:19 +03:00
|
|
|
/*
|
|
|
|
* vm.c::thread_memsize already counts th->ec->local_storage
|
|
|
|
*/
|
|
|
|
if (saved_ec->local_storage && fib != th->root_fiber) {
|
|
|
|
size += st_memsize(saved_ec->local_storage);
|
2009-09-08 19:27:31 +04:00
|
|
|
}
|
2015-12-09 03:38:32 +03:00
|
|
|
size += cont_memsize(&fib->cont);
|
2010-10-04 03:00:21 +04:00
|
|
|
return size;
|
2009-09-08 19:27:31 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 18:00:11 +04: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 23:12:43 +04:00
|
|
|
static void
|
|
|
|
cont_save_machine_stack(rb_thread_t *th, rb_context_t *cont)
|
2007-05-24 02:52:19 +04:00
|
|
|
{
|
2009-06-01 05:11:04 +04:00
|
|
|
size_t size;
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
|
2007-06-14 12:35:20 +04:00
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
if (th->ec->machine.stack_start > th->ec->machine.stack_end) {
|
|
|
|
size = cont->machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
|
|
|
|
cont->machine.stack_src = th->ec->machine.stack_end;
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
|
|
|
else {
|
2017-10-26 11:32:49 +03:00
|
|
|
size = cont->machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
|
|
|
|
cont->machine.stack_src = th->ec->machine.stack_start;
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
|
|
|
|
2014-01-28 10:09:58 +04:00
|
|
|
if (cont->machine.stack) {
|
|
|
|
REALLOC_N(cont->machine.stack, VALUE, 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
else {
|
2014-01-28 10:09:58 +04:00
|
|
|
cont->machine.stack = ALLOC_N(VALUE, 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2007-12-23 22:03:23 +03:00
|
|
|
FLUSH_REGISTER_WINDOWS;
|
2014-01-28 10:09:58 +04:00
|
|
|
MEMCPY(cont->machine.stack, cont->machine.stack_src, VALUE, 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2009-09-08 19:27:31 +04:00
|
|
|
static const rb_data_type_t cont_data_type = {
|
|
|
|
"continuation",
|
2019-05-31 23:25:24 +03:00
|
|
|
{cont_mark, cont_free, cont_memsize, cont_compact},
|
2014-12-01 09:38:04 +03:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-09-08 19:27:31 +04:00
|
|
|
};
|
|
|
|
|
2014-10-16 02:35:08 +04:00
|
|
|
static inline void
|
2011-06-14 16:57:50 +04:00
|
|
|
cont_save_thread(rb_context_t *cont, rb_thread_t *th)
|
2008-10-22 19:12:07 +04:00
|
|
|
{
|
2017-09-10 22:00:08 +03:00
|
|
|
rb_execution_context_t *sec = &cont->saved_ec;
|
2014-10-16 02:35:01 +04:00
|
|
|
|
2017-06-28 17:27:49 +03:00
|
|
|
VM_ASSERT(th->status == THREAD_RUNNABLE);
|
|
|
|
|
2008-10-22 19:12:07 +04:00
|
|
|
/* save thread context */
|
2017-10-26 11:32:49 +03:00
|
|
|
*sec = *th->ec;
|
2014-10-16 02:35:01 +04:00
|
|
|
|
2017-12-25 17:22:21 +03:00
|
|
|
/* saved_ec->machine.stack_end should be NULL */
|
2011-06-14 16:57:50 +04:00
|
|
|
/* because it may happen GC afterward */
|
2017-09-10 22:00:08 +03:00
|
|
|
sec->machine.stack_end = NULL;
|
2011-06-14 16:57:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cont_init(rb_context_t *cont, rb_thread_t *th)
|
|
|
|
{
|
|
|
|
/* save thread context */
|
|
|
|
cont_save_thread(cont, th);
|
2017-10-29 15:57:04 +03:00
|
|
|
cont->saved_ec.thread_ptr = th;
|
2017-09-10 22:00:08 +03:00
|
|
|
cont->saved_ec.local_storage = NULL;
|
|
|
|
cont->saved_ec.local_storage_recursive_hash = Qnil;
|
|
|
|
cont->saved_ec.local_storage_recursive_hash_for_trace = Qnil;
|
2018-06-23 16:41:06 +03:00
|
|
|
if (mjit_enabled) {
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 09:58:09 +03:00
|
|
|
cont->mjit_cont = mjit_cont_new(&cont->saved_ec);
|
|
|
|
}
|
2008-10-22 19: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 23:12:43 +04:00
|
|
|
static rb_context_t *
|
|
|
|
cont_new(VALUE klass)
|
|
|
|
{
|
|
|
|
rb_context_t *cont;
|
|
|
|
volatile VALUE contval;
|
2009-09-21 05: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 23:12:43 +04:00
|
|
|
|
2009-09-21 05:13:24 +04:00
|
|
|
THREAD_MUST_BE_RUNNING(th);
|
2009-09-08 19: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 23:12:43 +04:00
|
|
|
cont->self = contval;
|
2009-09-21 05: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 23:12:43 +04:00
|
|
|
return cont;
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
#if 0
|
|
|
|
void
|
|
|
|
show_vm_stack(const rb_execution_context_t *ec)
|
|
|
|
{
|
|
|
|
VALUE *p = ec->vm_stack;
|
|
|
|
while (p < ec->cfp->sp) {
|
|
|
|
fprintf(stderr, "%3d ", (int)(p - ec->vm_stack));
|
|
|
|
rb_obj_info_dump(*p);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
show_vm_pcs(const rb_control_frame_t *cfp,
|
|
|
|
const rb_control_frame_t *end_of_cfp)
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
while (cfp != end_of_cfp) {
|
|
|
|
int pc = 0;
|
|
|
|
if (cfp->iseq) {
|
|
|
|
pc = cfp->pc - cfp->iseq->body->iseq_encoded;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%2d pc: %d\n", i++, pc);
|
|
|
|
cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-07-30 10:07:48 +03:00
|
|
|
COMPILER_WARNING_PUSH
|
2018-01-02 09:41:39 +03:00
|
|
|
#ifdef __clang__
|
2018-07-30 10:07:48 +03:00
|
|
|
COMPILER_WARNING_IGNORED(-Wduplicate-decl-specifier)
|
2018-01-02 09:41:39 +03: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 23:12:43 +04:00
|
|
|
static VALUE
|
2016-12-08 02:47:59 +03:00
|
|
|
cont_capture(volatile int *volatile stat)
|
* 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 23:12:43 +04:00
|
|
|
{
|
2016-12-08 02:27:51 +03:00
|
|
|
rb_context_t *volatile cont;
|
2014-10-16 02:34:53 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2007-06-14 12:35:20 +04:00
|
|
|
volatile VALUE contval;
|
2017-10-26 11:32:49 +03:00
|
|
|
const rb_execution_context_t *ec = th->ec;
|
2007-05-30 09:56:13 +04:00
|
|
|
|
2009-09-21 05:13:24 +04:00
|
|
|
THREAD_MUST_BE_RUNNING(th);
|
2017-10-28 13:47:19 +03:00
|
|
|
rb_vm_stack_to_heap(th->ec);
|
2007-08-25 06:03:44 +04:00
|
|
|
cont = cont_new(rb_cContinuation);
|
2007-06-14 12:35:20 +04:00
|
|
|
contval = 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 23:12:43 +04:00
|
|
|
|
2008-11-18 19:19:37 +03:00
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
2017-08-22 03:41:24 +03:00
|
|
|
cont->saved_vm_stack.slen = ec->cfp->sp - ec->vm_stack;
|
|
|
|
cont->saved_vm_stack.clen = ec->vm_stack + ec->vm_stack_size - (VALUE*)ec->cfp;
|
|
|
|
cont->saved_vm_stack.ptr = ALLOC_N(VALUE, cont->saved_vm_stack.slen + cont->saved_vm_stack.clen);
|
2017-10-26 11:32:49 +03:00
|
|
|
MEMCPY(cont->saved_vm_stack.ptr,
|
|
|
|
ec->vm_stack,
|
|
|
|
VALUE, cont->saved_vm_stack.slen);
|
2017-08-22 03:41:24 +03:00
|
|
|
MEMCPY(cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
|
2017-10-26 11:32:49 +03:00
|
|
|
(VALUE*)ec->cfp,
|
|
|
|
VALUE,
|
|
|
|
cont->saved_vm_stack.clen);
|
2009-02-05 19:13:54 +03:00
|
|
|
#else
|
2017-08-22 03:41:24 +03:00
|
|
|
cont->saved_vm_stack.ptr = ALLOC_N(VALUE, ec->vm_stack_size);
|
|
|
|
MEMCPY(cont->saved_vm_stack.ptr, ec->vm_stack, VALUE, ec->vm_stack_size);
|
2008-11-18 19:19:37 +03:00
|
|
|
#endif
|
2019-06-20 06:30:29 +03:00
|
|
|
rb_ec_set_vm_stack(&cont->saved_ec, NULL, 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 23:12:43 +04:00
|
|
|
cont_save_machine_stack(th, cont);
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2013-11-15 21:15:31 +04:00
|
|
|
/* backup ensure_list to array for search in another context */
|
|
|
|
{
|
|
|
|
rb_ensure_list_t *p;
|
|
|
|
int size = 0;
|
|
|
|
rb_ensure_entry_t *entry;
|
2017-10-26 11:32:49 +03:00
|
|
|
for (p=th->ec->ensure_list; p; p=p->next)
|
2013-11-15 21:15:31 +04:00
|
|
|
size++;
|
|
|
|
entry = cont->ensure_array = ALLOC_N(rb_ensure_entry_t,size+1);
|
2017-10-26 11:32:49 +03:00
|
|
|
for (p=th->ec->ensure_list; p; p=p->next) {
|
2013-11-15 21:15:31 +04:00
|
|
|
if (!p->entry.marker)
|
|
|
|
p->entry.marker = rb_ary_tmp_new(0); /* dummy object */
|
|
|
|
*entry++ = p->entry;
|
|
|
|
}
|
|
|
|
entry->marker = 0;
|
|
|
|
}
|
|
|
|
|
2007-05-24 02:52:19 +04:00
|
|
|
if (ruby_setjmp(cont->jmpbuf)) {
|
2015-07-13 13:52:11 +03:00
|
|
|
VALUE value;
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2014-04-04 12:34:12 +04:00
|
|
|
VAR_INITIALIZED(cont);
|
2007-06-02 11:48:29 +04:00
|
|
|
value = cont->value;
|
2009-06-01 06:21:31 +04:00
|
|
|
if (cont->argc == -1) rb_exc_raise(value);
|
2007-06-02 11:48:29 +04:00
|
|
|
cont->value = Qnil;
|
2007-05-24 02:52:19 +04:00
|
|
|
*stat = 1;
|
2007-06-02 11:48:29 +04:00
|
|
|
return value;
|
2007-05-24 02: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 13:57:00 +04:00
|
|
|
return contval;
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 10:07:48 +03:00
|
|
|
COMPILER_WARNING_POP
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2017-08-10 09:01:57 +03:00
|
|
|
static inline void
|
|
|
|
fiber_restore_thread(rb_thread_t *th, rb_fiber_t *fib)
|
|
|
|
{
|
2017-10-26 11:32:49 +03:00
|
|
|
ec_switch(th, fib);
|
2017-11-06 08:41:48 +03:00
|
|
|
VM_ASSERT(th->ec->fiber_ptr == fib);
|
2017-08-10 09:01:57 +03:00
|
|
|
}
|
|
|
|
|
2014-10-16 02:35:08 +04:00
|
|
|
static inline void
|
2010-05-05 22:37:37 +04:00
|
|
|
cont_restore_thread(rb_context_t *cont)
|
2007-05-24 02:52:19 +04:00
|
|
|
{
|
2017-08-10 09:01:57 +03:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2007-05-24 02:52:19 +04:00
|
|
|
|
|
|
|
/* restore thread context */
|
2007-11-09 04:11:49 +03: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 23:12:43 +04:00
|
|
|
/* continuation */
|
2017-09-10 22:00:08 +03:00
|
|
|
rb_execution_context_t *sec = &cont->saved_ec;
|
2017-10-26 11:32:49 +03:00
|
|
|
rb_fiber_t *fib = NULL;
|
2007-06-07 07:48:13 +04:00
|
|
|
|
2017-11-06 08:41:48 +03:00
|
|
|
if (sec->fiber_ptr != NULL) {
|
|
|
|
fib = sec->fiber_ptr;
|
2017-10-26 11:32:49 +03:00
|
|
|
}
|
|
|
|
else if (th->root_fiber) {
|
|
|
|
fib = th->root_fiber;
|
|
|
|
}
|
2007-06-06 22:19:42 +04:00
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
if (fib && th->ec != &fib->cont.saved_ec) {
|
|
|
|
ec_switch(th, fib);
|
2007-06-06 22:19:42 +04:00
|
|
|
}
|
2017-10-26 11:32:49 +03:00
|
|
|
|
2018-11-26 22:59:08 +03:00
|
|
|
if (th->ec->trace_arg != sec->trace_arg) {
|
|
|
|
rb_raise(rb_eRuntimeError, "can't call across trace_func");
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
/* copy vm stack */
|
2008-11-18 19:19:37 +03:00
|
|
|
#ifdef CAPTURE_JUST_VALID_VM_STACK
|
2017-10-26 11:32:49 +03:00
|
|
|
MEMCPY(th->ec->vm_stack,
|
|
|
|
cont->saved_vm_stack.ptr,
|
|
|
|
VALUE, cont->saved_vm_stack.slen);
|
|
|
|
MEMCPY(th->ec->vm_stack + th->ec->vm_stack_size - cont->saved_vm_stack.clen,
|
|
|
|
cont->saved_vm_stack.ptr + cont->saved_vm_stack.slen,
|
|
|
|
VALUE, cont->saved_vm_stack.clen);
|
2009-02-05 19:13:54 +03:00
|
|
|
#else
|
2017-10-26 11:32:49 +03:00
|
|
|
MEMCPY(th->ec->vm_stack, cont->saved_vm_stack.ptr, VALUE, sec->vm_stack_size);
|
2008-11-18 19:19:37 +03:00
|
|
|
#endif
|
2017-06-26 10:46:11 +03:00
|
|
|
/* other members of ec */
|
2017-10-26 11:32:49 +03:00
|
|
|
|
|
|
|
th->ec->cfp = sec->cfp;
|
|
|
|
th->ec->raised_flag = sec->raised_flag;
|
|
|
|
th->ec->tag = sec->tag;
|
|
|
|
th->ec->protect_tag = sec->protect_tag;
|
|
|
|
th->ec->root_lep = sec->root_lep;
|
|
|
|
th->ec->root_svar = sec->root_svar;
|
|
|
|
th->ec->ensure_list = sec->ensure_list;
|
|
|
|
th->ec->errinfo = sec->errinfo;
|
|
|
|
|
|
|
|
VM_ASSERT(th->ec->vm_stack != NULL);
|
* 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 23:12:43 +04:00
|
|
|
}
|
2007-11-09 04:11:49 +03:00
|
|
|
else {
|
|
|
|
/* fiber */
|
2017-08-10 09:01:57 +03:00
|
|
|
fiber_restore_thread(th, (rb_fiber_t*)cont);
|
2007-11-09 04:11:49 +03:00
|
|
|
}
|
2010-05-05 22:37:37 +04:00
|
|
|
}
|
|
|
|
|
2011-06-06 18:06:59 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2018-11-20 13:18:04 +03:00
|
|
|
#if defined(FIBER_USE_COROUTINE)
|
2018-11-20 13:43:41 +03:00
|
|
|
static COROUTINE
|
|
|
|
fiber_entry(coroutine_context * from, coroutine_context * to)
|
2018-11-20 13:18:04 +03:00
|
|
|
{
|
|
|
|
rb_fiber_start();
|
|
|
|
}
|
|
|
|
#elif defined(_WIN32)
|
2010-05-05 22:37:37 +04:00
|
|
|
static void
|
|
|
|
fiber_set_stack_location(void)
|
|
|
|
{
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2010-05-16 06:46:16 +04:00
|
|
|
VALUE *ptr;
|
2010-05-08 20:15:20 +04:00
|
|
|
|
|
|
|
SET_MACHINE_STACK_END(&ptr);
|
2017-10-26 11:32:49 +03:00
|
|
|
th->ec->machine.stack_start = (void*)(((VALUE)ptr & RB_PAGE_MASK) + STACK_UPPER((void *)&ptr, 0, RB_PAGE_SIZE));
|
2010-05-05 22:37:37 +04:00
|
|
|
}
|
|
|
|
|
2018-05-08 02:52:48 +03:00
|
|
|
NORETURN(static VOID CALLBACK fiber_entry(void *arg));
|
2010-05-05 22:37:37 +04:00
|
|
|
static VOID CALLBACK
|
|
|
|
fiber_entry(void *arg)
|
|
|
|
{
|
|
|
|
fiber_set_stack_location();
|
|
|
|
rb_fiber_start();
|
|
|
|
}
|
2018-11-20 13:17:04 +03:00
|
|
|
#else
|
|
|
|
NORETURN(static void fiber_entry(void *arg));
|
|
|
|
static void
|
|
|
|
fiber_entry(void *arg)
|
|
|
|
{
|
|
|
|
rb_fiber_start();
|
|
|
|
}
|
|
|
|
#endif
|
2018-11-20 13:18:12 +03:00
|
|
|
#endif
|
2018-05-08 02:52:48 +03:00
|
|
|
|
2018-11-20 13:18:12 +03:00
|
|
|
#ifdef FIBER_ALLOCATE_STACK
|
2011-07-07 13: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-30 04:51:46 +04:00
|
|
|
* http://www.FreeBSD.org/cgi/query-pr.cgi?pr=158755
|
2011-07-07 13:09:44 +04:00
|
|
|
*/
|
2011-08-30 04:51:46 +04:00
|
|
|
#if defined(MAP_STACK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
|
2011-07-06 16:24:25 +04:00
|
|
|
#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON | MAP_STACK)
|
2010-05-05 22:37:37 +04:00
|
|
|
#else
|
2011-07-06 16:24:25 +04:00
|
|
|
#define FIBER_STACK_FLAGS (MAP_PRIVATE | MAP_ANON)
|
|
|
|
#endif
|
|
|
|
|
2018-05-08 02:14:07 +03:00
|
|
|
#define ERRNOMSG strerror(errno)
|
|
|
|
|
2011-07-06 16:31:53 +04:00
|
|
|
static char*
|
2010-05-05 22:37:37 +04:00
|
|
|
fiber_machine_stack_alloc(size_t size)
|
|
|
|
{
|
2011-07-06 16:31:53 +04:00
|
|
|
char *ptr;
|
2018-11-22 05:17:44 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD old_protect;
|
|
|
|
#endif
|
2010-05-05 22:37:37 +04:00
|
|
|
|
|
|
|
if (machine_stack_cache_index > 0) {
|
2018-11-22 05:17:44 +03:00
|
|
|
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;
|
2019-01-09 16:58:49 +03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-05 22:37:37 +04:00
|
|
|
/* TODO handle multiple machine stack size */
|
2018-11-22 05:17:44 +03:00
|
|
|
rb_bug("machine_stack_cache size is not canonicalized");
|
|
|
|
}
|
2019-01-09 16:58:49 +03:00
|
|
|
}
|
|
|
|
else {
|
2018-11-20 13:18:04 +03:00
|
|
|
#ifdef _WIN32
|
2018-11-22 05:17:44 +03:00
|
|
|
ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
rb_raise(rb_eFiberError, "can't allocate machine stack to fiber: %s", ERRNOMSG);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!VirtualProtect(ptr, RB_PAGE_SIZE, PAGE_READWRITE | PAGE_GUARD, &old_protect)) {
|
|
|
|
rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
|
|
|
|
}
|
2018-11-20 13:18:04 +03:00
|
|
|
#else
|
2018-11-22 05:17:44 +03:00
|
|
|
void *page;
|
|
|
|
STACK_GROW_DIR_DETECTION;
|
2011-07-06 16:29:09 +04:00
|
|
|
|
2018-11-22 05:17:44 +03:00
|
|
|
errno = 0;
|
|
|
|
ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, FIBER_STACK_FLAGS, -1, 0);
|
|
|
|
if (ptr == MAP_FAILED) {
|
|
|
|
rb_raise(rb_eFiberError, "can't alloc machine stack to fiber: %s", ERRNOMSG);
|
|
|
|
}
|
2011-07-06 16:29:09 +04:00
|
|
|
|
2018-11-22 05:17:44 +03:00
|
|
|
/* guard page setup */
|
|
|
|
page = ptr + STACK_DIR_UPPER(size - RB_PAGE_SIZE, 0);
|
|
|
|
if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) {
|
|
|
|
rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG);
|
|
|
|
}
|
2018-11-20 13:18:04 +03:00
|
|
|
#endif
|
2010-05-05 22:37:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
2018-11-20 13:18:21 +03:00
|
|
|
#endif
|
2010-05-05 22:37:37 +04:00
|
|
|
|
2018-11-20 13:51:27 +03:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 22:37:37 +04:00
|
|
|
static void
|
|
|
|
fiber_initialize_machine_stack_context(rb_fiber_t *fib, size_t size)
|
|
|
|
{
|
2017-09-10 22:00:08 +03:00
|
|
|
rb_execution_context_t *sec = &fib->cont.saved_ec;
|
2010-05-05 22:37:37 +04:00
|
|
|
|
2018-11-20 12:59:10 +03:00
|
|
|
#if defined(FIBER_USE_COROUTINE)
|
|
|
|
char *ptr;
|
|
|
|
STACK_GROW_DIR_DETECTION;
|
|
|
|
|
|
|
|
ptr = fiber_machine_stack_alloc(size);
|
|
|
|
fib->ss_sp = ptr;
|
|
|
|
fib->ss_size = size;
|
|
|
|
coroutine_initialize(&fib->context, fiber_entry, ptr+size, size);
|
|
|
|
sec->machine.stack_start = (VALUE*)(ptr + STACK_DIR_UPPER(0, size));
|
|
|
|
sec->machine.stack_maxsize = size - RB_PAGE_SIZE;
|
|
|
|
#elif defined(_WIN32)
|
2016-04-14 08:28:58 +03:00
|
|
|
# if defined(_MSC_VER) && _MSC_VER <= 1200
|
2015-07-21 13:52:09 +03:00
|
|
|
# define CreateFiberEx(cs, stacksize, flags, entry, param) \
|
|
|
|
CreateFiber((stacksize), (entry), (param))
|
|
|
|
# endif
|
2010-05-05 22:37:37 +04:00
|
|
|
fib->fib_handle = CreateFiberEx(size - 1, size, 0, fiber_entry, NULL);
|
2010-05-09 11: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");
|
|
|
|
}
|
|
|
|
}
|
2017-09-10 22:00:08 +03:00
|
|
|
sec->machine.stack_maxsize = size;
|
2010-05-05 22:37:37 +04:00
|
|
|
#else /* not WIN32 */
|
2011-07-06 16:31:53 +04:00
|
|
|
char *ptr;
|
2010-05-08 20:15:20 +04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2010-05-05 22:37:37 +04:00
|
|
|
|
|
|
|
ptr = fiber_machine_stack_alloc(size);
|
2014-06-07 11:11:31 +04:00
|
|
|
fib->ss_sp = ptr;
|
|
|
|
fib->ss_size = size;
|
2018-07-03 08:12:52 +03:00
|
|
|
if (fiber_context_create(&fib->context, fiber_entry, NULL, fib->ss_sp, fib->ss_size)) {
|
|
|
|
rb_raise(rb_eFiberError, "can't get context for creating fiber: %s", ERRNOMSG);
|
|
|
|
}
|
2017-09-10 22:00:08 +03:00
|
|
|
sec->machine.stack_start = (VALUE*)(ptr + STACK_DIR_UPPER(0, size));
|
|
|
|
sec->machine.stack_maxsize = size - RB_PAGE_SIZE;
|
2010-05-05 22:37:37 +04:00
|
|
|
#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)
|
|
|
|
{
|
2017-09-10 18:49:45 +03:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
2010-05-05 22:37:37 +04:00
|
|
|
|
2017-09-10 19:19:39 +03:00
|
|
|
/* save oldfib's machine stack / TODO: is it needed? */
|
2017-08-10 04:47:13 +03:00
|
|
|
if (!FIBER_TERMINATED_P(oldfib)) {
|
2010-10-12 17:18:21 +04:00
|
|
|
STACK_GROW_DIR_DETECTION;
|
2017-10-26 11:32:49 +03:00
|
|
|
SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
|
2010-05-08 20:15:20 +04:00
|
|
|
if (STACK_DIR_UPPER(0, 1)) {
|
2017-10-26 11:32:49 +03:00
|
|
|
oldfib->cont.machine.stack_size = th->ec->machine.stack_start - th->ec->machine.stack_end;
|
|
|
|
oldfib->cont.machine.stack = th->ec->machine.stack_end;
|
2010-05-08 20:15:20 +04:00
|
|
|
}
|
|
|
|
else {
|
2017-10-26 11:32:49 +03:00
|
|
|
oldfib->cont.machine.stack_size = th->ec->machine.stack_end - th->ec->machine.stack_start;
|
|
|
|
oldfib->cont.machine.stack = th->ec->machine.stack_start;
|
2010-05-05 22:37:37 +04:00
|
|
|
}
|
|
|
|
}
|
2017-09-10 18:49:45 +03:00
|
|
|
|
2010-05-05 22:37:37 +04:00
|
|
|
/* exchange machine_stack_start between oldfib and newfib */
|
2017-10-26 11:32:49 +03:00
|
|
|
oldfib->cont.saved_ec.machine.stack_start = th->ec->machine.stack_start;
|
2017-09-10 18:49:45 +03:00
|
|
|
|
2014-01-28 10:09:58 +04:00
|
|
|
/* oldfib->machine.stack_end should be NULL */
|
2017-09-10 22:00:08 +03:00
|
|
|
oldfib->cont.saved_ec.machine.stack_end = NULL;
|
2017-09-10 18:49:45 +03:00
|
|
|
|
|
|
|
/* restore thread context */
|
|
|
|
fiber_restore_thread(th, newfib);
|
|
|
|
|
2010-05-05 22:37:37 +04:00
|
|
|
/* swap machine context */
|
2018-11-20 12:59:10 +03:00
|
|
|
#if defined(FIBER_USE_COROUTINE)
|
|
|
|
coroutine_transfer(&oldfib->context, &newfib->context);
|
|
|
|
#elif defined(_WIN32)
|
2010-05-05 22:37:37 +04:00
|
|
|
SwitchToFiber(newfib->fib_handle);
|
|
|
|
#else
|
2018-11-20 12:59:10 +03:00
|
|
|
if (!newfib->context.uc_stack.ss_sp && th->root_fiber != newfib) {
|
|
|
|
rb_bug("non_root_fiber->context.uc_stac.ss_sp should not be NULL");
|
|
|
|
}
|
2010-05-21 22:55:50 +04:00
|
|
|
swapcontext(&oldfib->context, &newfib->context);
|
2010-05-05 22:37:37 +04:00
|
|
|
#endif
|
|
|
|
}
|
2018-11-20 13:51:27 +03:00
|
|
|
#endif /* FIBER_USE_NATIVE */
|
2010-05-05 22:37:37 +04:00
|
|
|
|
|
|
|
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 23:12:43 +04:00
|
|
|
|
2007-05-24 02:52:19 +04:00
|
|
|
/* restore machine stack */
|
2007-07-08 21: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
|
2014-01-28 10:09:58 +04:00
|
|
|
if (cont->machine.stack_src) {
|
2007-12-23 22:03:23 +03:00
|
|
|
FLUSH_REGISTER_WINDOWS;
|
2014-01-28 10:09:58 +04: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 23:12:43 +04:00
|
|
|
}
|
2007-05-24 02:52:19 +04:00
|
|
|
|
|
|
|
ruby_longjmp(cont->jmpbuf, 1);
|
|
|
|
}
|
|
|
|
|
2007-05-28 05:25:55 +04:00
|
|
|
NORETURN(NOINLINE(static void cont_restore_0(rb_context_t *, VALUE *)));
|
2007-05-24 02: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 23:12:43 +04:00
|
|
|
cont_restore_0(rb_context_t *cont, VALUE *addr_in_prev_frame)
|
2007-05-24 02:52:19 +04:00
|
|
|
{
|
2014-01-28 10:09:58 +04:00
|
|
|
if (cont->machine.stack_src) {
|
2009-01-17 05:11:38 +03:00
|
|
|
#ifdef HAVE_ALLOCA
|
|
|
|
#define STACK_PAD_SIZE 1
|
|
|
|
#else
|
2007-05-24 02:52:19 +04:00
|
|
|
#define STACK_PAD_SIZE 1024
|
2009-01-17 05:11:38 +03: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 23:12:43 +04:00
|
|
|
VALUE space[STACK_PAD_SIZE];
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2009-01-06 13:09:54 +03: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 23:12:43 +04:00
|
|
|
if (addr_in_prev_frame > &space[0]) {
|
|
|
|
/* Stack grows downward */
|
2009-01-06 13:09:54 +03:00
|
|
|
#endif
|
|
|
|
#if STACK_GROW_DIRECTION <= 0
|
2014-01-28 10:09:58 +04:00
|
|
|
volatile VALUE *const end = cont->machine.stack_src;
|
2009-01-17 05:11:38 +03:00
|
|
|
if (&space[0] > end) {
|
2009-01-06 13:09:54 +03:00
|
|
|
# ifdef HAVE_ALLOCA
|
2009-01-17 05:11:38 +03:00
|
|
|
volatile VALUE *sp = ALLOCA_N(VALUE, &space[0] - end);
|
2012-01-12 09:12:03 +04:00
|
|
|
space[0] = *sp;
|
2009-01-06 13:09:54 +03: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 23:12:43 +04:00
|
|
|
cont_restore_0(cont, &space[0]);
|
2009-01-06 13:09:54 +03: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 23:12:43 +04:00
|
|
|
}
|
2009-01-06 13:09:54 +03:00
|
|
|
#endif
|
|
|
|
#if !STACK_GROW_DIRECTION
|
2007-05-24 02: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 23:12:43 +04:00
|
|
|
else {
|
|
|
|
/* Stack grows upward */
|
2009-01-06 13:09:54 +03:00
|
|
|
#endif
|
|
|
|
#if STACK_GROW_DIRECTION >= 0
|
2014-01-28 10:09:58 +04:00
|
|
|
volatile VALUE *const end = cont->machine.stack_src + cont->machine.stack_size;
|
2009-01-17 05:11:38 +03:00
|
|
|
if (&space[STACK_PAD_SIZE] < end) {
|
2009-01-06 13:09:54 +03:00
|
|
|
# ifdef HAVE_ALLOCA
|
2009-01-17 05:11:38 +03:00
|
|
|
volatile VALUE *sp = ALLOCA_N(VALUE, end - &space[STACK_PAD_SIZE]);
|
2012-01-12 09:12:03 +04:00
|
|
|
space[0] = *sp;
|
2009-01-06 13:09:54 +03: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 23:12:43 +04:00
|
|
|
cont_restore_0(cont, &space[STACK_PAD_SIZE-1]);
|
2009-01-06 13:09:54 +03: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 23:12:43 +04:00
|
|
|
}
|
2009-01-06 13:09:54 +03:00
|
|
|
#endif
|
|
|
|
#if !STACK_GROW_DIRECTION
|
2007-05-24 02: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 23:12:43 +04:00
|
|
|
}
|
|
|
|
cont_restore_1(cont);
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-class: Continuation
|
|
|
|
*
|
2012-07-14 12:31:21 +04:00
|
|
|
* Continuation objects are generated by Kernel#callcc,
|
|
|
|
* after having +require+d <i>continuation</i>. They hold
|
2010-10-27 04:26:29 +04:00
|
|
|
* a return address and execution context, allowing a nonlocal return
|
2019-03-22 14:04:59 +03:00
|
|
|
* to the end of the #callcc block from anywhere within a
|
2012-07-14 12:31:21 +04:00
|
|
|
* program. Continuations are somewhat analogous to a structured
|
2010-10-27 04:26:29 +04:00
|
|
|
* version of C's <code>setjmp/longjmp</code> (although they contain
|
|
|
|
* more state, so you might consider them closer to threads).
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2007-05-24 02:52:19 +04:00
|
|
|
* For instance:
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2010-10-27 04:26:29 +04:00
|
|
|
* require "continuation"
|
2007-05-24 02:52:19 +04:00
|
|
|
* arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
|
2010-10-27 04:26:29 +04:00
|
|
|
* callcc{|cc| $cc = cc}
|
2007-05-24 02:52:19 +04:00
|
|
|
* puts(message = arr.shift)
|
|
|
|
* $cc.call unless message =~ /Max/
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2007-05-24 02:52:19 +04:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2007-05-24 02:52:19 +04:00
|
|
|
* Freddie
|
|
|
|
* Herbie
|
|
|
|
* Ron
|
|
|
|
* Max
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2015-09-20 04:07:40 +03:00
|
|
|
* Also you can call callcc in other methods:
|
|
|
|
*
|
|
|
|
* require "continuation"
|
|
|
|
*
|
|
|
|
* def g
|
|
|
|
* arr = [ "Freddie", "Herbie", "Ron", "Max", "Ringo" ]
|
|
|
|
* cc = callcc { |cc| cc }
|
|
|
|
* puts arr.shift
|
|
|
|
* return cc, arr.size
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* def f
|
|
|
|
* c, size = g
|
|
|
|
* c.call(c) if size > 1
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* f
|
|
|
|
*
|
2007-05-24 02:52:19 +04:00
|
|
|
* This (somewhat contrived) example allows the inner loop to abandon
|
|
|
|
* processing early:
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2010-10-27 04:26:29 +04:00
|
|
|
* require "continuation"
|
2007-05-24 02: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-27 04:26:29 +04:00
|
|
|
* puts
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2007-05-24 02:52:19 +04:00
|
|
|
* <em>produces:</em>
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2007-05-24 02: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-18 01:07:33 +04:00
|
|
|
* callcc {|cont| block } -> obj
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2012-07-14 12:31:21 +04:00
|
|
|
* Generates a Continuation object, which it passes to
|
2010-10-27 04:26:29 +04:00
|
|
|
* the associated block. You need to <code>require
|
|
|
|
* 'continuation'</code> before using this method. Performing a
|
2012-07-14 12:31:21 +04:00
|
|
|
* <em>cont</em><code>.call</code> will cause the #callcc
|
2010-10-27 04:26:29 +04:00
|
|
|
* to return (as will falling through the end of the block). The
|
2012-07-14 12:31:21 +04:00
|
|
|
* value returned by the #callcc is the value of the
|
2010-10-27 04:26:29 +04:00
|
|
|
* block, or the value passed to <em>cont</em><code>.call</code>. See
|
2012-07-14 12:31:21 +04:00
|
|
|
* class Continuation for more details. Also see
|
|
|
|
* Kernel#throw for an alternative mechanism for
|
2010-10-27 04:26:29 +04:00
|
|
|
* unwinding a call stack.
|
2007-05-24 02: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 23:12:43 +04:00
|
|
|
static VALUE
|
2014-06-18 10:16:39 +04:00
|
|
|
make_passing_arg(int argc, const 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 23:12:43 +04:00
|
|
|
{
|
2012-12-29 16:22:04 +04:00
|
|
|
switch (argc) {
|
2018-12-28 16:03:09 +03:00
|
|
|
case -1:
|
|
|
|
return 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 23:12:43 +04:00
|
|
|
case 0:
|
|
|
|
return Qnil;
|
|
|
|
case 1:
|
|
|
|
return argv[0];
|
|
|
|
default:
|
|
|
|
return rb_ary_new4(argc, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 21:15:31 +04:00
|
|
|
/* CAUTION!! : Currently, error in rollback_func is not supported */
|
|
|
|
/* same as rb_protect if set rollback_func to NULL */
|
|
|
|
void
|
|
|
|
ruby_register_rollback_func_for_ensure(VALUE (*ensure_func)(ANYARGS), VALUE (*rollback_func)(ANYARGS))
|
|
|
|
{
|
|
|
|
st_table **table_p = &GET_VM()->ensure_rollback_table;
|
|
|
|
if (UNLIKELY(*table_p == NULL)) {
|
|
|
|
*table_p = st_init_numtable();
|
|
|
|
}
|
|
|
|
st_insert(*table_p, (st_data_t)ensure_func, (st_data_t)rollback_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
lookup_rollback_func(VALUE (*ensure_func)(ANYARGS))
|
|
|
|
{
|
|
|
|
st_table *table = GET_VM()->ensure_rollback_table;
|
|
|
|
st_data_t val;
|
|
|
|
if (table && st_lookup(table, (st_data_t)ensure_func, &val))
|
|
|
|
return (VALUE) val;
|
|
|
|
return Qundef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
rollback_ensure_stack(VALUE self,rb_ensure_list_t *current,rb_ensure_entry_t *target)
|
|
|
|
{
|
|
|
|
rb_ensure_list_t *p;
|
|
|
|
rb_ensure_entry_t *entry;
|
2018-11-13 03:40:52 +03:00
|
|
|
size_t i, j;
|
2013-11-15 21:15:31 +04:00
|
|
|
size_t cur_size;
|
|
|
|
size_t target_size;
|
|
|
|
size_t base_point;
|
|
|
|
VALUE (*func)(ANYARGS);
|
|
|
|
|
|
|
|
cur_size = 0;
|
|
|
|
for (p=current; p; p=p->next)
|
|
|
|
cur_size++;
|
|
|
|
target_size = 0;
|
|
|
|
for (entry=target; entry->marker; entry++)
|
|
|
|
target_size++;
|
|
|
|
|
|
|
|
/* search common stack point */
|
|
|
|
p = current;
|
|
|
|
base_point = cur_size;
|
|
|
|
while (base_point) {
|
|
|
|
if (target_size >= base_point &&
|
|
|
|
p->entry.marker == target[target_size - base_point].marker)
|
|
|
|
break;
|
|
|
|
base_point --;
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rollback function check */
|
|
|
|
for (i=0; i < target_size - base_point; i++) {
|
|
|
|
if (!lookup_rollback_func(target[i].e_proc)) {
|
|
|
|
rb_raise(rb_eRuntimeError, "continuation called from out of critical rb_ensure scope");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* pop ensure stack */
|
|
|
|
while (cur_size > base_point) {
|
|
|
|
/* escape from ensure block */
|
|
|
|
(*current->entry.e_proc)(current->entry.data2);
|
|
|
|
current = current->next;
|
|
|
|
cur_size--;
|
|
|
|
}
|
|
|
|
/* push ensure stack */
|
2018-11-13 03:40:52 +03:00
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
func = (VALUE (*)(ANYARGS)) lookup_rollback_func(target[i - j - 1].e_proc);
|
|
|
|
if ((VALUE)func != Qundef) {
|
|
|
|
(*func)(target[i - j - 1].data2);
|
|
|
|
}
|
2013-11-15 21:15:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-24 02:52:19 +04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* cont.call(args, ...)
|
|
|
|
* cont[args, ...]
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2019-03-22 14:04:59 +03:00
|
|
|
* Invokes the continuation. The program continues from the end of
|
|
|
|
* the #callcc block. If no arguments are given, the original #callcc
|
|
|
|
* returns +nil+. If one argument is given, #callcc returns
|
|
|
|
* it. Otherwise, an array containing <i>args</i> is returned.
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2007-05-24 02: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)
|
|
|
|
{
|
2018-08-21 04:01:37 +03:00
|
|
|
rb_context_t *cont = cont_ptr(contval);
|
2007-05-24 02:52:19 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
|
2017-09-10 22:00:08 +03:00
|
|
|
if (cont_thread_value(cont) != th->self) {
|
2007-05-24 02:52:19 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "continuation called across threads");
|
|
|
|
}
|
2017-10-26 11:32:49 +03:00
|
|
|
if (cont->saved_ec.protect_tag != th->ec->protect_tag) {
|
2010-01-25 21:22:58 +03:00
|
|
|
rb_raise(rb_eRuntimeError, "continuation called across stack rewinding barrier");
|
|
|
|
}
|
2017-11-06 08:41:48 +03:00
|
|
|
if (cont->saved_ec.fiber_ptr) {
|
|
|
|
if (th->ec->fiber_ptr != cont->saved_ec.fiber_ptr) {
|
2007-06-15 07:20:13 +04:00
|
|
|
rb_raise(rb_eRuntimeError, "continuation called across fiber");
|
|
|
|
}
|
2007-06-06 05:55:09 +04:00
|
|
|
}
|
2017-10-26 11:32:49 +03:00
|
|
|
rollback_ensure_stack(contval, th->ec->ensure_list, cont->ensure_array);
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2008-12-01 06:00:48 +03:00
|
|
|
cont->argc = argc;
|
2007-06-02 11:48:29 +04:00
|
|
|
cont->value = make_passing_arg(argc, argv);
|
2007-05-24 02:52:19 +04:00
|
|
|
|
2007-08-23 11:55:37 +04:00
|
|
|
cont_restore_0(cont, &contval);
|
2007-05-24 02: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 23:12:43 +04:00
|
|
|
/*********/
|
|
|
|
/* fiber */
|
|
|
|
/*********/
|
|
|
|
|
2008-12-27 03:25:47 +03:00
|
|
|
/*
|
|
|
|
* Document-class: Fiber
|
|
|
|
*
|
|
|
|
* Fibers are primitives for implementing light weight cooperative
|
2009-02-22 17:23:33 +03: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-27 03:25:47 +03:00
|
|
|
*
|
|
|
|
* As opposed to other stackless light weight concurrency models, each fiber
|
2016-12-27 11:52:32 +03:00
|
|
|
* comes with a stack. This enables the fiber to be paused from deeply
|
|
|
|
* nested function calls within the fiber block. See the ruby(1)
|
|
|
|
* manpage to configure the size of the fiber stack(s).
|
2008-12-27 03:25:47 +03:00
|
|
|
*
|
2015-12-10 08:16:17 +03:00
|
|
|
* When a fiber is created it will not run automatically. Rather it must
|
2019-03-22 14:04:59 +03:00
|
|
|
* be explicitly asked to run using the Fiber#resume method.
|
2009-02-22 17:23:33 +03:00
|
|
|
* The code running inside the fiber can give up control by calling
|
2019-03-22 14:04:59 +03:00
|
|
|
* Fiber.yield in which case it yields control back to caller (the
|
|
|
|
* caller of the Fiber#resume).
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
|
|
|
* Upon yielding or termination the Fiber returns the value of the last
|
2008-12-27 03:25:47 +03:00
|
|
|
* executed expression
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* For instance:
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* fiber = Fiber.new do
|
|
|
|
* Fiber.yield 1
|
|
|
|
* 2
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* puts fiber.resume
|
|
|
|
* puts fiber.resume
|
|
|
|
* puts fiber.resume
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* <em>produces</em>
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
* FiberError: dead fiber called
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2019-03-22 14:04:59 +03:00
|
|
|
* The Fiber#resume method accepts an arbitrary number of parameters,
|
|
|
|
* if it is the first call to #resume then they will be passed as
|
|
|
|
* block arguments. Otherwise they will be the return value of the
|
|
|
|
* call to Fiber.yield
|
2008-12-27 03:25:47 +03:00
|
|
|
*
|
|
|
|
* Example:
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03: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 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* 12
|
|
|
|
* 14
|
|
|
|
* FiberError: dead fiber called
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-09-08 19:27:31 +04:00
|
|
|
static const rb_data_type_t fiber_data_type = {
|
|
|
|
"fiber",
|
2019-05-31 23:25:24 +03:00
|
|
|
{fiber_mark, fiber_free, fiber_memsize, fiber_compact,},
|
2014-12-01 09:38:04 +03:00
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
2009-09-08 19:27:31 +04:00
|
|
|
};
|
|
|
|
|
2008-11-28 18:23:09 +03:00
|
|
|
static VALUE
|
2007-11-09 04:11:49 +03: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 23:12:43 +04:00
|
|
|
{
|
2009-09-08 19:27:31 +04:00
|
|
|
return TypedData_Wrap_Struct(klass, &fiber_data_type, 0);
|
2008-11-28 18:23:09 +03:00
|
|
|
}
|
2007-11-09 04:11:49 +03:00
|
|
|
|
2008-11-28 18:23:09 +03:00
|
|
|
static rb_fiber_t*
|
|
|
|
fiber_t_alloc(VALUE fibval)
|
|
|
|
{
|
2009-09-21 05:13:24 +04:00
|
|
|
rb_fiber_t *fib;
|
|
|
|
rb_thread_t *th = GET_THREAD();
|
2008-11-28 18:23:09 +03:00
|
|
|
|
2010-11-03 20:08:35 +03:00
|
|
|
if (DATA_PTR(fibval) != 0) {
|
|
|
|
rb_raise(rb_eRuntimeError, "cannot initialize twice");
|
|
|
|
}
|
|
|
|
|
2009-09-21 05:13:24 +04:00
|
|
|
THREAD_MUST_BE_RUNNING(th);
|
2014-07-26 01:34:35 +04:00
|
|
|
fib = ZALLOC(rb_fiber_t);
|
2008-10-22 19:12:07 +04:00
|
|
|
fib->cont.self = fibval;
|
|
|
|
fib->cont.type = FIBER_CONTEXT;
|
2009-09-21 05:13:24 +04:00
|
|
|
cont_init(&fib->cont, th);
|
2017-11-06 08:41:48 +03:00
|
|
|
fib->cont.saved_ec.fiber_ptr = fib;
|
2014-10-16 02:35:08 +04:00
|
|
|
fib->prev = NULL;
|
2017-08-10 04:47:13 +03:00
|
|
|
|
|
|
|
/* fib->status == 0 == CREATED
|
|
|
|
* So that we don't need to set status: fiber_status_set(fib, FIBER_CREATED); */
|
|
|
|
VM_ASSERT(FIBER_CREATED_P(fib));
|
2007-11-09 04:11:49 +03:00
|
|
|
|
2008-11-28 18:23:09 +03:00
|
|
|
DATA_PTR(fibval) = fib;
|
|
|
|
|
2008-10-22 19:12:07 +04:00
|
|
|
return fib;
|
2007-11-09 04:11:49 +03:00
|
|
|
}
|
|
|
|
|
2016-07-28 14:02:30 +03:00
|
|
|
rb_control_frame_t *
|
2017-09-10 22:00:08 +03:00
|
|
|
rb_vm_push_frame(rb_execution_context_t *sec,
|
2016-07-28 14:02:30 +03:00
|
|
|
const rb_iseq_t *iseq,
|
|
|
|
VALUE type,
|
|
|
|
VALUE self,
|
|
|
|
VALUE specval,
|
|
|
|
VALUE cref_or_me,
|
|
|
|
const VALUE *pc,
|
|
|
|
VALUE *sp,
|
|
|
|
int local_size,
|
|
|
|
int stack_max);
|
|
|
|
|
2007-11-09 04:11:49 +03:00
|
|
|
static VALUE
|
2008-11-28 18:23:09 +03:00
|
|
|
fiber_init(VALUE fibval, VALUE proc)
|
2007-11-09 04:11:49 +03:00
|
|
|
{
|
2008-11-28 18:23:09 +03:00
|
|
|
rb_fiber_t *fib = fiber_t_alloc(fibval);
|
2008-10-22 19:12:07 +04:00
|
|
|
rb_context_t *cont = &fib->cont;
|
2017-09-10 22:00:08 +03:00
|
|
|
rb_execution_context_t *sec = &cont->saved_ec;
|
2014-10-16 02:35:01 +04:00
|
|
|
rb_thread_t *cth = GET_THREAD();
|
2018-09-12 23:49:10 +03:00
|
|
|
rb_vm_t *vm = cth->vm;
|
|
|
|
size_t fib_stack_bytes = vm->default_params.fiber_vm_stack_size;
|
|
|
|
size_t thr_stack_bytes = vm->default_params.thread_vm_stack_size;
|
|
|
|
VALUE *vm_stack;
|
* 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 23:12:43 +04:00
|
|
|
|
2008-10-22 19:12:07 +04:00
|
|
|
/* initialize cont */
|
2017-08-22 03:41:24 +03:00
|
|
|
cont->saved_vm_stack.ptr = NULL;
|
2018-09-12 23:49:10 +03:00
|
|
|
if (fib_stack_bytes == thr_stack_bytes) {
|
|
|
|
vm_stack = rb_thread_recycle_stack(fib_stack_bytes / sizeof(VALUE));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm_stack = ruby_xmalloc(fib_stack_bytes);
|
|
|
|
}
|
2019-06-05 14:39:17 +03:00
|
|
|
cont->free_vm_stack = 1;
|
2019-06-19 09:47:15 +03:00
|
|
|
rb_ec_initialize_vm_stack(sec, vm_stack, fib_stack_bytes / sizeof(VALUE));
|
2016-07-28 14:02:30 +03:00
|
|
|
|
2017-09-10 22:00:08 +03:00
|
|
|
sec->tag = NULL;
|
|
|
|
sec->local_storage = NULL;
|
|
|
|
sec->local_storage_recursive_hash = Qnil;
|
|
|
|
sec->local_storage_recursive_hash_for_trace = 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 23:12:43 +04:00
|
|
|
|
2017-06-28 18:25:30 +03:00
|
|
|
fib->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 23:12:43 +04:00
|
|
|
|
2011-06-06 18:06:59 +04:00
|
|
|
#if !FIBER_USE_NATIVE
|
2014-10-16 02:35:01 +04:00
|
|
|
MEMCPY(&cont->jmpbuf, &cth->root_jmpbuf, rb_jmpbuf_t, 1);
|
2010-05-05 22: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 23:12:43 +04:00
|
|
|
|
2008-10-22 19: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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2009-09-21 15:06:32 +04:00
|
|
|
/* :nodoc: */
|
2008-11-28 18:23:09 +03:00
|
|
|
static VALUE
|
|
|
|
rb_fiber_init(VALUE fibval)
|
2007-08-26 07:31:20 +04:00
|
|
|
{
|
2008-11-28 18:23:09 +03:00
|
|
|
return fiber_init(fibval, rb_block_proc());
|
2007-08-26 07:31:20 +04:00
|
|
|
}
|
|
|
|
|
2008-11-28 18:23:09 +03:00
|
|
|
VALUE
|
|
|
|
rb_fiber_new(VALUE (*func)(ANYARGS), VALUE obj)
|
2007-08-26 07:31:20 +04:00
|
|
|
{
|
2008-11-28 18:23:09 +03:00
|
|
|
return fiber_init(fiber_alloc(rb_cFiber), rb_proc_new(func, obj));
|
2007-08-26 07:31:20 +04:00
|
|
|
}
|
|
|
|
|
2017-11-06 10:44:28 +03:00
|
|
|
static void rb_fiber_terminate(rb_fiber_t *fib, int need_interrupt);
|
2007-08-21 22:51:39 +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 23:12:43 +04:00
|
|
|
void
|
|
|
|
rb_fiber_start(void)
|
|
|
|
{
|
2017-10-26 11:32:49 +03:00
|
|
|
rb_thread_t * volatile th = GET_THREAD();
|
2017-11-06 08:41:48 +03:00
|
|
|
rb_fiber_t *fib = th->ec->fiber_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 23:12:43 +04:00
|
|
|
rb_proc_t *proc;
|
2017-06-23 10:25:52 +03:00
|
|
|
enum ruby_tag_type state;
|
2017-11-06 10:44:28 +03:00
|
|
|
int need_interrupt = TRUE;
|
* 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 23:12:43 +04:00
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
VM_ASSERT(th->ec == ruby_current_execution_context_ptr);
|
2017-08-10 04:47:13 +03:00
|
|
|
VM_ASSERT(FIBER_RESUMED_P(fib));
|
|
|
|
|
2017-10-26 14:02:13 +03:00
|
|
|
EC_PUSH_TAG(th->ec);
|
2017-12-06 06:16:08 +03:00
|
|
|
if ((state = EC_EXEC_TAG()) == TAG_NONE) {
|
2014-04-04 12:34:12 +04:00
|
|
|
rb_context_t *cont = &VAR_FROM_MEMORY(fib)->cont;
|
2008-12-01 06:00:48 +03:00
|
|
|
int argc;
|
2013-08-07 11:19:57 +04:00
|
|
|
const VALUE *argv, args = cont->value;
|
2017-06-28 18:25:30 +03:00
|
|
|
GetProcPtr(fib->first_proc, proc);
|
* include/ruby/ruby.h: rename RARRAY_RAWPTR() to RARRAY_CONST_PTR().
RARRAY_RAWPTR(ary) returns (const VALUE *) type pointer and
usecase of this macro is not acquire raw pointer, but acquire
read-only pointer. So we rename to better name.
RSTRUCT_RAWPTR() is also renamed to RSTRUCT_CONST_PTR()
(I expect that nobody use it).
* array.c, compile.c, cont.c, enumerator.c, gc.c, proc.c, random.c,
string.c, struct.c, thread.c, vm_eval.c, vm_insnhelper.c:
catch up this change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43043 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-09-25 12:24:34 +04:00
|
|
|
argv = (argc = cont->argc) > 1 ? RARRAY_CONST_PTR(args) : &args;
|
2007-06-02 11:48:29 +04:00
|
|
|
cont->value = Qnil;
|
2017-10-26 11:32:49 +03:00
|
|
|
th->ec->errinfo = Qnil;
|
|
|
|
th->ec->root_lep = rb_vm_proc_local_ep(fib->first_proc);
|
|
|
|
th->ec->root_svar = Qfalse;
|
2015-08-21 12:51:01 +03:00
|
|
|
|
2017-10-29 16:19:14 +03:00
|
|
|
EXEC_EVENT_HOOK(th->ec, RUBY_EVENT_FIBER_SWITCH, th->self, 0, 0, 0, Qnil);
|
2017-10-27 09:06:31 +03:00
|
|
|
cont->value = rb_vm_invoke_proc(th->ec, proc, argc, argv, VM_BLOCK_HANDLER_NONE);
|
* 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 23:12:43 +04:00
|
|
|
}
|
2017-10-26 14:02:13 +03:00
|
|
|
EC_POP_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 23:12:43 +04:00
|
|
|
|
|
|
|
if (state) {
|
2017-11-06 10:44:28 +03:00
|
|
|
VALUE err = th->ec->errinfo;
|
2017-08-10 04:47:13 +03:00
|
|
|
VM_ASSERT(FIBER_RESUMED_P(fib));
|
|
|
|
|
2012-12-22 17:08:24 +04:00
|
|
|
if (state == TAG_RAISE || state == TAG_FATAL) {
|
2017-11-06 10:44:28 +03:00
|
|
|
rb_threadptr_pending_interrupt_enque(th, err);
|
2007-08-21 22:51:39 +04:00
|
|
|
}
|
|
|
|
else {
|
2017-11-06 10:44:28 +03:00
|
|
|
err = rb_vm_make_jump_tag_but_local_jump(state, err);
|
|
|
|
if (!NIL_P(err)) {
|
2012-12-23 14:18:58 +04:00
|
|
|
rb_threadptr_pending_interrupt_enque(th, err);
|
2017-11-06 10:44:28 +03:00
|
|
|
}
|
2007-08-21 22:51:39 +04:00
|
|
|
}
|
2017-11-06 10:44:28 +03:00
|
|
|
need_interrupt = TRUE;
|
* 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2017-11-06 10:44:28 +03:00
|
|
|
rb_fiber_terminate(fib, need_interrupt);
|
2017-08-10 09:26:52 +03:00
|
|
|
VM_UNREACHABLE(rb_fiber_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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2008-10-22 19:12:07 +04:00
|
|
|
static rb_fiber_t *
|
|
|
|
root_fiber_alloc(rb_thread_t *th)
|
|
|
|
{
|
2017-10-26 11:32:49 +03:00
|
|
|
VALUE fibval = fiber_alloc(rb_cFiber);
|
2017-11-06 08:41:48 +03:00
|
|
|
rb_fiber_t *fib = th->ec->fiber_ptr;
|
2017-10-26 11:32:49 +03:00
|
|
|
|
|
|
|
VM_ASSERT(DATA_PTR(fibval) == NULL);
|
2018-09-12 23:49:24 +03:00
|
|
|
VM_ASSERT(fib->cont.type == FIBER_CONTEXT);
|
2017-10-26 11:32:49 +03:00
|
|
|
VM_ASSERT(fib->status == FIBER_RESUMED);
|
|
|
|
|
|
|
|
th->root_fiber = fib;
|
|
|
|
DATA_PTR(fibval) = fib;
|
|
|
|
fib->cont.self = fibval;
|
2018-04-04 11:19:28 +03:00
|
|
|
|
2011-06-06 18:06:59 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2018-11-20 12:59:10 +03:00
|
|
|
#if defined(FIBER_USE_COROUTINE)
|
|
|
|
coroutine_initialize(&fib->context, NULL, NULL, 0);
|
|
|
|
#elif defined(_WIN32)
|
2018-04-04 11:19:28 +03:00
|
|
|
/* setup fib_handle for root Fiber */
|
2017-10-26 11:32:49 +03:00
|
|
|
if (fib->fib_handle == 0) {
|
2018-04-04 11:19:28 +03:00
|
|
|
if ((fib->fib_handle = ConvertThreadToFiber(0)) == 0) {
|
|
|
|
rb_bug("root_fiber_alloc: ConvertThreadToFiber() failed - %s\n", rb_w32_strerror(-1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_bug("root_fiber_alloc: fib_handle is not NULL.");
|
2017-10-26 11:32:49 +03:00
|
|
|
}
|
2010-05-05 22:37:37 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
2018-04-04 11:19:28 +03:00
|
|
|
|
2008-10-22 19:12:07 +04:00
|
|
|
return fib;
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
void
|
2018-04-04 11:19:28 +03:00
|
|
|
rb_threadptr_root_fiber_setup(rb_thread_t *th)
|
2017-10-26 11:32:49 +03:00
|
|
|
{
|
|
|
|
rb_fiber_t *fib = ruby_mimmalloc(sizeof(rb_fiber_t));
|
|
|
|
MEMZERO(fib, rb_fiber_t, 1);
|
2018-09-12 23:49:24 +03:00
|
|
|
fib->cont.type = FIBER_CONTEXT;
|
2017-11-06 08:41:48 +03:00
|
|
|
fib->cont.saved_ec.fiber_ptr = fib;
|
2017-10-29 15:57:04 +03:00
|
|
|
fib->cont.saved_ec.thread_ptr = th;
|
2017-10-26 11:32:49 +03:00
|
|
|
fiber_status_set(fib, FIBER_RESUMED); /* skip CREATED */
|
|
|
|
th->ec = &fib->cont.saved_ec;
|
2018-04-03 13:21:47 +03:00
|
|
|
|
2019-06-05 14:39:17 +03:00
|
|
|
VM_ASSERT(fib->cont.free_vm_stack == 0);
|
2019-06-05 09:23:04 +03:00
|
|
|
|
2018-04-04 11:19:28 +03:00
|
|
|
/* NOTE: On WIN32, fib_handle is not allocated yet. */
|
2017-10-26 11:32:49 +03:00
|
|
|
}
|
|
|
|
|
2017-10-26 17:21:31 +03:00
|
|
|
void
|
|
|
|
rb_threadptr_root_fiber_release(rb_thread_t *th)
|
|
|
|
{
|
|
|
|
if (th->root_fiber) {
|
|
|
|
/* ignore. A root fiber object will free th->ec */
|
|
|
|
}
|
|
|
|
else {
|
2018-09-12 23:49:24 +03:00
|
|
|
VM_ASSERT(th->ec->fiber_ptr->cont.type == FIBER_CONTEXT);
|
2017-11-06 08:41:48 +03:00
|
|
|
VM_ASSERT(th->ec->fiber_ptr->cont.self == 0);
|
2019-06-05 14:39:17 +03:00
|
|
|
|
2019-06-19 14:40:49 +03:00
|
|
|
// th->ec->fiber_ptr->cont.saved_ec.vm_stack = NULL;
|
2017-11-06 08:41:48 +03:00
|
|
|
fiber_free(th->ec->fiber_ptr);
|
2017-10-26 17:21:31 +03:00
|
|
|
|
|
|
|
if (th->ec == ruby_current_execution_context_ptr) {
|
|
|
|
ruby_current_execution_context_ptr = NULL;
|
|
|
|
}
|
|
|
|
th->ec = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 02:35:08 +04:00
|
|
|
static inline rb_fiber_t*
|
|
|
|
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 23:12:43 +04:00
|
|
|
{
|
2017-11-06 11:22:27 +03:00
|
|
|
rb_execution_context_t *ec = GET_EC();
|
|
|
|
if (ec->fiber_ptr->cont.self == 0) {
|
|
|
|
root_fiber_alloc(rb_ec_thread_ptr(ec));
|
* 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 23:12:43 +04:00
|
|
|
}
|
2017-11-06 11:22:27 +03:00
|
|
|
return ec->fiber_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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2014-10-16 02:35:08 +04:00
|
|
|
static inline rb_fiber_t*
|
|
|
|
return_fiber(void)
|
|
|
|
{
|
|
|
|
rb_fiber_t *fib = fiber_current();
|
|
|
|
rb_fiber_t *prev = fib->prev;
|
|
|
|
|
|
|
|
if (!prev) {
|
2017-10-26 17:38:22 +03:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
rb_fiber_t *root_fiber = th->root_fiber;
|
|
|
|
|
|
|
|
VM_ASSERT(root_fiber != NULL);
|
2014-10-16 02:35:08 +04:00
|
|
|
|
|
|
|
if (root_fiber == fib) {
|
|
|
|
rb_raise(rb_eFiberError, "can't yield from root fiber");
|
|
|
|
}
|
|
|
|
return root_fiber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fib->prev = NULL;
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_fiber_current(void)
|
|
|
|
{
|
|
|
|
return fiber_current()->cont.self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline VALUE
|
|
|
|
fiber_store(rb_fiber_t *next_fib, rb_thread_t *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 23:12:43 +04:00
|
|
|
{
|
2008-10-22 19: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 23:12:43 +04:00
|
|
|
|
2017-11-06 08:41:48 +03:00
|
|
|
if (th->ec->fiber_ptr != NULL) {
|
2018-11-22 05:17:44 +03:00
|
|
|
fib = th->ec->fiber_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 23:12:43 +04:00
|
|
|
}
|
|
|
|
else {
|
2018-11-22 05:17:44 +03:00
|
|
|
/* create root fiber */
|
|
|
|
fib = root_fiber_alloc(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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2017-08-10 04:47:13 +03:00
|
|
|
VM_ASSERT(FIBER_RESUMED_P(fib) || FIBER_TERMINATED_P(fib));
|
|
|
|
VM_ASSERT(FIBER_RUNNABLE_P(next_fib));
|
|
|
|
|
2017-08-10 07:03:48 +03:00
|
|
|
#if FIBER_USE_NATIVE
|
2017-08-10 04:47:13 +03:00
|
|
|
if (FIBER_CREATED_P(next_fib)) {
|
2018-11-22 05:17:44 +03:00
|
|
|
fiber_initialize_machine_stack_context(next_fib, th->vm->default_params.fiber_machine_stack_size);
|
2017-08-10 04:47:13 +03:00
|
|
|
}
|
2017-08-10 07:03:48 +03:00
|
|
|
#endif
|
2017-08-10 04:47:13 +03:00
|
|
|
|
|
|
|
if (FIBER_RESUMED_P(fib)) fiber_status_set(fib, FIBER_SUSPENDED);
|
2017-09-10 21:37:55 +03:00
|
|
|
|
|
|
|
#if FIBER_USE_NATIVE == 0
|
|
|
|
/* should (re-)allocate stack are before fib->status change to pass fiber_verify() */
|
|
|
|
cont_save_machine_stack(th, &fib->cont);
|
|
|
|
#endif
|
|
|
|
|
2017-08-10 04:47:13 +03:00
|
|
|
fiber_status_set(next_fib, FIBER_RESUMED);
|
|
|
|
|
2011-10-14 01:16:46 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2014-10-16 02:34:43 +04:00
|
|
|
fiber_setcontext(next_fib, fib);
|
|
|
|
/* restored */
|
2018-05-07 09:59:55 +03:00
|
|
|
#ifdef MAX_MACHINE_STACK_CACHE
|
2014-10-16 02:34:43 +04:00
|
|
|
if (terminated_machine_stack.ptr) {
|
2018-11-22 05:17:44 +03:00
|
|
|
if (machine_stack_cache_index < MAX_MACHINE_STACK_CACHE) {
|
|
|
|
machine_stack_cache[machine_stack_cache_index++] = terminated_machine_stack;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (terminated_machine_stack.ptr != fib->cont.machine.stack) {
|
2018-11-20 13:18:04 +03:00
|
|
|
#ifdef _WIN32
|
2018-11-22 05:17:44 +03:00
|
|
|
VirtualFree(terminated_machine_stack.ptr, 0, MEM_RELEASE);
|
2018-11-20 13:18:04 +03:00
|
|
|
#else
|
|
|
|
munmap((void*)terminated_machine_stack.ptr, terminated_machine_stack.size * sizeof(VALUE));
|
|
|
|
#endif
|
2018-11-22 05:17:44 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_bug("terminated fiber resumed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
terminated_machine_stack.ptr = NULL;
|
|
|
|
terminated_machine_stack.size = 0;
|
2014-10-16 02:34:43 +04:00
|
|
|
}
|
2018-12-28 16:03:18 +03:00
|
|
|
#endif /* MAX_MACHINE_STACK_CACHE */
|
2017-11-06 08:41:48 +03:00
|
|
|
fib = th->ec->fiber_ptr;
|
2014-10-16 02:34:43 +04:00
|
|
|
if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value);
|
|
|
|
return fib->cont.value;
|
|
|
|
|
|
|
|
#else /* FIBER_USE_NATIVE */
|
2018-12-01 06:49:52 +03:00
|
|
|
fib->cont.saved_ec.machine.stack_end = NULL;
|
2014-10-16 02:34:43 +04:00
|
|
|
if (ruby_setjmp(fib->cont.jmpbuf)) {
|
2018-11-22 05:17:44 +03:00
|
|
|
/* restored */
|
|
|
|
fib = th->ec->fiber_ptr;
|
|
|
|
if (fib->cont.argc == -1) rb_exc_raise(fib->cont.value);
|
|
|
|
if (next_fib->cont.value == Qundef) {
|
|
|
|
cont_restore_0(&next_fib->cont, &next_fib->cont.value);
|
|
|
|
VM_UNREACHABLE(fiber_store);
|
|
|
|
}
|
|
|
|
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 23:12:43 +04:00
|
|
|
}
|
|
|
|
else {
|
2018-11-22 05:17:44 +03:00
|
|
|
VALUE undef = Qundef;
|
|
|
|
cont_restore_0(&next_fib->cont, &undef);
|
|
|
|
VM_UNREACHABLE(fiber_store);
|
* 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 23:12:43 +04:00
|
|
|
}
|
2014-10-16 02:34:43 +04:00
|
|
|
#endif /* 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 22:51:39 +04:00
|
|
|
static inline VALUE
|
2014-10-16 02:35:08 +04:00
|
|
|
fiber_switch(rb_fiber_t *fib, int argc, const 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 23:12:43 +04:00
|
|
|
{
|
2007-06-02 11:48:29 +04:00
|
|
|
VALUE value;
|
2014-10-16 02:35:08 +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 23:12:43 +04:00
|
|
|
rb_thread_t *th = GET_THREAD();
|
|
|
|
|
2017-10-26 17:38:22 +03:00
|
|
|
/* make sure the root_fiber object is available */
|
|
|
|
if (th->root_fiber == NULL) root_fiber_alloc(th);
|
|
|
|
|
2017-11-06 08:41:48 +03:00
|
|
|
if (th->ec->fiber_ptr == fib) {
|
2011-11-21 01:17:57 +04:00
|
|
|
/* ignore fiber context switch
|
|
|
|
* because destination fiber is same as current fiber
|
|
|
|
*/
|
|
|
|
return make_passing_arg(argc, argv);
|
|
|
|
}
|
|
|
|
|
2017-09-10 22:00:08 +03:00
|
|
|
if (cont_thread_value(cont) != th->self) {
|
2007-06-02 11: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 23:12:43 +04:00
|
|
|
}
|
2017-10-26 11:32:49 +03:00
|
|
|
else if (cont->saved_ec.protect_tag != th->ec->protect_tag) {
|
2010-01-25 21:22:58 +03:00
|
|
|
rb_raise(rb_eFiberError, "fiber called across stack rewinding barrier");
|
|
|
|
}
|
2017-08-10 04:47:13 +03:00
|
|
|
else if (FIBER_TERMINATED_P(fib)) {
|
2009-06-01 06:21:31 +04:00
|
|
|
value = rb_exc_new2(rb_eFiberError, "dead fiber called");
|
2014-10-16 02:34:43 +04:00
|
|
|
|
2017-11-06 08:41:48 +03:00
|
|
|
if (!FIBER_TERMINATED_P(th->ec->fiber_ptr)) {
|
2017-08-10 04:47:13 +03:00
|
|
|
rb_exc_raise(value);
|
|
|
|
VM_UNREACHABLE(fiber_switch);
|
|
|
|
}
|
|
|
|
else {
|
2017-12-07 00:51:10 +03:00
|
|
|
/* th->ec->fiber_ptr is also dead => switch to root fiber */
|
2017-08-10 04:47:13 +03:00
|
|
|
/* (this means we're being called from rb_fiber_terminate, */
|
|
|
|
/* and the terminated fiber's return_fiber() is already dead) */
|
|
|
|
VM_ASSERT(FIBER_SUSPENDED_P(th->root_fiber));
|
|
|
|
|
|
|
|
cont = &th->root_fiber->cont;
|
|
|
|
cont->argc = -1;
|
|
|
|
cont->value = value;
|
2011-06-06 18:06:59 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2017-11-06 08:41:48 +03:00
|
|
|
fiber_setcontext(th->root_fiber, th->ec->fiber_ptr);
|
2010-05-05 22:37:37 +04:00
|
|
|
#else
|
2017-08-10 04:47:13 +03:00
|
|
|
cont_restore_0(cont, &value);
|
2010-05-05 22:37:37 +04:00
|
|
|
#endif
|
2017-08-10 04:47:13 +03:00
|
|
|
VM_UNREACHABLE(fiber_switch);
|
|
|
|
}
|
* 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 22:51:39 +04:00
|
|
|
if (is_resume) {
|
2014-10-16 02:35:08 +04:00
|
|
|
fib->prev = fiber_current();
|
2007-08-21 22:51:39 +04:00
|
|
|
}
|
2007-08-25 12:51:59 +04:00
|
|
|
|
2017-08-10 04:47:13 +03:00
|
|
|
VM_ASSERT(FIBER_RUNNABLE_P(fib));
|
|
|
|
|
2008-12-01 06:00:48 +03:00
|
|
|
cont->argc = argc;
|
2007-06-02 11:48:29 +04:00
|
|
|
cont->value = make_passing_arg(argc, argv);
|
2014-10-16 02:35:08 +04:00
|
|
|
value = fiber_store(fib, th);
|
2017-11-06 10:44:28 +03:00
|
|
|
RUBY_VM_CHECK_INTS(th->ec);
|
2007-08-21 22:51:39 +04:00
|
|
|
|
2017-10-29 16:19:14 +03:00
|
|
|
EXEC_EVENT_HOOK(th->ec, RUBY_EVENT_FIBER_SWITCH, th->self, 0, 0, 0, Qnil);
|
2015-08-21 12:51:01 +03:00
|
|
|
|
2007-06-02 11: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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 22:51:39 +04:00
|
|
|
VALUE
|
2014-10-16 02:35:08 +04:00
|
|
|
rb_fiber_transfer(VALUE fibval, int argc, const VALUE *argv)
|
2007-08-15 21:56:22 +04:00
|
|
|
{
|
2018-08-21 04:01:37 +03:00
|
|
|
return fiber_switch(fiber_ptr(fibval), argc, argv, 0);
|
2007-08-15 21:56:22 +04:00
|
|
|
}
|
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
void
|
|
|
|
rb_fiber_close(rb_fiber_t *fib)
|
|
|
|
{
|
2018-09-12 23:49:10 +03:00
|
|
|
rb_execution_context_t *ec = &fib->cont.saved_ec;
|
|
|
|
VALUE *vm_stack = ec->vm_stack;
|
|
|
|
size_t stack_bytes = ec->vm_stack_size * sizeof(VALUE);
|
|
|
|
|
2017-10-26 11:32:49 +03:00
|
|
|
fiber_status_set(fib, FIBER_TERMINATED);
|
2019-06-05 14:39:17 +03:00
|
|
|
if (fib->cont.free_vm_stack) {
|
|
|
|
if (stack_bytes == rb_ec_vm_ptr(ec)->default_params.thread_vm_stack_size) {
|
|
|
|
rb_thread_recycle_stack_release(vm_stack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ruby_xfree(vm_stack);
|
|
|
|
}
|
2017-10-26 11:32:49 +03:00
|
|
|
}
|
2019-06-05 14:39:17 +03:00
|
|
|
|
2019-06-20 06:30:29 +03:00
|
|
|
rb_ec_set_vm_stack(ec, NULL, 0);
|
2017-10-26 11:32:49 +03:00
|
|
|
|
|
|
|
#if !FIBER_USE_NATIVE
|
|
|
|
/* should not mark machine stack any more */
|
2018-09-12 23:49:10 +03:00
|
|
|
ec->machine.stack_end = NULL;
|
2017-10-26 11:32:49 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-10-16 02:35:08 +04:00
|
|
|
static void
|
2017-11-06 10:44:28 +03:00
|
|
|
rb_fiber_terminate(rb_fiber_t *fib, int need_interrupt)
|
2014-10-16 02:35:08 +04:00
|
|
|
{
|
|
|
|
VALUE value = fib->cont.value;
|
2017-11-06 10:44:28 +03:00
|
|
|
rb_fiber_t *ret_fib;
|
2017-08-10 04:47:13 +03:00
|
|
|
|
2017-11-06 10:44:28 +03:00
|
|
|
VM_ASSERT(FIBER_RESUMED_P(fib));
|
2017-10-26 11:32:49 +03:00
|
|
|
rb_fiber_close(fib);
|
|
|
|
|
2018-11-20 12:59:10 +03:00
|
|
|
#if FIBER_USE_NATIVE
|
|
|
|
#if defined(FIBER_USE_COROUTINE)
|
|
|
|
coroutine_destroy(&fib->context);
|
|
|
|
#elif !defined(_WIN32)
|
2018-05-07 09:59:55 +03:00
|
|
|
fib->context.uc_stack.ss_sp = NULL;
|
|
|
|
#endif
|
2018-11-20 13:18:08 +03:00
|
|
|
|
2018-05-07 09:59:55 +03:00
|
|
|
#ifdef MAX_MACHINE_STACK_CACHE
|
2014-10-16 02:35:08 +04:00
|
|
|
/* Ruby must not switch to other thread until storing terminated_machine_stack */
|
|
|
|
terminated_machine_stack.ptr = fib->ss_sp;
|
|
|
|
terminated_machine_stack.size = fib->ss_size / sizeof(VALUE);
|
|
|
|
fib->ss_sp = NULL;
|
|
|
|
fib->cont.machine.stack = NULL;
|
|
|
|
fib->cont.machine.stack_size = 0;
|
2018-11-20 13:18:08 +03:00
|
|
|
#endif
|
2014-10-16 02:35:08 +04:00
|
|
|
#endif
|
2017-10-26 11:32:49 +03:00
|
|
|
|
2017-11-06 10:44:28 +03:00
|
|
|
ret_fib = return_fiber();
|
|
|
|
if (need_interrupt) RUBY_VM_SET_INTERRUPT(&ret_fib->cont.saved_ec);
|
|
|
|
fiber_switch(ret_fib, 1, &value, 0);
|
2014-10-16 02:35:08 +04:00
|
|
|
}
|
|
|
|
|
2007-08-21 22:51:39 +04:00
|
|
|
VALUE
|
2014-06-18 10:16:39 +04:00
|
|
|
rb_fiber_resume(VALUE fibval, int argc, const 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 23:12:43 +04:00
|
|
|
{
|
2018-08-21 04:01:37 +03:00
|
|
|
rb_fiber_t *fib = fiber_ptr(fibval);
|
2007-08-21 22:51:39 +04:00
|
|
|
|
2018-12-28 16:03:14 +03:00
|
|
|
if (argc == -1 && FIBER_CREATED_P(fib)) {
|
|
|
|
rb_raise(rb_eFiberError, "cannot raise exception on unborn fiber");
|
|
|
|
}
|
|
|
|
|
2018-09-12 23:49:24 +03:00
|
|
|
if (fib->prev != 0 || fiber_is_root_p(fib)) {
|
2007-08-21 22:51:39 +04:00
|
|
|
rb_raise(rb_eFiberError, "double resume");
|
|
|
|
}
|
2018-12-28 16:03:14 +03:00
|
|
|
|
2015-12-25 02:23:46 +03:00
|
|
|
if (fib->transferred != 0) {
|
2011-11-09 08:26:39 +04:00
|
|
|
rb_raise(rb_eFiberError, "cannot resume transferred Fiber");
|
|
|
|
}
|
2007-08-21 22:51:39 +04:00
|
|
|
|
2014-10-16 02:35:08 +04:00
|
|
|
return fiber_switch(fib, argc, argv, 1);
|
2007-08-21 22:51:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
2014-06-18 10:16:39 +04:00
|
|
|
rb_fiber_yield(int argc, const VALUE *argv)
|
2007-08-21 22:51:39 +04:00
|
|
|
{
|
2014-10-16 02:35:08 +04:00
|
|
|
return fiber_switch(return_fiber(), 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2012-02-15 18:00:11 +04:00
|
|
|
void
|
2018-11-06 13:19:55 +03:00
|
|
|
rb_fiber_reset_root_local_storage(rb_thread_t *th)
|
2012-02-15 18:00:11 +04:00
|
|
|
{
|
2017-11-06 08:41:48 +03:00
|
|
|
if (th->root_fiber && th->root_fiber != th->ec->fiber_ptr) {
|
2017-10-26 11:32:49 +03:00
|
|
|
th->ec->local_storage = th->root_fiber->cont.saved_ec.local_storage;
|
2012-02-15 18:00:11 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-27 03:25:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.alive? -> true or false
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2010-10-27 04: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-27 03:25:47 +03:00
|
|
|
*/
|
2007-08-06 20:41:17 +04:00
|
|
|
VALUE
|
2008-10-22 19: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 23:12:43 +04:00
|
|
|
{
|
2018-08-21 04:01:37 +03:00
|
|
|
return FIBER_TERMINATED_P(fiber_ptr(fibval)) ? Qfalse : Qtrue;
|
* 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2008-12-27 03:25:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.resume(args, ...) -> obj
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2019-03-22 14:04:59 +03:00
|
|
|
* Resumes the fiber from the point at which the last Fiber.yield was
|
|
|
|
* called, or starts running it if it is the first call to
|
|
|
|
* #resume. Arguments passed to resume will be the value of the
|
|
|
|
* Fiber.yield expression or will be passed as block parameters to
|
|
|
|
* the fiber's block if this is the first #resume.
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* Alternatively, when resume is called it evaluates to the arguments passed
|
2019-03-22 14:04:59 +03:00
|
|
|
* to the next Fiber.yield statement inside the fiber's block
|
2008-12-27 03:25:47 +03:00
|
|
|
* or to the block value if it runs to completion without any
|
2019-03-22 14:04:59 +03:00
|
|
|
* Fiber.yield
|
2008-12-27 03:25:47 +03: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 23:12:43 +04:00
|
|
|
static VALUE
|
2007-08-21 22: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 23:12:43 +04:00
|
|
|
{
|
2007-08-21 22:51:39 +04:00
|
|
|
return rb_fiber_resume(fib, argc, argv);
|
|
|
|
}
|
|
|
|
|
2018-12-28 16:03:09 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.raise -> obj
|
|
|
|
* fiber.raise(string) -> obj
|
|
|
|
* fiber.raise(exception [, string [, array]]) -> obj
|
|
|
|
*
|
|
|
|
* Raises an exception in the fiber at the point at which the last
|
2019-03-22 14:04:59 +03:00
|
|
|
* Fiber.yield was called, or at the start if neither +resume+
|
2018-12-28 16:03:09 +03:00
|
|
|
* nor +raise+ were called before.
|
|
|
|
*
|
|
|
|
* With no arguments, raises a +RuntimeError+. With a single +String+
|
|
|
|
* argument, raises a +RuntimeError+ with the string as a message. Otherwise,
|
|
|
|
* the first parameter should be the name of an +Exception+ class (or an
|
|
|
|
* object that returns an +Exception+ object when sent an +exception+
|
|
|
|
* message). The optional second parameter sets the message associated with
|
|
|
|
* the exception, and the third parameter is an array of callback information.
|
|
|
|
* Exceptions are caught by the +rescue+ clause of <code>begin...end</code>
|
|
|
|
* blocks.
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
rb_fiber_raise(int argc, VALUE *argv, VALUE fib)
|
|
|
|
{
|
|
|
|
VALUE exc = rb_make_exception(argc, argv);
|
|
|
|
return rb_fiber_resume(fib, -1, &exc);
|
|
|
|
}
|
|
|
|
|
2008-12-27 03:25:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.transfer(args, ...) -> obj
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* Transfer control to another fiber, resuming it from where it last
|
2009-02-22 17:23:33 +03:00
|
|
|
* stopped or starting it if it was not resumed before. The calling
|
2010-10-27 04:26:29 +04:00
|
|
|
* fiber will be suspended much like in a call to
|
2019-03-22 14:04:59 +03:00
|
|
|
* Fiber.yield. You need to <code>require 'fiber'</code>
|
2010-10-27 04:26:29 +04:00
|
|
|
* before using this method.
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2009-11-03 20:46:28 +03:00
|
|
|
* The fiber which receives the transfer call is treats it much like
|
2008-12-27 03:25:47 +03:00
|
|
|
* a resume call. Arguments passed to transfer are treated like those
|
|
|
|
* passed to resume.
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03: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.
|
2012-05-02 14:13:28 +04:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* fiber1 = Fiber.new do
|
|
|
|
* puts "In Fiber 1"
|
|
|
|
* Fiber.yield
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* fiber2 = Fiber.new do
|
|
|
|
* puts "In Fiber 2"
|
|
|
|
* fiber1.transfer
|
2012-05-02 18:30:48 +04:00
|
|
|
* puts "Never see this message"
|
2012-05-02 14:13:28 +04:00
|
|
|
* end
|
|
|
|
*
|
|
|
|
* fiber3 = Fiber.new do
|
|
|
|
* puts "In Fiber 3"
|
|
|
|
* end
|
|
|
|
*
|
|
|
|
* fiber2.resume
|
|
|
|
* fiber3.resume
|
|
|
|
*
|
2014-05-25 05:17:52 +04:00
|
|
|
* <em>produces</em>
|
2012-05-02 14:13:28 +04:00
|
|
|
*
|
2012-05-03 04:14:10 +04:00
|
|
|
* In fiber 2
|
|
|
|
* In fiber 1
|
|
|
|
* In fiber 3
|
2012-05-02 14:13:28 +04:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
*/
|
2007-08-21 22:51:39 +04:00
|
|
|
static VALUE
|
2011-11-09 08:26:39 +04:00
|
|
|
rb_fiber_m_transfer(int argc, VALUE *argv, VALUE fibval)
|
2007-08-21 22:51:39 +04:00
|
|
|
{
|
2018-08-21 04:01:37 +03:00
|
|
|
rb_fiber_t *fib = fiber_ptr(fibval);
|
2015-12-25 02:23:46 +03:00
|
|
|
fib->transferred = 1;
|
2014-10-16 02:35:08 +04:00
|
|
|
return fiber_switch(fib, 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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2008-12-27 03:25:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Fiber.yield(args, ...) -> obj
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03:00
|
|
|
* Yields control back to the context that resumed the fiber, passing
|
|
|
|
* along any arguments that were passed to it. The fiber will resume
|
2019-03-22 14:04:59 +03:00
|
|
|
* processing at this point when #resume is called next.
|
|
|
|
* Any arguments passed to the next #resume will be the value that
|
|
|
|
* this Fiber.yield expression evaluates to.
|
2008-12-27 03:25:47 +03: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 23:12:43 +04:00
|
|
|
static VALUE
|
2007-08-21 22: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 23:12:43 +04:00
|
|
|
{
|
2007-08-21 22: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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2008-12-27 03:25:47 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* Fiber.current() -> fiber
|
2009-02-22 17:23:33 +03:00
|
|
|
*
|
2008-12-27 03:25:47 +03: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 23:12:43 +04:00
|
|
|
static VALUE
|
2007-08-21 22: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 23:12:43 +04:00
|
|
|
{
|
2007-08-21 22: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 23:12:43 +04:00
|
|
|
}
|
|
|
|
|
2017-08-10 05:58:36 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* fiber.to_s -> string
|
|
|
|
*
|
|
|
|
* Returns fiber information string.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
fiber_to_s(VALUE fibval)
|
|
|
|
{
|
2018-08-21 04:01:37 +03:00
|
|
|
const rb_fiber_t *fib = fiber_ptr(fibval);
|
2017-08-10 05:58:36 +03:00
|
|
|
const rb_proc_t *proc;
|
|
|
|
char status_info[0x10];
|
|
|
|
|
|
|
|
snprintf(status_info, 0x10, " (%s)", fiber_status_name(fib->status));
|
2017-09-02 04:47:43 +03:00
|
|
|
if (!rb_obj_is_proc(fib->first_proc)) {
|
|
|
|
VALUE str = rb_any_to_s(fibval);
|
|
|
|
strlcat(status_info, ">", sizeof(status_info));
|
|
|
|
rb_str_set_len(str, RSTRING_LEN(str)-1);
|
|
|
|
rb_str_cat_cstr(str, status_info);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
GetProcPtr(fib->first_proc, proc);
|
2017-08-10 05:58:36 +03:00
|
|
|
return rb_block_to_s(fibval, &proc->block, status_info);
|
|
|
|
}
|
2010-05-08 08:50:09 +04:00
|
|
|
|
2018-08-29 11:04:09 +03:00
|
|
|
#ifdef HAVE_WORKING_FORK
|
|
|
|
void
|
|
|
|
rb_fiber_atfork(rb_thread_t *th)
|
|
|
|
{
|
2018-08-30 22:14:37 +03:00
|
|
|
if (th->root_fiber) {
|
|
|
|
if (&th->root_fiber->cont.saved_ec != th->ec) {
|
|
|
|
th->root_fiber = th->ec->fiber_ptr;
|
|
|
|
}
|
|
|
|
th->root_fiber->prev = 0;
|
2018-08-29 11:04:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-08 08: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-24 02:52:19 +04:00
|
|
|
void
|
|
|
|
Init_Cont(void)
|
|
|
|
{
|
2011-06-06 18:06:59 +04:00
|
|
|
#if FIBER_USE_NATIVE
|
2010-05-05 22: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
|
2017-10-26 11:32:49 +03:00
|
|
|
SET_MACHINE_STACK_END(&th->ec->machine.stack_end);
|
2010-05-05 22: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 23:12:43 +04:00
|
|
|
rb_cFiber = rb_define_class("Fiber", rb_cObject);
|
2008-11-28 18:23:09 +03:00
|
|
|
rb_define_alloc_func(rb_cFiber, fiber_alloc);
|
2007-06-02 11:48:29 +04:00
|
|
|
rb_eFiberError = rb_define_class("FiberError", rb_eStandardError);
|
2007-09-26 14:26:35 +04:00
|
|
|
rb_define_singleton_method(rb_cFiber, "yield", rb_fiber_s_yield, -1);
|
2008-11-28 18:23:09 +03:00
|
|
|
rb_define_method(rb_cFiber, "initialize", rb_fiber_init, 0);
|
2007-09-26 14:26:35 +04:00
|
|
|
rb_define_method(rb_cFiber, "resume", rb_fiber_m_resume, -1);
|
2018-12-28 16:03:09 +03:00
|
|
|
rb_define_method(rb_cFiber, "raise", rb_fiber_raise, -1);
|
2017-08-10 05:58:36 +03:00
|
|
|
rb_define_method(rb_cFiber, "to_s", fiber_to_s, 0);
|
|
|
|
rb_define_alias(rb_cFiber, "inspect", "to_s");
|
2007-05-24 02:52:19 +04:00
|
|
|
}
|
|
|
|
|
2013-04-05 14:29:38 +04:00
|
|
|
RUBY_SYMBOL_EXPORT_BEGIN
|
2010-07-22 01:38:25 +04:00
|
|
|
|
2007-08-25 06:03:44 +04:00
|
|
|
void
|
2009-01-20 10:17:28 +03:00
|
|
|
ruby_Init_Continuation_body(void)
|
2007-08-25 06: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 10:17:28 +03:00
|
|
|
ruby_Init_Fiber_as_Coroutine(void)
|
2007-08-25 06: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-22 01:38:25 +04:00
|
|
|
|
2013-04-05 14:29:38 +04:00
|
|
|
RUBY_SYMBOL_EXPORT_END
|