Граф коммитов

382 Коммитов

Автор SHA1 Сообщение Дата
Koichi Sasada 38e931fa2c show "transferred" attribute on Fiber#to_s
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-24 16:46:21 +09:00
Koichi Sasada 4c3e3b8028 Revert "Fix Fiber#transfer"
This reverts commit fa8ac91e95.

Previous behavior is intentional.
2019-10-24 16:46:21 +09:00
Jeremy Evans fa8ac91e95
Fix Fiber#transfer
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]
2019-10-21 16:54:58 -07:00
Jeremy Evans dd2068ac8d Add rb_adjust_argv_kw_splat to internal.h
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.
2019-09-26 15:30:51 -07:00
Aaron Patterson f5e8d33761
Fix clang errors when pendantic errors enabled
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-26 14:58:11 -07:00
Jeremy Evans b193041b99 Fix keyword argument separation issues in Fiber#resume 2019-09-26 08:01:53 -07:00
Jeremy Evans fd2ef1a9bf Add VM_NO_KEYWORDS
I think this is easier to read than using literal 0 with comments
in every case where it is used.
2019-09-05 17:47:12 -07:00
Yusuke Endoh ce04392d8d Propagate kw_splat information
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-09-05 17:47:12 -07:00
卜部昌平 bc3e7924bc rb_proc_new / rb_fiber_new now free from ANYARGS
After 5e86b005c0, 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-27 15:52:26 +09:00
卜部昌平 703783324c rb_ensure now free from ANYARGS
After 5e86b005c0, 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-27 15:52:26 +09:00
Yusuke Endoh c5db4c28f9 cont.c: remove unused STACK_GROW_DIR_DETECTION
to suppress a waring for "unused variable"
2019-08-19 15:01:48 +09:00
Iain Barnett dce1e14e80 Use more different arguments in Fiber.yield documentation to make it clear (#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-17 14:24:45 +09:00
Aaron Patterson aac4d9d6c7
Rename rb_gc_mark_no_pin -> rb_gc_mark_movable
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-12 16:44:54 -04:00
Kazuhiro NISHIYAMA 0f10828fcf
Fix a typo [ci skip] 2019-08-13 01:03:14 +09:00
Kazuhiro NISHIYAMA 968c7b4398
Fix unused variable 2019-07-29 23:05:00 +09:00
Nobuyoshi Nakada efa380b006
Use PRIuSIZE instead of "%zu" 2019-07-25 05:54:38 +09:00
Samuel Williams 0a7093a8e9
Add documentation to `fiber_pool_allocate_memory`. 2019-07-19 20:18:42 +12:00
Samuel Williams 517f7f9b57
Fix 32-bit build and typo.
"Therefore, `fiber_pool_stack_free(&vacancy->stack)` can do the wrong
thing..." should be "... `fiber_pool_stack_free(stack)` ...".
2019-07-19 20:09:03 +12:00
Samuel Williams e004afd46d
Ensure that madvise does not clobber vacancy data.
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-19 19:42:09 +12:00
Samuel Williams cf93f98a60
Better usage of `rb_ec_clear_vm_stack` to maintain invariants. 2019-07-19 15:46:56 +12:00
Samuel Williams 6eef80d824
Improve ec assertions. 2019-07-19 15:46:56 +12:00
Koichi Sasada 4ec5b39ce8 initialize only Fiber's cfp.
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-19 08:53:19 +09:00
Samuel Williams 8ca32020b0
Revert "Ensure cfp is initialized to NULL."
This reverts commit d7fdf45a4a.
2019-07-19 11:32:40 +12:00
Samuel Williams d7fdf45a4a
Ensure cfp is initialized to NULL.
`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-19 11:10:01 +12:00
Samuel Williams 9dda0a03cc
Remove `rb_vm_push_frame` as it is no longer used. 2019-07-19 11:10:01 +12:00
Nobuyoshi Nakada a036a8a038
Adjust styles and indents 2019-07-19 06:35:15 +09:00
Koichi Sasada d40d8b3caf check saved_ec.cfp 2019-07-18 22:59:44 +09:00
Samuel Williams 9790b778a3
Ensure we don't have dangling cfp. 2019-07-19 00:33:03 +12:00
git 78bc6cd8df * remove trailing spaces. 2019-07-18 17:55:22 +09:00
Samuel Williams 38e3c65a33
Improve `fiber_pool_expand` allocation strategy.
If `mmap` fails to allocate memory, try half the size, and so on.

Limit FIBER_POOL_ALLOCATION_MAXIMUM_SIZE to 1024 stacks. In typical
configurations this limits the memory mapped region to ~128MB per
allocation.
2019-07-18 20:54:55 +12:00
Samuel Williams 311007bf40
Add experimental `RUBY_SHARED_FIBER_POOL_FREE_STACKS` to control madvise. 2019-07-18 20:54:55 +12:00
Samuel Williams 001f187ed6
Make fiber_pool more conservative on platforms with limited address space.
We use COROUTINE_LIMITED_ADDRESS_SPACE to select platforms where address
space is 32-bits or less. Fiber pool implementation enables more book
keeping, and reduces upper limits, in order to minimise address space
utilisation.
2019-07-18 20:54:55 +12:00
Samuel Williams 385ea910fc
Add `struct fiber_pool {int free_stacks;}` to control usage of madvise.
`madvise(free)` and similar operations are good because they avoid swap
usage by clearing the dirty bit on memory pages which are mapped but no
longer needed. However, there is some performance penalty if there is no
memory pressure. Therefore, we do it by default, but it can be avoided.
2019-07-18 20:54:55 +12:00
Samuel Williams 4d60a5820a
Add FIBER_POOL_ALLOCATION_FREE to control allocation/free strategy. 2019-07-18 20:54:55 +12:00
Samuel Williams 8ac9a7be0f
Limit expansion of fiber pool on 32-bit platforms.
On 32-bit platforms, expanding the fiber pool by a large amount may fail,
even if a smaller amount may succeed. We limit the maximum size of a single
allocation to maximise the number of fibers that can be allocated.

Additionally, we implement the book-keeping required to free allocations
when their usage falls to zero.
2019-07-18 20:54:54 +12:00
Samuel Williams 77f3319071
Enable `madvise` to release stack space back to OS. 2019-07-18 20:54:54 +12:00
Samuel Williams 7291fef55c
Improve build process and coroutine implementation selection. 2019-07-18 20:54:54 +12:00
Samuel Williams 91aae651bf
Stack copying implementation of coroutines. 2019-07-18 20:54:54 +12:00
Samuel Williams 14cf95cff3
Implement fiber pool for reduced fiber allocation overhead.
Replace previous stack cache with fiber pool cache. The fiber pool
allocates many stacks in a single memory region. Stack allocation
becomes O(log N) and fiber creation is amortized O(1). Around 10x
performance improvement was measured in micro-benchmarks.
2019-07-18 20:54:53 +12:00
Samuel Williams 1b82c877df
Make FIBER_USE_NATIVE the default and reformat code. 2019-07-18 20:54:53 +12:00
git 93c9405e0e * expand tabs. 2019-07-08 16:00:29 +09:00
Nobuyoshi Nakada 663d58ffde
Fix indent 2019-07-08 15:13:59 +09:00
Nobuyoshi Nakada a8c54932ba
Renamed fib to fiber 2019-07-08 15:07:43 +09:00
Jeremy Evans 7172ab0ec5 Use native coroutine implementation on OpenBSD-amd64
When using native fibers, do not load ucontext, as it isn't needed.
2019-06-26 16:23:11 -07:00
Samuel Williams 6c6bf9ffcb Add `ucontext` coroutine implementation for generic fallback. 2019-06-26 20:19:53 +12:00
git 5d4dfcd808 * remove trailing spaces. 2019-06-24 11:25:17 +09:00
Samuel Williams a071bed7a5 Print warning if not using native coroutine. 2019-06-24 14:24:49 +12:00
Samuel Williams f607e43352 Transition root fiber into state FIBER_TERMINATED.
During fork, it's possible that threads with root fibers are terminated,
but fiber state is not updated. `fiber_verify` will subsequently fail. We
forcefully enter the FIBER_TERMINATED state when terminating the root
fiber.
2019-06-20 22:36:30 +12:00
Samuel Williams e4cafa393f Ensure that vm_stack is cleared in `thread_cleanup_func_before_exec`.
If `vm_stack` is left dangling in a forked process, the gc attempts to scan
it, but it is invalid and will cause a segfault. Therefore, we clear it
before forking.

In order to simplify this, `rb_ec_clear_vm_stack` was introduced.
2019-06-20 16:44:50 +12:00
Samuel Williams c26c514494 Revert failed attempt at fixing invalid usage of vm_stack. 2019-06-20 15:30:29 +12:00