Ruby 4.1.0dev (2026-03-01 revision d68e4be1873e364c5ee24ed112bce4bc86e3a406)
thread_sync.c (d68e4be1873e364c5ee24ed112bce4bc86e3a406)
1/* included by thread.c */
2#include "ccan/list/list.h"
3#include "builtin.h"
4
5static VALUE rb_cMutex, rb_eClosedQueueError;
6
7/* Mutex */
8typedef struct rb_mutex_struct {
9 rb_serial_t ec_serial;
10 rb_thread_t *th; // even if the fiber is collected, we might need access to the thread in mutex_free
11 struct rb_mutex_struct *next_mutex;
12 struct ccan_list_head waitq; /* protected by GVL */
14
15/* sync_waiter is always on-stack */
17 VALUE self;
18 rb_thread_t *th;
19 rb_fiber_t *fiber;
20 struct ccan_list_node node;
21};
22
23static inline rb_fiber_t*
24nonblocking_fiber(rb_fiber_t *fiber)
25{
26 if (rb_fiberptr_blocking(fiber)) {
27 return NULL;
28 }
29
30 return fiber;
31}
32
34 VALUE self;
35 VALUE timeout;
36 rb_hrtime_t end;
37};
38
39#define MUTEX_ALLOW_TRAP FL_USER1
40
41static void
42sync_wakeup(struct ccan_list_head *head, long max)
43{
44 RUBY_DEBUG_LOG("max:%ld", max);
45
46 struct sync_waiter *cur = 0, *next;
47
48 ccan_list_for_each_safe(head, cur, next, node) {
49 ccan_list_del_init(&cur->node);
50
51 if (cur->th->status != THREAD_KILLED) {
52 if (cur->th->scheduler != Qnil && cur->fiber) {
53 rb_fiber_scheduler_unblock(cur->th->scheduler, cur->self, rb_fiberptr_self(cur->fiber));
54 }
55 else {
56 RUBY_DEBUG_LOG("target_th:%u", rb_th_serial(cur->th));
57 rb_threadptr_interrupt(cur->th);
58 cur->th->status = THREAD_RUNNABLE;
59 }
60
61 if (--max == 0) return;
62 }
63 }
64}
65
66static void
67wakeup_one(struct ccan_list_head *head)
68{
69 sync_wakeup(head, 1);
70}
71
72static void
73wakeup_all(struct ccan_list_head *head)
74{
75 sync_wakeup(head, LONG_MAX);
76}
77
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);
82#endif
83static const char* rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial);
84
85static size_t
86rb_mutex_num_waiting(rb_mutex_t *mutex)
87{
88 struct sync_waiter *w = 0;
89 size_t n = 0;
90
91 ccan_list_for_each(&mutex->waitq, w, node) {
92 n++;
93 }
94
95 return n;
96}
97
98rb_thread_t* rb_fiber_threadptr(const rb_fiber_t *fiber);
99
100static bool
101mutex_locked_p(rb_mutex_t *mutex)
102{
103 return mutex->ec_serial != 0;
104}
105
106static void
107mutex_free(void *ptr)
108{
109 rb_mutex_t *mutex = ptr;
110 if (mutex_locked_p(mutex)) {
111 const char *err = rb_mutex_unlock_th(mutex, mutex->th, 0);
112 if (err) rb_bug("%s", err);
113 }
114 ruby_xfree(ptr);
115}
116
117static size_t
118mutex_memsize(const void *ptr)
119{
120 return sizeof(rb_mutex_t);
121}
122
123static const rb_data_type_t mutex_data_type = {
124 "mutex",
125 {NULL, mutex_free, mutex_memsize,},
127};
128
129static rb_mutex_t *
130mutex_ptr(VALUE obj)
131{
132 rb_mutex_t *mutex;
133
134 TypedData_Get_Struct(obj, rb_mutex_t, &mutex_data_type, mutex);
135
136 return mutex;
137}
138
139VALUE
140rb_obj_is_mutex(VALUE obj)
141{
142 return RBOOL(rb_typeddata_is_kind_of(obj, &mutex_data_type));
143}
144
145static VALUE
146mutex_alloc(VALUE klass)
147{
148 VALUE obj;
149 rb_mutex_t *mutex;
150
151 obj = TypedData_Make_Struct(klass, rb_mutex_t, &mutex_data_type, mutex);
152
153 ccan_list_head_init(&mutex->waitq);
154 return obj;
155}
156
157VALUE
159{
160 return mutex_alloc(rb_cMutex);
161}
162
163VALUE
165{
166 rb_mutex_t *mutex = mutex_ptr(self);
167
168 return RBOOL(mutex_locked_p(mutex));
169}
170
171static void
172thread_mutex_insert(rb_thread_t *thread, rb_mutex_t *mutex)
173{
174 RUBY_ASSERT(!mutex->next_mutex);
175 if (thread->keeping_mutexes) {
176 mutex->next_mutex = thread->keeping_mutexes;
177 }
178
179 thread->keeping_mutexes = mutex;
180}
181
182static void
183thread_mutex_remove(rb_thread_t *thread, rb_mutex_t *mutex)
184{
185 rb_mutex_t **keeping_mutexes = &thread->keeping_mutexes;
186
187 while (*keeping_mutexes && *keeping_mutexes != mutex) {
188 // Move to the next mutex in the list:
189 keeping_mutexes = &(*keeping_mutexes)->next_mutex;
190 }
191
192 if (*keeping_mutexes) {
193 *keeping_mutexes = mutex->next_mutex;
194 mutex->next_mutex = NULL;
195 }
196}
197
198static void
199mutex_set_owner(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
200{
201 mutex->th = th;
202 mutex->ec_serial = ec_serial;
203}
204
205static void
206mutex_locked(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
207{
208 mutex_set_owner(mutex, th, ec_serial);
209 thread_mutex_insert(th, mutex);
210}
211
212static inline bool
213do_mutex_trylock(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
214{
215 if (mutex->ec_serial == 0) {
216 RUBY_DEBUG_LOG("%p ok", mutex);
217
218 mutex_locked(mutex, th, ec_serial);
219 return true;
220 }
221 else {
222 RUBY_DEBUG_LOG("%p ng", mutex);
223 return false;
224 }
225}
226
227static VALUE
228rb_mut_trylock(rb_execution_context_t *ec, VALUE self)
229{
230 return RBOOL(do_mutex_trylock(mutex_ptr(self), ec->thread_ptr, rb_ec_serial(ec)));
231}
232
233VALUE
235{
236 return rb_mut_trylock(GET_EC(), self);
237}
238
239static VALUE
240mutex_owned_p(rb_serial_t ec_serial, rb_mutex_t *mutex)
241{
242 return RBOOL(mutex->ec_serial == ec_serial);
243}
244
245static VALUE
246call_rb_fiber_scheduler_block(VALUE mutex)
247{
249}
250
251static VALUE
252delete_from_waitq(VALUE value)
253{
254 struct sync_waiter *sync_waiter = (void *)value;
255 ccan_list_del(&sync_waiter->node);
256
257 return Qnil;
258}
259
260static inline rb_atomic_t threadptr_get_interrupts(rb_thread_t *th);
261
263 VALUE self;
264 rb_mutex_t *mutex;
266};
267
268static inline void
269mutex_args_init(struct mutex_args *args, VALUE mutex)
270{
271 args->self = mutex;
272 args->mutex = mutex_ptr(mutex);
273 args->ec = GET_EC();
274}
275
276static VALUE
277do_mutex_lock(struct mutex_args *args, int interruptible_p)
278{
279 VALUE self = args->self;
280 rb_execution_context_t *ec = args->ec;
281 rb_thread_t *th = ec->thread_ptr;
282 rb_fiber_t *fiber = ec->fiber_ptr;
283 rb_serial_t ec_serial = rb_ec_serial(ec);
284 rb_mutex_t *mutex = args->mutex;
285 rb_atomic_t saved_ints = 0;
286
287 /* When running trap handler */
288 if (!FL_TEST_RAW(self, MUTEX_ALLOW_TRAP) &&
289 th->ec->interrupt_mask & TRAP_INTERRUPT_MASK) {
290 rb_raise(rb_eThreadError, "can't be called from trap context");
291 }
292
293 if (!do_mutex_trylock(mutex, th, ec_serial)) {
294 if (mutex->ec_serial == ec_serial) {
295 rb_raise(rb_eThreadError, "deadlock; recursive locking");
296 }
297
298 while (mutex->ec_serial != ec_serial) {
299 VM_ASSERT(mutex->ec_serial != 0);
300
301 VALUE scheduler = rb_fiber_scheduler_current();
302 if (scheduler != Qnil) {
303 struct sync_waiter sync_waiter = {
304 .self = self,
305 .th = th,
306 .fiber = nonblocking_fiber(fiber)
307 };
308
309 ccan_list_add_tail(&mutex->waitq, &sync_waiter.node);
310
311 rb_ensure(call_rb_fiber_scheduler_block, self, delete_from_waitq, (VALUE)&sync_waiter);
312
313 if (!mutex->ec_serial) {
314 mutex_set_owner(mutex, th, ec_serial);
315 }
316 }
317 else {
318 if (!th->vm->thread_ignore_deadlock && mutex->th == th) {
319 rb_raise(rb_eThreadError, "deadlock; lock already owned by another fiber belonging to the same thread");
320 }
321
322 struct sync_waiter sync_waiter = {
323 .self = self,
324 .th = th,
325 .fiber = nonblocking_fiber(fiber),
326 };
327
328 RUBY_DEBUG_LOG("%p wait", mutex);
329
330 // similar code with `sleep_forever`, but
331 // sleep_forever(SLEEP_DEADLOCKABLE) raises an exception.
332 // Ensure clause is needed like but `rb_ensure` a bit slow.
333 //
334 // begin
335 // sleep_forever(th, SLEEP_DEADLOCKABLE);
336 // ensure
337 // ccan_list_del(&sync_waiter.node);
338 // end
339 enum rb_thread_status prev_status = th->status;
340 th->status = THREAD_STOPPED_FOREVER;
341 rb_ractor_sleeper_threads_inc(th->ractor);
342 rb_check_deadlock(th->ractor);
343
344 RUBY_ASSERT(!th->locking_mutex);
345 th->locking_mutex = self;
346
347 ccan_list_add_tail(&mutex->waitq, &sync_waiter.node);
348 {
349 native_sleep(th, NULL);
350 }
351 ccan_list_del(&sync_waiter.node);
352
353 // unlocked by another thread while sleeping
354 if (!mutex->ec_serial) {
355 mutex_set_owner(mutex, th, ec_serial);
356 }
357
358 rb_ractor_sleeper_threads_dec(th->ractor);
359 th->status = prev_status;
360 th->locking_mutex = Qfalse;
361
362 RUBY_DEBUG_LOG("%p wakeup", mutex);
363 }
364
365 if (interruptible_p) {
366 /* release mutex before checking for interrupts...as interrupt checking
367 * code might call rb_raise() */
368 if (mutex->ec_serial == ec_serial) {
369 mutex->th = NULL;
370 mutex->ec_serial = 0;
371 }
372 RUBY_VM_CHECK_INTS_BLOCKING(th->ec); /* may release mutex */
373 if (!mutex->ec_serial) {
374 mutex_set_owner(mutex, th, ec_serial);
375 }
376 }
377 else {
378 // clear interrupt information
379 if (RUBY_VM_INTERRUPTED(th->ec)) {
380 // reset interrupts
381 if (saved_ints == 0) {
382 saved_ints = threadptr_get_interrupts(th);
383 }
384 else {
385 // ignore additional interrupts
386 threadptr_get_interrupts(th);
387 }
388 }
389 }
390 }
391
392 if (saved_ints) th->ec->interrupt_flag = saved_ints;
393 if (mutex->ec_serial == ec_serial) mutex_locked(mutex, th, ec_serial);
394 }
395
396 RUBY_DEBUG_LOG("%p locked", mutex);
397
398 // assertion
399 if (mutex_owned_p(ec_serial, mutex) == Qfalse) rb_bug("do_mutex_lock: mutex is not owned.");
400
401 return self;
402}
403
404static VALUE
405mutex_lock_uninterruptible(VALUE self)
406{
407 struct mutex_args args;
408 mutex_args_init(&args, self);
409 return do_mutex_lock(&args, 0);
410}
411
412static VALUE
413rb_mut_lock(rb_execution_context_t *ec, VALUE self)
414{
415 struct mutex_args args = {
416 .self = self,
417 .mutex = mutex_ptr(self),
418 .ec = ec,
419 };
420 return do_mutex_lock(&args, 1);
421}
422
423VALUE
425{
426 struct mutex_args args;
427 mutex_args_init(&args, self);
428 return do_mutex_lock(&args, 1);
429}
430
431static VALUE
432rb_mut_owned_p(rb_execution_context_t *ec, VALUE self)
433{
434 return mutex_owned_p(rb_ec_serial(ec), mutex_ptr(self));
435}
436
437VALUE
438rb_mutex_owned_p(VALUE self)
439{
440 return rb_mut_owned_p(GET_EC(), self);
441}
442
443static const char *
444rb_mutex_unlock_th(rb_mutex_t *mutex, rb_thread_t *th, rb_serial_t ec_serial)
445{
446 RUBY_DEBUG_LOG("%p", mutex);
447
448 if (mutex->ec_serial == 0) {
449 return "Attempt to unlock a mutex which is not locked";
450 }
451 else if (ec_serial && mutex->ec_serial != ec_serial) {
452 return "Attempt to unlock a mutex which is locked by another thread/fiber";
453 }
454
455 struct sync_waiter *cur = 0, *next;
456
457 mutex->ec_serial = 0;
458 thread_mutex_remove(th, mutex);
459
460 ccan_list_for_each_safe(&mutex->waitq, cur, next, node) {
461 ccan_list_del_init(&cur->node);
462
463 if (cur->th->scheduler != Qnil && cur->fiber) {
464 rb_fiber_scheduler_unblock(cur->th->scheduler, cur->self, rb_fiberptr_self(cur->fiber));
465 return NULL;
466 }
467 else {
468 switch (cur->th->status) {
469 case THREAD_RUNNABLE: /* from someone else calling Thread#run */
470 case THREAD_STOPPED_FOREVER: /* likely (rb_mutex_lock) */
471 RUBY_DEBUG_LOG("wakeup th:%u", rb_th_serial(cur->th));
472 rb_threadptr_interrupt(cur->th);
473 return NULL;
474 case THREAD_STOPPED: /* probably impossible */
475 rb_bug("unexpected THREAD_STOPPED");
476 case THREAD_KILLED:
477 /* not sure about this, possible in exit GC? */
478 rb_bug("unexpected THREAD_KILLED");
479 continue;
480 }
481 }
482 }
483
484 // We did not find any threads to wake up, so we can just return with no error:
485 return NULL;
486}
487
488static void
489do_mutex_unlock(struct mutex_args *args)
490{
491 const char *err;
492 rb_mutex_t *mutex = args->mutex;
493 rb_thread_t *th = rb_ec_thread_ptr(args->ec);
494
495 err = rb_mutex_unlock_th(mutex, th, rb_ec_serial(args->ec));
496 if (err) rb_raise(rb_eThreadError, "%s", err);
497}
498
499static VALUE
500do_mutex_unlock_safe(VALUE args)
501{
502 do_mutex_unlock((struct mutex_args *)args);
503 return Qnil;
504}
505
506/*
507 * call-seq:
508 * mutex.unlock -> self
509 *
510 * Releases the lock.
511 * Raises +ThreadError+ if +mutex+ wasn't locked by the current thread.
512 */
513VALUE
515{
516 struct mutex_args args;
517 mutex_args_init(&args, self);
518 do_mutex_unlock(&args);
519 return self;
520}
521
522static VALUE
523rb_mut_unlock(rb_execution_context_t *ec, VALUE self)
524{
525 struct mutex_args args = {
526 .self = self,
527 .mutex = mutex_ptr(self),
528 .ec = ec,
529 };
530 do_mutex_unlock(&args);
531 return self;
532}
533
534#if defined(HAVE_WORKING_FORK)
535static void
536rb_mutex_abandon_keeping_mutexes(rb_thread_t *th)
537{
538 rb_mutex_abandon_all(th->keeping_mutexes);
539 th->keeping_mutexes = NULL;
540}
541
542static void
543rb_mutex_abandon_locking_mutex(rb_thread_t *th)
544{
545 if (th->locking_mutex) {
546 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
547
548 ccan_list_head_init(&mutex->waitq);
549 th->locking_mutex = Qfalse;
550 }
551}
552
553static void
554rb_mutex_abandon_all(rb_mutex_t *mutexes)
555{
556 rb_mutex_t *mutex;
557
558 while (mutexes) {
559 mutex = mutexes;
560 mutexes = mutex->next_mutex;
561 mutex->ec_serial = 0;
562 mutex->next_mutex = 0;
563 ccan_list_head_init(&mutex->waitq);
564 }
565}
566#endif
567
569 VALUE self;
570 VALUE timeout;
571};
572
573static VALUE
574mutex_sleep_begin(VALUE _arguments)
575{
576 struct rb_mutex_sleep_arguments *arguments = (struct rb_mutex_sleep_arguments *)_arguments;
577 VALUE timeout = arguments->timeout;
578 VALUE woken = Qtrue;
579
580 VALUE scheduler = rb_fiber_scheduler_current();
581 if (scheduler != Qnil) {
582 rb_fiber_scheduler_kernel_sleep(scheduler, timeout);
583 }
584 else {
585 if (NIL_P(timeout)) {
586 rb_thread_sleep_deadly_allow_spurious_wakeup(arguments->self, Qnil, 0);
587 }
588 else {
589 struct timeval timeout_value = rb_time_interval(timeout);
590 rb_hrtime_t relative_timeout = rb_timeval2hrtime(&timeout_value);
591 /* permit spurious check */
592 woken = RBOOL(sleep_hrtime(GET_THREAD(), relative_timeout, 0));
593 }
594 }
595
596 return woken;
597}
598
599static VALUE
600rb_mut_sleep(rb_execution_context_t *ec, VALUE self, VALUE timeout)
601{
602 if (!NIL_P(timeout)) {
603 // Validate the argument:
604 rb_time_interval(timeout);
605 }
606
607 rb_mut_unlock(ec, self);
608 time_t beg = time(0);
609
610 struct rb_mutex_sleep_arguments arguments = {
611 .self = self,
612 .timeout = timeout,
613 };
614
615 VALUE woken = rb_ec_ensure(ec, mutex_sleep_begin, (VALUE)&arguments, mutex_lock_uninterruptible, self);
616
617 RUBY_VM_CHECK_INTS_BLOCKING(ec);
618 if (!woken) return Qnil;
619 time_t end = time(0) - beg;
620 return TIMET2NUM(end);
621}
622
623VALUE
625{
626 return rb_mut_sleep(GET_EC(), self, timeout);
627}
628
629VALUE
631{
632 struct mutex_args args;
633 mutex_args_init(&args, self);
634 do_mutex_lock(&args, 1);
635 return rb_ec_ensure(args.ec, func, arg, do_mutex_unlock_safe, (VALUE)&args);
636}
637
638static VALUE
639do_ec_yield(VALUE _ec)
640{
641 return rb_ec_yield((rb_execution_context_t *)_ec, Qundef);
642}
643
644VALUE
645rb_mut_synchronize(rb_execution_context_t *ec, VALUE self)
646{
647 struct mutex_args args = {
648 .self = self,
649 .mutex = mutex_ptr(self),
650 .ec = ec,
651 };
652 do_mutex_lock(&args, 1);
653 return rb_ec_ensure(args.ec, do_ec_yield, (VALUE)ec, do_mutex_unlock_safe, (VALUE)&args);
654}
655
656void
657rb_mutex_allow_trap(VALUE self, int val)
658{
659 Check_TypedStruct(self, &mutex_data_type);
660
661 if (val)
662 FL_SET_RAW(self, MUTEX_ALLOW_TRAP);
663 else
664 FL_UNSET_RAW(self, MUTEX_ALLOW_TRAP);
665}
666
667/* Queue */
668
669struct rb_queue {
670 struct ccan_list_head waitq;
671 rb_serial_t fork_gen;
672 long capa;
673 long len;
674 long offset;
675 VALUE *buffer;
676 int num_waiting;
677};
678
679#define szqueue_waitq(sq) &sq->q.waitq
680#define szqueue_pushq(sq) &sq->pushq
681
683 struct rb_queue q;
684 int num_waiting_push;
685 struct ccan_list_head pushq;
686 long max;
687};
688
689static void
690queue_mark_and_move(void *ptr)
691{
692 struct rb_queue *q = ptr;
693 /* no need to mark threads in waitq, they are on stack */
694 for (long index = 0; index < q->len; index++) {
695 rb_gc_mark_and_move(&q->buffer[((q->offset + index) % q->capa)]);
696 }
697}
698
699static inline void
700queue_free_buffer(struct rb_queue *q)
701{
702 if (q->buffer) {
703 SIZED_FREE_N(q->buffer, q->capa);
704 }
705}
706
707static void
708queue_free(void *ptr)
709{
710 struct rb_queue *q = ptr;
711 queue_free_buffer(q);
712 SIZED_FREE(q);
713}
714
715static size_t
716queue_memsize(const void *ptr)
717{
718 const struct rb_queue *q = ptr;
719 return sizeof(struct rb_queue) + (q->capa * sizeof(VALUE));
720}
721
722static const rb_data_type_t queue_data_type = {
723 .wrap_struct_name = "Thread::Queue",
724 .function = {
725 .dmark = queue_mark_and_move,
726 .dfree = queue_free,
727 .dsize = queue_memsize,
728 .dcompact = queue_mark_and_move,
729 },
730 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
731};
732
733static VALUE
734queue_alloc(VALUE klass)
735{
736 VALUE obj;
737 struct rb_queue *q;
738
739 obj = TypedData_Make_Struct(klass, struct rb_queue, &queue_data_type, q);
740 ccan_list_head_init(&q->waitq);
741 return obj;
742}
743
744static inline bool
745queue_fork_check(struct rb_queue *q)
746{
747 rb_serial_t fork_gen = GET_VM()->fork_gen;
748
749 if (RB_LIKELY(q->fork_gen == fork_gen)) {
750 return false;
751 }
752 /* forked children can't reach into parent thread stacks */
753 q->fork_gen = fork_gen;
754 ccan_list_head_init(&q->waitq);
755 q->num_waiting = 0;
756 return true;
757}
758
759static inline struct rb_queue *
760raw_queue_ptr(VALUE obj)
761{
762 struct rb_queue *q;
763
764 TypedData_Get_Struct(obj, struct rb_queue, &queue_data_type, q);
765 queue_fork_check(q);
766
767 return q;
768}
769
770static inline void
771check_queue(VALUE obj, struct rb_queue *q)
772{
773 if (RB_UNLIKELY(q->buffer == NULL)) {
774 rb_raise(rb_eTypeError, "%+"PRIsVALUE" not initialized", obj);
775 }
776}
777
778static inline struct rb_queue *
779queue_ptr(VALUE obj)
780{
781 struct rb_queue *q = raw_queue_ptr(obj);
782 check_queue(obj, q);
783 return q;
784}
785
786#define QUEUE_CLOSED FL_USER5
787
788static rb_hrtime_t
789queue_timeout2hrtime(VALUE timeout)
790{
791 if (NIL_P(timeout)) {
792 return (rb_hrtime_t)0;
793 }
794 rb_hrtime_t rel = 0;
795 if (FIXNUM_P(timeout)) {
796 rel = rb_sec2hrtime(NUM2TIMET(timeout));
797 }
798 else {
799 double2hrtime(&rel, rb_num2dbl(timeout));
800 }
801 return rb_hrtime_add(rel, rb_hrtime_now());
802}
803
804static void
805szqueue_mark_and_move(void *ptr)
806{
807 struct rb_szqueue *sq = ptr;
808
809 queue_mark_and_move(&sq->q);
810}
811
812static void
813szqueue_free(void *ptr)
814{
815 struct rb_szqueue *sq = ptr;
816 queue_free_buffer(&sq->q);
817 SIZED_FREE(sq);
818}
819
820static size_t
821szqueue_memsize(const void *ptr)
822{
823 const struct rb_szqueue *sq = ptr;
824 return sizeof(struct rb_szqueue) + (sq->q.capa * sizeof(VALUE));
825}
826
827static const rb_data_type_t szqueue_data_type = {
828 .wrap_struct_name = "Thread::SizedQueue",
829 .function = {
830 .dmark = szqueue_mark_and_move,
831 .dfree = szqueue_free,
832 .dsize = szqueue_memsize,
833 .dcompact = szqueue_mark_and_move,
834 },
835 .parent = &queue_data_type,
836 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
837};
838
839static VALUE
840szqueue_alloc(VALUE klass)
841{
842 struct rb_szqueue *sq;
843 VALUE obj = TypedData_Make_Struct(klass, struct rb_szqueue,
844 &szqueue_data_type, sq);
845 ccan_list_head_init(szqueue_waitq(sq));
846 ccan_list_head_init(szqueue_pushq(sq));
847 return obj;
848}
849
850static inline struct rb_szqueue *
851raw_szqueue_ptr(VALUE obj)
852{
853 struct rb_szqueue *sq;
854
855 TypedData_Get_Struct(obj, struct rb_szqueue, &szqueue_data_type, sq);
856 if (RB_UNLIKELY(queue_fork_check(&sq->q))) {
857 ccan_list_head_init(szqueue_pushq(sq));
858 sq->num_waiting_push = 0;
859 }
860
861 return sq;
862}
863
864static inline struct rb_szqueue *
865szqueue_ptr(VALUE obj)
866{
867 struct rb_szqueue *sq = raw_szqueue_ptr(obj);
868 check_queue(obj, &sq->q);
869 return sq;
870}
871
872static inline bool
873queue_closed_p(VALUE self)
874{
875 return FL_TEST_RAW(self, QUEUE_CLOSED) != 0;
876}
877
878/*
879 * Document-class: ClosedQueueError
880 *
881 * The exception class which will be raised when pushing into a closed
882 * Queue. See Thread::Queue#close and Thread::SizedQueue#close.
883 */
884
885NORETURN(static void raise_closed_queue_error(VALUE self));
886
887static void
888raise_closed_queue_error(VALUE self)
889{
890 rb_raise(rb_eClosedQueueError, "queue closed");
891}
892
893static VALUE
894queue_closed_result(VALUE self, struct rb_queue *q)
895{
896 RUBY_ASSERT(q->len == 0);
897 return Qnil;
898}
899
900#define QUEUE_INITIAL_CAPA 8
901
902static inline void
903ring_buffer_init(struct rb_queue *q, long initial_capa)
904{
905 q->buffer = ALLOC_N(VALUE, initial_capa);
906 q->capa = initial_capa;
907}
908
909static inline void
910ring_buffer_expand(struct rb_queue *q)
911{
912 RUBY_ASSERT(q->capa > 0);
913 VALUE *new_buffer = ALLOC_N(VALUE, q->capa * 2);
914 MEMCPY(new_buffer, q->buffer + q->offset, VALUE, q->capa - q->offset);
915 MEMCPY(new_buffer + (q->capa - q->offset), q->buffer, VALUE, q->offset);
916 VALUE *old_buffer = q->buffer;
917 q->buffer = new_buffer;
918 q->offset = 0;
919 ruby_sized_xfree(old_buffer, q->capa * sizeof(VALUE));
920 q->capa *= 2;
921}
922
923static void
924ring_buffer_push(VALUE self, struct rb_queue *q, VALUE obj)
925{
926 if (RB_UNLIKELY(q->len >= q->capa)) {
927 ring_buffer_expand(q);
928 }
929 RUBY_ASSERT(q->capa > q->len);
930 long index = (q->offset + q->len) % q->capa;
931 q->len++;
932 RB_OBJ_WRITE(self, &q->buffer[index], obj);
933}
934
935static VALUE
936ring_buffer_shift(struct rb_queue *q)
937{
938 if (!q->len) {
939 return Qnil;
940 }
941
942 VALUE obj = q->buffer[q->offset];
943 q->len--;
944 if (q->len == 0) {
945 q->offset = 0;
946 }
947 else {
948 q->offset = (q->offset + 1) % q->capa;
949 }
950 return obj;
951}
952
953static VALUE
954queue_initialize(rb_execution_context_t *ec, VALUE self, VALUE initial)
955{
956 struct rb_queue *q = raw_queue_ptr(self);
957 ccan_list_head_init(&q->waitq);
958 if (NIL_P(initial)) {
959 ring_buffer_init(q, QUEUE_INITIAL_CAPA);
960 }
961 else {
962 initial = rb_to_array(initial);
963 long len = RARRAY_LEN(initial);
964 long initial_capa = QUEUE_INITIAL_CAPA;
965 while (initial_capa < len) {
966 initial_capa *= 2;
967 }
968 ring_buffer_init(q, initial_capa);
969 MEMCPY(q->buffer, RARRAY_CONST_PTR(initial), VALUE, len);
970 q->len = len;
971 }
972 return self;
973}
974
975static VALUE
976queue_do_push(VALUE self, struct rb_queue *q, VALUE obj)
977{
978 check_queue(self, q);
979 if (queue_closed_p(self)) {
980 raise_closed_queue_error(self);
981 }
982 ring_buffer_push(self, q, obj);
983 wakeup_one(&q->waitq);
984 return self;
985}
986
987static VALUE
988queue_sleep(VALUE _args)
989{
990 struct queue_sleep_arg *args = (struct queue_sleep_arg *)_args;
991 rb_thread_sleep_deadly_allow_spurious_wakeup(args->self, args->timeout, args->end);
992 return Qnil;
993}
994
996 struct sync_waiter w;
997 union {
998 struct rb_queue *q;
999 struct rb_szqueue *sq;
1000 } as;
1001};
1002
1003static VALUE
1004queue_sleep_done(VALUE p)
1005{
1006 struct queue_waiter *qw = (struct queue_waiter *)p;
1007
1008 ccan_list_del(&qw->w.node);
1009 qw->as.q->num_waiting--;
1010
1011 return Qfalse;
1012}
1013
1014static VALUE
1015szqueue_sleep_done(VALUE p)
1016{
1017 struct queue_waiter *qw = (struct queue_waiter *)p;
1018
1019 ccan_list_del(&qw->w.node);
1020 qw->as.sq->num_waiting_push--;
1021
1022 return Qfalse;
1023}
1024
1025static inline VALUE
1026queue_do_pop(rb_execution_context_t *ec, VALUE self, struct rb_queue *q, VALUE non_block, VALUE timeout)
1027{
1028 if (q->len == 0) {
1029 if (RTEST(non_block)) {
1030 rb_raise(rb_eThreadError, "queue empty");
1031 }
1032
1033 if (RTEST(rb_equal(INT2FIX(0), timeout))) {
1034 return Qnil;
1035 }
1036 }
1037
1038 rb_hrtime_t end = queue_timeout2hrtime(timeout);
1039 while (q->len == 0) {
1040 if (queue_closed_p(self)) {
1041 return queue_closed_result(self, q);
1042 }
1043 else {
1044 RUBY_ASSERT(q->len == 0);
1045 RUBY_ASSERT(queue_closed_p(self) == 0);
1046
1047 struct queue_waiter queue_waiter = {
1048 .w = {.self = self, .th = ec->thread_ptr, .fiber = nonblocking_fiber(ec->fiber_ptr)},
1049 .as = {.q = q}
1050 };
1051
1052 struct ccan_list_head *waitq = &q->waitq;
1053
1054 ccan_list_add_tail(waitq, &queue_waiter.w.node);
1055 queue_waiter.as.q->num_waiting++;
1056
1058 .self = self,
1059 .timeout = timeout,
1060 .end = end
1061 };
1062
1063 rb_ensure(queue_sleep, (VALUE)&queue_sleep_arg, queue_sleep_done, (VALUE)&queue_waiter);
1064 if (!NIL_P(timeout) && (rb_hrtime_now() >= end))
1065 break;
1066 }
1067 }
1068
1069 return ring_buffer_shift(q);
1070}
1071
1072static VALUE
1073rb_queue_pop(rb_execution_context_t *ec, VALUE self, VALUE non_block, VALUE timeout)
1074{
1075 return queue_do_pop(ec, self, queue_ptr(self), non_block, timeout);
1076}
1077
1078static void
1079queue_clear(struct rb_queue *q)
1080{
1081 q->len = 0;
1082 q->offset = 0;
1083}
1084
1085static VALUE
1086szqueue_initialize(rb_execution_context_t *ec, VALUE self, VALUE vmax)
1087{
1088 long max = NUM2LONG(vmax);
1089 struct rb_szqueue *sq = raw_szqueue_ptr(self);
1090
1091 if (max <= 0) {
1092 rb_raise(rb_eArgError, "queue size must be positive");
1093 }
1094 ring_buffer_init(&sq->q, QUEUE_INITIAL_CAPA);
1095 ccan_list_head_init(szqueue_waitq(sq));
1096 ccan_list_head_init(szqueue_pushq(sq));
1097 sq->max = max;
1098
1099 return self;
1100}
1101
1102static VALUE
1103rb_szqueue_push(rb_execution_context_t *ec, VALUE self, VALUE object, VALUE non_block, VALUE timeout)
1104{
1105 struct rb_szqueue *sq = szqueue_ptr(self);
1106
1107 if (sq->q.len >= sq->max) {
1108 if (RTEST(non_block)) {
1109 rb_raise(rb_eThreadError, "queue full");
1110 }
1111
1112 if (RTEST(rb_equal(INT2FIX(0), timeout))) {
1113 return Qnil;
1114 }
1115 }
1116
1117 rb_hrtime_t end = queue_timeout2hrtime(timeout);
1118 while (sq->q.len >= sq->max) {
1119 if (queue_closed_p(self)) {
1120 raise_closed_queue_error(self);
1121 }
1122 else {
1123 struct queue_waiter queue_waiter = {
1124 .w = {.self = self, .th = ec->thread_ptr, .fiber = nonblocking_fiber(ec->fiber_ptr)},
1125 .as = {.sq = sq}
1126 };
1127
1128 struct ccan_list_head *pushq = szqueue_pushq(sq);
1129
1130 ccan_list_add_tail(pushq, &queue_waiter.w.node);
1131 sq->num_waiting_push++;
1132
1134 .self = self,
1135 .timeout = timeout,
1136 .end = end
1137 };
1138 rb_ensure(queue_sleep, (VALUE)&queue_sleep_arg, szqueue_sleep_done, (VALUE)&queue_waiter);
1139 if (!NIL_P(timeout) && rb_hrtime_now() >= end) {
1140 return Qnil;
1141 }
1142 }
1143 }
1144
1145 return queue_do_push(self, &sq->q, object);
1146}
1147
1148static VALUE
1149rb_szqueue_pop(rb_execution_context_t *ec, VALUE self, VALUE non_block, VALUE timeout)
1150{
1151 struct rb_szqueue *sq = szqueue_ptr(self);
1152 VALUE retval = queue_do_pop(ec, self, &sq->q, non_block, timeout);
1153
1154 if (sq->q.len < sq->max) {
1155 wakeup_one(szqueue_pushq(sq));
1156 }
1157
1158 return retval;
1159}
1160
1161/* ConditionalVariable */
1163 struct ccan_list_head waitq;
1164 rb_serial_t fork_gen;
1165};
1166
1167static size_t
1168condvar_memsize(const void *ptr)
1169{
1170 return sizeof(struct rb_condvar);
1171}
1172
1173static const rb_data_type_t cv_data_type = {
1174 "condvar",
1175 {0, RUBY_TYPED_DEFAULT_FREE, condvar_memsize,},
1176 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1177};
1178
1179static struct rb_condvar *
1180condvar_ptr(VALUE self)
1181{
1182 struct rb_condvar *cv;
1183 rb_serial_t fork_gen = GET_VM()->fork_gen;
1184
1185 TypedData_Get_Struct(self, struct rb_condvar, &cv_data_type, cv);
1186
1187 /* forked children can't reach into parent thread stacks */
1188 if (cv->fork_gen != fork_gen) {
1189 cv->fork_gen = fork_gen;
1190 ccan_list_head_init(&cv->waitq);
1191 }
1192
1193 return cv;
1194}
1195
1196static VALUE
1197condvar_alloc(VALUE klass)
1198{
1199 struct rb_condvar *cv;
1200 VALUE obj;
1201
1202 obj = TypedData_Make_Struct(klass, struct rb_condvar, &cv_data_type, cv);
1203 ccan_list_head_init(&cv->waitq);
1204
1205 return obj;
1206}
1207
1210 VALUE mutex;
1211 VALUE timeout;
1212};
1213
1214static ID id_sleep;
1215
1216static VALUE
1217do_sleep(VALUE args)
1218{
1219 struct sleep_call *p = (struct sleep_call *)args;
1220 if (CLASS_OF(p->mutex) == rb_cMutex) {
1221 return rb_mut_sleep(p->ec, p->mutex, p->timeout);
1222 }
1223 else {
1224 return rb_funcallv(p->mutex, id_sleep, 1, &p->timeout);
1225 }
1226}
1227
1228static VALUE
1229rb_condvar_wait(rb_execution_context_t *ec, VALUE self, VALUE mutex, VALUE timeout)
1230{
1231 struct rb_condvar *cv = condvar_ptr(self);
1232 struct sleep_call args = {
1233 .ec = ec,
1234 .mutex = mutex,
1235 .timeout = timeout,
1236 };
1237
1238 struct sync_waiter sync_waiter = {
1239 .self = mutex,
1240 .th = ec->thread_ptr,
1241 .fiber = nonblocking_fiber(ec->fiber_ptr)
1242 };
1243
1244 ccan_list_add_tail(&cv->waitq, &sync_waiter.node);
1245 return rb_ec_ensure(ec, do_sleep, (VALUE)&args, delete_from_waitq, (VALUE)&sync_waiter);
1246}
1247
1248static VALUE
1249rb_condvar_signal(rb_execution_context_t *ec, VALUE self)
1250{
1251 struct rb_condvar *cv = condvar_ptr(self);
1252 wakeup_one(&cv->waitq);
1253 return self;
1254}
1255
1256static VALUE
1257rb_condvar_broadcast(rb_execution_context_t *ec, VALUE self)
1258{
1259 struct rb_condvar *cv = condvar_ptr(self);
1260 wakeup_all(&cv->waitq);
1261 return self;
1262}
1263
1264/* Thread::Monitor */
1265
1267 long count;
1268 rb_serial_t ec_serial;
1269 VALUE mutex;
1270};
1271
1272static void
1273monitor_mark(void *ptr)
1274{
1275 struct rb_monitor *mc = ptr;
1276 rb_gc_mark_movable(mc->mutex);
1277}
1278
1279static void
1280monitor_compact(void *ptr)
1281{
1282 struct rb_monitor *mc = ptr;
1283 mc->mutex = rb_gc_location(mc->mutex);
1284}
1285
1286static const rb_data_type_t monitor_data_type = {
1287 .wrap_struct_name = "monitor",
1288 .function = {
1289 .dmark = monitor_mark,
1290 .dfree = RUBY_TYPED_DEFAULT_FREE,
1291 .dsize = NULL, // Fully embeded
1292 .dcompact = monitor_compact,
1293 },
1294 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
1295};
1296
1297static VALUE
1298monitor_alloc(VALUE klass)
1299{
1300 struct rb_monitor *mc;
1301 VALUE obj;
1302
1303 obj = TypedData_Make_Struct(klass, struct rb_monitor, &monitor_data_type, mc);
1304 RB_OBJ_WRITE(obj, &mc->mutex, rb_mutex_new());
1305 mc->ec_serial = 0;
1306 mc->count = 0;
1307
1308 return obj;
1309}
1310
1311static struct rb_monitor *
1312monitor_ptr(VALUE monitor)
1313{
1314 struct rb_monitor *mc;
1315 TypedData_Get_Struct(monitor, struct rb_monitor, &monitor_data_type, mc);
1316 return mc;
1317}
1318
1319static bool
1320mc_owner_p(struct rb_monitor *mc, rb_serial_t current_fiber_serial)
1321{
1322 return mc->ec_serial == current_fiber_serial;
1323}
1324
1325static VALUE
1326rb_monitor_try_enter(rb_execution_context_t *ec, VALUE monitor)
1327{
1328 struct rb_monitor *mc = monitor_ptr(monitor);
1329
1330 rb_serial_t current_fiber_serial = rb_ec_serial(ec);
1331 if (!mc_owner_p(mc, current_fiber_serial)) {
1332 if (!rb_mut_trylock(ec, mc->mutex)) {
1333 return Qfalse;
1334 }
1335 mc->ec_serial = current_fiber_serial;
1336 mc->count = 0;
1337 }
1338 mc->count += 1;
1339 return Qtrue;
1340}
1341
1343 VALUE monitor;
1344 struct rb_monitor *mc;
1345 rb_serial_t current_fiber_serial;
1347};
1348
1349static void
1350monitor_enter0(struct monitor_args *args)
1351{
1352 if (!mc_owner_p(args->mc, args->current_fiber_serial)) {
1353 struct mutex_args mut_args = {
1354 .self = args->mc->mutex,
1355 .mutex = mutex_ptr(args->mc->mutex),
1356 .ec= args->ec,
1357 };
1358 do_mutex_lock(&mut_args, 1);
1359 args->mc->ec_serial = args->current_fiber_serial;
1360 args->mc->count = 0;
1361 }
1362 args->mc->count++;
1363}
1364
1365static VALUE
1366rb_monitor_enter(rb_execution_context_t *ec, VALUE monitor)
1367{
1368 struct monitor_args args = {
1369 .monitor = monitor,
1370 .mc = monitor_ptr(monitor),
1371 .ec = ec,
1372 .current_fiber_serial = rb_ec_serial(ec),
1373 };
1374 monitor_enter0(&args);
1375 return Qnil;
1376}
1377
1378static inline void
1379monitor_check_owner0(struct monitor_args *args)
1380{
1381 if (!mc_owner_p(args->mc, args->current_fiber_serial)) {
1382 rb_raise(rb_eThreadError, "current fiber not owner");
1383 }
1384}
1385
1386static VALUE
1387rb_monitor_check_owner(rb_execution_context_t *ec, VALUE monitor)
1388{
1389 struct monitor_args args = {
1390 .monitor = monitor,
1391 .mc = monitor_ptr(monitor),
1392 .ec = ec,
1393 .current_fiber_serial = rb_ec_serial(ec),
1394 };
1395 monitor_check_owner0(&args);
1396 return Qnil;
1397}
1398
1399static void
1400monitor_exit0(struct monitor_args *args)
1401{
1402 monitor_check_owner0(args);
1403
1404 if (args->mc->count <= 0) rb_bug("monitor_exit: count:%d", (int)args->mc->count);
1405 args->mc->count--;
1406
1407 if (args->mc->count == 0) {
1408 args->mc->ec_serial = 0;
1409
1410 struct mutex_args mut_args = {
1411 .self = args->mc->mutex,
1412 .mutex = mutex_ptr(args->mc->mutex),
1413 .ec= args->ec,
1414 };
1415 do_mutex_unlock(&mut_args);
1416 }
1417}
1418
1419static VALUE
1420rb_monitor_exit(rb_execution_context_t *ec, VALUE monitor)
1421{
1422 struct monitor_args args = {
1423 .monitor = monitor,
1424 .mc = monitor_ptr(monitor),
1425 .ec = ec,
1426 .current_fiber_serial = rb_ec_serial(ec),
1427 };
1428 monitor_exit0(&args);
1429 return Qnil;
1430}
1431
1432static VALUE
1433rb_monitor_locked_p(rb_execution_context_t *ec, VALUE monitor)
1434{
1435 struct rb_monitor *mc = monitor_ptr(monitor);
1436 return rb_mutex_locked_p(mc->mutex);
1437}
1438
1439static VALUE
1440rb_monitor_owned_p(rb_execution_context_t *ec, VALUE monitor)
1441{
1442 struct rb_monitor *mc = monitor_ptr(monitor);
1443 return RBOOL(rb_mutex_locked_p(mc->mutex) && mc_owner_p(mc, rb_ec_serial(ec)));
1444}
1445
1446static VALUE
1447monitor_exit_for_cond(VALUE monitor)
1448{
1449 struct rb_monitor *mc = monitor_ptr(monitor);
1450 long cnt = mc->count;
1451 mc->ec_serial = 0;
1452 mc->count = 0;
1453 return LONG2NUM(cnt);
1454}
1455
1457 VALUE monitor;
1458 VALUE cond;
1459 VALUE timeout;
1460 VALUE count;
1461};
1462
1463static VALUE
1464monitor_wait_for_cond_body(VALUE v)
1465{
1466 struct wait_for_cond_data *data = (struct wait_for_cond_data *)v;
1467 struct rb_monitor *mc = monitor_ptr(data->monitor);
1468 // cond.wait(monitor.mutex, timeout)
1469 VALUE signaled = rb_funcall(data->cond, rb_intern("wait"), 2, mc->mutex, data->timeout);
1470 return RTEST(signaled) ? Qtrue : Qfalse;
1471}
1472
1473static VALUE
1474monitor_enter_for_cond(VALUE v)
1475{
1476 // assert(rb_mutex_owned_p(mc->mutex) == Qtrue)
1477 // but rb_mutex_owned_p is not exported...
1478
1479 struct wait_for_cond_data *data = (struct wait_for_cond_data *)v;
1480 struct rb_monitor *mc = monitor_ptr(data->monitor);
1481 mc->ec_serial = rb_ec_serial(GET_EC());
1482 mc->count = NUM2LONG(data->count);
1483 return Qnil;
1484}
1485
1486static VALUE
1487rb_monitor_wait_for_cond(rb_execution_context_t *ec, VALUE monitor, VALUE cond, VALUE timeout)
1488{
1489 VALUE count = monitor_exit_for_cond(monitor);
1490 struct wait_for_cond_data data = {
1491 monitor,
1492 cond,
1493 timeout,
1494 count,
1495 };
1496
1497 return rb_ensure(monitor_wait_for_cond_body, (VALUE)&data,
1498 monitor_enter_for_cond, (VALUE)&data);
1499}
1500
1501static VALUE
1502monitor_sync_ensure(VALUE v_args)
1503{
1504 monitor_exit0((struct monitor_args *)v_args);
1505 return Qnil;
1506}
1507
1508static VALUE
1509rb_monitor_synchronize(rb_execution_context_t *ec, VALUE monitor)
1510{
1511 struct monitor_args args = {
1512 .monitor = monitor,
1513 .mc = monitor_ptr(monitor),
1514 .ec = ec,
1515 .current_fiber_serial = rb_ec_serial(ec),
1516 };
1517 monitor_enter0(&args);
1518 return rb_ec_ensure(ec, do_ec_yield, (VALUE)ec, monitor_sync_ensure, (VALUE)&args);
1519}
1520
1521static void
1522Init_thread_sync(void)
1523{
1524 /* Mutex */
1525 rb_cMutex = rb_define_class_id_under(rb_cThread, rb_intern("Mutex"), rb_cObject);
1526 rb_define_alloc_func(rb_cMutex, mutex_alloc);
1527
1528 /* Queue */
1529 VALUE rb_cQueue = rb_define_class_id_under_no_pin(rb_cThread, rb_intern("Queue"), rb_cObject);
1530 rb_define_alloc_func(rb_cQueue, queue_alloc);
1531
1532 rb_eClosedQueueError = rb_define_class("ClosedQueueError", rb_eStopIteration);
1533
1534 VALUE rb_cSizedQueue = rb_define_class_id_under_no_pin(rb_cThread, rb_intern("SizedQueue"), rb_cQueue);
1535 rb_define_alloc_func(rb_cSizedQueue, szqueue_alloc);
1536
1537 /* CVar */
1538 VALUE rb_cConditionVariable = rb_define_class_id_under_no_pin(rb_cThread, rb_intern("ConditionVariable"), rb_cObject);
1539 rb_define_alloc_func(rb_cConditionVariable, condvar_alloc);
1540
1541 id_sleep = rb_intern("sleep");
1542
1543 /* Monitor */
1544 VALUE rb_cMonitor = rb_define_class_id_under_no_pin(rb_cThread, rb_intern("Monitor"), rb_cObject);
1545 rb_define_alloc_func(rb_cMonitor, monitor_alloc);
1546
1547 rb_provide("monitor.so");
1548 rb_provide("thread.rb");
1549}
1550
1551#include "thread_sync.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition atomic.h:69
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1596
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.
Definition class.c:1666
#define FL_UNSET_RAW
Old name of RB_FL_UNSET_RAW.
Definition fl_type.h:130
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:128
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#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.
Definition rtypeddata.h:106
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1418
VALUE rb_eStopIteration
StopIteration exception.
Definition enumerator.c:195
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition eval.c:1168
VALUE rb_eThreadError
ThreadError exception.
Definition eval.c:1035
VALUE rb_cObject
Object class.
Definition object.c:61
VALUE rb_cThread
Thread class.
Definition vm.c:671
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
Definition object.c:3875
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:176
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
Defines RBIMPL_HAS_BUILTIN.
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:695
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".
Definition time.c:2948
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.
Definition io.h:11
int len
Length of the buffer.
Definition io.h:8
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition rarray.h:52
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition rtypeddata.h:80
#define RUBY_TYPED_FREE_IMMEDIATELY
Macros to see if each corresponding flag is defined.
Definition rtypeddata.h:119
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition rtypeddata.h:736
#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...
Definition rtypeddata.h:561
VALUE rb_fiber_scheduler_current(void)
Identical to rb_fiber_scheduler_get(), except it also returns RUBY_Qnil in case of a blocking fiber.
Definition scheduler.c:458
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....
Definition scheduler.c:647
VALUE rb_fiber_scheduler_kernel_sleep(VALUE scheduler, VALUE duration)
Non-blocking sleep.
Definition scheduler.c:530
VALUE rb_fiber_scheduler_unblock(VALUE scheduler, VALUE blocker, VALUE fiber)
Wakes up a fiber previously blocked using rb_fiber_scheduler_block().
Definition scheduler.c:666
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:211
const char * wrap_struct_name
Name of structs of this kind.
Definition rtypeddata.h:218
VALUE flags
Type-specific behavioural characteristics.
Definition rtypeddata.h:325
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40