mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
Ractor::Port
* Added `Ractor::Port` * `Ractor::Port#receive` (support multi-threads) * `Rcator::Port#close` * `Ractor::Port#closed?` * Added some methods * `Ractor#join` * `Ractor#value` * `Ractor#monitor` * `Ractor#unmonitor` * Removed some methods * `Ractor#take` * `Ractor.yield` * Change the spec * `Racotr.select` You can wait for multiple sequences of messages with `Ractor::Port`. ```ruby ports = 3.times.map{ Ractor::Port.new } ports.map.with_index do |port, ri| Ractor.new port,ri do |port, ri| 3.times{|i| port << "r#{ri}-#{i}"} end end p ports.each{|port| pp 3.times.map{port.receive}} ``` In this example, we use 3 ports, and 3 Ractors send messages to them respectively. We can receive a series of messages from each port. You can use `Ractor#value` to get the last value of a Ractor's block: ```ruby result = Ractor.new do heavy_task() end.value ``` You can wait for the termination of a Ractor with `Ractor#join` like this: ```ruby Ractor.new do some_task() end.join ``` `#value` and `#join` are similar to `Thread#value` and `Thread#join`. To implement `#join`, `Ractor#monitor` (and `Ractor#unmonitor`) is introduced. This commit changes `Ractor.select()` method. It now only accepts ports or Ractors, and returns when a port receives a message or a Ractor terminates. We removes `Ractor.yield` and `Ractor#take` because: * `Ractor::Port` supports most of similar use cases in a simpler manner. * Removing them significantly simplifies the code. We also change the internal thread scheduler code (thread_pthread.c): * During barrier synchronization, we keep the `ractor_sched` lock to avoid deadlocks. This lock is released by `rb_ractor_sched_barrier_end()` which is called at the end of operations that require the barrier. * fix potential deadlock issues by checking interrupts just before setting UBF. https://bugs.ruby-lang.org/issues/21262
This commit is contained in:
parent
d2a1ad00cb
commit
ef2bb61018
Notes:
git
2025-05-30 19:01:47 +00:00
44 changed files with 2668 additions and 3517 deletions
26
vm_sync.c
26
vm_sync.c
|
@ -7,6 +7,7 @@
|
|||
|
||||
void rb_ractor_sched_barrier_start(rb_vm_t *vm, rb_ractor_t *cr);
|
||||
void rb_ractor_sched_barrier_join(rb_vm_t *vm, rb_ractor_t *cr);
|
||||
void rb_ractor_sched_barrier_end(rb_vm_t *vm, rb_ractor_t *cr);
|
||||
|
||||
static bool
|
||||
vm_locked(rb_vm_t *vm)
|
||||
|
@ -103,15 +104,26 @@ vm_lock_enter(rb_ractor_t *cr, rb_vm_t *vm, bool locked, bool no_barrier, unsign
|
|||
}
|
||||
|
||||
static void
|
||||
vm_lock_leave(rb_vm_t *vm, unsigned int *lev APPEND_LOCATION_ARGS)
|
||||
vm_lock_leave(rb_vm_t *vm, bool no_barrier, unsigned int *lev APPEND_LOCATION_ARGS)
|
||||
{
|
||||
rb_ractor_t *cr = vm->ractor.sync.lock_owner;
|
||||
|
||||
RUBY_DEBUG_LOG2(file, line, "rec:%u owner:%u%s", vm->ractor.sync.lock_rec,
|
||||
(unsigned int)rb_ractor_id(vm->ractor.sync.lock_owner),
|
||||
(unsigned int)rb_ractor_id(cr),
|
||||
vm->ractor.sync.lock_rec == 1 ? " (leave)" : "");
|
||||
|
||||
ASSERT_vm_locking();
|
||||
VM_ASSERT(vm->ractor.sync.lock_rec > 0);
|
||||
VM_ASSERT(vm->ractor.sync.lock_rec == *lev);
|
||||
VM_ASSERT(cr == GET_RACTOR());
|
||||
|
||||
#ifdef RUBY_THREAD_PTHREAD_H
|
||||
if (vm->ractor.sched.barrier_ractor == cr &&
|
||||
vm->ractor.sched.barrier_lock_rec == vm->ractor.sync.lock_rec) {
|
||||
VM_ASSERT(!no_barrier);
|
||||
rb_ractor_sched_barrier_end(vm, cr);
|
||||
}
|
||||
#endif
|
||||
|
||||
vm->ractor.sync.lock_rec--;
|
||||
*lev = vm->ractor.sync.lock_rec;
|
||||
|
@ -153,10 +165,16 @@ rb_vm_lock_enter_body_cr(rb_ractor_t *cr, unsigned int *lev APPEND_LOCATION_ARGS
|
|||
vm_lock_enter(cr, vm, vm_locked(vm), false, lev APPEND_LOCATION_PARAMS);
|
||||
}
|
||||
|
||||
void
|
||||
rb_vm_lock_leave_body_nb(unsigned int *lev APPEND_LOCATION_ARGS)
|
||||
{
|
||||
vm_lock_leave(GET_VM(), true, lev APPEND_LOCATION_PARAMS);
|
||||
}
|
||||
|
||||
void
|
||||
rb_vm_lock_leave_body(unsigned int *lev APPEND_LOCATION_ARGS)
|
||||
{
|
||||
vm_lock_leave(GET_VM(), lev APPEND_LOCATION_PARAMS);
|
||||
vm_lock_leave(GET_VM(), false, lev APPEND_LOCATION_PARAMS);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -174,7 +192,7 @@ rb_vm_unlock_body(LOCATION_ARGS)
|
|||
rb_vm_t *vm = GET_VM();
|
||||
ASSERT_vm_locking();
|
||||
VM_ASSERT(vm->ractor.sync.lock_rec == 1);
|
||||
vm_lock_leave(vm, &vm->ractor.sync.lock_rec APPEND_LOCATION_PARAMS);
|
||||
vm_lock_leave(vm, false, &vm->ractor.sync.lock_rec APPEND_LOCATION_PARAMS);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue