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

488 Коммитов

Автор SHA1 Сообщение Дата
Nobuyoshi Nakada db16629008
Fixed misspellings
Fixed misspellings reported at [Bug #16437], only in ruby and rubyspec.
2019-12-20 09:32:42 +09:00
Nobuyoshi Nakada c688026455
Suppress strict-aliasing warning by x86_64-w64-mingw32-gcc 7.4.0 2019-11-28 10:53:26 +09:00
Kazuhiro NISHIYAMA cf377c5556
Fix a typo [ci skip] 2019-11-04 17:12:18 +09:00
卜部昌平 f5e4063272 ruby_mimmalloc can return NULL
malloc can fail.  Should treat such situations.
2019-11-01 16:58:19 +09:00
Aaron Patterson 6147fa82a9
Fix continuation mark / compact 2019-10-28 13:09:10 -07:00
Jeremy Evans aba23d83f2 Fix documentation for Fiber#transfer [ci skip]
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-26 18:41:35 -07:00
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
Samuel Williams dbc2b89bc0 Ensure `vm_stack` is cleared after fork. 2019-06-20 13:41:18 +12:00
Samuel Williams 626483721a Set `cfp` to null (along with vm_stack) in `rb_fiber_close`. 2019-06-20 11:56:06 +12:00
Nobuyoshi Nakada ab6d8d0b65
Adjust indent 2019-06-19 20:40:49 +09:00
Samuel Williams d17344cfc5 Remove IA64 support. 2019-06-19 23:30:04 +12:00
Samuel Williams cb5da39f20 Use shared implementation of `rb_ec_initialize_vm_stack`. 2019-06-19 20:39:10 +12:00
Samuel Williams b8e4bea780 Track how stack was allocated for `cont_free`. 2019-06-19 20:39:10 +12:00
Samuel Williams 38791145eb Better handling of root fiber. 2019-06-19 20:39:10 +12:00
git 23e3c1704b * expand tabs. 2019-06-12 01:16:45 +09:00
Aaron Patterson 6db2d6d852
Add compaction support for more types.
This commit adds compaction support for:

* Fibers
* Continuations
* Autoload Constants
2019-06-11 09:16:14 -07:00
James Clarke ecf660e438
ia64: Don't clear register_stack_start
r59829 stopped clearing stack_start and enabled the code for
!FIBER_USE_NATIVE, but we need to do the same for register_stack_start
on ia64, otherwise we end up with NULL in cont_save_machine_stack.

Closes: https://github.com/ruby/ruby/pull/2155
2019-04-27 12:35:41 +09:00
nobu 56557ec28a [DOC] fix markups [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-03-22 11:04:59 +00:00
nobu 78d6e33702 Fix styles [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66762 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-09 13:58:49 +00:00
samuel 78e3cd2bb1 Fix #endif annotation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66612 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-28 13:03:18 +00:00
samuel 9ee79b6581 Don't allow rb_fiber_resume to raise exception on unborn fiber.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66611 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-28 13:03:14 +00:00
samuel 5fb9d1e11f Implement Fiber#raise. Fixes #10344.
This allows raising exceptions in another fiber, similarly to
Thread#raise.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66610 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-28 13:03:09 +00:00
samuel 4b99725de9 Avoid GCing dead stack after switching away from a fiber
Fixes <https://bugs.ruby-lang.org/issues/14561> and discussed
<https://bugs.ruby-lang.org/issues/15362>.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-01 03:49:52 +00:00
ko1 6e33c16ffd Give up support for cross-callcc set_trace_func.
* cont.c (cont_restore_thread): cause error if trace-status is changed.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-26 19:59:08 +00:00
ko1 96990203b7 Support targetting TracePoint [Feature #15289]
* vm_trace.c (rb_tracepoint_enable_for_target): support targetting
  TracePoint. [Feature #15289]

  Tragetting TracePoint is only enabled on specified method, proc
  and so on, example: `tp.enable(target: code)`.

  `code` should be consisted of InstructionSeuqnece (iseq)
  (RubyVM::InstructionSeuqnece.of(code) should not return nil)
  If code is a tree of iseq, TracePoint is enabled on all of
  iseqs in a tree.

  Enabled tragetting TracePoints can not enabled again with
  and without target.

* vm_core.h (rb_iseq_t): introduce `rb_iseq_t::local_hooks`
  to store local hooks.
  `rb_iseq_t::aux::trace_events` is renamed to
  `global_trace_events` to contrast with `local_hooks`.

* vm_core.h (rb_hook_list_t): add `rb_hook_list_t::running`
  to represent how many Threads/Fibers are used this list.
  If this field is 0, nobody using this hooks and we can
  delete it.

  This is why we can remove code from cont.c.

* vm_core.h (rb_vm_t): because of above change, we can eliminate
  `rb_vm_t::trace_running` field.
  Also renamed from `rb_vm_t::event_hooks` to `global_hooks`.

* vm_core.h, vm.c (ruby_vm_event_enabled_global_flags): renamed
  from `ruby_vm_event_enabled_flags.

* vm_core.h, vm.c (ruby_vm_event_local_num): added to count
  enabled targetting TracePoints.

* vm_core.h, vm_trace.c (rb_exec_event_hooks): accepts
  hook list.

* vm_core.h (rb_vm_global_hooks): added for convinience.

* method.h (rb_method_bmethod_t): added to maintain Proc
  and `rb_hook_list_t` for bmethod (defined by define_method).

* prelude.rb (TracePoint#enable): extracet a keyword parameter
  (because it is easy than writing in C).
  It calls `TracePoint#__enable` internal method written in C.

* vm_insnhelper.c (vm_trace): check also iseq->local_hooks.

* vm.c (invoke_bmethod): check def->body.bmethod.hooks.

* vm.c (hook_before_rewind): check iseq->local_hooks
  and def->body.bmethod.hooks before rewind by exception.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66003 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-26 18:16:39 +00:00
samuel 38f7bb481e Use VirtualAlloc/VirtualProtect/VirtualFree for windows stack allocation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-22 02:17:44 +00:00
nobu 5fe1ad969e Fix condition
FIBER_USE_NATIVE is always defined as 0 or 1, use `#if` instead of
`#ifdef`.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65882 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:51:27 +00:00
nobu be3af57b6f Make fiber_entry static
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65881 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:43:41 +00:00
samuel 1fa02b8dc9 Fix windows builds.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65878 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:18:21 +00:00
samuel 5e8a1dad86 Expose stack functions to coroutine and non-windows implementations.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65875 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:18:12 +00:00
samuel c5d3b83a9f Fix exposure of stack allocations.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65874 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:18:08 +00:00
samuel 8b1b056e1c Use malloc/free for windows stack allocation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65873 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:18:04 +00:00
samuel a3de4a092d Remove trailing whitespace.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:17:12 +00:00
samuel a8848860b8 Restore native non-coroutine code path.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65858 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:17:04 +00:00
samuel ff86d1b61b It cannot be const because it is being passed to destructive operation (destroy).
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65846 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 10:13:55 +00:00
samuel 990ddcfccf Better support for amd64 platforms
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65835 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 09:59:14 +00:00
samuel 07a324a0f6 Native implementation of coroutines to improve performance of fibers
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65834 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-20 09:59:10 +00:00
nobu 429245aa9f Prefix rb_ec_set_vm_stack
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65763 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-16 06:51:57 +00:00
shyouhei 7f6691ae77 suppress integer overflow warnings
* random.c: annotate rb_hash_start with NO_SANITIZE (seed.key.hash + h
  overflows and that seems intentional)
* bignum.c: avoid (size_t)--
* cont.c: ditto
* util.c: ditto
* vm_insnhelper.c: ditto



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65688 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-13 00:40:52 +00:00
shyouhei 5fcc43e2d3 cont.c: direct use of rb_thread_t
The only usage of rb_fiber_reset_root_local_storage() is from
ruby_vm_destruct(), where the object space is already terminated.
This `th->self` is not alive.  Why not just use `th` itself.

See also: https://travis-ci.org/ruby/ruby/jobs/451294954


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65574 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-06 10:19:55 +00:00
normal 12409ad28c fiber: fix crash on GC after forking
Remove the remainder of ROOT_FIBER_CONTEXT use and unnecessary
differences between the root and non-root fiber.  This makes
it easier to follow new root fiber at fork time.

Multiple sources of truth often leads to bugs, as in this case.
We can determinte root fiber by checking a fiber against the root_fiber
of its owner thread.  The new `fiber_is_root_p' function
supports that.

Now, we can care only about free-ing/recycling/munmap-ing stacks
as appropriate.

[Bug #15050]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64706 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-12 20:49:24 +00:00
normal d40694de72 cont.c (fiber_memsize): do not rely on ROOT_FIBER_CONTEXT
We can check if the fiber we're interested in is the
th->root_fiber for the owner thread, so there is no need to use
ROOT_FIBER_CONTEXT.

Note: there is no guarantee th->ec points to
&th->root_fiber->cont.saved_ec, thus vm::thread_memsize may not
account for root fiber correctly (pre-existing bug).

[Bug #15050]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64705 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-12 20:49:19 +00:00
normal 7ae24709a6 cont.c (ec_set_vm_stack): avoid needless casting
Am I missing something, here?  Casting was totally unnecessary
and ugly...

[ruby-core:88929]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64704 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-12 20:49:14 +00:00
normal c99b9eb039 share VM stack between threads and fibers if identical in size
ec->vm_stack is always allocated with malloc, so stack cache for
root fiber (thread stack) and non-root fibers can be shared as
long as the size is the same.  The purpose of this change is to
reduce dependencies on ROOT_FIBER_CONTEXT.

[Feature #15095] [Bug #15050]

v2: vm.c: fix build with USE_THREAD_DATA_RECYCLE==0

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64703 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-12 20:49:10 +00:00
normal 13f4923551 cont.c (rb_fiber_atfork): th->root_fiber may not exist
Otherwise, bootstraptest/test_fork.rb fails with -DVM_CHECK_MODE=2

[Bug #15041]

Fixes: r64589 "cont.c: set th->root_fiber to current fiber at fork"

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64593 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-30 19:14:37 +00:00
normal ac543abe91 cont.c: set th->root_fiber to current fiber at fork
Otherwise, th->root_fiber can point to an invalid Fiber,
because Fibers do not live across fork.  So consider
whatever Fiber is running the root fiber.

[ruby-core:88723] [Bug #15041]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64589 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-29 08:04:09 +00:00
normal 0b44744d46 cont.c (rb_context_t): remove ensure_list
It is unused (we use rb_execution_context_t.ensure_list instead)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64587 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-29 02:23:14 +00:00
kazu 84fd997fe6 Avoid compiler depend error
ref r64492

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64503 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-22 04:04:06 +00:00
nobu c8a34443c0 cont.c: fix syntax error
* cont.c (struct rb_fiber_struct): fix wrong usage of BITFIELD in
  r64487, which caused syntax error on pre-C99 compilers.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-21 02:11:39 +00:00
normal 383d7dba12 cont.c: replace "GetFooPtr" macros with "foo_ptr" functions
Following ko1's lead in r59192, this gets rid of non-obvious
assignments which happen inside macros.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64489 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-21 01:01:37 +00:00
normal aba0929a5a cont.c (struct rb_fiber_struct): bitfields for trasnferred and status
On 32-bit x86, this reduces the struct from 836 to 832 bytes and
brings us down to 13 (64-byte) cachelines (from 14).

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64487 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-20 23:48:03 +00:00
shyouhei d83536c980 reduce copy & paste
We see several occurrence of "diagnostic push/pop" so why not
make them macros.  Tested on GCC8 / Clang 6.

Note that ruby.h is intentionally left untouched because we don't
want to introduce new public macros.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64118 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-30 07:07:48 +00:00
normal 97538e813f cont.c (ec_switch): prevent delayed/missed trap interrupt race
timer-thread may set trap interrupt with rb_threadptr_check_signal
at any time independent of GVL.  This means timer-thread may set
the trap interrupt flag on the previous execution context; causing
the flag to be unnoticed until a future ec switch (or lost
completely if the ec is done).

Note: I avoid relying on th->interrupt_lock here and use
atomics because we won't be able to rely on it for proposed lazy
timer-thread [Misc #14937].

This regression affects Ruby 2.5 as it was introduced by moving
interrupt_flag to `ec' which is an unstable pointer.  Ruby <= 2.4
was unaffected because vm->main_thread->interrupt_flag never
changed.

[ruby-core:88119] [Bug #14939]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64062 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-26 08:30:10 +00:00
nobu a982952e32 adjust indent [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63840 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-03 13:25:59 +00:00
nobu 8fc203ba5f cont.c: handle errors for getcontext()
It may raise an error in a certain security configuration.
It is very likely to trigger a segmentation fault if `getcontext()` failed silently
and we just let it keep going.

Related to https://bugs.ruby-lang.org/issues/14883
[Fix GH-1903]

Based on the patch from Lion Yang <lion@aosc.io>

From: Lion Yang <lion@aosc.io>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63835 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-03 05:12:52 +00:00
k0kubun 06f54f0303 mjit.c: unify the variable name with method name
`RubyVM::MJIT.enabled?`.

It's set to be TRUE even before initialization is finished.
So it was actually not "mjit initialized predicate".

This flag is also used to check whether JIT-ed code should be called
or not, but I'm going to split the responsibility to another flag.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63732 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-23 13:41:06 +00:00
ko1 76daf51c36 fix type mismatch.
* cont.c (fiber_context_create): `func` should accept variable args.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63357 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-08 06:53:54 +00:00
nobu f5f218f7c2 cont.c: host out context setup
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63355 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-08 00:01:16 +00:00
nobu d7a9f59108 cont.c: wrapper function
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63352 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-07 23:52:48 +00:00
nobu cd36bea568 cont.c: refined error message
* cont.c (fiber_machine_stack_alloc): refined the error message on
  failure at setting a guard page.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63351 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-07 23:14:07 +00:00
nobu 8da1219d45 cont.c: machine stack cache condition
* cont.c (fiber_store, rb_fiber_terminate): separate the condition
  to cache machine stacks, which is not directly restricted to the
  platforms, and may be used on Windows too in the future.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63344 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-07 06:59:55 +00:00
normal 74fdf5232d cont.c: cleanup needless cast
It seems like leftover development step from r59557
("refactoring Fiber status").

I will make fiber_status use BITFIELD macro in a future
commit.

* cont.c (struct rb_fiber_struct): drop const from fiber_status
  (fiber_status_set): remove cast
  [ruby-core:86788] [Misc #14720]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63315 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-01 20:55:39 +00:00
ko1 51d227e3a5 refactoring r63073.
* cont.c (root_fiber_alloc): call `ConvertThreadToFiber()` here.

  `rb_fiber_t` for root_fiber is allocated before running Threads.
  Fiber objects wrapping this rb_fiber_t for root_fiber are created
  when root Fiber object is required explicitly (for example, Fiber
  switching and so on). We can put calling `ConvertThreadToFiber()`.
  In other words, we can pending `ConvertThreadToFiber()`
  until Fiber objects are created.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63090 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-04 08:19:28 +00:00
ko1 db3cc675f3 Fix Fiber with Thread issue on Windows [Bug #14642]
* cont.c (rb_threadptr_root_fiber_setup): divide into two functions:
  * rb_threadptr_root_fiber_setup_by_parent(): called by the parent thread.
  * rb_threadptr_root_fiber_setup_by_child(): called by the created thread.

  `rb_threadptr_root_fiber_setup()` is called by the parent thread and
  set fib->fib_handle by ConvertThreadToFiber() on the parent thread on
  Windows enveironment.
  This means that root_fib->fib_handle of child thread is initialized
  with parent thread's Fiber handle. Furthermore, second call of
  `ConvertThreadToFiber()` for the same thread fails.

  This patch solves this weird situateion. However, maybe we can make more
  clean code.

* thread.c (thread_start_func_2): call
  `rb_threadptr_root_fiber_setup_by_child()` at thread initialize routine.

* vm.c (th_init): call `rb_threadptr_root_fiber_setup_by_parent()`.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63073 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-04-03 10:21:47 +00:00
k0kubun fd44a5777f 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 06:58:09 +00:00
hsbt 5af43b1ec2 ia64: update ia64-specific code to use execution context
This change follows commit 837fd5e494
in '#ifdef __ia64' branches.

Noticed as a build failure by John Paul Adrian Glaubitz:

```
  cont.c:502:50: error: 'rb_thread_t {aka struct rb_thread_struct}'
    has no member named 'machine'
       size = cont->machine.register_stack_size =
           th->machine.register_stack_end - th->machine.register_stack_start;
               ^~
```

The change is trivial: update 'th->machine' usage to 'th->ec->machine'.
Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62106 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-30 05:43:42 +00:00
shyouhei b6a2d63eb3 explicit cast to void* required for %p
These functions take variadic arguments so no automatic type
promotion is expected.  You have to do it by hand.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61542 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-02 06:41:40 +00:00
shyouhei b4b0e8bd0b suppress warning for clang
In this function, "volatile" is specified twice in macro-expanded
`VAR_INITIALIZED(cont)` part.  That is a problem in fact.  However
I don't want to touch this line because it is already a messy
workaround for clang SEGV.  Let me just ignore.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61541 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-02 06:41:39 +00:00
ko1 c39bdb798d `$SAFE` as a process global state. [Feature #14250]
* vm_core.h (rb_vm_t): move `rb_execution_context_t::safe_level` to
  `rb_vm_t::safe_level_` because `$SAFE` is a process (VM) global state.

* vm_core.h (rb_proc_t): remove `rb_proc_t::safe_level` because `Proc`
  objects don't need to keep `$SAFE` at the creation.
  Also make `is_from_method` and `is_lambda` as 1 bit fields.

* cont.c (cont_restore_thread): no need to keep `$SAFE` for Continuation.

* eval.c (ruby_cleanup): use `rb_set_safe_level_force()` instead of access
  `vm->safe_level_` directly.

* eval_jump.c: End procs `END{}` doesn't keep `$SAFE`.

* proc.c (proc_dup): removed and introduce `rb_proc_dup` in vm.c.

* safe.c (rb_set_safe_level): don't check `$SAFE` 1 -> 0 changes.

* safe.c (safe_setter): use `rb_set_safe_level()`.

* thread.c (rb_thread_safe_level): `Thread#safe_level` returns `$SAFE`.
  It should be obsolete.

* transcode.c (load_transcoder_entry): `rb_safe_level()` only returns
  0 or 1 so that this check is not needed.

* vm.c (vm_proc_create_from_captured): don't need to keep `$SAFE` for Proc.

* vm.c (rb_proc_create): renamed to `proc_create`.

* vm.c (rb_proc_dup): moved from proc.c.

* vm.c (vm_invoke_proc): do not need to set and restore `$SAFE`
  for `Proc#call`.

* vm_eval.c (rb_eval_cmd): rename a local variable to represent clearer
  meaning.

* lib/drb/drb.rb: restore `$SAFE`.

* lib/erb.rb: restore `$SAFE`, too.

* test/lib/leakchecker.rb: check `$SAFE == 0` at the end of tests.

* test/rubygems/test_gem.rb: do not set `$SAFE = 1`.

* bootstraptest/test_proc.rb: catch up this change.

* spec/ruby/optional/capi/string_spec.rb: ditto.

* test/bigdecimal/test_bigdecimal.rb: ditto.

* test/fiddle/test_func.rb: ditto.

* test/fiddle/test_handle.rb: ditto.

* test/net/imap/test_imap_response_parser.rb: ditto.

* test/pathname/test_pathname.rb: ditto.

* test/readline/test_readline.rb: ditto.

* test/ruby/test_file.rb: ditto.

* test/ruby/test_optimization.rb: ditto.

* test/ruby/test_proc.rb: ditto.

* test/ruby/test_require.rb: ditto.

* test/ruby/test_thread.rb: ditto.

* test/rubygems/test_gem_specification.rb: ditto.

* test/test_tempfile.rb: ditto.

* test/test_tmpdir.rb: ditto.

* test/win32ole/test_win32ole.rb: ditto.

* test/win32ole/test_win32ole_event.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61510 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-28 20:09:24 +00:00
k0kubun 65d3461e95 cont.c: fix comment to follow field name change
saved_thread was renamed to saved_ec in r59831 [ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61475 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-25 14:22:21 +00:00
normal 3f799727b5 cont.c: update comment for ec refactoring
* cont.c (fiber_switch): update comment (ec.fiber => ec->fiber_ptr)

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61060 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-06 21:51:10 +00:00
ko1 0e34638f79 remove `PUSH_TAG`/`EXEC_AG`/`POP_TAG`/`JUMO_TAG`.
* eval_intern.h: remove non-`EC_` prefix *_TAG() macros.
  Use `EC_` prefix macros explicitly.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-06 03:16:08 +00:00
ko1 c761514c76 fix up r60782 and r60783.
* cont.c (cont_restore_thread): re-check a condition (pointed by nobu).


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60786 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-16 04:37:02 +00:00
ko1 e8ccf9f758 avoid duplicated-cond compile error.
* cont.c (cont_restore_thread): fix duplicated-cond compile error.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60783 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-16 02:53:17 +00:00
ko1 23e452b17b cleanup hook cleanup code.
* vm_trace.c: before this patch, deleted hooks are remvoed at
  *the beggining* of hooks (exec_hooks_precheck).
  This patch cleanup deleted hooks at
  (1) just after hook is deleted (TracePoint#disable and so on)
  (2) just after executing hooks (exec_hooks_postcheck)
  Most of time (1) is enough, but if some threads running hooks,
  we need to wait cleaning up deleted hooks until threads finish
  running the hooks. This is why (2) is introduced (and this is
  why current impl cleanup deleted hooks at the beggining of hooks).

* test/lib/tracepointchecker.rb: check also the number of delete
  waiting hooks.

* cont.c (cont_restore_thread): fix VM->trace_running count.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60782 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-16 02:47:58 +00:00
ko1 49d0bbe73f use `GET_EC()` directly.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60673 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-06 08:22:27 +00:00
ko1 5dfdaa9299 move rb_thread_t::interrupt_flag and mask
to rb_execution_context_t.

* vm_core.h (rb_thread_t): move
  `rb_thread_t::interrupt_flag` and
  `rb_thread_t::interrupt_mask` to rb_execution_context_t.

  RUBY_VM_CHECK_INTS() accepts `ec` instead of `th`.

* cont.c (rb_fiber_terminate): to propagate interrupt information,
  add new parameter `need_interrupt`.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60672 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-06 07:44:28 +00:00
ko1 e5e735549d renmae ec::fiber to ec::fiber_ptr.
* vm_core.h (rb_execution_context_t): renmae ec::fiber to
  ec::fiber_ptr make consistent with ec::thread_ptr.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60670 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-11-06 05:41:48 +00:00
ko1 a288b87c4c EXEC_EVENT_HOOK(ec, ...)
* vm_core.h (EXEC_EVENT_HOOK): accepts `ec` instead of `th`.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60539 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-29 13:19:14 +00:00
ko1 382aa678d0 `ec` has `thread_ptr`.
* cont.c (rb_context_struct): move rb_context_t::thread_ptr to
  rb_execution_context_t::thread_ptr.

* cont.c (rb_fiberptr_thread_ptr): removed.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60537 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-29 12:57:04 +00:00
ko1 5a7bd5349c `th` -> `ec` for some functions.
* vm.c (rb_vm_get_binding_creatable_next_cfp): accepts `ec` instead of `th`.

* vm.c (rb_vm_stack_to_heap): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60508 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-28 10:47:19 +00:00
ko1 88910e94a8 `th` -> `ec` for block related functions.
* vm.c: the following functions accept `ec` instead of `th`.
  * invoke_block
  * invoke_bmethod
  * invoke_iseq_block_from_c
  * invoke_block_from_c_bh
  * check_block_handler
  * vm_yield_with_cref
  * vm_yield
  * vm_yield_with_block
  * vm_yield_force_blockarg
  * invoke_block_from_c_proc
  * vm_invoke_proc
  * vm_invoke_bmethod
  * rb_vm_invoke_proc

* vm_insnhelper.c: ditto.
  * vm_yield_with_cfunc
  * vm_yield_with_symbol
  * vm_callee_setup_block_arg
  * vm_yield_setup_args
  * vm_invoke_iseq_block
  * vm_invoke_symbol_block
  * vm_invoke_ifunc_block
  * vm_invoke_block


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60476 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-27 06:06:31 +00:00
ko1 d77305fc9c make root fiber at switching.
* cont.c (fiber_switch): make sure the root fiber object is available
  before the first switching.

* test/ruby/test_fiber.rb: remove "skip".


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60453 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 14:38:22 +00:00
ko1 8afee516be fix freeing `th->ec` bugs.
* vm.c (thread_free): simply call rb_threadptr_root_fiber_release().

* cont.c (rb_threadptr_root_fiber_release): release th->ec (ec->fiber)
  iff root_fiber is NULL. If root_fiber is available, then ignore it
  and root fiber object will free th->ec too.

* cont.c (rb_threadptr_root_fiber_setup): do not set th->root_fiber.
  th->root_fiber will be set if a root fiber object is created.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60451 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 14:21:31 +00:00
ko1 4552f74715 introduce EC_*_TAG() instead of TH_*_TAG()
* eval_intern.h:
  introduce EC_*_TAG() macros instead of TH_*_TAG() macros.
  * TH_PUSH_TAG() -> EC_PUSH_TAG()
  * TH_POP_TAG() -> EC_POP_TAG()
  * TH_TMPPOP_TAG() -> EC_TMPPOP_TAG()
  * TH_REPUSH_TAG() -> EC_REPUSH_TAG()
  * TH_EXEC_TAG() -> EC_EXEC_TAG()
  * TH_JUMP_TAG() -> EC_JUMP_TAG()

  rb_threadptr_tag_state() , rb_ec_tag_jump() also accept `ec` instead of `th`.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60450 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 11:02:13 +00:00
ko1 e6309f0d50 fix comments
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60442 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 08:42:44 +00:00
ko1 837fd5e494 Use rb_execution_context_t instead of rb_thread_t
to represent execution context [Feature #14038]

* vm_core.h (rb_thread_t): rb_thread_t::ec is now a pointer.
  There are many code using `th` to represent execution context
  (such as cfp, VM stack and so on). To access `ec`, they need to
  use `th->ec->...` (adding one indirection) so that we need to
  replace them by passing `ec` instead of `th`.

* vm_core.h (GET_EC()): introduced to access current ec. Also
  remove `ruby_current_thread` global variable.

* cont.c (rb_context_t): introduce rb_context_t::thread_ptr instead of
  rb_context_t::thread_value.

* cont.c (ec_set_vm_stack): added to update vm_stack explicitly.

* cont.c (ec_switch): added to switch ec explicitly.

* cont.c (rb_fiber_close): added to terminate fibers explicitly.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60440 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 08:32:49 +00:00
normal 73f3dfd3b9 cont.c: update comment to match r59776 [ci skip]
* cont.c (fiber_switch): th->fiber => th->ec.fiber in comment

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-12 18:41:05 +00:00
ko1 b53b37758d store ec instead of thread in rb_context_t.
* cont.c (rb_context_t): introduce saved_ec instaad of saved_thread.
  We only need to transfer ec data (not all of thread data).

  Introduce `thread_value` field to point creation thread.
  To acccess this field, `cont_thread_value()` is introduced.

* vm.c (rb_execution_context_mark): remove `static` and use it
  from cont.c (use this function instead of `rb_thread_mark`).

* vm_insnhelper.c (rb_vm_push_frame): accept ec instead of th.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-10 19:00:08 +00:00
ko1 2a01ac897d avoid false positive on fiber_verify().
* cont.c (fiber_store): move `cont_save_machine_stack()` timing to
  avoid `fiber_verify()` false positive on `FIBER_USE_NATIVE == 0`
  and `GC.stress = true`.
  This patch is very dirty and it should be removed soon.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59830 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-10 18:37:55 +00:00
ko1 525532ff43 clear `stack_end`.
* cont.c (cont_save_thread): clear only `stack_end`. Clearing tells
  GC mark function to ignore this macine stack (not allocated yet).
  `stack_start` will be used by machine stack store/restore phase
  (on FIBER_USE_NATIVE == 0), so that only `stack_end` is cleared.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59829 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-10 17:30:16 +00:00
nobu 93272b35f1 cont.c: fix typo [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59827 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-10 16:19:39 +00:00
ko1 4f0c9a5cc4 move th->machine to ec->machine.
* vm_core.h: move rb_thread_t::machine to rb_execution_context_t::machine.

* vm_core.h, gc.c (rb_gc_mark_machine_stack): accept ec instead of th.
  it enables to call this func from rb_execution_context_mark() in vm.c.

* cont.c (fiber_setcontext): catch up this fix.
  fiber_restore_thread() restores machine stack information too.

* gc.c: catch up structure layout changes.

* thread.c: ditto.

* thread_pthread.c: ditto.

* thread_win32.c: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59825 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-10 15:49:45 +00:00
normal 01fdd48fed fiber: fix machine stack marking when FIBER_USE_NATIVE is 0
* cont.c (cont_mark): mark Fiber machine stack correctly when
  FIBER_USE_NATIVE is 0
* test/ruby/test_fiber.rb (test_mark_fiber): new test
  [Bug #13875] [ruby-core:82681]

This bug appears to be introduced with r59557.
("refactoring Fiber status")

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59785 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-08 23:51:14 +00:00
nobu b3907bb5dc fix up r59776
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59777 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-08 07:22:47 +00:00
ko1 8e03791373 move th->fiber to ec->fiber.
* vm_core.h (rb_thread_t::fiber): move fiber field to
  rb_execution_context_t::fiber.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59776 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-08 06:21:30 +00:00
nobu 0465916576 cont.c: fix root fiber to_s
* cont.c (fiber_to_s): fix Fiber#to_s on root fibers which have no
  procs.  [ruby-core:82629] [Bug #13859]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59721 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-02 01:47:43 +00:00
ko1 e553df868c remove debug code.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59645 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-23 02:23:47 +00:00
ko1 da4ce796d2 split out struct cont_saved_vm_stack.
* cont.c: introduce `struct cont_saved_vm_stack` which was part of
  `struct rb_context_struct`.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59631 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-22 00:41:24 +00:00
nagachika f6e90bb938 fix a typo in comment.
* cont.c (typedef struct rb_context_struct): fix a typo in comment.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59570 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-10 12:57:07 +00:00
ko1 c9bb3cae31 use VM_UNREACHABLE() instead of rb_bug().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-10 06:26:52 +00:00