Commit graph

114 commits

Author SHA1 Message Date
nagachika
6181839531 merge revision(s) eacedcfe44: [Backport #19105]
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>
	---
	 test/fiber/test_mutex.rb | 22 +++++++++++++++++++++-
	 thread_sync.c            |  4 ++++
	 2 files changed, 25 insertions(+), 1 deletion(-)
2022-11-13 11:18:27 +09: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
Benoit Daloze
af1926e859 Fix copy/paste error from 5bb5e706f1 2020-09-18 11:11:47 +02:00
Benoit Daloze
5bb5e706f1 Only interrupt when there is no scheduler in sync_wakeup()
* 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 d01954632d
2020-09-18 10:39:27 +02:00
Benoit Daloze
d9b943b8e5 Cleanup commented code
* Mutex operations no longer disable the Fiber scheduler.
2020-09-17 17:29:43 +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
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
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
Koichi Sasada
79df14c04b Introduce Ractor mechanism for parallel execution
This commit introduces Ractor mechanism to run Ruby program in
parallel. See doc/ractor.md for more details about Ractor.
See ticket [Feature #17100] to see the implementation details
and discussions.

[Feature #17100]

This commit does not complete the implementation. You can find
many bugs on using Ractor. Also the specification will be changed
so that this feature is experimental. You will see a warning when
you make the first Ractor with `Ractor.new`.

I hope this feature can help programmers from thread-safety issues.
2020-09-03 21:11:06 +09:00
卜部昌平
f402dc3557 rb_szqueue_push: do not goto into a branch
I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.
2020-06-29 11:05:41 +09:00
Samuel Williams
0e3b0fcdba
Thread scheduler for light weight concurrency. 2020-05-14 22:10:55 +12:00
Nobuyoshi Nakada
5d430c1b34
Added more NORETURN declarations 2020-05-11 00:40:14 +09:00
卜部昌平
115fec062c more on NULL versus functions.
Function pointers are not void*.  See also
ce4ea956d2
8427fca49b
2020-02-07 14:24:19 +09:00
Koichi Sasada
d8d581bfc4 add assertion for mutex_lock.
After do_mutex_lock(mutex), the mutex should be owned by the current
thread. Adding an assertion for this assumption.
2019-10-28 12:19:18 +09:00
Nobuyoshi Nakada
0c6f36668a
Adjusted spaces [ci skip] 2019-09-27 10:20:56 +09:00
卜部昌平
69683968a9 fix arity of rb_mutex_synchronize_m
This is just a trivial mistake introduced in
6c56dae4b2.
2019-08-29 18:34:09 +09:00
卜部昌平
703783324c rb_ensure now free from ANYARGS
After 5e86b005c0, I now think ANYARGS is
dangerous and should be extinct.  This commit deletes ANYARGS from
rb_ensure, which also revealed many arity / type mismatches.
2019-08-27 15:52:26 +09:00
Neeraj Bhunwal
c1d78a7f0e do_mutex_lock: release mutex before checking for interrupts (fixes issue 15360) 2019-06-11 15:44:11 -07:00
Nobuyoshi Nakada
b1aecef873
Use UNALIGNED_MEMBER_PTR
* internal.h (UNALIGNED_MEMBER_ACCESS, UNALIGNED_MEMBER_PTR):
  moved from eval_intern.h.

* compile.c iseq.c, vm.c: use UNALIGNED_MEMBER_PTR for `entries`
  in `struct iseq_catch_table`.

* vm_eval.c, vm_insnhelper.c: use UNALIGNED_MEMBER_PTR for `body`
  in `rb_method_definition_t`.
2019-05-31 16:04:16 +09:00
normal
0fd53f519f thread_sync.c (rb_mutex_t): eliminate fork_gen
The true bug fork_gen was hiding was rb_mutex_abandon_locking_mutex
failing to unconditionally clear the waitq of mutexes it was
waiting on.  So we fix rb_mutex_abandon_locking_mutex, instead,
and eliminate rb_mutex_cleanup_keeping_mutexes.

This commit was tested heavily on a single-core Pentium-M which
was my most reliable reproducer of the "crash.rb" script from
[Bug #15383]

[Bug #14578] [Bug #15383]

Note: [Bug #15430] turned out to be an entirely different
problem: RLIMIT_NPROC limit was hit on the CI VMs.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66489 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-22 01:41:18 +00:00
normal
fa5601e7b9 thread_sync.c (rb_mutex_cleanup_keeping_mutexes): update fork_gen
... when clearing waitq. Otherwise, we risk redundantly clearing
valid waiters in future calls to `mutex_ptr`.

Note: I am not sure if this fixes [Bug #15430], and even if it
did, fork_gen is a belt-and-suspenders redundancy for [Bug #15383]
which wastes one word for every Mutex object.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66477 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-21 12:32:52 +00:00
normal
2a742d5f9b thread_sync.c (rb_mutex_abandon_keeping_mutexes): remove unnecessary check
rb_mutex_abandon_all functions fine when passed a NULL value,
so let the compiler deal with the complexity of the branch
instead of the person reading the code.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66476 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-21 12:32:48 +00:00
normal
1df9c2bc1c thread_sync.c (mutex_ptr): only reinitalize waitqueue at fork
Mutexes need to remain locked after forking.

This fixes "[BUG] invalid keeping_mutexes: Attempt to unlock a
mutex which is locked by another thread" and should
fix test_fork_while_parent_locked failures in CI

[ruby-core:90581] [Bug #15424]
[ruby-core:90595] [Bug #15430]

Fixes: r66230 ("handle mutexes held by parent threads in children")

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66438 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-18 09:21:05 +00:00