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