140 Commits

Author SHA1 Message Date
Kunshan Wang
ce1dfe81c2 [DOC] Doc for Thread::ConditionVariable
Documented the necessity of calling `wait` in a loop.  We modified the
example to demonstrate the idiomatic use, and added a third thread `a2`
to demonstrate another reason that necessitates the loop.

Mentioned spurious wake-up in the doc.
2025-04-02 10:47:52 -04:00
Samuel Williams
a8c2d5e7be
Ensure fiber scheduler re-acquires mutex when interrupted from sleep. (#12158)
[Bug #20907]
2024-11-23 23:54:12 +00:00
Peter Zhu
80700f453e Replace assert with RUBY_ASSERT in thread_sync.c
assert does not print the bug report, only the file and line number of
the assertion that failed. RUBY_ASSERT prints the full bug report, which
makes it much easier to debug.
2024-02-12 15:07:47 -05:00
Jeremy Evans
c546ee3bb5 Remove SizedQueue#freeze
Queue#freeze uses the same C function, so SizedQueue#freeze can use
that via normal method lookup.
2024-01-17 13:17:25 -08:00
Victor Shepelev
570d7b2c3e
[DOC] Adjust some new features wording/examples. (#9183)
* Reword Range#overlap? docs last paragraph.

* Docs: add explanation about Queue#freeze

* Docs: Add :rescue event docs for TracePoint

* Docs: Enhance Module#set_temporary_name documentation

* Docs: Slightly expand Process::Status deprecations

* Fix MatchData#named_captures rendering glitch

* Improve Dir.fchdir examples

* Adjust Refinement#target docs
2023-12-14 23:01:48 +02: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
Jeremy Evans
62181e17da Make {Queue,SizedQueue}#freeze raise TypeError
Fixes [Bug #17146]
2023-09-27 19:57:50 +01:00
Samuel Williams
be21a056d2
Try default gcc 9.4.0 to see if it exhibits the same compiler bugs. (#8394)
* Revert "Extract `do_mutex_lock_check_interrupts` to try and fix `ppc64le`. (#8393)"

This reverts commit 5184b40dd4dc446660cd35c3e53896324e95b317.

* .travis.yml: Try default gcc 9.4.0 instead of gcc-10 in ppc64le and s390x.

Use gcc 9.4.0 instead of gcc-10 to avoid the current failures by a possible GCC
10 compiler bug in the Travis ppc64le and s390x cases. And it also aligns with
RubyCI Ubuntu ppc64le and s390x where the default gcc is used.

---------

Co-authored-by: Jun Aruga <jaruga@ruby-lang.org>
2023-09-08 20:44:46 +12:00
Samuel Williams
5184b40dd4
Extract do_mutex_lock_check_interrupts to try and fix ppc64le. (#8393)
We found some tests were hanging in `do_mutex_lock`, specifically the
fiber scheduler autoload test. After much investigation, it may be a code
generation bug. Because we didn't change the code, but only extracted it
into a separate function, and it appears to fix the problem.
2023-09-08 00:32:54 +12:00
Shane Becker
fff4773085 [DOC] Add parenthetical to explain what FIFO abbrev means 2023-07-13 21:39:31 +09:00
Shane Becker
7524675330 [DOC] Fix example code indentation from 3 to 2 2023-07-13 14:22:47 +09:00
Samuel Williams
2c4b2053ca
Correctly clean up keeping_mutexes before resuming any other threads. (#7460)
It's possible (but very rare) to have a race condition between setting
`mutex->fiber = NULL` and `thread_mutex_remove(th, mutex)` which results
in the following bug:

```
[BUG] invalid keeping_mutexes: Attempt to unlock a mutex which is not locked
```

Fixes <https://bugs.ruby-lang.org/issues/19480>.
2023-03-07 20:23:00 +13:00
Nobuyoshi Nakada
a56d959ed5
Replace PACKED_STRUCT and PACKED_STRUCT_UNALIGNED 2023-02-08 12:35:27 +09:00
Nobuyoshi Nakada
f0f2535c4d Add queue_list and szqueue_list macros 2023-01-21 00:30:34 +09:00
Peter Zhu
8872ebec6a Fix compilation warnings in thread_sync.c
Fixes the following compilation warnings:

thread_sync.c:908:48: warning: taking address of packed member of `struct rb_queue` may result in an unaligned pointer value [-Waddress-of-packed-member]

thread_sync.c:1181:48: warning: taking address of packed member of `struct rb_queue` may result in an unaligned pointer value [-Waddress-of-packed-member]
2023-01-19 12:22:02 -05:00
Peter Zhu
4fa7d38324 Don't redefine RB_OBJ_WRITE
RB_OBJ_WRITE already exists in rgengc.h, so we shouldn't redefine it in
gc.h.
2023-01-18 08:49:32 -05:00
Jean byroot Boussier
eacedcfe44
mutex: Raise a ThreadError when detecting a fiber deadlock (#6680)
[Bug #19105]

If no fiber scheduler is registered and the fiber that
owns the lock and the one that try to acquire it
both belong to the same thread, we're in a deadlock case.

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2022-11-09 00:43:16 +13:00
Samuel Williams
7f175e5648
Avoid missed wakeup with fiber scheduler and Fiber.blocking. (#6588)
* Ensure that blocked fibers don't prevent valid wakeups.
2022-10-20 13:38:52 +13:00
Nobuyoshi Nakada
637144b834
Adjust indents [ci skip] 2022-10-18 00:23:23 +09:00
Jean Boussier
60defe0a68 thread_sync.c: Clarify and document the behavior of timeout == 0
[Feature #18982]

Instead of introducing an `exception: false` argument to have `non_block`
return nil rather than raise, we can clearly document that a timeout of 0
immediately returns.

The code is refactored a bit to avoid doing a time calculation in
such case.
2022-10-17 16:56:00 +02:00
Jean Boussier
fe61cad749 Implement SizedQueue#push(timeout: sec)
[Feature #18944]

If both `non_block=true` and `timeout:` are supplied, ArgumentError
is raised.
2022-08-18 10:07:37 +02:00
Nobuyoshi Nakada
58c8b6e862
Adjust styles [ci skip] 2022-08-06 10:13:20 +09:00
Jean Boussier
6b2fc33ae2 thread_sync.c: pass proper argument to queue_sleep in rb_szqueue_push
When I removed the SizeQueue#push timeout from my PR, I forgot to
update the `queue_sleep` parameters to be a `queue_sleep_arg`.

Somehow this worked on most archs, but on Solaris/Sparc it would
legitimately crash when trying to access the `timeout` and `end`
members of the struct.
2022-08-04 11:48:31 +02:00
Jean Boussier
e3aabe93aa Implement Queue#pop(timeout: sec)
[Feature #18774]

As well as `SizedQueue#pop(timeout: sec)`

If both `non_block=true` and `timeout:` are supplied, ArgumentError
is raised.
2022-08-02 11:04:28 +02:00
Peter Zhu
efb91ff19b Rename rb_ary_tmp_new to rb_ary_hidden_new
rb_ary_tmp_new suggests that the array is temporary in some way, but
that's not true, it just creates an array that's hidden and not on the
transient heap. This commit renames it to rb_ary_hidden_new.
2022-07-26 09:12:09 -04:00
Takashi Kokubun
5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Nobuyoshi Nakada
42a0bed351
Prefix ccan headers (#4568)
* Prefixed ccan headers

* Remove unprefixed names in ccan/build_assert

* Remove unprefixed names in ccan/check_type

* Remove unprefixed names in ccan/container_of

* Remove unprefixed names in ccan/list

Co-authored-by: Samuel Williams <samuel.williams@oriontransfer.co.nz>
2022-03-30 20:36:31 +13:00
Victor Shepelev
0fcc58a18c
[DOC] Improve Thread::Queue.new docs [ci skip] 2021-12-15 11:25:33 +09:00
Nobuyoshi Nakada
4a09b7de06
Suppress address-of-packed-member warning by gcc 2021-11-28 17:45:55 +09:00
John Hawthorn
5a048829a4 Add WB_PROTECTED to mutexes
mutex_mark is (basically) NULL, so we don't have any references to mark.
This means we should safely be able to mark Mutex as WB_PROTECTED
without changing anything else.
2021-09-17 10:03:39 +09:00
S.H
378e8cdad6
Using RBOOL macro 2021-08-02 12:06:44 +09:00
Nobuyoshi Nakada
070557afc4 Distinguish signal and timeout [Bug #16608] 2021-07-25 13:09:03 -07:00
Samuel Williams
42130a64f0
Replace copy coroutine with pthread implementation. 2021-07-01 11:23:03 +12:00
Nobuyoshi Nakada
9eae8cdefb
Prefer qualified names under Thread 2021-06-29 11:41:10 +09:00
Nobuyoshi Nakada
b7d01b0d1b
Refined define_thread_class
Reduce duplications
* ID caluculations of the same name
* checks against the same name
* registration to the root module hash
2021-06-28 16:52:49 +09:00
Nobuyoshi Nakada
e4f891ce8d
Adjust styles [ci skip]
* --braces-after-func-def-line
* --dont-cuddle-else
* --procnames-start-lines
* --space-after-for
* --space-after-if
* --space-after-while
2021-06-17 10:13:40 +09:00
Nobuyoshi Nakada
b32e6c168a
Convert initial contents before allocating queue buffer 2021-06-15 15:55:52 +09:00
Nobuyoshi Nakada
2a6bfd2246
Properly convert time_t [Bug #17645] 2021-03-14 20:18:50 +09:00
Nobuyoshi Nakada
8a5a91eead
--dont-cuddle-else [ci skip] 2021-03-14 20:18:30 +09:00
Nobuyoshi Nakada
1f0e0dfb22
Thread::Queue.new should accept an Enumerable [Feature #17327]
Enumerable implements #to_a but not #to_array.
2021-02-12 12:21:49 +09:00
Chris Seaton
c3b2bb0969
The Queue constructor should take an initial set of objects
Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2021-02-11 19:14:18 +09:00
Samuel Williams
5f69a7f604
Expose scheduler as public interface & bug fixes. (#3945)
* Rename `rb_scheduler` to `rb_fiber_scheduler`.

* Use public interface if available.

* Use `rb_check_funcall` where possible.

* Don't use `unblock` unless the fiber was non-blocking.
2021-02-09 19:39:56 +13:00
Nobuyoshi Nakada
834b404b9a
[DOC] Fixed indent [ci skip] 2021-01-14 21:35:38 +09:00
Samuel Williams
3b5b309b7b Proposed method for dealing with stack locals which have non-local lifetime. 2020-12-05 11:38:56 +13:00
Nobuyoshi Nakada
0d52dce3a3
strip trailing spaces and adjusted indents [ci skip] 2020-11-12 19:27:20 +09:00
Samuel Williams
c39984ec5c Tidy up book keeping for thread->keeping_mutexes.
When a scheduler is present, it's entirely possible for
`th->keeping_mutexes` to be updated while enumerating the waitq. Therefore
it must be fetched only during the removal operation.
2020-11-08 20:40:52 +13:00
Samuel Williams
f73135233b Don't try to resume blocked fiber on dead thread. 2020-11-08 20:40:52 +13:00
Samuel Williams
a08ee8330d Rename to Fiber#set_scheduler. 2020-11-07 23:39:50 +13:00
Samuel Williams
70f08f1eed Make Thread#join non-blocking. 2020-09-21 11:48:44 +12:00
Benoit Daloze
6987c8997e Remove from waiter in Mutex#lock with ensure when calling rb_scheduler_block()
* Previously this could lead to an invalid waiter entry and then trying
  to wake up that waiter would result in various issues in rb_mutex_unlock_th().
2020-09-20 13:35:39 +02:00