* 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
154 lines
4.6 KiB
C
154 lines
4.6 KiB
C
#ifndef RUBY_VM_SYNC_H
|
|
#define RUBY_VM_SYNC_H
|
|
|
|
#include "vm_debug.h"
|
|
#include "debug_counter.h"
|
|
|
|
#if USE_RUBY_DEBUG_LOG
|
|
#define LOCATION_ARGS const char *file, int line
|
|
#define LOCATION_PARAMS file, line
|
|
#define APPEND_LOCATION_ARGS , const char *file, int line
|
|
#define APPEND_LOCATION_PARAMS , file, line
|
|
#else
|
|
#define LOCATION_ARGS void
|
|
#define LOCATION_PARAMS
|
|
#define APPEND_LOCATION_ARGS
|
|
#define APPEND_LOCATION_PARAMS
|
|
#endif
|
|
|
|
bool rb_vm_locked_p(void);
|
|
void rb_vm_lock_body(LOCATION_ARGS);
|
|
void rb_vm_unlock_body(LOCATION_ARGS);
|
|
|
|
struct rb_ractor_struct;
|
|
NOINLINE(void rb_vm_lock_enter_body_cr(struct rb_ractor_struct *cr, unsigned int *lev APPEND_LOCATION_ARGS));
|
|
NOINLINE(void rb_vm_lock_enter_body_nb(unsigned int *lev APPEND_LOCATION_ARGS));
|
|
NOINLINE(void rb_vm_lock_enter_body(unsigned int *lev APPEND_LOCATION_ARGS));
|
|
void rb_vm_lock_leave_body_nb(unsigned int *lev APPEND_LOCATION_ARGS);
|
|
void rb_vm_lock_leave_body(unsigned int *lev APPEND_LOCATION_ARGS);
|
|
void rb_vm_barrier(void);
|
|
|
|
#if RUBY_DEBUG
|
|
// GET_VM()
|
|
#include "vm_core.h"
|
|
#endif
|
|
|
|
RUBY_EXTERN struct rb_ractor_struct *ruby_single_main_ractor; // ractor.c
|
|
|
|
static inline bool
|
|
rb_multi_ractor_p(void)
|
|
{
|
|
if (LIKELY(ruby_single_main_ractor)) {
|
|
// 0 on boot time.
|
|
RUBY_ASSERT(GET_VM()->ractor.cnt <= 1);
|
|
return false;
|
|
}
|
|
else {
|
|
// multi-ractor mode can run ractor.cnt == 1
|
|
return true;
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_lock(const char *file, int line)
|
|
{
|
|
RB_DEBUG_COUNTER_INC(vm_sync_lock);
|
|
|
|
if (rb_multi_ractor_p()) {
|
|
rb_vm_lock_body(LOCATION_PARAMS);
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_unlock(const char *file, int line)
|
|
{
|
|
if (rb_multi_ractor_p()) {
|
|
rb_vm_unlock_body(LOCATION_PARAMS);
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_lock_enter(unsigned int *lev, const char *file, int line)
|
|
{
|
|
RB_DEBUG_COUNTER_INC(vm_sync_lock_enter);
|
|
|
|
if (rb_multi_ractor_p()) {
|
|
rb_vm_lock_enter_body(lev APPEND_LOCATION_PARAMS);
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_lock_enter_nb(unsigned int *lev, const char *file, int line)
|
|
{
|
|
RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_nb);
|
|
|
|
if (rb_multi_ractor_p()) {
|
|
rb_vm_lock_enter_body_nb(lev APPEND_LOCATION_PARAMS);
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_lock_leave_nb(unsigned int *lev, const char *file, int line)
|
|
{
|
|
if (rb_multi_ractor_p()) {
|
|
rb_vm_lock_leave_body_nb(lev APPEND_LOCATION_PARAMS);
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_lock_leave(unsigned int *lev, const char *file, int line)
|
|
{
|
|
if (rb_multi_ractor_p()) {
|
|
rb_vm_lock_leave_body(lev APPEND_LOCATION_PARAMS);
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_lock_enter_cr(struct rb_ractor_struct *cr, unsigned int *levp, const char *file, int line)
|
|
{
|
|
RB_DEBUG_COUNTER_INC(vm_sync_lock_enter_cr);
|
|
rb_vm_lock_enter_body_cr(cr, levp APPEND_LOCATION_PARAMS);
|
|
}
|
|
|
|
static inline void
|
|
rb_vm_lock_leave_cr(struct rb_ractor_struct *cr, unsigned int *levp, const char *file, int line)
|
|
{
|
|
rb_vm_lock_leave_body(levp APPEND_LOCATION_PARAMS);
|
|
}
|
|
|
|
#define RB_VM_LOCKED_P() rb_vm_locked_p()
|
|
|
|
#define RB_VM_LOCK() rb_vm_lock(__FILE__, __LINE__)
|
|
#define RB_VM_UNLOCK() rb_vm_unlock(__FILE__, __LINE__)
|
|
|
|
#define RB_VM_LOCK_ENTER_CR_LEV(cr, levp) rb_vm_lock_enter_cr(cr, levp, __FILE__, __LINE__)
|
|
#define RB_VM_LOCK_LEAVE_CR_LEV(cr, levp) rb_vm_lock_leave_cr(cr, levp, __FILE__, __LINE__)
|
|
#define RB_VM_LOCK_ENTER_LEV(levp) rb_vm_lock_enter(levp, __FILE__, __LINE__)
|
|
#define RB_VM_LOCK_LEAVE_LEV(levp) rb_vm_lock_leave(levp, __FILE__, __LINE__)
|
|
|
|
#define RB_VM_LOCK_ENTER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV(&_lev);
|
|
#define RB_VM_LOCK_LEAVE() RB_VM_LOCK_LEAVE_LEV(&_lev); }
|
|
#define RB_VM_LOCKING() \
|
|
for (unsigned int vm_locking_level, vm_locking_do = (RB_VM_LOCK_ENTER_LEV(&vm_locking_level), 1); \
|
|
vm_locking_do; RB_VM_LOCK_LEAVE_LEV(&vm_locking_level), vm_locking_do = 0)
|
|
|
|
#define RB_VM_LOCK_ENTER_LEV_NB(levp) rb_vm_lock_enter_nb(levp, __FILE__, __LINE__)
|
|
#define RB_VM_LOCK_LEAVE_LEV_NB(levp) rb_vm_lock_leave_nb(levp, __FILE__, __LINE__)
|
|
#define RB_VM_LOCK_ENTER_NO_BARRIER() { unsigned int _lev; RB_VM_LOCK_ENTER_LEV_NB(&_lev);
|
|
#define RB_VM_LOCK_LEAVE_NO_BARRIER() RB_VM_LOCK_LEAVE_LEV_NB(&_lev); }
|
|
#define RB_VM_LOCKING_NO_BARRIER() \
|
|
for (unsigned int vm_locking_level, vm_locking_do = (RB_VM_LOCK_ENTER_LEV_NB(&vm_locking_level), 1); \
|
|
vm_locking_do; RB_VM_LOCK_LEAVE_LEV_NB(&vm_locking_level), vm_locking_do = 0)
|
|
|
|
#if RUBY_DEBUG > 0
|
|
void RUBY_ASSERT_vm_locking(void);
|
|
void RUBY_ASSERT_vm_unlocking(void);
|
|
#define ASSERT_vm_locking() RUBY_ASSERT_vm_locking()
|
|
#define ASSERT_vm_unlocking() RUBY_ASSERT_vm_unlocking()
|
|
#else
|
|
#define ASSERT_vm_locking()
|
|
#define ASSERT_vm_unlocking()
|
|
#endif
|
|
|
|
#endif // RUBY_VM_SYNC_H
|