2#include "ccan/list/list.h"
5static VALUE rb_cMutex, rb_eClosedQueueError;
12 struct ccan_list_head waitq;
20 struct ccan_list_node node;
23static inline rb_fiber_t*
24nonblocking_fiber(rb_fiber_t *fiber)
26 if (rb_fiberptr_blocking(fiber)) {
39#define MUTEX_ALLOW_TRAP FL_USER1
42sync_wakeup(
struct ccan_list_head *head,
rb_len_t max)
44 RUBY_DEBUG_LOG(
"max:%"PRIdLEN, max);
48 ccan_list_for_each_safe(head, cur, next, node) {
49 ccan_list_del_init(&cur->node);
51 if (cur->th->status != THREAD_KILLED) {
52 if (cur->th->scheduler !=
Qnil && cur->fiber) {
56 RUBY_DEBUG_LOG(
"target_th:%u", rb_th_serial(cur->th));
57 rb_threadptr_interrupt(cur->th);
58 cur->th->status = THREAD_RUNNABLE;
61 if (--max == 0)
return;
67wakeup_one(
struct ccan_list_head *head)
73wakeup_all(
struct ccan_list_head *head)
78#if defined(HAVE_WORKING_FORK)
79static void rb_mutex_abandon_all(rb_mutex_t *mutexes);
80static void rb_mutex_abandon_keeping_mutexes(rb_thread_t *th);
81static void rb_mutex_abandon_locking_mutex(rb_thread_t *th);
83static const char* rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial);
86rb_mutex_num_waiting(rb_mutex_t *mutex)
91 ccan_list_for_each(&mutex->waitq, w, node) {
98rb_thread_t* rb_fiber_threadptr(
const rb_fiber_t *fiber);
101mutex_locked_p(rb_mutex_t *mutex)
103 return mutex->ec_serial != 0;
106static void thread_mutex_remove(rb_thread_t *thread, rb_mutex_t *mutex);
111 rb_mutex_t *mutex = ptr;
112 if (mutex_locked_p(mutex)) {
113 thread_mutex_remove(mutex->th, mutex);
119mutex_memsize(
const void *ptr)
121 return sizeof(rb_mutex_t);
126 {NULL, mutex_free, mutex_memsize,},
141rb_obj_is_mutex(
VALUE obj)
143 return RBOOL(rb_typeddata_is_kind_of(obj, &mutex_data_type));
147mutex_alloc(
VALUE klass)
154 ccan_list_head_init(&mutex->waitq);
161 return mutex_alloc(rb_cMutex);
167 rb_mutex_t *mutex = mutex_ptr(self);
169 return RBOOL(mutex_locked_p(mutex));
173thread_mutex_insert(rb_thread_t *thread, rb_mutex_t *mutex)
176 if (thread->keeping_mutexes) {
177 mutex->next_mutex = thread->keeping_mutexes;
180 thread->keeping_mutexes = mutex;
184thread_mutex_remove(rb_thread_t *thread, rb_mutex_t *mutex)
186 rb_mutex_t **keeping_mutexes = &thread->keeping_mutexes;
188 while (*keeping_mutexes && *keeping_mutexes != mutex) {
190 keeping_mutexes = &(*keeping_mutexes)->next_mutex;
193 if (*keeping_mutexes) {
194 *keeping_mutexes = mutex->next_mutex;
195 mutex->next_mutex = NULL;
200mutex_set_owner(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
203 mutex->ec_serial = ec_serial;
207mutex_locked(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
209 mutex_set_owner(mutex, th, ec_serial);
210 thread_mutex_insert(th, mutex);
214do_mutex_trylock(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
216 if (mutex->ec_serial == 0) {
217 RUBY_DEBUG_LOG(
"%p ok", mutex);
219 mutex_locked(mutex, th, ec_serial);
223 RUBY_DEBUG_LOG(
"%p ng", mutex);
229rb_mut_trylock(rb_execution_context_t *ec,
VALUE self)
231 return RBOOL(do_mutex_trylock(mutex_ptr(self), ec->thread_ptr, rb_ec_serial(ec)));
237 return rb_mut_trylock(GET_EC(), self);
241mutex_owned_p(rb_serial_t ec_serial, rb_mutex_t *mutex)
243 return RBOOL(mutex->ec_serial == ec_serial);
247call_rb_fiber_scheduler_block(
VALUE mutex)
253delete_from_waitq(
VALUE value)
261static inline rb_atomic_t threadptr_get_interrupts(rb_thread_t *th);
266 rb_execution_context_t *ec;
273 args->mutex = mutex_ptr(mutex);
278do_mutex_lock(
struct mutex_args *args,
int interruptible_p)
280 VALUE self = args->self;
281 rb_execution_context_t *ec = args->ec;
282 rb_thread_t *th = ec->thread_ptr;
283 rb_fiber_t *fiber = ec->fiber_ptr;
284 rb_serial_t ec_serial = rb_ec_serial(ec);
285 rb_mutex_t *mutex = args->mutex;
290 th->ec->interrupt_mask & TRAP_INTERRUPT_MASK) {
294 if (!do_mutex_trylock(mutex, th, ec_serial)) {
295 if (mutex->ec_serial == ec_serial) {
299 while (mutex->ec_serial != ec_serial) {
300 VM_ASSERT(mutex->ec_serial != 0);
303 if (scheduler !=
Qnil) {
307 .fiber = nonblocking_fiber(fiber)
310 ccan_list_add_tail(&mutex->waitq, &
sync_waiter.node);
314 if (!mutex->ec_serial) {
315 mutex_set_owner(mutex, th, ec_serial);
319 if (!th->vm->thread_ignore_deadlock && mutex->th == th) {
320 rb_raise(
rb_eThreadError,
"deadlock; lock already owned by another fiber belonging to the same thread");
326 .fiber = nonblocking_fiber(fiber),
329 RUBY_DEBUG_LOG(
"%p wait", mutex);
340 enum rb_thread_status prev_status = th->status;
341 th->status = THREAD_STOPPED_FOREVER;
342 rb_ractor_sleeper_threads_inc(th->ractor);
343 rb_check_deadlock(th->ractor);
346 th->locking_mutex = self;
348 ccan_list_add_tail(&mutex->waitq, &
sync_waiter.node);
350 native_sleep(th, NULL);
355 if (!mutex->ec_serial) {
356 mutex_set_owner(mutex, th, ec_serial);
359 rb_ractor_sleeper_threads_dec(th->ractor);
360 th->status = prev_status;
361 th->locking_mutex =
Qfalse;
363 RUBY_DEBUG_LOG(
"%p wakeup", mutex);
366 if (interruptible_p) {
369 if (mutex->ec_serial == ec_serial) {
371 mutex->ec_serial = 0;
373 RUBY_VM_CHECK_INTS_BLOCKING(th->ec);
374 if (!mutex->ec_serial) {
375 mutex_set_owner(mutex, th, ec_serial);
380 if (RUBY_VM_INTERRUPTED(th->ec)) {
382 if (saved_ints == 0) {
383 saved_ints = threadptr_get_interrupts(th);
387 threadptr_get_interrupts(th);
393 if (saved_ints) th->ec->interrupt_flag = saved_ints;
394 if (mutex->ec_serial == ec_serial) mutex_locked(mutex, th, ec_serial);
397 RUBY_DEBUG_LOG(
"%p locked", mutex);
400 if (mutex_owned_p(ec_serial, mutex) ==
Qfalse) rb_bug(
"do_mutex_lock: mutex is not owned.");
406mutex_lock_uninterruptible(
VALUE self)
409 mutex_args_init(&args, self);
410 return do_mutex_lock(&args, 0);
414rb_mut_lock(rb_execution_context_t *ec,
VALUE self)
418 .mutex = mutex_ptr(self),
421 return do_mutex_lock(&args, 1);
428 mutex_args_init(&args, self);
429 return do_mutex_lock(&args, 1);
433rb_mut_owned_p(rb_execution_context_t *ec,
VALUE self)
435 return mutex_owned_p(rb_ec_serial(ec), mutex_ptr(self));
439rb_mutex_owned_p(
VALUE self)
441 return rb_mut_owned_p(GET_EC(), self);
445rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
447 RUBY_DEBUG_LOG(
"%p", mutex);
449 if (mutex->ec_serial == 0) {
450 return "Attempt to unlock a mutex which is not locked";
452 else if (ec_serial && mutex->ec_serial != ec_serial) {
453 return "Attempt to unlock a mutex which is locked by another thread/fiber";
458 mutex->ec_serial = 0;
459 thread_mutex_remove(th, mutex);
461 ccan_list_for_each_safe(&mutex->waitq, cur, next, node) {
462 ccan_list_del_init(&cur->node);
464 if (cur->th->scheduler !=
Qnil && cur->fiber) {
469 switch (cur->th->status) {
470 case THREAD_RUNNABLE:
471 case THREAD_STOPPED_FOREVER:
472 RUBY_DEBUG_LOG(
"wakeup th:%u", rb_th_serial(cur->th));
473 rb_threadptr_interrupt(cur->th);
478 rb_bug(
"unexpected %s", cur->th->status == THREAD_STOPPED ?
"THREAD_STOPPED" :
"THREAD_KILLED");
491 rb_mutex_t *mutex = args->mutex;
492 rb_thread_t *th = rb_ec_thread_ptr(args->ec);
494 err = rb_mutex_unlock_th(mutex, th, rb_ec_serial(args->ec));
499do_mutex_unlock_safe(
VALUE args)
509 mutex_args_init(&args, self);
510 do_mutex_unlock(&args);
515rb_mut_unlock(rb_execution_context_t *ec,
VALUE self)
519 .mutex = mutex_ptr(self),
522 do_mutex_unlock(&args);
526#if defined(HAVE_WORKING_FORK)
528rb_mutex_abandon_keeping_mutexes(rb_thread_t *th)
530 rb_mutex_abandon_all(th->keeping_mutexes);
531 th->keeping_mutexes = NULL;
535rb_mutex_abandon_locking_mutex(rb_thread_t *th)
537 if (th->locking_mutex) {
538 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
540 ccan_list_head_init(&mutex->waitq);
541 th->locking_mutex =
Qfalse;
546rb_mutex_abandon_all(rb_mutex_t *mutexes)
552 mutexes = mutex->next_mutex;
553 mutex->ec_serial = 0;
554 mutex->next_mutex = 0;
555 ccan_list_head_init(&mutex->waitq);
566mutex_sleep_begin(
VALUE _arguments)
569 VALUE timeout = arguments->timeout;
573 if (scheduler !=
Qnil) {
577 if (
NIL_P(timeout)) {
578 rb_thread_sleep_deadly_allow_spurious_wakeup(arguments->self,
Qnil, 0);
582 rb_hrtime_t relative_timeout = rb_timeval2hrtime(&timeout_value);
584 woken = RBOOL(sleep_hrtime(GET_THREAD(), relative_timeout, 0));
592rb_mut_sleep(rb_execution_context_t *ec,
VALUE self,
VALUE timeout)
594 if (!
NIL_P(timeout)) {
599 rb_mut_unlock(ec, self);
600 time_t beg = time(0);
607 VALUE woken = rb_ec_ensure(ec, mutex_sleep_begin, (
VALUE)&arguments, mutex_lock_uninterruptible, self);
609 RUBY_VM_CHECK_INTS_BLOCKING(ec);
610 if (!woken)
return Qnil;
611 time_t end = time(0) - beg;
612 return TIMET2NUM(end);
618 return rb_mut_sleep(GET_EC(), self, timeout);
625 mutex_args_init(&args, self);
626 do_mutex_lock(&args, 1);
627 return rb_ec_ensure(args.ec, func, arg, do_mutex_unlock_safe, (
VALUE)&args);
631do_ec_yield(
VALUE _ec)
633 return rb_ec_yield((rb_execution_context_t *)_ec,
Qundef);
637rb_mut_synchronize(rb_execution_context_t *ec,
VALUE self)
641 .mutex = mutex_ptr(self),
644 do_mutex_lock(&args, 1);
645 return rb_ec_ensure(args.ec, do_ec_yield, (
VALUE)ec, do_mutex_unlock_safe, (
VALUE)&args);
649rb_mutex_allow_trap(
VALUE self,
int val)
662 struct ccan_list_head waitq;
663 rb_serial_t fork_gen;
671#define szqueue_waitq(sq) &sq->q.waitq
672#define szqueue_pushq(sq) &sq->pushq
676 int num_waiting_push;
677 struct ccan_list_head pushq;
682queue_mark_and_move(
void *ptr)
686 for (
rb_len_t index = 0; index < q->len; index++) {
687 rb_gc_mark_and_move(&q->buffer[((q->offset + index) % q->capa)]);
692queue_free_buffer(
struct rb_queue *q)
695 SIZED_FREE_N(q->buffer, q->capa);
703 queue_free_buffer(q);
708queue_memsize(
const void *ptr)
715 .wrap_struct_name =
"Thread::Queue",
717 .dmark = queue_mark_and_move,
719 .dsize = queue_memsize,
720 .dcompact = queue_mark_and_move,
722 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
726queue_alloc(
VALUE klass)
732 ccan_list_head_init(&q->waitq);
739 rb_serial_t fork_gen = GET_VM()->fork_gen;
741 if (RB_LIKELY(q->fork_gen == fork_gen)) {
745 q->fork_gen = fork_gen;
746 ccan_list_head_init(&q->waitq);
752raw_queue_ptr(
VALUE obj)
765 if (RB_UNLIKELY(q->buffer == NULL)) {
766 rb_raise(
rb_eTypeError,
"%+"PRIsVALUE
" not initialized", obj);
773 struct rb_queue *q = raw_queue_ptr(obj);
778#define QUEUE_CLOSED FL_USER5
781queue_timeout2hrtime(
VALUE timeout)
783 if (
NIL_P(timeout)) {
784 return (rb_hrtime_t)0;
788 rel = rb_sec2hrtime(NUM2TIMET(timeout));
793 return rb_hrtime_add(rel, rb_hrtime_now());
797szqueue_mark_and_move(
void *ptr)
801 queue_mark_and_move(&sq->q);
805szqueue_free(
void *ptr)
808 queue_free_buffer(&sq->q);
813szqueue_memsize(
const void *ptr)
820 .wrap_struct_name =
"Thread::SizedQueue",
822 .dmark = szqueue_mark_and_move,
823 .dfree = szqueue_free,
824 .dsize = szqueue_memsize,
825 .dcompact = szqueue_mark_and_move,
827 .parent = &queue_data_type,
828 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
832szqueue_alloc(
VALUE klass)
836 &szqueue_data_type, sq);
837 ccan_list_head_init(szqueue_waitq(sq));
838 ccan_list_head_init(szqueue_pushq(sq));
843raw_szqueue_ptr(
VALUE obj)
848 if (RB_UNLIKELY(queue_fork_check(&sq->q))) {
849 ccan_list_head_init(szqueue_pushq(sq));
850 sq->num_waiting_push = 0;
857szqueue_ptr(
VALUE obj)
860 check_queue(obj, &sq->q);
865queue_closed_p(
VALUE self)
877NORETURN(
static void raise_closed_queue_error(
VALUE self));
880raise_closed_queue_error(
VALUE self)
882 rb_raise(rb_eClosedQueueError,
"queue closed");
892#define QUEUE_INITIAL_CAPA 8
898 q->capa = initial_capa;
902ring_buffer_expand(
struct rb_queue *q)
906 MEMCPY(new_buffer, q->buffer + q->offset,
VALUE, q->capa - q->offset);
907 MEMCPY(new_buffer + (q->capa - q->offset), q->buffer,
VALUE, q->offset);
908 VALUE *old_buffer = q->buffer;
909 q->buffer = new_buffer;
911 ruby_xfree_sized(old_buffer, q->capa *
sizeof(
VALUE));
918 if (RB_UNLIKELY(q->len >= q->capa)) {
919 ring_buffer_expand(q);
922 rb_len_t index = (q->offset + q->len) % q->capa;
928ring_buffer_shift(
struct rb_queue *q)
934 VALUE obj = q->buffer[q->offset];
940 q->offset = (q->offset + 1) % q->capa;
946queue_initialize(rb_execution_context_t *ec,
VALUE self,
VALUE initial)
948 struct rb_queue *q = raw_queue_ptr(self);
949 ccan_list_head_init(&q->waitq);
950 if (
NIL_P(initial)) {
951 ring_buffer_init(q, QUEUE_INITIAL_CAPA);
954 initial = rb_to_array(initial);
956 rb_len_t initial_capa = QUEUE_INITIAL_CAPA;
957 while (initial_capa <
len) {
960 ring_buffer_init(q, initial_capa);
970 check_queue(self, q);
971 if (queue_closed_p(self)) {
972 raise_closed_queue_error(self);
974 ring_buffer_push(self, q, obj);
975 wakeup_one(&q->waitq);
980queue_sleep(
VALUE _args)
983 rb_thread_sleep_deadly_allow_spurious_wakeup(args->self, args->timeout, args->end);
996queue_sleep_done(
VALUE p)
1000 ccan_list_del(&qw->w.node);
1001 qw->as.q->num_waiting--;
1007szqueue_sleep_done(
VALUE p)
1011 ccan_list_del(&qw->w.node);
1012 qw->as.sq->num_waiting_push--;
1021 if (
RTEST(non_block)) {
1030 rb_hrtime_t end = queue_timeout2hrtime(timeout);
1031 while (q->len == 0) {
1032 if (queue_closed_p(self)) {
1033 return queue_closed_result(self, q);
1040 .w = {.self = self, .th = ec->thread_ptr, .fiber = nonblocking_fiber(ec->fiber_ptr)},
1044 struct ccan_list_head *waitq = &q->waitq;
1056 if (!
NIL_P(timeout) && (rb_hrtime_now() >= end))
1061 return ring_buffer_shift(q);
1065rb_queue_pop(rb_execution_context_t *ec,
VALUE self,
VALUE non_block,
VALUE timeout)
1067 return queue_do_pop(ec, self, queue_ptr(self), non_block, timeout);
1078szqueue_initialize(rb_execution_context_t *ec,
VALUE self,
VALUE vmax)
1081 struct rb_szqueue *sq = raw_szqueue_ptr(self);
1084 rb_raise(rb_eArgError,
"queue size must be positive");
1086 ring_buffer_init(&sq->q, QUEUE_INITIAL_CAPA);
1087 ccan_list_head_init(szqueue_waitq(sq));
1088 ccan_list_head_init(szqueue_pushq(sq));
1095rb_szqueue_push(rb_execution_context_t *ec,
VALUE self,
VALUE object,
VALUE non_block,
VALUE timeout)
1099 if (sq->q.len >= sq->max) {
1100 if (
RTEST(non_block)) {
1109 rb_hrtime_t end = queue_timeout2hrtime(timeout);
1110 while (sq->q.len >= sq->max) {
1111 if (queue_closed_p(self)) {
1112 raise_closed_queue_error(self);
1116 .w = {.self = self, .th = ec->thread_ptr, .fiber = nonblocking_fiber(ec->fiber_ptr)},
1120 struct ccan_list_head *pushq = szqueue_pushq(sq);
1123 sq->num_waiting_push++;
1131 if (!
NIL_P(timeout) && rb_hrtime_now() >= end) {
1137 return queue_do_push(self, &sq->q,
object);
1141rb_szqueue_pop(rb_execution_context_t *ec,
VALUE self,
VALUE non_block,
VALUE timeout)
1144 VALUE retval = queue_do_pop(ec, self, &sq->q, non_block, timeout);
1146 if (sq->q.len < sq->max) {
1147 wakeup_one(szqueue_pushq(sq));
1155 struct ccan_list_head waitq;
1156 rb_serial_t fork_gen;
1160condvar_memsize(
const void *ptr)
1168 0, 0, RUBY_TYPED_THREAD_SAFE_FREE|RUBY_TYPED_WB_PROTECTED
1172condvar_ptr(
VALUE self)
1175 rb_serial_t fork_gen = GET_VM()->fork_gen;
1180 if (cv->fork_gen != fork_gen) {
1181 cv->fork_gen = fork_gen;
1182 ccan_list_head_init(&cv->waitq);
1189condvar_alloc(
VALUE klass)
1195 ccan_list_head_init(&cv->waitq);
1201 rb_execution_context_t *ec;
1212 if (
CLASS_OF(p->mutex) == rb_cMutex) {
1213 return rb_mut_sleep(p->ec, p->mutex, p->timeout);
1216 return rb_funcallv(p->mutex, id_sleep, 1, &p->timeout);
1221rb_condvar_wait(rb_execution_context_t *ec,
VALUE self,
VALUE mutex,
VALUE timeout)
1232 .th = ec->thread_ptr,
1233 .fiber = nonblocking_fiber(ec->fiber_ptr)
1236 ccan_list_add_tail(&cv->waitq, &
sync_waiter.node);
1241rb_condvar_signal(rb_execution_context_t *ec,
VALUE self)
1244 wakeup_one(&cv->waitq);
1249rb_condvar_broadcast(rb_execution_context_t *ec,
VALUE self)
1252 wakeup_all(&cv->waitq);
1260 rb_serial_t ec_serial;
1265monitor_mark(
void *ptr)
1268 rb_gc_mark_movable(mc->mutex);
1272monitor_compact(
void *ptr)
1275 rb_gc_update_moved(&mc->mutex);
1279 .wrap_struct_name =
"monitor",
1281 .dmark = monitor_mark,
1284 .dcompact = monitor_compact,
1290monitor_alloc(
VALUE klass)
1304monitor_ptr(
VALUE monitor)
1312mc_owner_p(
struct rb_monitor *mc, rb_serial_t current_fiber_serial)
1314 return mc->ec_serial == current_fiber_serial;
1318rb_monitor_try_enter(rb_execution_context_t *ec,
VALUE monitor)
1320 struct rb_monitor *mc = monitor_ptr(monitor);
1322 rb_serial_t current_fiber_serial = rb_ec_serial(ec);
1323 if (!mc_owner_p(mc, current_fiber_serial)) {
1324 if (!rb_mut_trylock(ec, mc->mutex)) {
1327 mc->ec_serial = current_fiber_serial;
1337 rb_serial_t current_fiber_serial;
1338 rb_execution_context_t *ec;
1344 if (!mc_owner_p(args->mc, args->current_fiber_serial)) {
1346 .self = args->mc->mutex,
1347 .mutex = mutex_ptr(args->mc->mutex),
1350 do_mutex_lock(&mut_args, 1);
1351 args->mc->ec_serial = args->current_fiber_serial;
1352 args->mc->count = 0;
1360rb_monitor_enter(rb_execution_context_t *ec,
VALUE monitor)
1364 .mc = monitor_ptr(monitor),
1366 .current_fiber_serial = rb_ec_serial(ec),
1368 monitor_enter0(&args);
1375 if (!mc_owner_p(args->mc, args->current_fiber_serial)) {
1381rb_monitor_check_owner(rb_execution_context_t *ec,
VALUE monitor)
1385 .mc = monitor_ptr(monitor),
1387 .current_fiber_serial = rb_ec_serial(ec),
1389 monitor_check_owner0(&args);
1396 monitor_check_owner0(args);
1398 if (args->mc->count <= 0) rb_bug(
"monitor_exit: count:%d", (
int)args->mc->count);
1401 if (args->mc->count == 0) {
1402 args->mc->ec_serial = 0;
1405 .self = args->mc->mutex,
1406 .mutex = mutex_ptr(args->mc->mutex),
1409 do_mutex_unlock(&mut_args);
1414rb_monitor_exit(rb_execution_context_t *ec,
VALUE monitor)
1418 .mc = monitor_ptr(monitor),
1420 .current_fiber_serial = rb_ec_serial(ec),
1422 monitor_exit0(&args);
1427rb_monitor_locked_p(rb_execution_context_t *ec,
VALUE monitor)
1429 struct rb_monitor *mc = monitor_ptr(monitor);
1434rb_monitor_owned_p(rb_execution_context_t *ec,
VALUE monitor)
1436 struct rb_monitor *mc = monitor_ptr(monitor);
1441monitor_exit_for_cond(
VALUE monitor)
1443 struct rb_monitor *mc = monitor_ptr(monitor);
1444 long cnt = mc->count;
1458monitor_wait_for_cond_body(
VALUE v)
1461 struct rb_monitor *mc = monitor_ptr(data->monitor);
1463 VALUE signaled =
rb_funcall(data->cond, rb_intern(
"wait"), 2, mc->mutex, data->timeout);
1468monitor_enter_for_cond(
VALUE v)
1474 struct rb_monitor *mc = monitor_ptr(data->monitor);
1475 mc->ec_serial = rb_ec_serial(GET_EC());
1481rb_monitor_wait_for_cond(rb_execution_context_t *ec,
VALUE monitor,
VALUE cond,
VALUE timeout)
1483 VALUE count = monitor_exit_for_cond(monitor);
1492 monitor_enter_for_cond, (
VALUE)&data);
1496monitor_sync_ensure(
VALUE v_args)
1503rb_monitor_synchronize(rb_execution_context_t *ec,
VALUE monitor)
1507 .mc = monitor_ptr(monitor),
1509 .current_fiber_serial = rb_ec_serial(ec),
1511 monitor_enter0(&args);
1512 return rb_ec_ensure(ec, do_ec_yield, (
VALUE)ec, monitor_sync_ensure, (
VALUE)&args);
1516Init_thread_sync(
void)
1528 VALUE rb_cSizedQueue = rb_define_class_id_under_no_pin(
rb_cThread, rb_intern(
"SizedQueue"), rb_cQueue);
1535 id_sleep = rb_intern(
"sleep");
1545#include "thread_sync.rbinc"
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
VALUE rb_define_class_id_under(VALUE outer, ID id, VALUE super)
Identical to rb_define_class_under(), except it takes the name in ID instead of C's string.
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define CLASS_OF
Old name of rb_class_of.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define NIL_P
Old name of RB_NIL_P.
#define Check_TypedStruct(v, t)
Old name of rb_check_typeddata.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define NUM2LEN
Old name of RB_NUM2LEN.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eStopIteration
StopIteration exception.
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
VALUE rb_eThreadError
ThreadError exception.
VALUE rb_cObject
Object class.
VALUE rb_cThread
Thread class.
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
VALUE rb_fiber_scheduler_block(VALUE scheduler, VALUE blocker, VALUE timeout)
Non-blocking wait for the passed "blocker", which is for instance Thread.join or Mutex....
VALUE rb_fiber_scheduler_kernel_sleep(VALUE scheduler, VALUE duration)
Non-blocking sleep.
VALUE rb_fiber_scheduler_unblock(VALUE scheduler, VALUE blocker, VALUE fiber)
Wakes up a fiber previously blocked using rb_fiber_scheduler_block().
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
VALUE rb_mutex_new(void)
Creates a mutex.
VALUE rb_mutex_trylock(VALUE mutex)
Attempts to lock the mutex, without waiting for other threads to unlock it.
VALUE rb_mutex_locked_p(VALUE mutex)
Queries if there are any threads that holds the lock.
VALUE rb_mutex_synchronize(VALUE mutex, VALUE(*func)(VALUE arg), VALUE arg)
Obtains the lock, runs the passed function, and releases the lock when it completes.
VALUE rb_mutex_sleep(VALUE self, VALUE timeout)
Releases the lock held in the mutex and waits for the period of time; reacquires the lock on wakeup.
VALUE rb_mutex_unlock(VALUE mutex)
Releases the mutex.
VALUE rb_mutex_lock(VALUE mutex)
Attempts to lock the mutex.
struct timeval rb_time_interval(VALUE num)
Creates a "time interval".
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
int capa
Designed capacity of the buffer.
int len
Length of the buffer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define RTEST
This is an old name of RB_TEST.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
#define RB_LEN_MAX
Maximum possible value that a rb_len_t can take.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.