Commit graph

156 commits

Author SHA1 Message Date
Nobuyoshi Nakada
92f850ae84
[DOC] Hide Ractor::Selector
It is not enabled by default currently.
2024-12-25 11:13:07 +09:00
lukeg
0d81177c20 Fix calls to require_internal in multi-ractor mode
After a ractor is started (multi-ractor mode), any calls to
require_internal will hang the process due to deadlock. For example,
loading a new encoding will deadlock after a ractor starts.

Fixes [Bug #19562]
2024-12-24 11:40:00 +09:00
Luke Gruber
38af38edcb Fix ractor move of unshareable frozen objects
These objects didn't retain their frozen status after the move

Bug [#19408]
2024-12-24 11:38:44 +09:00
Yudai Takada
b0d40d3d03
[DOC] Fix typos in comments in ractor.c 2024-12-22 18:08:39 +09:00
Koichi Sasada
e09c23433e followup 0bdb38ba6b
(forgot to amend...)
2024-12-13 17:05:58 +09:00
Koichi Sasada
0bdb38ba6b Ractor.set_if_absent(key)
to initialize ractor local storage in thread-safety.
[Feature #20875]
2024-12-13 06:22:13 +09:00
Matt Valentine-House
551be8219e Place all non-default GC API behind USE_SHARED_GC
So that it doesn't get included in the generated binaries for builds
that don't support loading shared GC modules

Co-Authored-By: Peter Zhu <peter@peterzhu.ca>
2024-11-25 13:05:23 +00:00
Koichi Sasada
97aaf6f760 introduce rb_ec_check_ints()
to avoid TLS issue with N:M threads.
2024-11-08 18:02:46 +09:00
Koichi Sasada
0d63b9b4a4 check closing flag
`Ractor.receive` and `Ractor.yield` should stop when the
incoming/outgoing port is closed.
2024-11-08 18:02:46 +09:00
Koichi Sasada
aa63699d10 support require in non-main Ractors
Many libraries should be loaded on the main ractor because of
setting constants with unshareable objects and so on.

This patch allows to call `requore` on non-main Ractors by
asking the main ractor to call `require` on it. The calling ractor
waits for the result of `require` from the main ractor.

If the `require` call failed with some reasons, an exception
objects will be deliverred from the main ractor to the calling ractor
if it is copy-able.

Same on `require_relative` and `require` by `autoload`.

Now `Ractor.new{pp obj}` works well (the first call of `pp` requires
`pp` library implicitly).

[Feature #20627]
2024-11-08 18:02:46 +09:00
Matt Valentine-House
1634280e1c Fix shared GC with -DRUBY_DEBUG
RUBY_DEBUG enables ractor assertions, which sets up some space at the
end of each RVALUE to store the associated ractor ID. We need to make
sure the function that does this is visible to shared GC libraries.
2024-10-24 16:08:46 +01:00
Peter Zhu
fbabe13b7e Use rb_id_table_foreach_values for marking Ractor local storage
Since we only mark the values, we can use rb_id_table_foreach_values to
avoid the overhead of converting the key to an ID.
2024-10-18 13:06:56 -04:00
Peter Zhu
c083a3ffcd Fix memory leak reported in main ractor when RUBY_FREE_AT_EXIT
STACK OF 1 INSTANCE OF 'ROOT LEAK: <calloc in rb_ractor_main_alloc>':
6   dyld                                  0x1840e20e0 start + 2360
5   miniruby                              0x1006796c8 main + 88  main.c:62
4   miniruby                              0x10072f4a4 ruby_init + 16  eval.c:99
3   miniruby                              0x10072f328 ruby_setup + 104  eval.c:81
2   miniruby                              0x1008d08c0 Init_BareVM + 508  vm.c:4276
1   miniruby                              0x1007f8944 rb_ractor_main_alloc + 76  ractor.c:2034
0   libsystem_malloc.dylib                0x1842a4cac _malloc_zone_calloc_instrumented_or_legacy + 128
====
    1 (96 bytes) ROOT LEAK: <calloc in rb_ractor_main_alloc 0x1347075d0> [96]
2024-07-16 15:50:00 -04:00
Peter Zhu
51bd816517 [Feature #20470] Split GC into gc_impl.c
This commit splits gc.c into two files:

- gc.c now only contains code not specific to Ruby GC. This includes
  code to mark objects (which the GC implementation may choose not to
  use) and wrappers for internal APIs that the implementation may need
  to use (e.g. locking the VM).

- gc_impl.c now contains the implementation of Ruby's GC. This includes
  marking, sweeping, compaction, and statistics. Most importantly,
  gc_impl.c only uses public APIs in Ruby and a limited set of functions
  exposed in gc.c. This allows us to build gc_impl.c independently of
  Ruby and plug Ruby's GC into itself.
2024-07-03 09:03:40 -04:00
Étienne Barrié
1376881e9a Stop marking chilled strings as frozen
They were initially made frozen to avoid false positives for cases such
as:

    str = str.dup if str.frozen?

But this may cause bugs and is generally confusing for users.

[Feature #20205]

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2024-05-28 07:32:33 +02:00
Luke Gruber
6747fbe77d
Fix interrupts during Ractor.select
Fixes [Bug #20168]
2024-05-05 15:14:53 +00:00
Peter Zhu
214811974b Add ruby_mimcalloc
Many places call ruby_mimmalloc then MEMZERO. This can be reduced by
using ruby_mimcalloc instead.
2024-04-24 15:30:43 -04:00
Étienne Barrié
12be40ae6b Implement chilled strings
[Feature #20205]

As a path toward enabling frozen string literals by default in the future,
this commit introduce "chilled strings". From a user perspective chilled
strings pretend to be frozen, but on the first attempt to mutate them,
they lose their frozen status and emit a warning rather than to raise a
`FrozenError`.

Implementation wise, `rb_compile_option_struct.frozen_string_literal` is
no longer a boolean but a tri-state of `enabled/disabled/unset`.

When code is compiled with frozen string literals neither explictly enabled
or disabled, string literals are compiled with a new `putchilledstring`
instruction. This instruction is identical to `putstring` except it marks
the String with the `STR_CHILLED (FL_USER3)` and `FL_FREEZE` flags.

Chilled strings have the `FL_FREEZE` flag as to minimize the need to check
for chilled strings across the codebase, and to improve compatibility with
C extensions.

Notes:
  - `String#freeze`: clears the chilled flag.
  - `String#-@`: acts as if the string was mutable.
  - `String#+@`: acts as if the string was mutable.
  - `String#clone`: copies the chilled flag.

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2024-03-19 09:26:49 +01:00
Nobuyoshi Nakada
28a2105a55
Prefer enum ruby_tag_type over int 2024-03-17 15:57:19 +09:00
Nobuyoshi Nakada
361b3efe16
Use UNDEF_P 2024-01-30 14:48:59 +09:00
KJ Tsanaktsidis
ef276858d9 Trigger postponed jobs on running_ec if that is available
Currently, any postponed job triggered from a non-ruby thread gets sent
to the main thread, but if the main thread is sleeping it won't be
checking ints. Instead, we should try and interrupt running_ec if that's
possible, and only fall back to the main thread if it's not.

[Bug #20197]
2024-01-25 13:10:35 +11:00
Luke Gruber
32c4b0125f Set Ractor moved object's shape to original object's shape
Fixes [Bug #19409]
2024-01-02 08:10:59 +09:00
John Hawthorn
1f0304218c Use main_thread->ec from rb_vm_main_ractor_ec
rb_vm_main_ractor_ec was introduced to allow rb_postponed_job_* to work
when fired on non-Ruby threads, which have no EC set, and that is its
only use.

When RUBY_MN_THREADS=1 is set ractor->threads.running_ec is NULL when
the shared thread is sleeping. This instead grabs the EC directly from
the main thread which seems to always be set.

Fixes [Bug #20016]

Co-authored-by: Dustin Brown <dbrown9@gmail.com>
2023-12-21 09:17:22 -08:00
Koichi Sasada
054f56fd3e moved object should not have a shape ID
fix [Bug #19917]
2023-12-20 07:04:32 +09:00
Koichi Sasada
c9a9b8036c remove Ractor::Selector from Ruby level
`Ractor::Selector` is not approved by Matz so remove it from
Ruby-level.

The implementation is used by `Ractor.select` so most of implementation
was remaind and calling `rb_init_ractor_selector()`, `Ractor::Selector`
will be defined. I will provide `ractor-selector` gem to try it.
2023-12-16 01:00:01 +09:00
Peter Zhu
10f44dfeff Fix Ractor sharing for too complex Objects 2023-11-28 17:43:22 -05:00
Peter Zhu
6eb5a9cf8f Fix Ractor sharing for too complex generic ivars 2023-11-28 17:43:22 -05:00
Peter Zhu
e2d950733e Add ST table to gen_ivtbl for complex shapes
On 32-bit systems, we must store the shape ID in the gen_ivtbl to not
lose the shape. If we directly store the ST table into the generic
ivar table, then we lose the shape. This makes it impossible to
determine the shape of the object and whether it is too complex or not.
2023-10-31 12:07:54 -04:00
Koichi Sasada
be1bbd5b7d M:N thread scheduler for Ractors
This patch introduce M:N thread scheduler for Ractor system.

In general, M:N thread scheduler employs N native threads (OS threads)
to manage M user-level threads (Ruby threads in this case).
On the Ruby interpreter, 1 native thread is provided for 1 Ractor
and all Ruby threads are managed by the native thread.

From Ruby 1.9, the interpreter uses 1:1 thread scheduler which means
1 Ruby thread has 1 native thread. M:N scheduler change this strategy.

Because of compatibility issue (and stableness issue of the implementation)
main Ractor doesn't use M:N scheduler on default. On the other words,
threads on the main Ractor will be managed with 1:1 thread scheduler.

There are additional settings by environment variables:

`RUBY_MN_THREADS=1` enables M:N thread scheduler on the main ractor.
Note that non-main ractors use the M:N scheduler without this
configuration. With this configuration, single ractor applications
run threads on M:1 thread scheduler (green threads, user-level threads).

`RUBY_MAX_CPU=n` specifies maximum number of native threads for
M:N scheduler (default: 8).

This patch will be reverted soon if non-easy issues are found.

[Bug #19842]
2023-10-12 14:47:01 +09:00
Peter Zhu
1e7b67f733 [Feature #19730] Remove transient heap 2023-07-13 09:27:33 -04:00
S-H-GAMELINKS
092c9b266a Reuse rb_ractor_make_shareable function for rb_ractor_make_shareable_copy function 2023-07-10 19:06:18 +09:00
Nobuyoshi Nakada
39933496ae
Remove duplicate declarations 2023-06-30 23:59:05 +09:00
Nobuyoshi Nakada
b934976024
Prefer 0 over NULL as function pointers
SunC warns use of `NULL`, pointer to data as function pointers.
2023-06-23 03:15:55 +09:00
Peter Zhu
5199f2aaf9 Implement Hash AR tables on VWA 2023-05-17 09:19:40 -04:00
Koichi Sasada
ffce3117b6 add new debug log line on rb_ractor_terminate_all
to leave the trace.
2023-04-26 17:58:32 +09:00
Koichi Sasada
f5b824c745 show debug log for ractor_terminal_interrupt_all 2023-03-30 14:56:37 +09:00
Koichi Sasada
30b43f4f1a rb_ractor_thread_list() only for current ractor
so that no need to lock the ractor.
2023-03-30 14:56:37 +09:00
Koichi Sasada
94e4182267 rb_current_ractor_raw(b)
`rb_current_ractor()` expects it has valid `ec` and `r`.
`rb_current_ractor_raw()` with a parameter `false` allows to return
NULL if `ec` is not available.
2023-03-30 14:56:23 +09:00
Aaron Patterson
54dbd8bea8 Use an st table for "too complex" objects
st tables will maintain insertion order so we can marshal dump / load
objects with instance variables in the same order they were set on that
particular instance

[ruby-core:112926] [Bug #19535]

Co-Authored-By: Jemma Issroff <jemmaissroff@gmail.com>
2023-03-20 13:54:18 -07:00
Nobuyoshi Nakada
bcd0aa896f
Fix -Wclobbered warning from gcc 12
A variable modified in `EXEC_TAG` block should be `volatile`.

```
ractor.c: In function 'ractor_try_yield':
ractor.c:1251:97: warning: argument 'obj' might be clobbered by 'longjmp' or 'vfork' [-Wclobbered]
 1251 | ractor_try_yield(rb_execution_context_t *ec, rb_ractor_t *cr, struct rb_ractor_queue *ts, VALUE obj, VALUE move, bool exc, bool is_will)
      |                                                                                           ~~~~~~^~~
```
2023-03-19 21:57:22 +09:00
Nobuyoshi Nakada
ce47ee00ae Fix indirect counter increment
`*pcnt++` just dereferences `pcnt` then increments the local variable,
but has no side effect.
2023-03-15 13:59:11 +09:00
Takashi Kokubun
23ec248e48 s/mjit/rjit/ 2023-03-06 23:44:01 -08:00
Takashi Kokubun
2e875549a9 s/MJIT/RJIT/ 2023-03-06 23:44:01 -08:00
Takashi Kokubun
233ddfac54 Stop exporting symbols for MJIT 2023-03-06 21:59:23 -08:00
Koichi Sasada
941d36d195 fix timing bug
passing will and closing notification can conflict and
`Ractor::Selector#empty?` can return wrong answer.
This patch fix it.

```
     s = Ractor::Selector.new
     s.add Ractor.new{10}
     s.add Ractor.new{20}
     r, v = s.wait
     vs = []
     vs << v
     r, v = s.wait
     vs << v
     [*vs.sort, s.empty?]
  #=> "[10, 20, false]" (expected "[10, 20, true]")
```
2023-03-03 10:19:37 +09:00
Koichi Sasada
5875fce6ce Ractor::Selector#empty?
It returns the waiting set is empty or not.

Also add Ractor::Selector's tests.
2023-03-03 00:08:02 +09:00
Koichi Sasada
a4421bd73c Rewrite Ractor synchronization mechanism
This patch rewrites Ractor synchronization mechanism, send/receive
and take/yield.

* API
  * Ractor::Selector is introduced for lightweight waiting
    for many ractors.
* Data structure
  * remove `struct rb_ractor_waiting_list` and use
    `struct rb_ractor_queue takers_queue` to manage takers.
  * remove `rb_ractor_t::yield_atexit` and use
    `rb_ractor_t::sync::will_basket::type` to check the will.
  * add `rb_ractor_basket::p.take` to represent a taking ractor.
* Synchronization protocol
  * For the Ractor local GC, `take` can not make a copy object
    directly so ask to generate the copy from the yielding ractor.
  * The following steps shows what `r1.take` does on `r0`.
    * step1: (r0) register `r0` into `r1`'s takers.
    * step2: (r0) check `r1`'s status and wakeup r0 if `r1` is waiting
             for yielding a value.
    * step3: (r0) sleep until `r1` wakes up `r0`.
  * The following steps shows what `Ractor.yield(v)` on `r1`.
    * step1: (r1) check first takers of `r1` and if there is (`r0`),
             make a copy object of `v` and pass it to `r0` and
             wakes up `r0`.
    * step2: (r1) if there is no taker ractors, sleep until
             another ractor try to take.
2023-03-02 14:31:54 +09:00
Koichi Sasada
1f936d654a ractor_queue_enq/deq doesn't need rq param
`rq` is always `r`'s queue.
2023-02-15 15:42:51 +09:00
Matt Valentine-House
72aba64fff Merge gc.h and internal/gc.h
[Feature #19425]
2023-02-09 10:32:29 -05:00
Peter Zhu
2296b877d8 Remove rb_hash_st_table
It's a duplicate of RHASH_ST_TABLE.
2023-01-31 15:48:09 -05:00