54 Commits
Author SHA1 Message Date
nagachika 6a8c166498 merge revision(s) 5c7af72304d0ad33cd3f21b24a4bc44e8acd5b2c,d650b17686d49c2ce8e6a87039861154e93d4621: [Backport #18497]
Assuming EXIT_SUCCESS equals 0 is not portable

	---
	 test/ruby/test_fiber.rb | 6 +++---
	 1 file changed, 3 insertions(+), 3 deletions(-)

	`rb_fiber_terminate` must not return [Bug #18497]

	In a forked process from a fiber, the fiber becomes the only
	fiber, `fiber_switch` does nothing as there is no other fibers,
	`rb_fiber_terminate` does not terminate the fiber.  In that case,
	reaches the end of `fiber_entry` finaly, which is declared as
	"COROUTINE" and should never return.
	---
	 cont.c                     |  3 ++-
	 eval_intern.h              |  2 +-
	 test/fiber/test_process.rb | 15 +++++++++++++++
	 test/ruby/test_fiber.rb    |  5 +++++
	 4 files changed, 23 insertions(+), 2 deletions(-)
2022-03-13 11:47:39 +09:00
Samuel Williams 8ac058b645 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-09-26 20:49:54 +09:00
Nobuyoshi Nakada 7d4a0c4b93 Suppress exception report in inner thread 2021-09-26 20:49:54 +09:00
Nobuyoshi Nakada 24f146bee1 Close leaked file descriptors 2021-09-26 20:49:54 +09:00
Samuel Williams 5e9ec35104 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-09-26 20:49:54 +09:00
nagachikaandSamuel Williams 95dc88c888 partially merge revision(s) 5f69a7f604: [Backport #17666]
Co-authored-by: Samuel Williams <@ioquatix>
https://github.com/nagachika/ruby/pull/1/commits/2cee515f024f3295945f312cb6b052f972f9c93d
2021-07-25 13:49:53 +09:00
nagachika 13f93ad16d merge revision(s) 611e711085c7e3984555a79626d025c8b876eced,a9c5c2d614f30a616970245fef3e7ffc151e2ecf: [Backport #17527]
Test incorrect behaviour of `rb_io_wait_readable/writable`.

	---
	 test/fiber/test_io.rb | 35 +++++++++++++++++++++++++++++++++++
	 1 file changed, 35 insertions(+)

	Check errno before invoking scheduler in
	 `rb_io_wait_readable/writable`.

	See <https://bugs.ruby-lang.org/issues/17527> for more details.
	---
	 io.c | 66 +++++++++++++++++++++++++++++++++++-------------------------------
	 1 file changed, 35 insertions(+), 31 deletions(-)
2021-04-24 14:04:04 +09:00
Delton Ding f1591b3858 Add verbose nil in testing 2020-12-23 03:27:14 +09:00
Delton Ding c6d7e02b90 Enable Fiber.current and Fiber#alive? call inside ractor 2020-12-23 03:27:14 +09:00
Samuel Williams 2553c5f94a Add support for non-blocking Process.wait. 2020-12-09 08:55:35 +13:00
Benoit Daloze b8eb08e096 Fix TestFiberMutex#test_condition_variable assertion
* Now that it works correctly.
2020-11-08 16:49:33 +01: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 a08ee8330d Rename to Fiber#set_scheduler. 2020-11-07 23:39:50 +13:00
Nobuyoshi Nakada 1d3024da26 Refined assertions for better failure messages 2020-10-01 13:48:15 +09:00
Nobuyoshi Nakada eef12cdc06 strip trailing spaces [ci skip] 2020-10-01 13:47:57 +09:00
Samuel Williams dd2e95fb26 Remove Thread.scheduler from public interface.
It's implementation is equivalent to:

Thread.current.scheduler unless Thread.current.blocking?
2020-10-01 16:56:05 +13:00
Samuel Williams 7f29020590 Raise an exception if the scheduler was already closed. 2020-10-01 16:02:03 +13:00
Samuel Williams 13660105e2 Don't call Scheduler#close if it doesn't exist. 2020-10-01 16:02:03 +13:00
Samuel Williams 388281c5c9 Fix order of operations during rb_ec_finalize. 2020-09-30 16:34:38 +13:00
Kazuhiro NISHIYAMA ac414139ec Remove unnecessary executable bit [ci skip] 2020-09-28 23:07:43 +09:00
Yusuke Endoh 416bb11a5e test/fiber/scheduler.rb: Prevent "instance variable @urgent not initialized" 2020-09-24 19:29:54 +09:00
Nobuyoshi Nakada b57c54679b strip trailing spaces [ci skip] 2020-09-23 17:47:30 +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 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
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 264889ec3d Fix Mutex#unlock with a scheduler and thread contention
* It would hit "[BUG] unexpected THREAD_STOPPED" before.
2020-09-17 15:15:43 +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 8eea66a0ca Add support for Queue & SizedQueue. 2020-09-14 16:44:09 +12: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 132453fa52 Rename Fiber{} to Fiber.schedule{}. 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
Yusuke Endoh 701217572f test/fiber/test_backtrace.rb: prevent a warning
```
/home/chkbuild/chkbuild/tmp/build/20200818T033004Z/ruby/test/fiber/test_backtrace.rb:9:
warning: ambiguous first argument; put parentheses or a space even after
`/' operator
/home/chkbuild/chkbuild/tmp/build/20200818T033004Z/ruby/test/fiber/test_backtrace.rb:15:
warning: ambiguous first argument; put parentheses or a space even after
`/' operator
```
2020-08-18 14:36:39 +09:00
Samuel Williams 0a218a97ad Expose ec -> backtrace (internal) and use it to implement fiber backtrace.
See <https://bugs.ruby-lang.org/issues/16815> for more details.
2020-08-18 00:56:35 +12:00
Yusuke Endoh 098e8c2873 Suppress "assigned but unused variable" warnings 2020-07-23 23:44:25 +09:00
Nobuyoshi Nakada d2bf6133f6 Remove trailing spaces [ci skip] 2020-07-20 13:34:16 +09:00
Samuel Williams 9f6a3d0306 Add multi-threaded I/O test. 2020-07-20 13:20:58 +12:00
Samuel Williams e6e9cef06e Improve consistency of tests. 2020-07-20 13:20:58 +12:00
Samuel Williams 238464863a Remove fiber HTTP test.
The HTTP test hits a remote website which isn't always available and is
not self-contained. Ideally we will rewrite this test with an internal
web server.
2020-07-16 13:43:56 +12:00
Nobuyoshi Nakada 79d06483a8 Close sockets if blocking to fix leaked fds 2020-07-15 16:36:57 +09:00
Nobuyoshi Nakada 101d56ddca Improved assertions 2020-06-09 21:42:25 +09:00