summaryrefslogtreecommitdiff
path: root/cont.c
AgeCommit message (Collapse)Author
2020-10-14Remove duplicated line [ci skip]Kazuhiro NISHIYAMA
ref bf3b2a43741e4f72be21bc6acf24d37e7fcff61c
2020-10-12relax Fiber#transfer's restrictionKoichi Sasada
Using Fiber#transfer with Fiber#resume for a same Fiber is limited (once Fiber#transfer is called for a fiber, the fiber can not be resumed more). This restriction was introduced to protect the resume/yield chain, but we realized that it is too much to protect the chain. Instead of the current restriction, we introduce some other protections. (1) can not transfer to the resuming fiber. (2) can not transfer to the yielding fiber. (3) can not resume transferred fiber. (4) can not yield from not-resumed fiber. [Bug #17221] Also at the end of a transferred fiber, it had continued on root fiber. However, if the root fiber resumed a fiber (and that fiber can resumed another fiber), this behavior also breaks the resume/yield chain. So at the end of a transferred fiber, switch to the edge of resume chain from root fiber. For example, root fiber resumed f1 and f1 resumed f2, transferred to f3 and f3 terminated, then continue from the fiber f2 (it was continued from root fiber without this patch). Notes: Merged: https://github.com/ruby/ruby/pull/3636
2020-10-01Fix a use-after-free bug reported by ASANAaron Patterson
If a fiber and thread are collected at the same time, the thread might get collected first and the pointer on the fiber will go bad. I don't think we need to check whether or not this is the main fiber in order to release its stack Notes: Merged: https://github.com/ruby/ruby/pull/3571
2020-09-14Make Mutex per-Fiber instead of per-ThreadBenoit Daloze
* Enables Mutex to be used as synchronization between multiple Fibers of the same Thread. * With a Fiber scheduler we can yield to another Fiber on contended Mutex#lock instead of blocking the entire thread. * This also makes the behavior of Mutex consistent across CRuby, JRuby and TruffleRuby. * [Feature #16792] Notes: Merged: https://github.com/ruby/ruby/pull/3434
2020-09-14Rename `Fiber{}` to `Fiber.schedule{}`.Samuel Williams
Notes: Merged: https://github.com/ruby/ruby/pull/3434
2020-09-03Introduce Ractor mechanism for parallel executionKoichi Sasada
This commit introduces Ractor mechanism to run Ruby program in parallel. See doc/ractor.md for more details about Ractor. See ticket [Feature #17100] to see the implementation details and discussions. [Feature #17100] This commit does not complete the implementation. You can find many bugs on using Ractor. Also the specification will be changed so that this feature is experimental. You will see a warning when you make the first Ractor with `Ractor.new`. I hope this feature can help programmers from thread-safety issues. Notes: Merged: https://github.com/ruby/ruby/pull/3365
2020-08-18Expose ec -> backtrace (internal) and use it to implement fiber backtrace.Samuel Williams
See <https://bugs.ruby-lang.org/issues/16815> for more details. Notes: Merged: https://github.com/ruby/ruby/pull/3422
2020-06-05Ensure that the head of the vacancy list is correctly inserted into the ↵Samuel Williams
linked list. See <https://bugs.ruby-lang.org/issues/16814> for more details. Notes: Merged: https://github.com/ruby/ruby/pull/3182
2020-05-25Fix documentation for Fiber#raise [ci skip]Jeremy Evans
Fixes [Bug #16912]
2020-05-14Thread scheduler for light weight concurrency.Samuel Williams
Notes: Merged: https://github.com/ruby/ruby/pull/3032 Merged-By: ioquatix <samuel@codeotaku.com>
2020-05-11sed -i 's|ruby/impl|ruby/internal|'卜部昌平
To fix build failures. Notes: Merged: https://github.com/ruby/ruby/pull/3079
2020-05-11sed -i s|ruby/3|ruby/impl|g卜部昌平
This shall fix compile errors. Notes: Merged: https://github.com/ruby/ruby/pull/3079
2020-05-11Added more NORETURN declarationsNobuyoshi Nakada
2020-04-08Merge pull request #2991 from shyouhei/ruby.h卜部昌平
Split ruby.h Notes: Merged-By: shyouhei <shyouhei@ruby-lang.org>
2020-02-28Prevent unloading methods used in root_fiber while calling another Fiber (#2939)Takashi Kokubun
Fixing SEGVs like: http://ci.rvm.jp/results/trunk-mjit-wait@silicon-docker/2744905 http://ci.rvm.jp/results/trunk-mjit-wait@silicon-docker/2744420 http://ci.rvm.jp/results/trunk-mjit-wait@silicon-docker/2741400 Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
2020-01-11Let execution context local storage be an ID tableLourens Naudé
Notes: Merged: https://github.com/ruby/ruby/pull/2814
2020-01-02Fully separate positional arguments and keyword argumentsJeremy Evans
This removes the warnings added in 2.7, and changes the behavior so that a final positional hash is not treated as keywords or vice-versa. To handle the arg_setup_block splat case correctly with keyword arguments, we need to check if we are taking a keyword hash. That case didn't have a test, but it affects real-world code, so add a test for it. This removes rb_empty_keyword_given_p() and related code, as that is not needed in Ruby 3. The empty keyword case is the same as the no keyword case in Ruby 3. This changes rb_scan_args to implement keyword argument separation for C functions when the : character is used. For backwards compatibility, it returns a duped hash. This is a bad idea for performance, but not duping the hash breaks at least Enumerator::ArithmeticSequence#inspect. Instead of having RB_PASS_CALLED_KEYWORDS be a number, simplify the code by just making it be rb_keyword_given_p(). Notes: Merged: https://github.com/ruby/ruby/pull/2794
2019-12-26decouple internal.h headers卜部昌平
Saves comitters' daily life by avoid #include-ing everything from internal.h to make each file do so instead. This would significantly speed up incremental builds. We take the following inclusion order in this changeset: 1. "ruby/config.h", where _GNU_SOURCE is defined (must be the very first thing among everything). 2. RUBY_EXTCONF_H if any. 3. Standard C headers, sorted alphabetically. 4. Other system headers, maybe guarded by #ifdef 5. Everything else, sorted alphabetically. Exceptions are those win32-related headers, which tend not be self- containing (headers have inclusion order dependencies). Notes: Merged: https://github.com/ruby/ruby/pull/2711
2019-12-20Fixed misspellingsNobuyoshi Nakada
Fixed misspellings reported at [Bug #16437], only in ruby and rubyspec.
2019-11-28Suppress strict-aliasing warning by x86_64-w64-mingw32-gcc 7.4.0Nobuyoshi Nakada
2019-11-04Fix a typo [ci skip]Kazuhiro NISHIYAMA
2019-11-01ruby_mimmalloc can return NULL卜部昌平
malloc can fail. Should treat such situations.
2019-10-28Fix continuation mark / compactAaron Patterson
2019-10-26Fix documentation for Fiber#transfer [ci skip]Jeremy Evans
Fiber#transfer prevents calling Fiber#resume on the receiver of the transfer method, not the fiber calling transfer. Transfering back to a fiber does not allow later calling resume on the fiber. Once transfer has been called on a fiber, you can never call resume on the fiber. Calling resume on a transferred fiber is not a double resume error, it is a different FiberError (cannot resume transferred Fiber). For details on the differences between transferred fibers and regular fibers, see Sasada-san's RubyKaigi 2017 presentation (in short, Fiber#transfer is for coroutine, Fiber#resume is for semi-coroutine).
2019-10-24show "transferred" attribute on Fiber#to_sKoichi Sasada
If a fiber is invoked with transfer method (such as "f.transfer"), then the invoked fiber ("f") is labeled as "transferred" and this fiber can not be invoked with Fiber#resume. This patch adds transferred attribute for "Fiber#to_s" (and inspect).
2019-10-24Revert "Fix Fiber#transfer"Koichi Sasada
This reverts commit fa8ac91e957a076f6df1adaecad7896817138009. Previous behavior is intentional.
2019-10-21Fix Fiber#transferJeremy Evans
Fiber#transfer previously made it impossible to resume the fiber if it was transferred to (no resuming the target of Fiber#transfer). However, the documentation specifies that you cannot resume a fiber that has transferred to another fiber (no resuming the source of Fiber#transfer), unless control is transferred back. Fix the code by setting the transferred flag on the current/source fiber, and unsetting the transferred flag on the target fiber. Fixes [Bug #9664] Fixes [Bug #12555] Notes: Merged: https://github.com/ruby/ruby/pull/2588 Merged-By: jeremyevans <code@jeremyevans.net>
2019-09-26Add rb_adjust_argv_kw_splat to internal.hJeremy Evans
We are calling this in a few other files, it is better to have it in a header than adding prototypes to the other files. Notes: Merged: https://github.com/ruby/ruby/pull/2491
2019-09-26Fix clang errors when pendantic errors enabledAaron Patterson
I've been compiling with: ``` set -lx cflags '-std=c99 -Werror=pedantic -pedantic-errors' ``` But compilation would fail with the following: ``` cont.c:296:90: error: format specifies type 'void *' but the argument has type 'struct fiber_pool_stack *' [-Werror,-Wformat-pedantic] if (DEBUG) fprintf(stderr, "fiber_pool_stack_alloca(%p): %"PRIuSIZE"/%"PRIuSIZE"\n", stack, offset, stack->available); ~~ ^~~~~ cont.c:467:24: error: format specifies type 'void *' but the argument has type 'struct fiber_pool *' [-Werror,-Wformat-pedantic] count, fiber_pool, fiber_pool->used, fiber_pool->count, size, fiber_pool->vm_stack_size); ^~~~~~~~~~ cont.c:588:83: error: format specifies type 'void *' but the argument has type 'struct fiber_pool_vacancy *' [-Werror,-Wformat-pedantic] if (DEBUG) fprintf(stderr, "fiber_pool_stack_acquire: %p used=%"PRIuSIZE"\n", fiber_pool->vacancies, fiber_pool->used); ~~ ^~~~~~~~~~~~~~~~~~~~~ cont.c:736:76: error: format specifies type 'void *' but the argument has type 'rb_fiber_t *' (aka 'struct rb_fiber_struct *') [-Werror,-Wformat-pedantic] if (DEBUG) fprintf(stderr, "fiber_stack_release: %p, stack.base=%p\n", fiber, fiber->stack.base); ``` This commit just fixes the pedantic errors
2019-09-26Fix keyword argument separation issues in Fiber#resumeJeremy Evans
Notes: Merged: https://github.com/ruby/ruby/pull/2484
2019-09-05Add VM_NO_KEYWORDSJeremy Evans
I think this is easier to read than using literal 0 with comments in every case where it is used.
2019-09-05Propagate kw_splat informationYusuke Endoh
The kw_splat flag is whether the original call passes keyword or not. Some types of methods (e.g., bmethod and sym_proc) drops the information. This change tries to propagate the flag to the final callee, as far as I can.
2019-08-27rb_proc_new / rb_fiber_new now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from rb_proc_new / rb_fiber_new, and applies RB_BLOCK_CALL_FUNC_ARGLIST wherever necessary.
2019-08-27rb_ensure now free from ANYARGS卜部昌平
After 5e86b005c0f2ef30df2f9906c7e2f3abefe286a2, I now think ANYARGS is dangerous and should be extinct. This commit deletes ANYARGS from rb_ensure, which also revealed many arity / type mismatches.
2019-08-19cont.c: remove unused STACK_GROW_DIR_DETECTIONYusuke Endoh
to suppress a waring for "unused variable"
2019-08-17Use more different arguments in Fiber.yield documentation to make it clear ↵Iain Barnett
(#2170) https://github.com/ruby/ruby/pull/2170#issuecomment-489880700 Documentation is for those who don't know, remember, or understand (to any degree) the language, it should attempt to be clear above all other things. The example given is needlessly unclear because if you use a block it's common for arguments to be reused on every entry to the block. In Fiber's case this is not so. First time round 10 goes in, 12 comes out. Second time round 14 goes in, 14 comes out… was that because 14 is 12 + 2 or because it's "the return value of the call to Fiber.yield". It's the latter because it says so but why does the example need to make anyone think the former? Using different numbers makes it immediately clear what's happening whether the description is there or not.
2019-08-12Rename rb_gc_mark_no_pin -> rb_gc_mark_movableAaron Patterson
Renaming this function. "No pin" leaks some implementation details. We just want users to know that if they mark this object, the reference may move and they'll need to update the reference accordingly.
2019-08-13Fix a typo [ci skip]Kazuhiro NISHIYAMA
2019-07-29Fix unused variableKazuhiro NISHIYAMA
2019-07-25Use PRIuSIZE instead of "%zu"Nobuyoshi Nakada
2019-07-19Add documentation to `fiber_pool_allocate_memory`.Samuel Williams
2019-07-19Fix 32-bit build and typo.Samuel Williams
"Therefore, `fiber_pool_stack_free(&vacancy->stack)` can do the wrong thing..." should be "... `fiber_pool_stack_free(stack)` ...".
2019-07-19Ensure that madvise does not clobber vacancy data.Samuel Williams
After calling `fiber_pool_vacancy_reset`, `vacancy->stack` and `stack` are no longer in sync. Therefore, `fiber_pool_stack_free(&vacancy->stack)` can do the wrong thing and clobber the vacancy data. Additionally, when testing using VM_CHECK_MODE > 0, use MADV_DONTNEED if possible, to catch issues w.r.t. clobbered vacancy data earlier.
2019-07-19Better usage of `rb_ec_clear_vm_stack` to maintain invariants.Samuel Williams
2019-07-19Improve ec assertions.Samuel Williams
2019-07-19initialize only Fiber's cfp.Koichi Sasada
fiber->cont.saved_ec.cfp should be initialized by NULL because no vm_stack is allocated. However, cont_init() captures current Fiber's cfp for continuation, so it should only initialize fibers.
2019-07-19Revert "Ensure cfp is initialized to NULL."Samuel Williams
This reverts commit d7fdf45a4ae1bcb6fac30a24b025d4f20149ba0a.
2019-07-19Ensure cfp is initialized to NULL.Samuel Williams
`cont_init` didn't initialize `cont->saved_ec.cfp`. Calling `cont_mark` would result in an invalid `cfp` in `rb_execution_context_mark`. Because fibers lazy-initialize the stack, fibers that are created but not resumed could cause this problem to occur.
2019-07-19Remove `rb_vm_push_frame` as it is no longer used.Samuel Williams
2019-07-19Adjust styles and indentsNobuyoshi Nakada