aboutsummaryrefslogtreecommitdiffstats
path: root/thread_sync.c
Commit message (Collapse)AuthorAgeFilesLines
* M:N thread scheduler for RactorsKoichi Sasada2023-10-121-44/+64
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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]
* Make {Queue,SizedQueue}#freeze raise TypeErrorJeremy Evans2023-09-271-0/+17
| | | | Fixes [Bug #17146]
* Try default `gcc` 9.4.0 to see if it exhibits the same compiler bugs. (#8394)Samuel Williams2023-09-081-23/+9
| | | | | | | | | | | | | | | * 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>
* Extract `do_mutex_lock_check_interrupts` to try and fix `ppc64le`. (#8393)Samuel Williams2023-09-081-9/+23
| | | | | | 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.
* [DOC] Add parenthetical to explain what FIFO abbrev meansShane Becker2023-07-131-2/+2
|
* [DOC] Fix example code indentation from 3 to 2Shane Becker2023-07-131-6/+6
|
* Correctly clean up `keeping_mutexes` before resuming any other threads. (#7460)Samuel Williams2023-03-071-30/+27
| | | | | | | | | | | 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>.
* Replace `PACKED_STRUCT` and `PACKED_STRUCT_UNALIGNED`Nobuyoshi Nakada2023-02-081-4/+6
|
* Add `queue_list` and `szqueue_list` macrosNobuyoshi Nakada2023-01-211-2/+4
|
* Fix compilation warnings in thread_sync.cPeter Zhu2023-01-191-2/+2
| | | | | | | | 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]
* Don't redefine RB_OBJ_WRITEPeter Zhu2023-01-181-2/+2
| | | | | RB_OBJ_WRITE already exists in rgengc.h, so we shouldn't redefine it in gc.h.
* mutex: Raise a ThreadError when detecting a fiber deadlock (#6680)Jean byroot Boussier2022-11-091-0/+4
| | | | | | | | | [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>
* Avoid missed wakeup with fiber scheduler and Fiber.blocking. (#6588)Samuel Williams2022-10-201-8/+17
| | | * Ensure that blocked fibers don't prevent valid wakeups.
* Adjust indents [ci skip]Nobuyoshi Nakada2022-10-181-12/+12
|
* thread_sync.c: Clarify and document the behavior of timeout == 0Jean Boussier2022-10-171-20/+25
| | | | | | | | | | | [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.
* Implement SizedQueue#push(timeout: sec)Jean Boussier2022-08-181-35/+13
| | | | | | | [Feature #18944] If both `non_block=true` and `timeout:` are supplied, ArgumentError is raised.
* Adjust styles [ci skip]Nobuyoshi Nakada2022-08-061-2/+4
|
* thread_sync.c: pass proper argument to queue_sleep in rb_szqueue_pushJean Boussier2022-08-041-1/+7
| | | | | | | | | 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.
* Implement Queue#pop(timeout: sec)Jean Boussier2022-08-021-60/+45
| | | | | | | | | [Feature #18774] As well as `SizedQueue#pop(timeout: sec)` If both `non_block=true` and `timeout:` are supplied, ArgumentError is raised.
* Rename rb_ary_tmp_new to rb_ary_hidden_newPeter Zhu2022-07-261-1/+1
| | | | | | 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.
* Expand tabs [ci skip]Takashi Kokubun2022-07-211-36/+36
| | | | [Misc #18891]
* Prefix ccan headers (#4568)Nobuyoshi Nakada2022-03-301-38/+38
| | | | | | | | | | | | | * 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>
* [DOC] Improve Thread::Queue.new docs [ci skip]Victor Shepelev2021-12-151-4/+16
|
* Suppress address-of-packed-member warning by gccNobuyoshi Nakada2021-11-281-1/+3
|
* Add WB_PROTECTED to mutexesJohn Hawthorn2021-09-171-1/+1
| | | | | | 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.
* Using RBOOL macroS.H2021-08-021-18/+7
|
* Distinguish signal and timeout [Bug #16608]Nobuyoshi Nakada2021-07-251-7/+11
|
* Replace copy coroutine with pthread implementation.Samuel Williams2021-07-011-47/+38
|
* Prefer qualified names under ThreadNobuyoshi Nakada2021-06-291-49/+49
|
* Refined define_thread_classNobuyoshi Nakada2021-06-281-4/+4
| | | | | | | Reduce duplications * ID caluculations of the same name * checks against the same name * registration to the root module hash
* Adjust styles [ci skip]Nobuyoshi Nakada2021-06-171-3/+7
| | | | | | | | | * --braces-after-func-def-line * --dont-cuddle-else * --procnames-start-lines * --space-after-for * --space-after-if * --space-after-while
* Convert initial contents before allocating queue bufferNobuyoshi Nakada2021-06-151-2/+4
|
* Properly convert time_t [Bug #17645]Nobuyoshi Nakada2021-03-141-1/+1
|
* --dont-cuddle-else [ci skip]Nobuyoshi Nakada2021-03-141-5/+10
|
* Thread::Queue.new should accept an Enumerable [Feature #17327]Nobuyoshi Nakada2021-02-121-1/+1
| | | | Enumerable implements #to_a but not #to_array.
* The Queue constructor should take an initial set of objectsChris Seaton2021-02-111-3/+15
| | | | Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
* Expose scheduler as public interface & bug fixes. (#3945)Samuel Williams2021-02-091-10/+10
| | | | | | | | | * 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.
* [DOC] Fixed indent [ci skip]Nobuyoshi Nakada2021-01-141-1/+1
|
* Proposed method for dealing with stack locals which have non-local lifetime.Samuel Williams2020-12-051-44/+51
|
* strip trailing spaces and adjusted indents [ci skip]Nobuyoshi Nakada2020-11-121-1/+1
|
* Tidy up book keeping for `thread->keeping_mutexes`.Samuel Williams2020-11-081-18/+35
| | | | | | 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.
* Don't try to resume blocked fiber on dead thread.Samuel Williams2020-11-081-5/+5
|
* Rename to `Fiber#set_scheduler`.Samuel Williams2020-11-071-3/+3
|
* Make `Thread#join` non-blocking.Samuel Williams2020-09-211-1/+1
|
* Remove from waiter in Mutex#lock with ensure when calling rb_scheduler_block()Benoit Daloze2020-09-201-6/+13
| | | | | * 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().
* Fix copy/paste error from 5bb5e706f1d310a467075630145d2cc277045765Benoit Daloze2020-09-181-1/+1
|
* Only interrupt when there is no scheduler in sync_wakeup()Benoit Daloze2020-09-181-2/+4
| | | | | | | | * When there is a scheduler, the Fiber that would be blocked has already been rescheduled and there is no point to interrupt something else. That blocked Fiber will be rescheduled as the next call to the scheduler (e.g., IO, sleep, other blocking sync). * See discussion on https://github.com/ruby/ruby/commit/d01954632d
* Cleanup commented codeBenoit Daloze2020-09-171-4/+0
| | | | * Mutex operations no longer disable the Fiber scheduler.
* Add missing goto found;Benoit Daloze2020-09-171-0/+1
| | | | | * To still remove the lock from the Thread's list of acquired locks. * Also to not wake up other waiters and preserve blocking behavior.
* Fix Mutex#unlock with a scheduler and thread contentionBenoit Daloze2020-09-171-13/+13
| | | | * It would hit "[BUG] unexpected THREAD_STOPPED" before.