Commit graph

47 commits

Author SHA1 Message Date
Samuel Williams
73c9d6ccaa
Allow IO#close to interrupt IO operations on fibers using fiber_interrupt hook. (#12839) 2025-05-23 14:55:05 +09:00
Samuel Williams
9c268302bf
Introduce Fiber::Scheduler#blocking_operation_wait. (#12016)
Redirect `rb_nogvl` blocking operations to the fiber scheduler if possible
to prevent stalling the event loop.

[Feature #20876]
2024-11-20 19:40:17 +13:00
Samuel Williams
3b9896acfc
Revert "Introduce Fiber Scheduler blocking_region hook. (#11963)" (#12013)
This reverts some of commit 87fb44dff6.

We will rename and propose a slightly different interface.
2024-11-06 22:19:40 +13:00
Samuel Williams
87fb44dff6
Introduce Fiber Scheduler blocking_region hook. (#11963) 2024-10-31 17:26:37 +13:00
Samuel Williams
bdb1fc1e5b
Prefer to use Fiber#transfer in scheduler implementation. (#10479) 2024-04-07 19:57:15 +12:00
Samuel Williams
648870b5c5
Support IO#pread / IO#pwrite using fiber scheduler. (#7594)
* Skip test if non-blocking file IO is not supported.
2023-03-31 00:48:55 +13:00
Samuel Williams
7abe47b85a
Improve robustness of io_wait implementation. (#7456)
- Restore correct handling of `duration`.
- Don't delete from `@readable` or `@writable` unless it was added.
- A little more documentation.
2023-03-07 19:38:58 +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
Samuel Williams
8a420670a2
Introduce Fiber::Scheduler#io_select hook for non-blocking IO.select. (#6559) 2022-10-15 19:59:04 +13:00
Samuel Williams
04d291a490
Simplify implementation of scheduler io_read and io_write. (#6527) 2022-10-12 15:56:35 +13:00
Samuel Williams
ced1d17280
Improvements to IO::Buffer implementation and documentation. (#6525) 2022-10-12 12:59:05 +13:00
Samuel Williams
42bcc629fb Retain reference to blocking fibers. 2022-05-25 15:24:24 +12:00
Samuel Williams
9fbf94ff04 Improve interface for get/set/copy. 2021-12-21 12:25:42 +13:00
Samuel Williams
42d3231154
Introduce io_result wrapper for passing [-errno, size] in VALUE. 2021-12-18 18:19:30 +13:00
Nobuyoshi Nakada
a88b19d3d0
Suppress the “experimental" warnings for IO::Buffer
As this warning is emitted just once per processes, needs in each
files when parallel testing.
2021-11-23 16:08:53 +09:00
Samuel Williams
cb8434563d Add alternative optional hook for scheduler_close to allow public usage of close. 2021-09-20 22:07:58 +12:00
Lars Kanis
9c0582704f Fix example fiber scheduler reg. writable events
There were two issues:

1. When an IO object is waiting for writablility only (as in test_tcp_accept) the selected hash is empty.
   Therefore selected[fiber] returns nil but needs to default to 0 in order to be or'ed with IO::WRITABLE.

2. When an IO object is waiting for read- or writability (as in test_tcp_connect), but only one of these
   two events arrive, the Fiber and IO object need to be removed from the other `@readable` or `@writable` list.
2021-08-26 21:49:12 +12:00
Samuel Williams
2d4f29e77e Fix potential hang when joining threads.
If the thread termination invokes user code after `th->status` becomes
`THREAD_KILLED`, and the user unblock function causes that `th->status` to
become something else (e.g. `THREAD_RUNNING`), threads waiting in
`thread_join_sleep` will hang forever. We move the unblock function call
to before the thread status is updated, and allow threads to join as soon
as `th->value` becomes defined.

This reverts commit 6505c77501.
2021-08-03 22:23:48 +12:00
Yusuke Endoh
6505c77501 Revert "Fix potential hang when joining threads."
This reverts commit 13f8521c63.

20210727T230009Z.fail.html.gz
20210728T000009Z.fail.html.gz

This revert is to confirm whether the commit is the cause.
If the failures consistently occur after this revert, I'll
reintroduce the commit.
2021-07-28 11:05:36 +09:00
Samuel Williams
13f8521c63 Fix potential hang when joining threads.
If the thread termination invokes user code after `th->status` becomes
`THREAD_KILLED`, and the user unblock function causes that `th->status` to
become something else (e.g. `THREAD_RUNNING`), threads waiting in
`thread_join_sleep` will hang forever. We move the unblock function call
to before the thread status is updated, and allow threads to join as soon
as `th->value` becomes defined.
2021-07-27 18:23:30 +12:00
Nobuyoshi Nakada
9eae8cdefb
Prefer qualified names under Thread 2021-06-29 11:41:10 +09:00
Samuel Williams
050a895439
Wake up join list within thread EC context. (#4471)
* Wake up join list within thread EC context.

* Consume items from join list so that they are not re-executed.

If `rb_fiber_scheduler_unblock` raises an exception, it can result in a
segfault if `rb_threadptr_join_list_wakeup` is not within a valid EC. This
change moves `rb_threadptr_join_list_wakeup` into the thread's top level EC
which initially caused an infinite loop because on exception will retry. We
explicitly remove items from the thread's join list to avoid this situation.

* Verify the required scheduler interface.

* Test several scheduler hooks methods with broken `unblock` implementation.
2021-06-14 17:56:53 +12:00
Samuel Williams
2792acc8f2
Add scheduler hook Addrinfo.getaddrinfo. (#4375)
Co-authored-by: Bruno Sutic <code@brunosutic.com>
2021-06-14 16:21:08 +12:00
Samuel Williams
511acba4ae Update method name and add documentation. 2021-03-30 18:38:42 +13:00
Samuel Williams
4c53dc970b Add hook for Timeout.timeout. 2021-03-30 18:38:42 +13: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
Samuel Williams
2553c5f94a Add support for non-blocking Process.wait. 2020-12-09 08:55:35 +13:00
Samuel Williams
bed4848661 Urgent notification pipe has same lifetime as scheduler. 2020-11-08 20:40:52 +13:00
Samuel Williams
57b83dad4c Defer kernel_sleep to block to avoid exiting the event loop when duration is nil. 2020-11-08 20:40:52 +13:00
Samuel Williams
7f29020590 Raise an exception if the scheduler was already closed. 2020-10-01 16:02:03 +13:00
Yusuke Endoh
416bb11a5e test/fiber/scheduler.rb: Prevent "instance variable @urgent not initialized" 2020-09-24 19:29:54 +09:00
Benoit Daloze
2b73e6ba71 Simplify the implementation of Scheduler#block
* This shows block() with a timeout is similar to #kernel_sleep and also
  does not need to change `@blocking`.
2020-09-21 16:04:20 +02:00
Samuel Williams
70f08f1eed Make Thread#join non-blocking. 2020-09-21 11:48:44 +12:00
Samuel Williams
501fff14c7 When setting current thread scheduler to nil, invoke #close. 2020-09-21 09:51:33 +12:00
Benoit Daloze
d01954632d Add missing goto found;
* To still remove the lock from the Thread's list of acquired locks.
* Also to not wake up other waiters and preserve blocking behavior.
2020-09-17 17:26:52 +02:00
Benoit Daloze
deffb63021 Document the various scheduler hooks 2020-09-17 14:59:38 +02:00
Benoit Daloze
9472d16061 Call scheduler.block instead of scheduler.kernel_sleep for blocking Queue/SizedQueue operations
* scheduler.unblock was already already called before but with no corresponding scheduler.block
* add test that Queue#pop makes the scheduler wait until it gets an element.
2020-09-17 14:59:38 +02:00
Benoit Daloze
738a089b3a Rename scheduler.{mutex_lock,mutex_unlock} to {block,unblock}
* Move #kernel_sleep next to #block as it is similar
2020-09-17 14:30:40 +02:00
Benoit Daloze
c3acfcc78d Use a similar pattern for waiting and ready 2020-09-17 14:14:49 +02:00
Benoit Daloze
10290da54d Add a note at the top of the test scheduler 2020-09-16 11:37:06 +02:00
Samuel Williams
0f613cc5f1 Add support for ConditionVariable. 2020-09-14 16:44:09 +12:00
Samuel Williams
1a0cfe2839 Improve handling of urgent notification pipe. 2020-09-14 16:44:09 +12:00
Benoit Daloze
178c1b0922 Make Mutex per-Fiber instead of per-Thread
* Enables Mutex to be used as synchronization between multiple Fibers
  of the same Thread.
* With a Fiber scheduler we can yield to another Fiber on contended
  Mutex#lock instead of blocking the entire thread.
* This also makes the behavior of Mutex consistent across CRuby, JRuby and TruffleRuby.
* [Feature #16792]
2020-09-14 16:44:09 +12:00
Samuel Williams
d387029f39 Standardised scheduler interface. 2020-09-14 16:44:09 +12:00
Samuel Williams
905e9c8093 Simplify bitmasks for IO events. 2020-09-14 16:44:09 +12:00
Samuel Williams
e6e9cef06e Improve consistency of tests. 2020-07-20 13:20:58 +12:00
Samuel Williams
6fa8455ebb Move test/scheduler -> test/fiber [Bug #16892][ruby-core:98366]. 2020-05-15 13:26:23 +12:00
Renamed from test/scheduler/scheduler.rb (Browse further)