Ruby 4.1.0dev (2026-11-19 revision 2e6b197f6849401b2c25894b5b8ac39ac3c2558e)
thread_sync.c (2e6b197f6849401b2c25894b5b8ac39ac3c2558e)
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 */
13} rb_mutex_t;
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, rb_len_t max)
43{
44 RUBY_DEBUG_LOG("max:%"PRIdLEN, 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, RB_LEN_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;
266 rb_execution_context_t *ec;
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 case THREAD_KILLED: /* not sure about this, possible in exit GC? */
477 /* MSVC 14.52 for ARM64 crashes with /Os on two separate rb_bug calls here */
478 rb_bug("unexpected %s", cur->th->status == THREAD_STOPPED ? "THREAD_STOPPED" : "THREAD_KILLED");
479 }
480 }
481 }
482
483 // We did not find any threads to wake up, so we can just return with no error:
484 return NULL;
485}
486
487static void
488do_mutex_unlock(struct mutex_args *args)
489{
490 const char *err;
491 rb_mutex_t *mutex = args->mutex;
492 rb_thread_t *th = rb_ec_thread_ptr(args->ec);
493
494 err = rb_mutex_unlock_th(mutex, th, rb_ec_serial(args->ec));
495 if (err) rb_raise(rb_eThreadError, "%s", err);
496}
497
498static VALUE
499do_mutex_unlock_safe(VALUE args)
500{
501 do_mutex_unlock((struct mutex_args *)args);
502 return Qnil;
503}
504
505VALUE
507{
508 struct mutex_args args;
509 mutex_args_init(&args, self);
510 do_mutex_unlock(&args);
511 return self;
512}
513
514static VALUE
515rb_mut_unlock(rb_execution_context_t *ec, VALUE self)
516{
517 struct mutex_args args = {
518 .self = self,
519 .mutex = mutex_ptr(self),
520 .ec = ec,
521 };
522 do_mutex_unlock(&args);
523 return self;
524}
525
526#if defined(HAVE_WORKING_FORK)
527static void
528rb_mutex_abandon_keeping_mutexes(rb_thread_t *th)
529{
530 rb_mutex_abandon_all(th->keeping_mutexes);
531 th->keeping_mutexes = NULL;
532}
533
534static void
535rb_mutex_abandon_locking_mutex(rb_thread_t *th)
536{
537 if (th->locking_mutex) {
538 rb_mutex_t *mutex = mutex_ptr(th->locking_mutex);
539
540 ccan_list_head_init(&mutex->waitq);
541 th->locking_mutex = Qfalse;
542 }
543}
544
545static void
546rb_mutex_abandon_all(rb_mutex_t *mutexes)
547{
548 rb_mutex_t *mutex;
549
550 while (mutexes) {
551 mutex = mutexes;
552 mutexes = mutex->next_mutex;
553 mutex->ec_serial = 0;
554 mutex->next_mutex = 0;
555 ccan_list_head_init(&mutex->waitq);
556 }
557}
558#endif
559
561 VALUE self;
562 VALUE timeout;
563};
564
565static VALUE
566mutex_sleep_begin(VALUE _arguments)
567{
568 struct rb_mutex_sleep_arguments *arguments = (struct rb_mutex_sleep_arguments *)_arguments;
569 VALUE timeout = arguments->timeout;
570 VALUE woken = Qtrue;
571
572 VALUE scheduler = rb_fiber_scheduler_current();
573 if (scheduler != Qnil) {
574 rb_fiber_scheduler_kernel_sleep(scheduler, timeout);
575 }
576 else {
577 if (NIL_P(timeout)) {
578 rb_thread_sleep_deadly_allow_spurious_wakeup(arguments->self, Qnil, 0);
579 }
580 else {
581 struct timeval timeout_value = rb_time_interval(timeout);
582 rb_hrtime_t relative_timeout = rb_timeval2hrtime(&timeout_value);
583 /* permit spurious check */
584 woken = RBOOL(sleep_hrtime(GET_THREAD(), relative_timeout, 0));
585 }
586 }
587
588 return woken;
589}
590
591static VALUE
592rb_mut_sleep(rb_execution_context_t *ec, VALUE self, VALUE timeout)
593{
594 if (!NIL_P(timeout)) {
595 // Validate the argument:
596 rb_time_interval(timeout);
597 }
598
599 rb_mut_unlock(ec, self);
600 time_t beg = time(0);
601
602 struct rb_mutex_sleep_arguments arguments = {
603 .self = self,
604 .timeout = timeout,
605 };
606
607 VALUE woken = rb_ec_ensure(ec, mutex_sleep_begin, (VALUE)&arguments, mutex_lock_uninterruptible, self);
608
609 RUBY_VM_CHECK_INTS_BLOCKING(ec);
610 if (!woken) return Qnil;
611 time_t end = time(0) - beg;
612 return TIMET2NUM(end);
613}
614
615VALUE
617{
618 return rb_mut_sleep(GET_EC(), self, timeout);
619}
620
621VALUE
623{
624 struct mutex_args args;
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);
628}
629
630static VALUE
631do_ec_yield(VALUE _ec)
632{
633 return rb_ec_yield((rb_execution_context_t *)_ec, Qundef);
634}
635
636VALUE
637rb_mut_synchronize(rb_execution_context_t *ec, VALUE self)
638{
639 struct mutex_args args = {
640 .self = self,
641 .mutex = mutex_ptr(self),
642 .ec = ec,
643 };
644 do_mutex_lock(&args, 1);
645 return rb_ec_ensure(args.ec, do_ec_yield, (VALUE)ec, do_mutex_unlock_safe, (VALUE)&args);
646}
647
648void
649rb_mutex_allow_trap(VALUE self, int val)
650{
651 Check_TypedStruct(self, &mutex_data_type);
652
653 if (val)
654 FL_SET_RAW(self, MUTEX_ALLOW_TRAP);
655 else
656 FL_UNSET_RAW(self, MUTEX_ALLOW_TRAP);
657}
658
659/* Queue */
660
661struct rb_queue {
662 struct ccan_list_head waitq;
663 rb_serial_t fork_gen;
664 rb_len_t capa;
665 rb_len_t len;
666 rb_len_t offset;
667 VALUE *buffer;
668 int num_waiting;
669};
670
671#define szqueue_waitq(sq) &sq->q.waitq
672#define szqueue_pushq(sq) &sq->pushq
673
675 struct rb_queue q;
676 int num_waiting_push;
677 struct ccan_list_head pushq;
678 rb_len_t max;
679};
680
681static void
682queue_mark_and_move(void *ptr)
683{
684 struct rb_queue *q = ptr;
685 /* no need to mark threads in waitq, they are on stack */
686 for (rb_len_t index = 0; index < q->len; index++) {
687 rb_gc_mark_and_move(&q->buffer[((q->offset + index) % q->capa)]);
688 }
689}
690
691static inline void
692queue_free_buffer(struct rb_queue *q)
693{
694 if (q->buffer) {
695 SIZED_FREE_N(q->buffer, q->capa);
696 }
697}
698
699static void
700queue_free(void *ptr)
701{
702 struct rb_queue *q = ptr;
703 queue_free_buffer(q);
704 SIZED_FREE(q);
705}
706
707static size_t
708queue_memsize(const void *ptr)
709{
710 const struct rb_queue *q = ptr;
711 return sizeof(struct rb_queue) + (q->capa * sizeof(VALUE));
712}
713
714static const rb_data_type_t queue_data_type = {
715 .wrap_struct_name = "Thread::Queue",
716 .function = {
717 .dmark = queue_mark_and_move,
718 .dfree = queue_free,
719 .dsize = queue_memsize,
720 .dcompact = queue_mark_and_move,
721 },
722 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
723};
724
725static VALUE
726queue_alloc(VALUE klass)
727{
728 VALUE obj;
729 struct rb_queue *q;
730
731 obj = TypedData_Make_Struct(klass, struct rb_queue, &queue_data_type, q);
732 ccan_list_head_init(&q->waitq);
733 return obj;
734}
735
736static inline bool
737queue_fork_check(struct rb_queue *q)
738{
739 rb_serial_t fork_gen = GET_VM()->fork_gen;
740
741 if (RB_LIKELY(q->fork_gen == fork_gen)) {
742 return false;
743 }
744 /* forked children can't reach into parent thread stacks */
745 q->fork_gen = fork_gen;
746 ccan_list_head_init(&q->waitq);
747 q->num_waiting = 0;
748 return true;
749}
750
751static inline struct rb_queue *
752raw_queue_ptr(VALUE obj)
753{
754 struct rb_queue *q;
755
756 TypedData_Get_Struct(obj, struct rb_queue, &queue_data_type, q);
757 queue_fork_check(q);
758
759 return q;
760}
761
762static inline void
763check_queue(VALUE obj, struct rb_queue *q)
764{
765 if (RB_UNLIKELY(q->buffer == NULL)) {
766 rb_raise(rb_eTypeError, "%+"PRIsVALUE" not initialized", obj);
767 }
768}
769
770static inline struct rb_queue *
771queue_ptr(VALUE obj)
772{
773 struct rb_queue *q = raw_queue_ptr(obj);
774 check_queue(obj, q);
775 return q;
776}
777
778#define QUEUE_CLOSED FL_USER5
779
780static rb_hrtime_t
781queue_timeout2hrtime(VALUE timeout)
782{
783 if (NIL_P(timeout)) {
784 return (rb_hrtime_t)0;
785 }
786 rb_hrtime_t rel = 0;
787 if (FIXNUM_P(timeout)) {
788 rel = rb_sec2hrtime(NUM2TIMET(timeout));
789 }
790 else {
791 double2hrtime(&rel, rb_num2dbl(timeout));
792 }
793 return rb_hrtime_add(rel, rb_hrtime_now());
794}
795
796static void
797szqueue_mark_and_move(void *ptr)
798{
799 struct rb_szqueue *sq = ptr;
800
801 queue_mark_and_move(&sq->q);
802}
803
804static void
805szqueue_free(void *ptr)
806{
807 struct rb_szqueue *sq = ptr;
808 queue_free_buffer(&sq->q);
809 SIZED_FREE(sq);
810}
811
812static size_t
813szqueue_memsize(const void *ptr)
814{
815 const struct rb_szqueue *sq = ptr;
816 return sizeof(struct rb_szqueue) + (sq->q.capa * sizeof(VALUE));
817}
818
819static const rb_data_type_t szqueue_data_type = {
820 .wrap_struct_name = "Thread::SizedQueue",
821 .function = {
822 .dmark = szqueue_mark_and_move,
823 .dfree = szqueue_free,
824 .dsize = szqueue_memsize,
825 .dcompact = szqueue_mark_and_move,
826 },
827 .parent = &queue_data_type,
828 .flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED,
829};
830
831static VALUE
832szqueue_alloc(VALUE klass)
833{
834 struct rb_szqueue *sq;
835 VALUE obj = TypedData_Make_Struct(klass, struct rb_szqueue,
836 &szqueue_data_type, sq);
837 ccan_list_head_init(szqueue_waitq(sq));
838 ccan_list_head_init(szqueue_pushq(sq));
839 return obj;
840}
841
842static inline struct rb_szqueue *
843raw_szqueue_ptr(VALUE obj)
844{
845 struct rb_szqueue *sq;
846
847 TypedData_Get_Struct(obj, struct rb_szqueue, &szqueue_data_type, sq);
848 if (RB_UNLIKELY(queue_fork_check(&sq->q))) {
849 ccan_list_head_init(szqueue_pushq(sq));
850 sq->num_waiting_push = 0;
851 }
852
853 return sq;
854}
855
856static inline struct rb_szqueue *
857szqueue_ptr(VALUE obj)
858{
859 struct rb_szqueue *sq = raw_szqueue_ptr(obj);
860 check_queue(obj, &sq->q);
861 return sq;
862}
863
864static inline bool
865queue_closed_p(VALUE self)
866{
867 return FL_TEST_RAW(self, QUEUE_CLOSED) != 0;
868}
869
870/*
871 * Document-class: ClosedQueueError
872 *
873 * The exception class which will be raised when pushing into a closed
874 * Queue. See Thread::Queue#close and Thread::SizedQueue#close.
875 */
876
877NORETURN(static void raise_closed_queue_error(VALUE self));
878
879static void
880raise_closed_queue_error(VALUE self)
881{
882 rb_raise(rb_eClosedQueueError, "queue closed");
883}
884
885static VALUE
886queue_closed_result(VALUE self, struct rb_queue *q)
887{
888 RUBY_ASSERT(q->len == 0);
889 return Qnil;
890}
891
892#define QUEUE_INITIAL_CAPA 8
893
894static inline void
895ring_buffer_init(struct rb_queue *q, rb_len_t initial_capa)
896{
897 q->buffer = ALLOC_N(VALUE, initial_capa);
898 q->capa = initial_capa;
899}
900
901static inline void
902ring_buffer_expand(struct rb_queue *q)
903{
904 RUBY_ASSERT(q->capa > 0);
905 VALUE *new_buffer = ALLOC_N(VALUE, q->capa * 2);
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;
910 q->offset = 0;
911 ruby_xfree_sized(old_buffer, q->capa * sizeof(VALUE));
912 q->capa *= 2;
913}
914
915static void
916ring_buffer_push(VALUE self, struct rb_queue *q, VALUE obj)
917{
918 if (RB_UNLIKELY(q->len >= q->capa)) {
919 ring_buffer_expand(q);
920 }
921 RUBY_ASSERT(q->capa > q->len);
922 rb_len_t index = (q->offset + q->len) % q->capa;
923 q->len++;
924 RB_OBJ_WRITE(self, &q->buffer[index], obj);
925}
926
927static VALUE
928ring_buffer_shift(struct rb_queue *q)
929{
930 if (!q->len) {
931 return Qnil;
932 }
933
934 VALUE obj = q->buffer[q->offset];
935 q->len--;
936 if (q->len == 0) {
937 q->offset = 0;
938 }
939 else {
940 q->offset = (q->offset + 1) % q->capa;
941 }
942 return obj;
943}
944
945static VALUE
946queue_initialize(rb_execution_context_t *ec, VALUE self, VALUE initial)
947{
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);
952 }
953 else {
954 initial = rb_to_array(initial);
955 rb_len_t len = RARRAY_LEN(initial);
956 rb_len_t initial_capa = QUEUE_INITIAL_CAPA;
957 while (initial_capa < len) {
958 initial_capa *= 2;
959 }
960 ring_buffer_init(q, initial_capa);
961 MEMCPY(q->buffer, RARRAY_CONST_PTR(initial), VALUE, len);
962 q->len = len;
963 }
964 return self;
965}
966
967static VALUE
968queue_do_push(VALUE self, struct rb_queue *q, VALUE obj)
969{
970 check_queue(self, q);
971 if (queue_closed_p(self)) {
972 raise_closed_queue_error(self);
973 }
974 ring_buffer_push(self, q, obj);
975 wakeup_one(&q->waitq);
976 return self;
977}
978
979static VALUE
980queue_sleep(VALUE _args)
981{
982 struct queue_sleep_arg *args = (struct queue_sleep_arg *)_args;
983 rb_thread_sleep_deadly_allow_spurious_wakeup(args->self, args->timeout, args->end);
984 return Qnil;
985}
986
988 struct sync_waiter w;
989 union {
990 struct rb_queue *q;
991 struct rb_szqueue *sq;
992 } as;
993};
994
995static VALUE
996queue_sleep_done(VALUE p)
997{
998 struct queue_waiter *qw = (struct queue_waiter *)p;
999
1000 ccan_list_del(&qw->w.node);
1001 qw->as.q->num_waiting--;
1002
1003 return Qfalse;
1004}
1005
1006static VALUE
1007szqueue_sleep_done(VALUE p)
1008{
1009 struct queue_waiter *qw = (struct queue_waiter *)p;
1010
1011 ccan_list_del(&qw->w.node);
1012 qw->as.sq->num_waiting_push--;
1013
1014 return Qfalse;
1015}
1016
1017static inline VALUE
1018queue_do_pop(rb_execution_context_t *ec, VALUE self, struct rb_queue *q, VALUE non_block, VALUE timeout)
1019{
1020 if (q->len == 0) {
1021 if (RTEST(non_block)) {
1022 rb_raise(rb_eThreadError, "queue empty");
1023 }
1024
1025 if (RTEST(rb_equal(INT2FIX(0), timeout))) {
1026 return Qnil;
1027 }
1028 }
1029
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);
1034 }
1035 else {
1036 RUBY_ASSERT(q->len == 0);
1037 RUBY_ASSERT(queue_closed_p(self) == 0);
1038
1039 struct queue_waiter queue_waiter = {
1040 .w = {.self = self, .th = ec->thread_ptr, .fiber = nonblocking_fiber(ec->fiber_ptr)},
1041 .as = {.q = q}
1042 };
1043
1044 struct ccan_list_head *waitq = &q->waitq;
1045
1046 ccan_list_add_tail(waitq, &queue_waiter.w.node);
1047 queue_waiter.as.q->num_waiting++;
1048
1050 .self = self,
1051 .timeout = timeout,
1052 .end = end
1053 };
1054
1055 rb_ensure(queue_sleep, (VALUE)&queue_sleep_arg, queue_sleep_done, (VALUE)&queue_waiter);
1056 if (!NIL_P(timeout) && (rb_hrtime_now() >= end))
1057 break;
1058 }
1059 }
1060
1061 return ring_buffer_shift(q);
1062}
1063
1064static VALUE
1065rb_queue_pop(rb_execution_context_t *ec, VALUE self, VALUE non_block, VALUE timeout)
1066{
1067 return queue_do_pop(ec, self, queue_ptr(self), non_block, timeout);
1068}
1069
1070static void
1071queue_clear(struct rb_queue *q)
1072{
1073 q->len = 0;
1074 q->offset = 0;
1075}
1076
1077static VALUE
1078szqueue_initialize(rb_execution_context_t *ec, VALUE self, VALUE vmax)
1079{
1080 rb_len_t max = NUM2LEN(vmax);
1081 struct rb_szqueue *sq = raw_szqueue_ptr(self);
1082
1083 if (max <= 0) {
1084 rb_raise(rb_eArgError, "queue size must be positive");
1085 }
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));
1089 sq->max = max;
1090
1091 return self;
1092}
1093
1094static VALUE
1095rb_szqueue_push(rb_execution_context_t *ec, VALUE self, VALUE object, VALUE non_block, VALUE timeout)
1096{
1097 struct rb_szqueue *sq = szqueue_ptr(self);
1098
1099 if (sq->q.len >= sq->max) {
1100 if (RTEST(non_block)) {
1101 rb_raise(rb_eThreadError, "queue full");
1102 }
1103
1104 if (RTEST(rb_equal(INT2FIX(0), timeout))) {
1105 return Qnil;
1106 }
1107 }
1108
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);
1113 }
1114 else {
1115 struct queue_waiter queue_waiter = {
1116 .w = {.self = self, .th = ec->thread_ptr, .fiber = nonblocking_fiber(ec->fiber_ptr)},
1117 .as = {.sq = sq}
1118 };
1119
1120 struct ccan_list_head *pushq = szqueue_pushq(sq);
1121
1122 ccan_list_add_tail(pushq, &queue_waiter.w.node);
1123 sq->num_waiting_push++;
1124
1126 .self = self,
1127 .timeout = timeout,
1128 .end = end
1129 };
1130 rb_ensure(queue_sleep, (VALUE)&queue_sleep_arg, szqueue_sleep_done, (VALUE)&queue_waiter);
1131 if (!NIL_P(timeout) && rb_hrtime_now() >= end) {
1132 return Qnil;
1133 }
1134 }
1135 }
1136
1137 return queue_do_push(self, &sq->q, object);
1138}
1139
1140static VALUE
1141rb_szqueue_pop(rb_execution_context_t *ec, VALUE self, VALUE non_block, VALUE timeout)
1142{
1143 struct rb_szqueue *sq = szqueue_ptr(self);
1144 VALUE retval = queue_do_pop(ec, self, &sq->q, non_block, timeout);
1145
1146 if (sq->q.len < sq->max) {
1147 wakeup_one(szqueue_pushq(sq));
1148 }
1149
1150 return retval;
1151}
1152
1153/* ConditionalVariable */
1155 struct ccan_list_head waitq;
1156 rb_serial_t fork_gen;
1157};
1158
1159static size_t
1160condvar_memsize(const void *ptr)
1161{
1162 return sizeof(struct rb_condvar);
1163}
1164
1165static const rb_data_type_t cv_data_type = {
1166 "condvar",
1167 {0, RUBY_TYPED_DEFAULT_FREE, condvar_memsize,},
1168 0, 0, RUBY_TYPED_THREAD_SAFE_FREE|RUBY_TYPED_WB_PROTECTED
1169};
1170
1171static struct rb_condvar *
1172condvar_ptr(VALUE self)
1173{
1174 struct rb_condvar *cv;
1175 rb_serial_t fork_gen = GET_VM()->fork_gen;
1176
1177 TypedData_Get_Struct(self, struct rb_condvar, &cv_data_type, cv);
1178
1179 /* forked children can't reach into parent thread stacks */
1180 if (cv->fork_gen != fork_gen) {
1181 cv->fork_gen = fork_gen;
1182 ccan_list_head_init(&cv->waitq);
1183 }
1184
1185 return cv;
1186}
1187
1188static VALUE
1189condvar_alloc(VALUE klass)
1190{
1191 struct rb_condvar *cv;
1192 VALUE obj;
1193
1194 obj = TypedData_Make_Struct(klass, struct rb_condvar, &cv_data_type, cv);
1195 ccan_list_head_init(&cv->waitq);
1196
1197 return obj;
1198}
1199
1201 rb_execution_context_t *ec;
1202 VALUE mutex;
1203 VALUE timeout;
1204};
1205
1206static ID id_sleep;
1207
1208static VALUE
1209do_sleep(VALUE args)
1210{
1211 struct sleep_call *p = (struct sleep_call *)args;
1212 if (CLASS_OF(p->mutex) == rb_cMutex) {
1213 return rb_mut_sleep(p->ec, p->mutex, p->timeout);
1214 }
1215 else {
1216 return rb_funcallv(p->mutex, id_sleep, 1, &p->timeout);
1217 }
1218}
1219
1220static VALUE
1221rb_condvar_wait(rb_execution_context_t *ec, VALUE self, VALUE mutex, VALUE timeout)
1222{
1223 struct rb_condvar *cv = condvar_ptr(self);
1224 struct sleep_call args = {
1225 .ec = ec,
1226 .mutex = mutex,
1227 .timeout = timeout,
1228 };
1229
1230 struct sync_waiter sync_waiter = {
1231 .self = mutex,
1232 .th = ec->thread_ptr,
1233 .fiber = nonblocking_fiber(ec->fiber_ptr)
1234 };
1235
1236 ccan_list_add_tail(&cv->waitq, &sync_waiter.node);
1237 return rb_ec_ensure(ec, do_sleep, (VALUE)&args, delete_from_waitq, (VALUE)&sync_waiter);
1238}
1239
1240static VALUE
1241rb_condvar_signal(rb_execution_context_t *ec, VALUE self)
1242{
1243 struct rb_condvar *cv = condvar_ptr(self);
1244 wakeup_one(&cv->waitq);
1245 return self;
1246}
1247
1248static VALUE
1249rb_condvar_broadcast(rb_execution_context_t *ec, VALUE self)
1250{
1251 struct rb_condvar *cv = condvar_ptr(self);
1252 wakeup_all(&cv->waitq);
1253 return self;
1254}
1255
1256/* Thread::Monitor */
1257
1259 long count;
1260 rb_serial_t ec_serial;
1261 VALUE mutex;
1262};
1263
1264static void
1265monitor_mark(void *ptr)
1266{
1267 struct rb_monitor *mc = ptr;
1268 rb_gc_mark_movable(mc->mutex);
1269}
1270
1271static void
1272monitor_compact(void *ptr)
1273{
1274 struct rb_monitor *mc = ptr;
1275 rb_gc_update_moved(&mc->mutex);
1276}
1277
1278static const rb_data_type_t monitor_data_type = {
1279 .wrap_struct_name = "monitor",
1280 .function = {
1281 .dmark = monitor_mark,
1282 .dfree = RUBY_TYPED_DEFAULT_FREE,
1283 .dsize = NULL, // Fully embeded
1284 .dcompact = monitor_compact,
1285 },
1286 .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
1287};
1288
1289static VALUE
1290monitor_alloc(VALUE klass)
1291{
1292 struct rb_monitor *mc;
1293 VALUE obj;
1294
1295 obj = TypedData_Make_Struct(klass, struct rb_monitor, &monitor_data_type, mc);
1296 RB_OBJ_WRITE(obj, &mc->mutex, rb_mutex_new());
1297 mc->ec_serial = 0;
1298 mc->count = 0;
1299
1300 return obj;
1301}
1302
1303static struct rb_monitor *
1304monitor_ptr(VALUE monitor)
1305{
1306 struct rb_monitor *mc;
1307 TypedData_Get_Struct(monitor, struct rb_monitor, &monitor_data_type, mc);
1308 return mc;
1309}
1310
1311static bool
1312mc_owner_p(struct rb_monitor *mc, rb_serial_t current_fiber_serial)
1313{
1314 return mc->ec_serial == current_fiber_serial;
1315}
1316
1317static VALUE
1318rb_monitor_try_enter(rb_execution_context_t *ec, VALUE monitor)
1319{
1320 struct rb_monitor *mc = monitor_ptr(monitor);
1321
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)) {
1325 return Qfalse;
1326 }
1327 mc->ec_serial = current_fiber_serial;
1328 mc->count = 0;
1329 }
1330 mc->count += 1;
1331 return Qtrue;
1332}
1333
1335 VALUE monitor;
1336 struct rb_monitor *mc;
1337 rb_serial_t current_fiber_serial;
1338 rb_execution_context_t *ec;
1339};
1340
1341static void
1342monitor_enter0(struct monitor_args *args)
1343{
1344 if (!mc_owner_p(args->mc, args->current_fiber_serial)) {
1345 struct mutex_args mut_args = {
1346 .self = args->mc->mutex,
1347 .mutex = mutex_ptr(args->mc->mutex),
1348 .ec= args->ec,
1349 };
1350 do_mutex_lock(&mut_args, 1);
1351 args->mc->ec_serial = args->current_fiber_serial;
1352 args->mc->count = 0;
1353 }
1354 args->mc->count++;
1355 /* mc is embedded in the monitor, which nothing else keeps on the stack while the lock blocks */
1356 RB_GC_GUARD(args->monitor);
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"
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:1605
#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:402
#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 NUM2LEN
Old name of RB_NUM2LEN.
Definition len_t.h:34
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition fl_type.h:126
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1457
VALUE rb_eStopIteration
StopIteration exception.
Definition enumerator.c:196
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition eval.c:1198
VALUE rb_eThreadError
ThreadError exception.
Definition eval.c:1059
VALUE rb_cObject
Object class.
Definition object.c:60
VALUE rb_cThread
Thread class.
Definition vm.c:719
double rb_num2dbl(VALUE num)
Converts an instance of rb_cNumeric into C's double.
Definition object.c:3845
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:492
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
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:581
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:770
VALUE rb_fiber_scheduler_kernel_sleep(VALUE scheduler, VALUE duration)
Non-blocking sleep.
Definition scheduler.c:653
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:789
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1149
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:1107
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:963
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:2984
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:375
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:170
#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
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:239
#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
#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.
Definition value.h:142
#define RB_LEN_MAX
Maximum possible value that a rb_len_t can take.
Definition value.h:156
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