Ruby  3.4.0dev (2024-11-05 revision e440268d51fe02b303e3817a7a733a0dac1c5091)
ractor.c (e440268d51fe02b303e3817a7a733a0dac1c5091)
1 // Ractor implementation
2 
3 #include "ruby/ruby.h"
4 #include "ruby/thread.h"
5 #include "ruby/ractor.h"
6 #include "ruby/thread_native.h"
7 #include "vm_core.h"
8 #include "eval_intern.h"
9 #include "vm_sync.h"
10 #include "ractor_core.h"
11 #include "internal/complex.h"
12 #include "internal/error.h"
13 #include "internal/gc.h"
14 #include "internal/hash.h"
15 #include "internal/ractor.h"
16 #include "internal/rational.h"
17 #include "internal/struct.h"
18 #include "internal/thread.h"
19 #include "variable.h"
20 #include "yjit.h"
21 #include "rjit.h"
22 
24 static VALUE rb_cRactorSelector;
25 
26 VALUE rb_eRactorUnsafeError;
27 VALUE rb_eRactorIsolationError;
28 static VALUE rb_eRactorError;
29 static VALUE rb_eRactorRemoteError;
30 static VALUE rb_eRactorMovedError;
31 static VALUE rb_eRactorClosedError;
32 static VALUE rb_cRactorMovedObject;
33 
34 static void vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *r, const char *file, int line);
35 
36 // Ractor locking
37 
38 static void
39 ASSERT_ractor_unlocking(rb_ractor_t *r)
40 {
41 #if RACTOR_CHECK_MODE > 0
42  // GET_EC is NULL in an RJIT worker
43  if (rb_current_execution_context(false) != NULL && r->sync.locked_by == rb_ractor_self(GET_RACTOR())) {
44  rb_bug("recursive ractor locking");
45  }
46 #endif
47 }
48 
49 static void
50 ASSERT_ractor_locking(rb_ractor_t *r)
51 {
52 #if RACTOR_CHECK_MODE > 0
53  // GET_EC is NULL in an RJIT worker
54  if (rb_current_execution_context(false) != NULL && r->sync.locked_by != rb_ractor_self(GET_RACTOR())) {
55  rp(r->sync.locked_by);
56  rb_bug("ractor lock is not acquired.");
57  }
58 #endif
59 }
60 
61 static void
62 ractor_lock(rb_ractor_t *r, const char *file, int line)
63 {
64  RUBY_DEBUG_LOG2(file, line, "locking r:%u%s", r->pub.id, rb_current_ractor_raw(false) == r ? " (self)" : "");
65 
66  ASSERT_ractor_unlocking(r);
67  rb_native_mutex_lock(&r->sync.lock);
68 
69 #if RACTOR_CHECK_MODE > 0
70  if (rb_current_execution_context(false) != NULL) { // GET_EC is NULL in an RJIT worker
71  rb_ractor_t *cr = rb_current_ractor_raw(false);
72  r->sync.locked_by = cr ? rb_ractor_self(cr) : Qundef;
73  }
74 #endif
75 
76  RUBY_DEBUG_LOG2(file, line, "locked r:%u%s", r->pub.id, rb_current_ractor_raw(false) == r ? " (self)" : "");
77 }
78 
79 static void
80 ractor_lock_self(rb_ractor_t *cr, const char *file, int line)
81 {
82  VM_ASSERT(cr == GET_RACTOR());
83 #if RACTOR_CHECK_MODE > 0
84  VM_ASSERT(cr->sync.locked_by != cr->pub.self);
85 #endif
86  ractor_lock(cr, file, line);
87 }
88 
89 static void
90 ractor_unlock(rb_ractor_t *r, const char *file, int line)
91 {
92  ASSERT_ractor_locking(r);
93 #if RACTOR_CHECK_MODE > 0
94  r->sync.locked_by = Qnil;
95 #endif
96  rb_native_mutex_unlock(&r->sync.lock);
97 
98  RUBY_DEBUG_LOG2(file, line, "r:%u%s", r->pub.id, rb_current_ractor_raw(false) == r ? " (self)" : "");
99 }
100 
101 static void
102 ractor_unlock_self(rb_ractor_t *cr, const char *file, int line)
103 {
104  VM_ASSERT(cr == GET_RACTOR());
105 #if RACTOR_CHECK_MODE > 0
106  VM_ASSERT(cr->sync.locked_by == cr->pub.self);
107 #endif
108  ractor_unlock(cr, file, line);
109 }
110 
111 #define RACTOR_LOCK(r) ractor_lock(r, __FILE__, __LINE__)
112 #define RACTOR_UNLOCK(r) ractor_unlock(r, __FILE__, __LINE__)
113 #define RACTOR_LOCK_SELF(r) ractor_lock_self(r, __FILE__, __LINE__)
114 #define RACTOR_UNLOCK_SELF(r) ractor_unlock_self(r, __FILE__, __LINE__)
115 
116 void
117 rb_ractor_lock_self(rb_ractor_t *r)
118 {
119  RACTOR_LOCK_SELF(r);
120 }
121 
122 void
123 rb_ractor_unlock_self(rb_ractor_t *r)
124 {
125  RACTOR_UNLOCK_SELF(r);
126 }
127 
128 // Ractor status
129 
130 static const char *
131 ractor_status_str(enum ractor_status status)
132 {
133  switch (status) {
134  case ractor_created: return "created";
135  case ractor_running: return "running";
136  case ractor_blocking: return "blocking";
137  case ractor_terminated: return "terminated";
138  }
139  rb_bug("unreachable");
140 }
141 
142 static void
143 ractor_status_set(rb_ractor_t *r, enum ractor_status status)
144 {
145  RUBY_DEBUG_LOG("r:%u [%s]->[%s]", r->pub.id, ractor_status_str(r->status_), ractor_status_str(status));
146 
147  // check 1
148  if (r->status_ != ractor_created) {
149  VM_ASSERT(r == GET_RACTOR()); // only self-modification is allowed.
150  ASSERT_vm_locking();
151  }
152 
153  // check2: transition check. assume it will be vanished on non-debug build.
154  switch (r->status_) {
155  case ractor_created:
156  VM_ASSERT(status == ractor_blocking);
157  break;
158  case ractor_running:
159  VM_ASSERT(status == ractor_blocking||
160  status == ractor_terminated);
161  break;
162  case ractor_blocking:
163  VM_ASSERT(status == ractor_running);
164  break;
165  case ractor_terminated:
166  rb_bug("unreachable");
167  break;
168  }
169 
170  r->status_ = status;
171 }
172 
173 static bool
174 ractor_status_p(rb_ractor_t *r, enum ractor_status status)
175 {
176  return rb_ractor_status_p(r, status);
177 }
178 
179 // Ractor data/mark/free
180 
181 static struct rb_ractor_basket *ractor_queue_at(rb_ractor_t *r, struct rb_ractor_queue *rq, int i);
182 static void ractor_local_storage_mark(rb_ractor_t *r);
183 static void ractor_local_storage_free(rb_ractor_t *r);
184 
185 static void
186 ractor_queue_mark(struct rb_ractor_queue *rq)
187 {
188  for (int i=0; i<rq->cnt; i++) {
189  struct rb_ractor_basket *b = ractor_queue_at(NULL, rq, i);
190  rb_gc_mark(b->sender);
191 
192  switch (b->type.e) {
193  case basket_type_yielding:
194  case basket_type_take_basket:
195  case basket_type_deleted:
196  case basket_type_reserved:
197  // ignore
198  break;
199  default:
200  rb_gc_mark(b->p.send.v);
201  }
202  }
203 }
204 
205 static void
206 ractor_mark(void *ptr)
207 {
208  rb_ractor_t *r = (rb_ractor_t *)ptr;
209 
210  ractor_queue_mark(&r->sync.recv_queue);
211  ractor_queue_mark(&r->sync.takers_queue);
212 
213  rb_gc_mark(r->receiving_mutex);
214 
215  rb_gc_mark(r->loc);
216  rb_gc_mark(r->name);
217  rb_gc_mark(r->r_stdin);
218  rb_gc_mark(r->r_stdout);
219  rb_gc_mark(r->r_stderr);
220  rb_hook_list_mark(&r->pub.hooks);
221 
222  if (r->threads.cnt > 0) {
223  rb_thread_t *th = 0;
224  ccan_list_for_each(&r->threads.set, th, lt_node) {
225  VM_ASSERT(th != NULL);
226  rb_gc_mark(th->self);
227  }
228  }
229 
230  ractor_local_storage_mark(r);
231 }
232 
233 static void
234 ractor_queue_free(struct rb_ractor_queue *rq)
235 {
236  free(rq->baskets);
237 }
238 
239 static void
240 ractor_free(void *ptr)
241 {
242  rb_ractor_t *r = (rb_ractor_t *)ptr;
243  RUBY_DEBUG_LOG("free r:%d", rb_ractor_id(r));
244  rb_native_mutex_destroy(&r->sync.lock);
245 #ifdef RUBY_THREAD_WIN32_H
246  rb_native_cond_destroy(&r->sync.cond);
247 #endif
248  ractor_queue_free(&r->sync.recv_queue);
249  ractor_queue_free(&r->sync.takers_queue);
250  ractor_local_storage_free(r);
251  rb_hook_list_free(&r->pub.hooks);
252 
253  if (r->newobj_cache) {
254  RUBY_ASSERT(r == ruby_single_main_ractor);
255 
256  rb_gc_ractor_cache_free(r->newobj_cache);
257  r->newobj_cache = NULL;
258  }
259 
260  ruby_xfree(r);
261 }
262 
263 static size_t
264 ractor_queue_memsize(const struct rb_ractor_queue *rq)
265 {
266  return sizeof(struct rb_ractor_basket) * rq->size;
267 }
268 
269 static size_t
270 ractor_memsize(const void *ptr)
271 {
272  rb_ractor_t *r = (rb_ractor_t *)ptr;
273 
274  // TODO: more correct?
275  return sizeof(rb_ractor_t) +
276  ractor_queue_memsize(&r->sync.recv_queue) +
277  ractor_queue_memsize(&r->sync.takers_queue);
278 }
279 
280 static const rb_data_type_t ractor_data_type = {
281  "ractor",
282  {
283  ractor_mark,
284  ractor_free,
285  ractor_memsize,
286  NULL, // update
287  },
288  0, 0, RUBY_TYPED_FREE_IMMEDIATELY /* | RUBY_TYPED_WB_PROTECTED */
289 };
290 
291 bool
292 rb_ractor_p(VALUE gv)
293 {
294  if (rb_typeddata_is_kind_of(gv, &ractor_data_type)) {
295  return true;
296  }
297  else {
298  return false;
299  }
300 }
301 
302 static inline rb_ractor_t *
303 RACTOR_PTR(VALUE self)
304 {
305  VM_ASSERT(rb_ractor_p(self));
306  rb_ractor_t *r = DATA_PTR(self);
307  return r;
308 }
309 
310 static rb_atomic_t ractor_last_id;
311 
312 #if RACTOR_CHECK_MODE > 0
313 uint32_t
314 rb_ractor_current_id(void)
315 {
316  if (GET_THREAD()->ractor == NULL) {
317  return 1; // main ractor
318  }
319  else {
320  return rb_ractor_id(GET_RACTOR());
321  }
322 }
323 #endif
324 
325 // Ractor queue
326 
327 static void
328 ractor_queue_setup(struct rb_ractor_queue *rq)
329 {
330  rq->size = 2;
331  rq->cnt = 0;
332  rq->start = 0;
333  rq->baskets = malloc(sizeof(struct rb_ractor_basket) * rq->size);
334 }
335 
336 static struct rb_ractor_basket *
337 ractor_queue_head(rb_ractor_t *r, struct rb_ractor_queue *rq)
338 {
339  if (r != NULL) ASSERT_ractor_locking(r);
340  return &rq->baskets[rq->start];
341 }
342 
343 static struct rb_ractor_basket *
344 ractor_queue_at(rb_ractor_t *r, struct rb_ractor_queue *rq, int i)
345 {
346  if (r != NULL) ASSERT_ractor_locking(r);
347  return &rq->baskets[(rq->start + i) % rq->size];
348 }
349 
350 static void
351 ractor_queue_advance(rb_ractor_t *r, struct rb_ractor_queue *rq)
352 {
353  ASSERT_ractor_locking(r);
354 
355  if (rq->reserved_cnt == 0) {
356  rq->cnt--;
357  rq->start = (rq->start + 1) % rq->size;
358  rq->serial++;
359  }
360  else {
361  ractor_queue_at(r, rq, 0)->type.e = basket_type_deleted;
362  }
363 }
364 
365 static bool
366 ractor_queue_skip_p(rb_ractor_t *r, struct rb_ractor_queue *rq, int i)
367 {
368  struct rb_ractor_basket *b = ractor_queue_at(r, rq, i);
369  return basket_type_p(b, basket_type_deleted) ||
370  basket_type_p(b, basket_type_reserved);
371 }
372 
373 static void
374 ractor_queue_compact(rb_ractor_t *r, struct rb_ractor_queue *rq)
375 {
376  ASSERT_ractor_locking(r);
377 
378  while (rq->cnt > 0 && basket_type_p(ractor_queue_at(r, rq, 0), basket_type_deleted)) {
379  ractor_queue_advance(r, rq);
380  }
381 }
382 
383 static bool
384 ractor_queue_empty_p(rb_ractor_t *r, struct rb_ractor_queue *rq)
385 {
386  ASSERT_ractor_locking(r);
387 
388  if (rq->cnt == 0) {
389  return true;
390  }
391 
392  ractor_queue_compact(r, rq);
393 
394  for (int i=0; i<rq->cnt; i++) {
395  if (!ractor_queue_skip_p(r, rq, i)) {
396  return false;
397  }
398  }
399 
400  return true;
401 }
402 
403 static bool
404 ractor_queue_deq(rb_ractor_t *r, struct rb_ractor_queue *rq, struct rb_ractor_basket *basket)
405 {
406  ASSERT_ractor_locking(r);
407 
408  for (int i=0; i<rq->cnt; i++) {
409  if (!ractor_queue_skip_p(r, rq, i)) {
410  struct rb_ractor_basket *b = ractor_queue_at(r, rq, i);
411  *basket = *b;
412 
413  // remove from queue
414  b->type.e = basket_type_deleted;
415  ractor_queue_compact(r, rq);
416  return true;
417  }
418  }
419 
420  return false;
421 }
422 
423 static void
424 ractor_queue_enq(rb_ractor_t *r, struct rb_ractor_queue *rq, struct rb_ractor_basket *basket)
425 {
426  ASSERT_ractor_locking(r);
427 
428  if (rq->size <= rq->cnt) {
429  rq->baskets = realloc(rq->baskets, sizeof(struct rb_ractor_basket) * rq->size * 2);
430  for (int i=rq->size - rq->start; i<rq->cnt; i++) {
431  rq->baskets[i + rq->start] = rq->baskets[i + rq->start - rq->size];
432  }
433  rq->size *= 2;
434  }
435  rq->baskets[(rq->start + rq->cnt++) % rq->size] = *basket;
436  // fprintf(stderr, "%s %p->cnt:%d\n", RUBY_FUNCTION_NAME_STRING, (void *)rq, rq->cnt);
437 }
438 
439 static void
440 ractor_queue_delete(rb_ractor_t *r, struct rb_ractor_queue *rq, struct rb_ractor_basket *basket)
441 {
442  basket->type.e = basket_type_deleted;
443 }
444 
445 // Ractor basket
446 
447 static VALUE ractor_reset_belonging(VALUE obj); // in this file
448 
449 static VALUE
450 ractor_basket_value(struct rb_ractor_basket *b)
451 {
452  switch (b->type.e) {
453  case basket_type_ref:
454  break;
455  case basket_type_copy:
456  case basket_type_move:
457  case basket_type_will:
458  b->type.e = basket_type_ref;
459  b->p.send.v = ractor_reset_belonging(b->p.send.v);
460  break;
461  default:
462  rb_bug("unreachable");
463  }
464 
465  return b->p.send.v;
466 }
467 
468 static VALUE
469 ractor_basket_accept(struct rb_ractor_basket *b)
470 {
471  VALUE v = ractor_basket_value(b);
472 
473  if (b->p.send.exception) {
474  VALUE cause = v;
475  VALUE err = rb_exc_new_cstr(rb_eRactorRemoteError, "thrown by remote Ractor.");
476  rb_ivar_set(err, rb_intern("@ractor"), b->sender);
477  rb_ec_setup_exception(NULL, err, cause);
478  rb_exc_raise(err);
479  }
480 
481  return v;
482 }
483 
484 // Ractor synchronizations
485 
486 #if USE_RUBY_DEBUG_LOG
487 static const char *
488 wait_status_str(enum rb_ractor_wait_status wait_status)
489 {
490  switch ((int)wait_status) {
491  case wait_none: return "none";
492  case wait_receiving: return "receiving";
493  case wait_taking: return "taking";
494  case wait_yielding: return "yielding";
495  case wait_receiving|wait_taking: return "receiving|taking";
496  case wait_receiving|wait_yielding: return "receiving|yielding";
497  case wait_taking|wait_yielding: return "taking|yielding";
498  case wait_receiving|wait_taking|wait_yielding: return "receiving|taking|yielding";
499  }
500  rb_bug("unreachable");
501 }
502 
503 static const char *
504 wakeup_status_str(enum rb_ractor_wakeup_status wakeup_status)
505 {
506  switch (wakeup_status) {
507  case wakeup_none: return "none";
508  case wakeup_by_send: return "by_send";
509  case wakeup_by_yield: return "by_yield";
510  case wakeup_by_take: return "by_take";
511  case wakeup_by_close: return "by_close";
512  case wakeup_by_interrupt: return "by_interrupt";
513  case wakeup_by_retry: return "by_retry";
514  }
515  rb_bug("unreachable");
516 }
517 
518 static const char *
519 basket_type_name(enum rb_ractor_basket_type type)
520 {
521  switch (type) {
522  case basket_type_none: return "none";
523  case basket_type_ref: return "ref";
524  case basket_type_copy: return "copy";
525  case basket_type_move: return "move";
526  case basket_type_will: return "will";
527  case basket_type_deleted: return "deleted";
528  case basket_type_reserved: return "reserved";
529  case basket_type_take_basket: return "take_basket";
530  case basket_type_yielding: return "yielding";
531  }
532  VM_ASSERT(0);
533  return NULL;
534 }
535 #endif // USE_RUBY_DEBUG_LOG
536 
537 static bool
538 ractor_sleeping_by(const rb_ractor_t *r, enum rb_ractor_wait_status wait_status)
539 {
540  return (r->sync.wait.status & wait_status) && r->sync.wait.wakeup_status == wakeup_none;
541 }
542 
543 #ifdef RUBY_THREAD_PTHREAD_H
544 // thread_*.c
545 void rb_ractor_sched_wakeup(rb_ractor_t *r);
546 #else
547 
548 static void
549 rb_ractor_sched_wakeup(rb_ractor_t *r)
550 {
551  rb_native_cond_broadcast(&r->sync.cond);
552 }
553 #endif
554 
555 
556 static bool
557 ractor_wakeup(rb_ractor_t *r, enum rb_ractor_wait_status wait_status, enum rb_ractor_wakeup_status wakeup_status)
558 {
559  ASSERT_ractor_locking(r);
560 
561  RUBY_DEBUG_LOG("r:%u wait_by:%s -> wait:%s wakeup:%s",
562  rb_ractor_id(r),
563  wait_status_str(r->sync.wait.status),
564  wait_status_str(wait_status),
565  wakeup_status_str(wakeup_status));
566 
567  if (ractor_sleeping_by(r, wait_status)) {
568  r->sync.wait.wakeup_status = wakeup_status;
569  rb_ractor_sched_wakeup(r);
570  return true;
571  }
572  else {
573  return false;
574  }
575 }
576 
577 static void
578 ractor_sleep_interrupt(void *ptr)
579 {
580  rb_ractor_t *r = ptr;
581 
582  RACTOR_LOCK(r);
583  {
584  ractor_wakeup(r, wait_receiving | wait_taking | wait_yielding, wakeup_by_interrupt);
585  }
586  RACTOR_UNLOCK(r);
587 }
588 
589 typedef void (*ractor_sleep_cleanup_function)(rb_ractor_t *cr, void *p);
590 
591 static void
592 ractor_check_ints(rb_execution_context_t *ec, rb_ractor_t *cr, ractor_sleep_cleanup_function cf_func, void *cf_data)
593 {
594  if (cr->sync.wait.status != wait_none) {
595  enum rb_ractor_wait_status prev_wait_status = cr->sync.wait.status;
596  cr->sync.wait.status = wait_none;
597  cr->sync.wait.wakeup_status = wakeup_by_interrupt;
598 
599  RACTOR_UNLOCK(cr);
600  {
601  if (cf_func) {
602  enum ruby_tag_type state;
603  EC_PUSH_TAG(ec);
604  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
606  }
607  EC_POP_TAG();
608 
609  if (state) {
610  (*cf_func)(cr, cf_data);
611  EC_JUMP_TAG(ec, state);
612  }
613  }
614  else {
616  }
617  }
618 
619  // reachable?
620  RACTOR_LOCK(cr);
621  cr->sync.wait.status = prev_wait_status;
622  }
623 }
624 
625 #ifdef RUBY_THREAD_PTHREAD_H
626 void rb_ractor_sched_sleep(rb_execution_context_t *ec, rb_ractor_t *cr, rb_unblock_function_t *ubf);
627 #else
628 
629 // win32
630 static void
631 ractor_cond_wait(rb_ractor_t *r)
632 {
633 #if RACTOR_CHECK_MODE > 0
634  VALUE locked_by = r->sync.locked_by;
635  r->sync.locked_by = Qnil;
636 #endif
637  rb_native_cond_wait(&r->sync.cond, &r->sync.lock);
638 
639 #if RACTOR_CHECK_MODE > 0
640  r->sync.locked_by = locked_by;
641 #endif
642 }
643 
644 static void *
645 ractor_sleep_wo_gvl(void *ptr)
646 {
647  rb_ractor_t *cr = ptr;
648  RACTOR_LOCK_SELF(cr);
649  {
650  VM_ASSERT(cr->sync.wait.status != wait_none);
651  if (cr->sync.wait.wakeup_status == wakeup_none) {
652  ractor_cond_wait(cr);
653  }
654  cr->sync.wait.status = wait_none;
655  }
656  RACTOR_UNLOCK_SELF(cr);
657  return NULL;
658 }
659 
660 static void
661 rb_ractor_sched_sleep(rb_execution_context_t *ec, rb_ractor_t *cr, rb_unblock_function_t *ubf)
662 {
663  RACTOR_UNLOCK(cr);
664  {
665  rb_nogvl(ractor_sleep_wo_gvl, cr,
666  ubf, cr,
668  }
669  RACTOR_LOCK(cr);
670 }
671 #endif
672 
673 static enum rb_ractor_wakeup_status
674 ractor_sleep_with_cleanup(rb_execution_context_t *ec, rb_ractor_t *cr, enum rb_ractor_wait_status wait_status,
675  ractor_sleep_cleanup_function cf_func, void *cf_data)
676 {
677  enum rb_ractor_wakeup_status wakeup_status;
678  VM_ASSERT(GET_RACTOR() == cr);
679 
680  // TODO: multi-threads
681  VM_ASSERT(cr->sync.wait.status == wait_none);
682  VM_ASSERT(wait_status != wait_none);
683  cr->sync.wait.status = wait_status;
684  cr->sync.wait.wakeup_status = wakeup_none;
685 
686  // fprintf(stderr, "%s r:%p status:%s, wakeup_status:%s\n", RUBY_FUNCTION_NAME_STRING, (void *)cr,
687  // wait_status_str(cr->sync.wait.status), wakeup_status_str(cr->sync.wait.wakeup_status));
688 
689  RUBY_DEBUG_LOG("sleep by %s", wait_status_str(wait_status));
690 
691  while (cr->sync.wait.wakeup_status == wakeup_none) {
692  rb_ractor_sched_sleep(ec, cr, ractor_sleep_interrupt);
693  ractor_check_ints(ec, cr, cf_func, cf_data);
694  }
695 
696  cr->sync.wait.status = wait_none;
697 
698  // TODO: multi-thread
699  wakeup_status = cr->sync.wait.wakeup_status;
700  cr->sync.wait.wakeup_status = wakeup_none;
701 
702  RUBY_DEBUG_LOG("wakeup %s", wakeup_status_str(wakeup_status));
703 
704  return wakeup_status;
705 }
706 
707 static enum rb_ractor_wakeup_status
708 ractor_sleep(rb_execution_context_t *ec, rb_ractor_t *cr, enum rb_ractor_wait_status wait_status)
709 {
710  return ractor_sleep_with_cleanup(ec, cr, wait_status, 0, NULL);
711 }
712 
713 // Ractor.receive
714 
715 static void
716 ractor_recursive_receive_if(rb_ractor_t *r)
717 {
718  if (r->receiving_mutex && rb_mutex_owned_p(r->receiving_mutex)) {
719  rb_raise(rb_eRactorError, "can not call receive/receive_if recursively");
720  }
721 }
722 
723 static VALUE
724 ractor_try_receive(rb_execution_context_t *ec, rb_ractor_t *cr, struct rb_ractor_queue *rq)
725 {
726  struct rb_ractor_basket basket;
727  ractor_recursive_receive_if(cr);
728  bool received = false;
729 
730  RACTOR_LOCK_SELF(cr);
731  {
732  RUBY_DEBUG_LOG("rq->cnt:%d", rq->cnt);
733  received = ractor_queue_deq(cr, rq, &basket);
734  }
735  RACTOR_UNLOCK_SELF(cr);
736 
737  if (!received) {
738  if (cr->sync.incoming_port_closed) {
739  rb_raise(rb_eRactorClosedError, "The incoming port is already closed");
740  }
741  return Qundef;
742  }
743  else {
744  return ractor_basket_accept(&basket);
745  }
746 }
747 
748 static void
749 ractor_wait_receive(rb_execution_context_t *ec, rb_ractor_t *cr, struct rb_ractor_queue *rq)
750 {
751  VM_ASSERT(cr == rb_ec_ractor_ptr(ec));
752  ractor_recursive_receive_if(cr);
753 
754  RACTOR_LOCK(cr);
755  {
756  while (ractor_queue_empty_p(cr, rq)) {
757  ractor_sleep(ec, cr, wait_receiving);
758  }
759  }
760  RACTOR_UNLOCK(cr);
761 }
762 
763 static VALUE
764 ractor_receive(rb_execution_context_t *ec, rb_ractor_t *cr)
765 {
766  VM_ASSERT(cr == rb_ec_ractor_ptr(ec));
767  VALUE v;
768  struct rb_ractor_queue *rq = &cr->sync.recv_queue;
769 
770  while (UNDEF_P(v = ractor_try_receive(ec, cr, rq))) {
771  ractor_wait_receive(ec, cr, rq);
772  }
773 
774  return v;
775 }
776 
777 #if 0
778 static void
779 rq_dump(struct rb_ractor_queue *rq)
780 {
781  bool bug = false;
782  for (int i=0; i<rq->cnt; i++) {
783  struct rb_ractor_basket *b = ractor_queue_at(NULL, rq, i);
784  fprintf(stderr, "%d (start:%d) type:%s %p %s\n", i, rq->start, basket_type_name(b->type),
785  (void *)b, RSTRING_PTR(RARRAY_AREF(b->v, 1)));
786  if (basket_type_p(b, basket_type_reserved) bug = true;
787  }
788  if (bug) rb_bug("!!");
789 }
790 #endif
791 
793  rb_ractor_t *cr;
794  struct rb_ractor_queue *rq;
795  VALUE v;
796  int index;
797  bool success;
798 };
799 
800 static void
801 ractor_receive_if_lock(rb_ractor_t *cr)
802 {
803  VALUE m = cr->receiving_mutex;
804  if (m == Qfalse) {
805  m = cr->receiving_mutex = rb_mutex_new();
806  }
807  rb_mutex_lock(m);
808 }
809 
810 static VALUE
811 receive_if_body(VALUE ptr)
812 {
813  struct receive_block_data *data = (struct receive_block_data *)ptr;
814 
815  ractor_receive_if_lock(data->cr);
816  VALUE block_result = rb_yield(data->v);
817  rb_ractor_t *cr = data->cr;
818 
819  RACTOR_LOCK_SELF(cr);
820  {
821  struct rb_ractor_basket *b = ractor_queue_at(cr, data->rq, data->index);
822  VM_ASSERT(basket_type_p(b, basket_type_reserved));
823  data->rq->reserved_cnt--;
824 
825  if (RTEST(block_result)) {
826  ractor_queue_delete(cr, data->rq, b);
827  ractor_queue_compact(cr, data->rq);
828  }
829  else {
830  b->type.e = basket_type_ref;
831  }
832  }
833  RACTOR_UNLOCK_SELF(cr);
834 
835  data->success = true;
836 
837  if (RTEST(block_result)) {
838  return data->v;
839  }
840  else {
841  return Qundef;
842  }
843 }
844 
845 static VALUE
846 receive_if_ensure(VALUE v)
847 {
848  struct receive_block_data *data = (struct receive_block_data *)v;
849  rb_ractor_t *cr = data->cr;
850 
851  if (!data->success) {
852  RACTOR_LOCK_SELF(cr);
853  {
854  struct rb_ractor_basket *b = ractor_queue_at(cr, data->rq, data->index);
855  VM_ASSERT(basket_type_p(b, basket_type_reserved));
856  b->type.e = basket_type_deleted;
857  data->rq->reserved_cnt--;
858  }
859  RACTOR_UNLOCK_SELF(cr);
860  }
861 
862  rb_mutex_unlock(cr->receiving_mutex);
863  return Qnil;
864 }
865 
866 static VALUE
867 ractor_receive_if(rb_execution_context_t *ec, VALUE crv, VALUE b)
868 {
869  if (!RTEST(b)) rb_raise(rb_eArgError, "no block given");
870 
871  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
872  unsigned int serial = (unsigned int)-1;
873  int index = 0;
874  struct rb_ractor_queue *rq = &cr->sync.recv_queue;
875 
876  while (1) {
877  VALUE v = Qundef;
878 
879  ractor_wait_receive(ec, cr, rq);
880 
881  RACTOR_LOCK_SELF(cr);
882  {
883  if (serial != rq->serial) {
884  serial = rq->serial;
885  index = 0;
886  }
887 
888  // check newer version
889  for (int i=index; i<rq->cnt; i++) {
890  if (!ractor_queue_skip_p(cr, rq, i)) {
891  struct rb_ractor_basket *b = ractor_queue_at(cr, rq, i);
892  v = ractor_basket_value(b);
893  b->type.e = basket_type_reserved;
894  rq->reserved_cnt++;
895  index = i;
896  break;
897  }
898  }
899  }
900  RACTOR_UNLOCK_SELF(cr);
901 
902  if (!UNDEF_P(v)) {
903  struct receive_block_data data = {
904  .cr = cr,
905  .rq = rq,
906  .v = v,
907  .index = index,
908  .success = false,
909  };
910 
911  VALUE result = rb_ensure(receive_if_body, (VALUE)&data,
912  receive_if_ensure, (VALUE)&data);
913 
914  if (!UNDEF_P(result)) return result;
915  index++;
916  }
917 
918  RUBY_VM_CHECK_INTS(ec);
919  }
920 }
921 
922 static void
923 ractor_send_basket(rb_execution_context_t *ec, rb_ractor_t *r, struct rb_ractor_basket *b)
924 {
925  bool closed = false;
926 
927  RACTOR_LOCK(r);
928  {
929  if (r->sync.incoming_port_closed) {
930  closed = true;
931  }
932  else {
933  ractor_queue_enq(r, &r->sync.recv_queue, b);
934  ractor_wakeup(r, wait_receiving, wakeup_by_send);
935  }
936  }
937  RACTOR_UNLOCK(r);
938 
939  if (closed) {
940  rb_raise(rb_eRactorClosedError, "The incoming-port is already closed");
941  }
942 }
943 
944 // Ractor#send
945 
946 static VALUE ractor_move(VALUE obj); // in this file
947 static VALUE ractor_copy(VALUE obj); // in this file
948 
949 static void
950 ractor_basket_prepare_contents(VALUE obj, VALUE move, volatile VALUE *pobj, enum rb_ractor_basket_type *ptype)
951 {
952  VALUE v;
953  enum rb_ractor_basket_type type;
954 
955  if (rb_ractor_shareable_p(obj)) {
956  type = basket_type_ref;
957  v = obj;
958  }
959  else if (!RTEST(move)) {
960  v = ractor_copy(obj);
961  type = basket_type_copy;
962  }
963  else {
964  type = basket_type_move;
965  v = ractor_move(obj);
966  }
967 
968  *pobj = v;
969  *ptype = type;
970 }
971 
972 static void
973 ractor_basket_fill_(rb_ractor_t *cr, struct rb_ractor_basket *basket, VALUE obj, bool exc)
974 {
975  VM_ASSERT(cr == GET_RACTOR());
976 
977  basket->sender = cr->pub.self;
978  basket->p.send.exception = exc;
979  basket->p.send.v = obj;
980 }
981 
982 static void
983 ractor_basket_fill(rb_ractor_t *cr, struct rb_ractor_basket *basket, VALUE obj, VALUE move, bool exc)
984 {
985  VALUE v;
986  enum rb_ractor_basket_type type;
987  ractor_basket_prepare_contents(obj, move, &v, &type);
988  ractor_basket_fill_(cr, basket, v, exc);
989  basket->type.e = type;
990 }
991 
992 static void
993 ractor_basket_fill_will(rb_ractor_t *cr, struct rb_ractor_basket *basket, VALUE obj, bool exc)
994 {
995  ractor_basket_fill_(cr, basket, obj, exc);
996  basket->type.e = basket_type_will;
997 }
998 
999 static VALUE
1000 ractor_send(rb_execution_context_t *ec, rb_ractor_t *r, VALUE obj, VALUE move)
1001 {
1002  struct rb_ractor_basket basket;
1003  // TODO: Ractor local GC
1004  ractor_basket_fill(rb_ec_ractor_ptr(ec), &basket, obj, move, false);
1005  ractor_send_basket(ec, r, &basket);
1006  return r->pub.self;
1007 }
1008 
1009 // Ractor#take
1010 
1011 static bool
1012 ractor_take_has_will(rb_ractor_t *r)
1013 {
1014  ASSERT_ractor_locking(r);
1015 
1016  return basket_type_p(&r->sync.will_basket, basket_type_will);
1017 }
1018 
1019 static bool
1020 ractor_take_will(rb_ractor_t *r, struct rb_ractor_basket *b)
1021 {
1022  ASSERT_ractor_locking(r);
1023 
1024  if (ractor_take_has_will(r)) {
1025  *b = r->sync.will_basket;
1026  r->sync.will_basket.type.e = basket_type_none;
1027  return true;
1028  }
1029  else {
1030  VM_ASSERT(basket_type_p(&r->sync.will_basket, basket_type_none));
1031  return false;
1032  }
1033 }
1034 
1035 static bool
1036 ractor_take_will_lock(rb_ractor_t *r, struct rb_ractor_basket *b)
1037 {
1038  ASSERT_ractor_unlocking(r);
1039  bool taken;
1040 
1041  RACTOR_LOCK(r);
1042  {
1043  taken = ractor_take_will(r, b);
1044  }
1045  RACTOR_UNLOCK(r);
1046 
1047  return taken;
1048 }
1049 
1050 static bool
1051 ractor_register_take(rb_ractor_t *cr, rb_ractor_t *r, struct rb_ractor_basket *take_basket,
1052  bool is_take, struct rb_ractor_selector_take_config *config, bool ignore_error)
1053 {
1054  struct rb_ractor_basket b = {
1055  .type.e = basket_type_take_basket,
1056  .sender = cr->pub.self,
1057  .p = {
1058  .take = {
1059  .basket = take_basket,
1060  .config = config,
1061  },
1062  },
1063  };
1064  bool closed = false;
1065 
1066  RACTOR_LOCK(r);
1067  {
1068  if (is_take && ractor_take_will(r, take_basket)) {
1069  RUBY_DEBUG_LOG("take over a will of r:%d", rb_ractor_id(r));
1070  }
1071  else if (!is_take && ractor_take_has_will(r)) {
1072  RUBY_DEBUG_LOG("has_will");
1073  VM_ASSERT(config != NULL);
1074  config->closed = true;
1075  }
1076  else if (r->sync.outgoing_port_closed) {
1077  closed = true;
1078  }
1079  else {
1080  RUBY_DEBUG_LOG("register in r:%d", rb_ractor_id(r));
1081  ractor_queue_enq(r, &r->sync.takers_queue, &b);
1082 
1083  if (basket_none_p(take_basket)) {
1084  ractor_wakeup(r, wait_yielding, wakeup_by_take);
1085  }
1086  }
1087  }
1088  RACTOR_UNLOCK(r);
1089 
1090  if (closed) {
1091  if (!ignore_error) rb_raise(rb_eRactorClosedError, "The outgoing-port is already closed");
1092  return false;
1093  }
1094  else {
1095  return true;
1096  }
1097 }
1098 
1099 static bool
1100 ractor_deregister_take(rb_ractor_t *r, struct rb_ractor_basket *take_basket)
1101 {
1102  struct rb_ractor_queue *ts = &r->sync.takers_queue;
1103  bool deleted = false;
1104 
1105  RACTOR_LOCK(r);
1106  {
1107  if (r->sync.outgoing_port_closed) {
1108  // ok
1109  }
1110  else {
1111  for (int i=0; i<ts->cnt; i++) {
1112  struct rb_ractor_basket *b = ractor_queue_at(r, ts, i);
1113  if (basket_type_p(b, basket_type_take_basket) && b->p.take.basket == take_basket) {
1114  ractor_queue_delete(r, ts, b);
1115  deleted = true;
1116  }
1117  }
1118  if (deleted) {
1119  ractor_queue_compact(r, ts);
1120  }
1121  }
1122  }
1123  RACTOR_UNLOCK(r);
1124 
1125  return deleted;
1126 }
1127 
1128 static VALUE
1129 ractor_try_take(rb_ractor_t *cr, rb_ractor_t *r, struct rb_ractor_basket *take_basket)
1130 {
1131  bool taken;
1132 
1133  RACTOR_LOCK_SELF(cr);
1134  {
1135  if (basket_none_p(take_basket) || basket_type_p(take_basket, basket_type_yielding)) {
1136  taken = false;
1137  }
1138  else {
1139  taken = true;
1140  }
1141  }
1142  RACTOR_UNLOCK_SELF(cr);
1143 
1144  if (taken) {
1145  RUBY_DEBUG_LOG("taken");
1146  if (basket_type_p(take_basket, basket_type_deleted)) {
1147  VM_ASSERT(r->sync.outgoing_port_closed);
1148  rb_raise(rb_eRactorClosedError, "The outgoing-port is already closed");
1149  }
1150  return ractor_basket_accept(take_basket);
1151  }
1152  else {
1153  RUBY_DEBUG_LOG("not taken");
1154  return Qundef;
1155  }
1156 }
1157 
1158 
1159 #if VM_CHECK_MODE > 0
1160 static bool
1161 ractor_check_specific_take_basket_lock(rb_ractor_t *r, struct rb_ractor_basket *tb)
1162 {
1163  bool ret = false;
1164  struct rb_ractor_queue *ts = &r->sync.takers_queue;
1165 
1166  RACTOR_LOCK(r);
1167  {
1168  for (int i=0; i<ts->cnt; i++) {
1169  struct rb_ractor_basket *b = ractor_queue_at(r, ts, i);
1170  if (basket_type_p(b, basket_type_take_basket) && b->p.take.basket == tb) {
1171  ret = true;
1172  break;
1173  }
1174  }
1175  }
1176  RACTOR_UNLOCK(r);
1177 
1178  return ret;
1179 }
1180 #endif
1181 
1182 static void
1183 ractor_take_cleanup(rb_ractor_t *cr, rb_ractor_t *r, struct rb_ractor_basket *tb)
1184 {
1185  retry:
1186  if (basket_none_p(tb)) { // not yielded yet
1187  if (!ractor_deregister_take(r, tb)) {
1188  // not in r's takers queue
1189  rb_thread_sleep(0);
1190  goto retry;
1191  }
1192  }
1193  else {
1194  VM_ASSERT(!ractor_check_specific_take_basket_lock(r, tb));
1195  }
1196 }
1197 
1199  rb_ractor_t *r;
1200  struct rb_ractor_basket *tb;
1201 };
1202 
1203 static void
1204 ractor_wait_take_cleanup(rb_ractor_t *cr, void *ptr)
1205 {
1207  ractor_take_cleanup(cr, data->r, data->tb);
1208 }
1209 
1210 static void
1211 ractor_wait_take(rb_execution_context_t *ec, rb_ractor_t *cr, rb_ractor_t *r, struct rb_ractor_basket *take_basket)
1212 {
1213  struct take_wait_take_cleanup_data data = {
1214  .r = r,
1215  .tb = take_basket,
1216  };
1217 
1218  RACTOR_LOCK_SELF(cr);
1219  {
1220  if (basket_none_p(take_basket) || basket_type_p(take_basket, basket_type_yielding)) {
1221  ractor_sleep_with_cleanup(ec, cr, wait_taking, ractor_wait_take_cleanup, &data);
1222  }
1223  }
1224  RACTOR_UNLOCK_SELF(cr);
1225 }
1226 
1227 static VALUE
1228 ractor_take(rb_execution_context_t *ec, rb_ractor_t *r)
1229 {
1230  RUBY_DEBUG_LOG("from r:%u", rb_ractor_id(r));
1231  VALUE v;
1232  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1233 
1234  struct rb_ractor_basket take_basket = {
1235  .type.e = basket_type_none,
1236  .sender = 0,
1237  };
1238 
1239  ractor_register_take(cr, r, &take_basket, true, NULL, false);
1240 
1241  while (UNDEF_P(v = ractor_try_take(cr, r, &take_basket))) {
1242  ractor_wait_take(ec, cr, r, &take_basket);
1243  }
1244 
1245  VM_ASSERT(!basket_none_p(&take_basket));
1246  VM_ASSERT(!ractor_check_specific_take_basket_lock(r, &take_basket));
1247 
1248  return v;
1249 }
1250 
1251 // Ractor.yield
1252 
1253 static bool
1254 ractor_check_take_basket(rb_ractor_t *cr, struct rb_ractor_queue *rs)
1255 {
1256  ASSERT_ractor_locking(cr);
1257 
1258  for (int i=0; i<rs->cnt; i++) {
1259  struct rb_ractor_basket *b = ractor_queue_at(cr, rs, i);
1260  if (basket_type_p(b, basket_type_take_basket) &&
1261  basket_none_p(b->p.take.basket)) {
1262  return true;
1263  }
1264  }
1265 
1266  return false;
1267 }
1268 
1269 static bool
1270 ractor_deq_take_basket(rb_ractor_t *cr, struct rb_ractor_queue *rs, struct rb_ractor_basket *b)
1271 {
1272  ASSERT_ractor_unlocking(cr);
1273  struct rb_ractor_basket *first_tb = NULL;
1274  bool found = false;
1275 
1276  RACTOR_LOCK_SELF(cr);
1277  {
1278  while (ractor_queue_deq(cr, rs, b)) {
1279  if (basket_type_p(b, basket_type_take_basket)) {
1280  struct rb_ractor_basket *tb = b->p.take.basket;
1281 
1282  if (RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_yielding) == basket_type_none) {
1283  found = true;
1284  break;
1285  }
1286  else {
1287  ractor_queue_enq(cr, rs, b);
1288  if (first_tb == NULL) first_tb = tb;
1289  struct rb_ractor_basket *head = ractor_queue_head(cr, rs);
1290  VM_ASSERT(head != NULL);
1291  if (basket_type_p(head, basket_type_take_basket) && head->p.take.basket == first_tb) {
1292  break; // loop detected
1293  }
1294  }
1295  }
1296  else {
1297  VM_ASSERT(basket_none_p(b));
1298  }
1299  }
1300 
1301  if (found && b->p.take.config && !b->p.take.config->oneshot) {
1302  ractor_queue_enq(cr, rs, b);
1303  }
1304  }
1305  RACTOR_UNLOCK_SELF(cr);
1306 
1307  return found;
1308 }
1309 
1310 static bool
1311 ractor_try_yield(rb_execution_context_t *ec, rb_ractor_t *cr, struct rb_ractor_queue *ts, volatile VALUE obj, VALUE move, bool exc, bool is_will)
1312 {
1313  ASSERT_ractor_unlocking(cr);
1314 
1315  struct rb_ractor_basket b;
1316 
1317  if (ractor_deq_take_basket(cr, ts, &b)) {
1318  VM_ASSERT(basket_type_p(&b, basket_type_take_basket));
1319  VM_ASSERT(basket_type_p(b.p.take.basket, basket_type_yielding));
1320 
1321  rb_ractor_t *tr = RACTOR_PTR(b.sender);
1322  struct rb_ractor_basket *tb = b.p.take.basket;
1323  enum rb_ractor_basket_type type;
1324 
1325  RUBY_DEBUG_LOG("basket from r:%u", rb_ractor_id(tr));
1326 
1327  if (is_will) {
1328  type = basket_type_will;
1329  }
1330  else {
1331  enum ruby_tag_type state;
1332 
1333  // begin
1334  EC_PUSH_TAG(ec);
1335  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1336  // TODO: Ractor local GC
1337  ractor_basket_prepare_contents(obj, move, &obj, &type);
1338  }
1339  EC_POP_TAG();
1340  // rescue
1341  if (state) {
1342  RACTOR_LOCK_SELF(cr);
1343  {
1344  b.p.take.basket->type.e = basket_type_none;
1345  ractor_queue_enq(cr, ts, &b);
1346  }
1347  RACTOR_UNLOCK_SELF(cr);
1348  EC_JUMP_TAG(ec, state);
1349  }
1350  }
1351 
1352  RACTOR_LOCK(tr);
1353  {
1354  VM_ASSERT(basket_type_p(tb, basket_type_yielding));
1355  // fill atomic
1356  RUBY_DEBUG_LOG("fill %sbasket from r:%u", is_will ? "will " : "", rb_ractor_id(tr));
1357  ractor_basket_fill_(cr, tb, obj, exc);
1358  if (RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_yielding, type) != basket_type_yielding) {
1359  rb_bug("unreachable");
1360  }
1361  ractor_wakeup(tr, wait_taking, wakeup_by_yield);
1362  }
1363  RACTOR_UNLOCK(tr);
1364 
1365  return true;
1366  }
1367  else {
1368  RUBY_DEBUG_LOG("no take basket");
1369  return false;
1370  }
1371 }
1372 
1373 static void
1374 ractor_wait_yield(rb_execution_context_t *ec, rb_ractor_t *cr, struct rb_ractor_queue *ts)
1375 {
1376  RACTOR_LOCK_SELF(cr);
1377  {
1378  while (!ractor_check_take_basket(cr, ts)) {
1379  ractor_sleep(ec, cr, wait_yielding);
1380  }
1381  }
1382  RACTOR_UNLOCK_SELF(cr);
1383 }
1384 
1385 static VALUE
1386 ractor_yield(rb_execution_context_t *ec, rb_ractor_t *cr, VALUE obj, VALUE move)
1387 {
1388  struct rb_ractor_queue *ts = &cr->sync.takers_queue;
1389 
1390  while (!ractor_try_yield(ec, cr, ts, obj, move, false, false)) {
1391  ractor_wait_yield(ec, cr, ts);
1392  }
1393 
1394  return Qnil;
1395 }
1396 
1397 // Ractor::Selector
1398 
1400  rb_ractor_t *r;
1401  struct rb_ractor_basket take_basket;
1402  st_table *take_ractors; // rb_ractor_t * => (struct rb_ractor_selector_take_config *)
1403 };
1404 
1405 static int
1406 ractor_selector_mark_ractors_i(st_data_t key, st_data_t value, st_data_t data)
1407 {
1408  const rb_ractor_t *r = (rb_ractor_t *)key;
1409  rb_gc_mark(r->pub.self);
1410  return ST_CONTINUE;
1411 }
1412 
1413 static void
1414 ractor_selector_mark(void *ptr)
1415 {
1416  struct rb_ractor_selector *s = ptr;
1417 
1418  if (s->take_ractors) {
1419  st_foreach(s->take_ractors, ractor_selector_mark_ractors_i, 0);
1420  }
1421 
1422  switch (s->take_basket.type.e) {
1423  case basket_type_ref:
1424  case basket_type_copy:
1425  case basket_type_move:
1426  case basket_type_will:
1427  rb_gc_mark(s->take_basket.sender);
1428  rb_gc_mark(s->take_basket.p.send.v);
1429  break;
1430  default:
1431  break;
1432  }
1433 }
1434 
1435 static int
1436 ractor_selector_release_i(st_data_t key, st_data_t val, st_data_t data)
1437 {
1438  struct rb_ractor_selector *s = (struct rb_ractor_selector *)data;
1439  struct rb_ractor_selector_take_config *config = (struct rb_ractor_selector_take_config *)val;
1440 
1441  if (!config->closed) {
1442  ractor_deregister_take((rb_ractor_t *)key, &s->take_basket);
1443  }
1444  free(config);
1445  return ST_CONTINUE;
1446 }
1447 
1448 static void
1449 ractor_selector_free(void *ptr)
1450 {
1451  struct rb_ractor_selector *s = ptr;
1452  st_foreach(s->take_ractors, ractor_selector_release_i, (st_data_t)s);
1453  st_free_table(s->take_ractors);
1454  ruby_xfree(ptr);
1455 }
1456 
1457 static size_t
1458 ractor_selector_memsize(const void *ptr)
1459 {
1460  const struct rb_ractor_selector *s = ptr;
1461  return sizeof(struct rb_ractor_selector) +
1462  st_memsize(s->take_ractors) +
1463  s->take_ractors->num_entries * sizeof(struct rb_ractor_selector_take_config);
1464 }
1465 
1466 static const rb_data_type_t ractor_selector_data_type = {
1467  "ractor/selector",
1468  {
1469  ractor_selector_mark,
1470  ractor_selector_free,
1471  ractor_selector_memsize,
1472  NULL, // update
1473  },
1474  0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
1475 };
1476 
1477 static struct rb_ractor_selector *
1478 RACTOR_SELECTOR_PTR(VALUE selv)
1479 {
1480  VM_ASSERT(rb_typeddata_is_kind_of(selv, &ractor_selector_data_type));
1481 
1482  return (struct rb_ractor_selector *)DATA_PTR(selv);
1483 }
1484 
1485 // Ractor::Selector.new
1486 
1487 static VALUE
1488 ractor_selector_create(VALUE klass)
1489 {
1490  struct rb_ractor_selector *s;
1491  VALUE selv = TypedData_Make_Struct(klass, struct rb_ractor_selector, &ractor_selector_data_type, s);
1492  s->take_basket.type.e = basket_type_reserved;
1493  s->take_ractors = st_init_numtable(); // ractor (ptr) -> take_config
1494  return selv;
1495 }
1496 
1497 // Ractor::Selector#add(r)
1498 
1499 static VALUE
1500 ractor_selector_add(VALUE selv, VALUE rv)
1501 {
1502  if (!rb_ractor_p(rv)) {
1503  rb_raise(rb_eArgError, "Not a ractor object");
1504  }
1505 
1506  rb_ractor_t *r = RACTOR_PTR(rv);
1507  struct rb_ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1508 
1509  if (st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1510  rb_raise(rb_eArgError, "already added");
1511  }
1512 
1513  struct rb_ractor_selector_take_config *config = malloc(sizeof(struct rb_ractor_selector_take_config));
1514  VM_ASSERT(config != NULL);
1515  config->closed = false;
1516  config->oneshot = false;
1517 
1518  if (ractor_register_take(GET_RACTOR(), r, &s->take_basket, false, config, true)) {
1519  st_insert(s->take_ractors, (st_data_t)r, (st_data_t)config);
1520  }
1521 
1522  return rv;
1523 }
1524 
1525 // Ractor::Selector#remove(r)
1526 
1527 static VALUE
1528 ractor_selector_remove(VALUE selv, VALUE rv)
1529 {
1530  if (!rb_ractor_p(rv)) {
1531  rb_raise(rb_eArgError, "Not a ractor object");
1532  }
1533 
1534  rb_ractor_t *r = RACTOR_PTR(rv);
1535  struct rb_ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1536 
1537  RUBY_DEBUG_LOG("r:%u", rb_ractor_id(r));
1538 
1539  if (!st_lookup(s->take_ractors, (st_data_t)r, NULL)) {
1540  rb_raise(rb_eArgError, "not added yet");
1541  }
1542 
1543  ractor_deregister_take(r, &s->take_basket);
1544  struct rb_ractor_selector_take_config *config;
1545  st_delete(s->take_ractors, (st_data_t *)&r, (st_data_t *)&config);
1546  free(config);
1547 
1548  return rv;
1549 }
1550 
1551 // Ractor::Selector#clear
1552 
1554  VALUE selv;
1556 };
1557 
1558 static int
1559 ractor_selector_clear_i(st_data_t key, st_data_t val, st_data_t data)
1560 {
1561  VALUE selv = (VALUE)data;
1562  rb_ractor_t *r = (rb_ractor_t *)key;
1563  ractor_selector_remove(selv, r->pub.self);
1564  return ST_CONTINUE;
1565 }
1566 
1567 static VALUE
1568 ractor_selector_clear(VALUE selv)
1569 {
1570  struct rb_ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1571 
1572  st_foreach(s->take_ractors, ractor_selector_clear_i, (st_data_t)selv);
1573  st_clear(s->take_ractors);
1574  return selv;
1575 }
1576 
1577 static VALUE
1578 ractor_selector_empty_p(VALUE selv)
1579 {
1580  struct rb_ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1581  return s->take_ractors->num_entries == 0 ? Qtrue : Qfalse;
1582 }
1583 
1584 static int
1585 ractor_selector_wait_i(st_data_t key, st_data_t val, st_data_t dat)
1586 {
1587  rb_ractor_t *r = (rb_ractor_t *)key;
1588  struct rb_ractor_basket *tb = (struct rb_ractor_basket *)dat;
1589  int ret;
1590 
1591  if (!basket_none_p(tb)) {
1592  RUBY_DEBUG_LOG("already taken:%s", basket_type_name(tb->type.e));
1593  return ST_STOP;
1594  }
1595 
1596  RACTOR_LOCK(r);
1597  {
1598  if (basket_type_p(&r->sync.will_basket, basket_type_will)) {
1599  RUBY_DEBUG_LOG("r:%u has will", rb_ractor_id(r));
1600 
1601  if (RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_will) == basket_type_none) {
1602  ractor_take_will(r, tb);
1603  ret = ST_STOP;
1604  }
1605  else {
1606  RUBY_DEBUG_LOG("has will, but already taken (%s)", basket_type_name(tb->type.e));
1607  ret = ST_CONTINUE;
1608  }
1609  }
1610  else if (r->sync.outgoing_port_closed) {
1611  RUBY_DEBUG_LOG("r:%u is closed", rb_ractor_id(r));
1612 
1613  if (RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_deleted) == basket_type_none) {
1614  tb->sender = r->pub.self;
1615  ret = ST_STOP;
1616  }
1617  else {
1618  RUBY_DEBUG_LOG("closed, but already taken (%s)", basket_type_name(tb->type.e));
1619  ret = ST_CONTINUE;
1620  }
1621  }
1622  else {
1623  RUBY_DEBUG_LOG("wakeup r:%u", rb_ractor_id(r));
1624  ractor_wakeup(r, wait_yielding, wakeup_by_take);
1625  ret = ST_CONTINUE;
1626  }
1627  }
1628  RACTOR_UNLOCK(r);
1629 
1630  return ret;
1631 }
1632 
1633 // Ractor::Selector#wait
1634 
1635 static void
1636 ractor_selector_wait_cleaup(rb_ractor_t *cr, void *ptr)
1637 {
1638  struct rb_ractor_basket *tb = (struct rb_ractor_basket *)ptr;
1639 
1640  RACTOR_LOCK_SELF(cr);
1641  {
1642  while (basket_type_p(tb, basket_type_yielding)) rb_thread_sleep(0);
1643  // if tb->type is not none, taking is succeeded, but interruption ignore it unfortunately.
1644  tb->type.e = basket_type_reserved;
1645  }
1646  RACTOR_UNLOCK_SELF(cr);
1647 }
1648 
1649 static VALUE
1650 ractor_selector__wait(VALUE selv, VALUE do_receivev, VALUE do_yieldv, VALUE yield_value, VALUE move)
1651 {
1652  rb_execution_context_t *ec = GET_EC();
1653  struct rb_ractor_selector *s = RACTOR_SELECTOR_PTR(selv);
1654  struct rb_ractor_basket *tb = &s->take_basket;
1655  struct rb_ractor_basket taken_basket;
1656  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
1657  bool do_receive = !!RTEST(do_receivev);
1658  bool do_yield = !!RTEST(do_yieldv);
1659  VALUE ret_v, ret_r;
1660  enum rb_ractor_wait_status wait_status;
1661  struct rb_ractor_queue *rq = &cr->sync.recv_queue;
1662  struct rb_ractor_queue *ts = &cr->sync.takers_queue;
1663 
1664  RUBY_DEBUG_LOG("start");
1665 
1666  retry:
1667  RUBY_DEBUG_LOG("takers:%ld", s->take_ractors->num_entries);
1668 
1669  // setup wait_status
1670  wait_status = wait_none;
1671  if (s->take_ractors->num_entries > 0) wait_status |= wait_taking;
1672  if (do_receive) wait_status |= wait_receiving;
1673  if (do_yield) wait_status |= wait_yielding;
1674 
1675  RUBY_DEBUG_LOG("wait:%s", wait_status_str(wait_status));
1676 
1677  if (wait_status == wait_none) {
1678  rb_raise(rb_eRactorError, "no taking ractors");
1679  }
1680 
1681  // check recv_queue
1682  if (do_receive && !UNDEF_P(ret_v = ractor_try_receive(ec, cr, rq))) {
1683  ret_r = ID2SYM(rb_intern("receive"));
1684  goto success;
1685  }
1686 
1687  // check takers
1688  if (do_yield && ractor_try_yield(ec, cr, ts, yield_value, move, false, false)) {
1689  ret_v = Qnil;
1690  ret_r = ID2SYM(rb_intern("yield"));
1691  goto success;
1692  }
1693 
1694  // check take_basket
1695  VM_ASSERT(basket_type_p(&s->take_basket, basket_type_reserved));
1696  s->take_basket.type.e = basket_type_none;
1697  // kick all take target ractors
1698  st_foreach(s->take_ractors, ractor_selector_wait_i, (st_data_t)tb);
1699 
1700  RACTOR_LOCK_SELF(cr);
1701  {
1702  retry_waiting:
1703  while (1) {
1704  if (!basket_none_p(tb)) {
1705  RUBY_DEBUG_LOG("taken:%s from r:%u", basket_type_name(tb->type.e),
1706  tb->sender ? rb_ractor_id(RACTOR_PTR(tb->sender)) : 0);
1707  break;
1708  }
1709  if (do_receive && !ractor_queue_empty_p(cr, rq)) {
1710  RUBY_DEBUG_LOG("can receive (%d)", rq->cnt);
1711  break;
1712  }
1713  if (do_yield && ractor_check_take_basket(cr, ts)) {
1714  RUBY_DEBUG_LOG("can yield");
1715  break;
1716  }
1717 
1718  ractor_sleep_with_cleanup(ec, cr, wait_status, ractor_selector_wait_cleaup, tb);
1719  }
1720 
1721  taken_basket = *tb;
1722 
1723  // ensure
1724  // tb->type.e = basket_type_reserved # do it atomic in the following code
1725  if (taken_basket.type.e == basket_type_yielding ||
1726  RUBY_ATOMIC_CAS(tb->type.atomic, taken_basket.type.e, basket_type_reserved) != taken_basket.type.e) {
1727 
1728  if (basket_type_p(tb, basket_type_yielding)) {
1729  RACTOR_UNLOCK_SELF(cr);
1730  {
1731  rb_thread_sleep(0);
1732  }
1733  RACTOR_LOCK_SELF(cr);
1734  }
1735  goto retry_waiting;
1736  }
1737  }
1738  RACTOR_UNLOCK_SELF(cr);
1739 
1740  // check the taken resutl
1741  switch (taken_basket.type.e) {
1742  case basket_type_none:
1743  VM_ASSERT(do_receive || do_yield);
1744  goto retry;
1745  case basket_type_yielding:
1746  rb_bug("unreachable");
1747  case basket_type_deleted: {
1748  ractor_selector_remove(selv, taken_basket.sender);
1749 
1750  rb_ractor_t *r = RACTOR_PTR(taken_basket.sender);
1751  if (ractor_take_will_lock(r, &taken_basket)) {
1752  RUBY_DEBUG_LOG("has_will");
1753  }
1754  else {
1755  RUBY_DEBUG_LOG("no will");
1756  // rb_raise(rb_eRactorClosedError, "The outgoing-port is already closed");
1757  // remove and retry wait
1758  goto retry;
1759  }
1760  break;
1761  }
1762  case basket_type_will:
1763  // no more messages
1764  ractor_selector_remove(selv, taken_basket.sender);
1765  break;
1766  default:
1767  break;
1768  }
1769 
1770  RUBY_DEBUG_LOG("taken_basket:%s", basket_type_name(taken_basket.type.e));
1771 
1772  ret_v = ractor_basket_accept(&taken_basket);
1773  ret_r = taken_basket.sender;
1774  success:
1775  return rb_ary_new_from_args(2, ret_r, ret_v);
1776 }
1777 
1778 static VALUE
1779 ractor_selector_wait(int argc, VALUE *argv, VALUE selector)
1780 {
1781  VALUE options;
1782  ID keywords[3];
1783  VALUE values[3];
1784 
1785  keywords[0] = rb_intern("receive");
1786  keywords[1] = rb_intern("yield_value");
1787  keywords[2] = rb_intern("move");
1788 
1789  rb_scan_args(argc, argv, "0:", &options);
1790  rb_get_kwargs(options, keywords, 0, numberof(values), values);
1791  return ractor_selector__wait(selector,
1792  values[0] == Qundef ? Qfalse : RTEST(values[0]),
1793  values[1] != Qundef, values[1], values[2]);
1794 }
1795 
1796 static VALUE
1797 ractor_selector_new(int argc, VALUE *ractors, VALUE klass)
1798 {
1799  VALUE selector = ractor_selector_create(klass);
1800 
1801  for (int i=0; i<argc; i++) {
1802  ractor_selector_add(selector, ractors[i]);
1803  }
1804 
1805  return selector;
1806 }
1807 
1808 static VALUE
1809 ractor_select_internal(rb_execution_context_t *ec, VALUE self, VALUE ractors, VALUE do_receive, VALUE do_yield, VALUE yield_value, VALUE move)
1810 {
1811  VALUE selector = ractor_selector_new(RARRAY_LENINT(ractors), (VALUE *)RARRAY_CONST_PTR(ractors), rb_cRactorSelector);
1812  VALUE result;
1813  int state;
1814 
1815  EC_PUSH_TAG(ec);
1816  if ((state = EC_EXEC_TAG()) == TAG_NONE) {
1817  result = ractor_selector__wait(selector, do_receive, do_yield, yield_value, move);
1818  }
1819  EC_POP_TAG();
1820  if (state != TAG_NONE) {
1821  // ensure
1822  ractor_selector_clear(selector);
1823 
1824  // jump
1825  EC_JUMP_TAG(ec, state);
1826  }
1827 
1828  RB_GC_GUARD(ractors);
1829  return result;
1830 }
1831 
1832 // Ractor#close_incoming
1833 
1834 static VALUE
1835 ractor_close_incoming(rb_execution_context_t *ec, rb_ractor_t *r)
1836 {
1837  VALUE prev;
1838 
1839  RACTOR_LOCK(r);
1840  {
1841  if (!r->sync.incoming_port_closed) {
1842  prev = Qfalse;
1843  r->sync.incoming_port_closed = true;
1844  if (ractor_wakeup(r, wait_receiving, wakeup_by_close)) {
1845  VM_ASSERT(ractor_queue_empty_p(r, &r->sync.recv_queue));
1846  RUBY_DEBUG_LOG("cancel receiving");
1847  }
1848  }
1849  else {
1850  prev = Qtrue;
1851  }
1852  }
1853  RACTOR_UNLOCK(r);
1854  return prev;
1855 }
1856 
1857 // Ractor#close_outgoing
1858 
1859 static VALUE
1860 ractor_close_outgoing(rb_execution_context_t *ec, rb_ractor_t *r)
1861 {
1862  VALUE prev;
1863 
1864  RACTOR_LOCK(r);
1865  {
1866  struct rb_ractor_queue *ts = &r->sync.takers_queue;
1867  rb_ractor_t *tr;
1868  struct rb_ractor_basket b;
1869 
1870  if (!r->sync.outgoing_port_closed) {
1871  prev = Qfalse;
1872  r->sync.outgoing_port_closed = true;
1873  }
1874  else {
1875  VM_ASSERT(ractor_queue_empty_p(r, ts));
1876  prev = Qtrue;
1877  }
1878 
1879  // wakeup all taking ractors
1880  while (ractor_queue_deq(r, ts, &b)) {
1881  if (basket_type_p(&b, basket_type_take_basket)) {
1882  tr = RACTOR_PTR(b.sender);
1883  struct rb_ractor_basket *tb = b.p.take.basket;
1884 
1885  if (RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_none, basket_type_yielding) == basket_type_none) {
1886  b.p.take.basket->sender = r->pub.self;
1887  if (RUBY_ATOMIC_CAS(tb->type.atomic, basket_type_yielding, basket_type_deleted) != basket_type_yielding) {
1888  rb_bug("unreachable");
1889  }
1890  RUBY_DEBUG_LOG("set delete for r:%u", rb_ractor_id(RACTOR_PTR(b.sender)));
1891  }
1892 
1893  if (b.p.take.config) {
1894  b.p.take.config->closed = true;
1895  }
1896 
1897  // TODO: deadlock-able?
1898  RACTOR_LOCK(tr);
1899  {
1900  ractor_wakeup(tr, wait_taking, wakeup_by_close);
1901  }
1902  RACTOR_UNLOCK(tr);
1903  }
1904  }
1905 
1906  // raising yielding Ractor
1907  ractor_wakeup(r, wait_yielding, wakeup_by_close);
1908 
1909  VM_ASSERT(ractor_queue_empty_p(r, ts));
1910  }
1911  RACTOR_UNLOCK(r);
1912  return prev;
1913 }
1914 
1915 // creation/termination
1916 
1917 static uint32_t
1918 ractor_next_id(void)
1919 {
1920  uint32_t id;
1921 
1922  id = (uint32_t)(RUBY_ATOMIC_FETCH_ADD(ractor_last_id, 1) + 1);
1923 
1924  return id;
1925 }
1926 
1927 static void
1928 vm_insert_ractor0(rb_vm_t *vm, rb_ractor_t *r, bool single_ractor_mode)
1929 {
1930  RUBY_DEBUG_LOG("r:%u ractor.cnt:%u++", r->pub.id, vm->ractor.cnt);
1931  VM_ASSERT(single_ractor_mode || RB_VM_LOCKED_P());
1932 
1933  ccan_list_add_tail(&vm->ractor.set, &r->vmlr_node);
1934  vm->ractor.cnt++;
1935 
1936  if (r->newobj_cache) {
1937  VM_ASSERT(r == ruby_single_main_ractor);
1938  }
1939  else {
1940  r->newobj_cache = rb_gc_ractor_cache_alloc();
1941  }
1942 }
1943 
1944 static void
1945 cancel_single_ractor_mode(void)
1946 {
1947  // enable multi-ractor mode
1948  RUBY_DEBUG_LOG("enable multi-ractor mode");
1949 
1950  VALUE was_disabled = rb_gc_enable();
1951 
1952  rb_gc_start();
1953 
1954  if (was_disabled) {
1955  rb_gc_disable();
1956  }
1957 
1958  ruby_single_main_ractor = NULL;
1959 }
1960 
1961 static void
1962 vm_insert_ractor(rb_vm_t *vm, rb_ractor_t *r)
1963 {
1964  VM_ASSERT(ractor_status_p(r, ractor_created));
1965 
1966  if (rb_multi_ractor_p()) {
1967  RB_VM_LOCK();
1968  {
1969  vm_insert_ractor0(vm, r, false);
1970  vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
1971  }
1972  RB_VM_UNLOCK();
1973  }
1974  else {
1975  if (vm->ractor.cnt == 0) {
1976  // main ractor
1977  vm_insert_ractor0(vm, r, true);
1978  ractor_status_set(r, ractor_blocking);
1979  ractor_status_set(r, ractor_running);
1980  }
1981  else {
1982  cancel_single_ractor_mode();
1983  vm_insert_ractor0(vm, r, true);
1984  vm_ractor_blocking_cnt_inc(vm, r, __FILE__, __LINE__);
1985  }
1986  }
1987 }
1988 
1989 static void
1990 vm_remove_ractor(rb_vm_t *vm, rb_ractor_t *cr)
1991 {
1992  VM_ASSERT(ractor_status_p(cr, ractor_running));
1993  VM_ASSERT(vm->ractor.cnt > 1);
1994  VM_ASSERT(cr->threads.cnt == 1);
1995 
1996  RB_VM_LOCK();
1997  {
1998  RUBY_DEBUG_LOG("ractor.cnt:%u-- terminate_waiting:%d",
1999  vm->ractor.cnt, vm->ractor.sync.terminate_waiting);
2000 
2001  VM_ASSERT(vm->ractor.cnt > 0);
2002  ccan_list_del(&cr->vmlr_node);
2003 
2004  if (vm->ractor.cnt <= 2 && vm->ractor.sync.terminate_waiting) {
2005  rb_native_cond_signal(&vm->ractor.sync.terminate_cond);
2006  }
2007  vm->ractor.cnt--;
2008 
2009  rb_gc_ractor_cache_free(cr->newobj_cache);
2010  cr->newobj_cache = NULL;
2011 
2012  ractor_status_set(cr, ractor_terminated);
2013  }
2014  RB_VM_UNLOCK();
2015 }
2016 
2017 static VALUE
2018 ractor_alloc(VALUE klass)
2019 {
2020  rb_ractor_t *r;
2021  VALUE rv = TypedData_Make_Struct(klass, rb_ractor_t, &ractor_data_type, r);
2023  r->pub.self = rv;
2024  VM_ASSERT(ractor_status_p(r, ractor_created));
2025  return rv;
2026 }
2027 
2028 rb_ractor_t *
2029 rb_ractor_main_alloc(void)
2030 {
2031  rb_ractor_t *r = ruby_mimcalloc(1, sizeof(rb_ractor_t));
2032  if (r == NULL) {
2033  fprintf(stderr, "[FATAL] failed to allocate memory for main ractor\n");
2034  exit(EXIT_FAILURE);
2035  }
2036  r->pub.id = ++ractor_last_id;
2037  r->loc = Qnil;
2038  r->name = Qnil;
2039  r->pub.self = Qnil;
2040  r->newobj_cache = rb_gc_ractor_cache_alloc();
2041  ruby_single_main_ractor = r;
2042 
2043  return r;
2044 }
2045 
2046 #if defined(HAVE_WORKING_FORK)
2047 void
2048 rb_ractor_atfork(rb_vm_t *vm, rb_thread_t *th)
2049 {
2050  // initialize as a main ractor
2051  vm->ractor.cnt = 0;
2052  vm->ractor.blocking_cnt = 0;
2053  ruby_single_main_ractor = th->ractor;
2054  th->ractor->status_ = ractor_created;
2055 
2056  rb_ractor_living_threads_init(th->ractor);
2057  rb_ractor_living_threads_insert(th->ractor, th);
2058 
2059  VM_ASSERT(vm->ractor.blocking_cnt == 0);
2060  VM_ASSERT(vm->ractor.cnt == 1);
2061 }
2062 #endif
2063 
2064 void rb_thread_sched_init(struct rb_thread_sched *, bool atfork);
2065 
2066 void
2067 rb_ractor_living_threads_init(rb_ractor_t *r)
2068 {
2069  ccan_list_head_init(&r->threads.set);
2070  r->threads.cnt = 0;
2071  r->threads.blocking_cnt = 0;
2072 }
2073 
2074 static void
2075 ractor_init(rb_ractor_t *r, VALUE name, VALUE loc)
2076 {
2077  ractor_queue_setup(&r->sync.recv_queue);
2078  ractor_queue_setup(&r->sync.takers_queue);
2079  rb_native_mutex_initialize(&r->sync.lock);
2080  rb_native_cond_initialize(&r->barrier_wait_cond);
2081 
2082 #ifdef RUBY_THREAD_WIN32_H
2083  rb_native_cond_initialize(&r->sync.cond);
2084  rb_native_cond_initialize(&r->barrier_wait_cond);
2085 #endif
2086 
2087  // thread management
2088  rb_thread_sched_init(&r->threads.sched, false);
2089  rb_ractor_living_threads_init(r);
2090 
2091  // naming
2092  if (!NIL_P(name)) {
2093  rb_encoding *enc;
2094  StringValueCStr(name);
2095  enc = rb_enc_get(name);
2096  if (!rb_enc_asciicompat(enc)) {
2097  rb_raise(rb_eArgError, "ASCII incompatible encoding (%s)",
2098  rb_enc_name(enc));
2099  }
2100  name = rb_str_new_frozen(name);
2101  }
2102  r->name = name;
2103  r->loc = loc;
2104 }
2105 
2106 void
2107 rb_ractor_main_setup(rb_vm_t *vm, rb_ractor_t *r, rb_thread_t *th)
2108 {
2109  r->pub.self = TypedData_Wrap_Struct(rb_cRactor, &ractor_data_type, r);
2110  FL_SET_RAW(r->pub.self, RUBY_FL_SHAREABLE);
2111  ractor_init(r, Qnil, Qnil);
2112  r->threads.main = th;
2113  rb_ractor_living_threads_insert(r, th);
2114 }
2115 
2116 static VALUE
2117 ractor_create(rb_execution_context_t *ec, VALUE self, VALUE loc, VALUE name, VALUE args, VALUE block)
2118 {
2119  VALUE rv = ractor_alloc(self);
2120  rb_ractor_t *r = RACTOR_PTR(rv);
2121  ractor_init(r, name, loc);
2122 
2123  // can block here
2124  r->pub.id = ractor_next_id();
2125  RUBY_DEBUG_LOG("r:%u", r->pub.id);
2126 
2127  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
2128  r->verbose = cr->verbose;
2129  r->debug = cr->debug;
2130 
2131  rb_yjit_before_ractor_spawn();
2132  rb_rjit_before_ractor_spawn();
2133  rb_thread_create_ractor(r, args, block);
2134 
2135  RB_GC_GUARD(rv);
2136  return rv;
2137 }
2138 
2139 static void
2140 ractor_yield_atexit(rb_execution_context_t *ec, rb_ractor_t *cr, VALUE v, bool exc)
2141 {
2142  if (cr->sync.outgoing_port_closed) {
2143  return;
2144  }
2145 
2146  ASSERT_ractor_unlocking(cr);
2147 
2148  struct rb_ractor_queue *ts = &cr->sync.takers_queue;
2149 
2150  retry:
2151  if (ractor_try_yield(ec, cr, ts, v, Qfalse, exc, true)) {
2152  // OK.
2153  }
2154  else {
2155  bool retry = false;
2156  RACTOR_LOCK(cr);
2157  {
2158  if (!ractor_check_take_basket(cr, ts)) {
2159  VM_ASSERT(cr->sync.wait.status == wait_none);
2160  RUBY_DEBUG_LOG("leave a will");
2161  ractor_basket_fill_will(cr, &cr->sync.will_basket, v, exc);
2162  }
2163  else {
2164  RUBY_DEBUG_LOG("rare timing!");
2165  retry = true; // another ractor is waiting for the yield.
2166  }
2167  }
2168  RACTOR_UNLOCK(cr);
2169 
2170  if (retry) goto retry;
2171  }
2172 }
2173 
2174 void
2175 rb_ractor_atexit(rb_execution_context_t *ec, VALUE result)
2176 {
2177  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
2178  ractor_yield_atexit(ec, cr, result, false);
2179 }
2180 
2181 void
2182 rb_ractor_atexit_exception(rb_execution_context_t *ec)
2183 {
2184  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
2185  ractor_yield_atexit(ec, cr, ec->errinfo, true);
2186 }
2187 
2188 void
2189 rb_ractor_teardown(rb_execution_context_t *ec)
2190 {
2191  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
2192  ractor_close_incoming(ec, cr);
2193  ractor_close_outgoing(ec, cr);
2194 
2195  // sync with rb_ractor_terminate_interrupt_main_thread()
2196  RB_VM_LOCK_ENTER();
2197  {
2198  VM_ASSERT(cr->threads.main != NULL);
2199  cr->threads.main = NULL;
2200  }
2201  RB_VM_LOCK_LEAVE();
2202 }
2203 
2204 void
2205 rb_ractor_receive_parameters(rb_execution_context_t *ec, rb_ractor_t *r, int len, VALUE *ptr)
2206 {
2207  for (int i=0; i<len; i++) {
2208  ptr[i] = ractor_receive(ec, r);
2209  }
2210 }
2211 
2212 void
2213 rb_ractor_send_parameters(rb_execution_context_t *ec, rb_ractor_t *r, VALUE args)
2214 {
2215  int len = RARRAY_LENINT(args);
2216  for (int i=0; i<len; i++) {
2217  ractor_send(ec, r, RARRAY_AREF(args, i), false);
2218  }
2219 }
2220 
2221 bool
2222 rb_ractor_main_p_(void)
2223 {
2224  VM_ASSERT(rb_multi_ractor_p());
2225  rb_execution_context_t *ec = GET_EC();
2226  return rb_ec_ractor_ptr(ec) == rb_ec_vm_ptr(ec)->ractor.main_ractor;
2227 }
2228 
2229 bool
2230 rb_obj_is_main_ractor(VALUE gv)
2231 {
2232  if (!rb_ractor_p(gv)) return false;
2233  rb_ractor_t *r = DATA_PTR(gv);
2234  return r == GET_VM()->ractor.main_ractor;
2235 }
2236 
2237 int
2238 rb_ractor_living_thread_num(const rb_ractor_t *r)
2239 {
2240  return r->threads.cnt;
2241 }
2242 
2243 // only for current ractor
2244 VALUE
2245 rb_ractor_thread_list(void)
2246 {
2247  rb_ractor_t *r = GET_RACTOR();
2248  rb_thread_t *th = 0;
2249  VALUE ary = rb_ary_new();
2250 
2251  ccan_list_for_each(&r->threads.set, th, lt_node) {
2252  switch (th->status) {
2253  case THREAD_RUNNABLE:
2254  case THREAD_STOPPED:
2255  case THREAD_STOPPED_FOREVER:
2256  rb_ary_push(ary, th->self);
2257  default:
2258  break;
2259  }
2260  }
2261 
2262  return ary;
2263 }
2264 
2265 void
2266 rb_ractor_living_threads_insert(rb_ractor_t *r, rb_thread_t *th)
2267 {
2268  VM_ASSERT(th != NULL);
2269 
2270  RACTOR_LOCK(r);
2271  {
2272  RUBY_DEBUG_LOG("r(%d)->threads.cnt:%d++", r->pub.id, r->threads.cnt);
2273  ccan_list_add_tail(&r->threads.set, &th->lt_node);
2274  r->threads.cnt++;
2275  }
2276  RACTOR_UNLOCK(r);
2277 
2278  // first thread for a ractor
2279  if (r->threads.cnt == 1) {
2280  VM_ASSERT(ractor_status_p(r, ractor_created));
2281  vm_insert_ractor(th->vm, r);
2282  }
2283 }
2284 
2285 static void
2286 vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *r, const char *file, int line)
2287 {
2288  ractor_status_set(r, ractor_blocking);
2289 
2290  RUBY_DEBUG_LOG2(file, line, "vm->ractor.blocking_cnt:%d++", vm->ractor.blocking_cnt);
2291  vm->ractor.blocking_cnt++;
2292  VM_ASSERT(vm->ractor.blocking_cnt <= vm->ractor.cnt);
2293 }
2294 
2295 void
2296 rb_vm_ractor_blocking_cnt_inc(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line)
2297 {
2298  ASSERT_vm_locking();
2299  VM_ASSERT(GET_RACTOR() == cr);
2300  vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2301 }
2302 
2303 void
2304 rb_vm_ractor_blocking_cnt_dec(rb_vm_t *vm, rb_ractor_t *cr, const char *file, int line)
2305 {
2306  ASSERT_vm_locking();
2307  VM_ASSERT(GET_RACTOR() == cr);
2308 
2309  RUBY_DEBUG_LOG2(file, line, "vm->ractor.blocking_cnt:%d--", vm->ractor.blocking_cnt);
2310  VM_ASSERT(vm->ractor.blocking_cnt > 0);
2311  vm->ractor.blocking_cnt--;
2312 
2313  ractor_status_set(cr, ractor_running);
2314 }
2315 
2316 static void
2317 ractor_check_blocking(rb_ractor_t *cr, unsigned int remained_thread_cnt, const char *file, int line)
2318 {
2319  VM_ASSERT(cr == GET_RACTOR());
2320 
2321  RUBY_DEBUG_LOG2(file, line,
2322  "cr->threads.cnt:%u cr->threads.blocking_cnt:%u vm->ractor.cnt:%u vm->ractor.blocking_cnt:%u",
2323  cr->threads.cnt, cr->threads.blocking_cnt,
2324  GET_VM()->ractor.cnt, GET_VM()->ractor.blocking_cnt);
2325 
2326  VM_ASSERT(cr->threads.cnt >= cr->threads.blocking_cnt + 1);
2327 
2328  if (remained_thread_cnt > 0 &&
2329  // will be block
2330  cr->threads.cnt == cr->threads.blocking_cnt + 1) {
2331  // change ractor status: running -> blocking
2332  rb_vm_t *vm = GET_VM();
2333  ASSERT_vm_unlocking();
2334 
2335  RB_VM_LOCK();
2336  {
2337  rb_vm_ractor_blocking_cnt_inc(vm, cr, file, line);
2338  }
2339  RB_VM_UNLOCK();
2340  }
2341 }
2342 
2343 void rb_threadptr_remove(rb_thread_t *th);
2344 
2345 void
2346 rb_ractor_living_threads_remove(rb_ractor_t *cr, rb_thread_t *th)
2347 {
2348  VM_ASSERT(cr == GET_RACTOR());
2349  RUBY_DEBUG_LOG("r->threads.cnt:%d--", cr->threads.cnt);
2350  ractor_check_blocking(cr, cr->threads.cnt - 1, __FILE__, __LINE__);
2351 
2352  rb_threadptr_remove(th);
2353 
2354  if (cr->threads.cnt == 1) {
2355  vm_remove_ractor(th->vm, cr);
2356  }
2357  else {
2358  RACTOR_LOCK(cr);
2359  {
2360  ccan_list_del(&th->lt_node);
2361  cr->threads.cnt--;
2362  }
2363  RACTOR_UNLOCK(cr);
2364  }
2365 }
2366 
2367 void
2368 rb_ractor_blocking_threads_inc(rb_ractor_t *cr, const char *file, int line)
2369 {
2370  RUBY_DEBUG_LOG2(file, line, "cr->threads.blocking_cnt:%d++", cr->threads.blocking_cnt);
2371 
2372  VM_ASSERT(cr->threads.cnt > 0);
2373  VM_ASSERT(cr == GET_RACTOR());
2374 
2375  ractor_check_blocking(cr, cr->threads.cnt, __FILE__, __LINE__);
2376  cr->threads.blocking_cnt++;
2377 }
2378 
2379 void
2380 rb_ractor_blocking_threads_dec(rb_ractor_t *cr, const char *file, int line)
2381 {
2382  RUBY_DEBUG_LOG2(file, line,
2383  "r->threads.blocking_cnt:%d--, r->threads.cnt:%u",
2384  cr->threads.blocking_cnt, cr->threads.cnt);
2385 
2386  VM_ASSERT(cr == GET_RACTOR());
2387 
2388  if (cr->threads.cnt == cr->threads.blocking_cnt) {
2389  rb_vm_t *vm = GET_VM();
2390 
2391  RB_VM_LOCK_ENTER();
2392  {
2393  rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2394  }
2395  RB_VM_LOCK_LEAVE();
2396  }
2397 
2398  cr->threads.blocking_cnt--;
2399 }
2400 
2401 void
2402 rb_ractor_vm_barrier_interrupt_running_thread(rb_ractor_t *r)
2403 {
2404  VM_ASSERT(r != GET_RACTOR());
2405  ASSERT_ractor_unlocking(r);
2406  ASSERT_vm_locking();
2407 
2408  RACTOR_LOCK(r);
2409  {
2410  if (ractor_status_p(r, ractor_running)) {
2411  rb_execution_context_t *ec = r->threads.running_ec;
2412  if (ec) {
2413  RUBY_VM_SET_VM_BARRIER_INTERRUPT(ec);
2414  }
2415  }
2416  }
2417  RACTOR_UNLOCK(r);
2418 }
2419 
2420 void
2421 rb_ractor_terminate_interrupt_main_thread(rb_ractor_t *r)
2422 {
2423  VM_ASSERT(r != GET_RACTOR());
2424  ASSERT_ractor_unlocking(r);
2425  ASSERT_vm_locking();
2426 
2427  rb_thread_t *main_th = r->threads.main;
2428  if (main_th) {
2429  if (main_th->status != THREAD_KILLED) {
2430  RUBY_VM_SET_TERMINATE_INTERRUPT(main_th->ec);
2431  rb_threadptr_interrupt(main_th);
2432  }
2433  else {
2434  RUBY_DEBUG_LOG("killed (%p)", (void *)main_th);
2435  }
2436  }
2437 }
2438 
2439 void rb_thread_terminate_all(rb_thread_t *th); // thread.c
2440 
2441 static void
2442 ractor_terminal_interrupt_all(rb_vm_t *vm)
2443 {
2444  if (vm->ractor.cnt > 1) {
2445  // send terminate notification to all ractors
2446  rb_ractor_t *r = 0;
2447  ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2448  if (r != vm->ractor.main_ractor) {
2449  RUBY_DEBUG_LOG("r:%d", rb_ractor_id(r));
2450  rb_ractor_terminate_interrupt_main_thread(r);
2451  }
2452  }
2453  }
2454 }
2455 
2456 void rb_add_running_thread(rb_thread_t *th);
2457 void rb_del_running_thread(rb_thread_t *th);
2458 
2459 void
2460 rb_ractor_terminate_all(void)
2461 {
2462  rb_vm_t *vm = GET_VM();
2463  rb_ractor_t *cr = vm->ractor.main_ractor;
2464 
2465  RUBY_DEBUG_LOG("ractor.cnt:%d", (int)vm->ractor.cnt);
2466 
2467  VM_ASSERT(cr == GET_RACTOR()); // only main-ractor's main-thread should kick it.
2468 
2469  if (vm->ractor.cnt > 1) {
2470  RB_VM_LOCK();
2471  {
2472  ractor_terminal_interrupt_all(vm); // kill all ractors
2473  }
2474  RB_VM_UNLOCK();
2475  }
2476  rb_thread_terminate_all(GET_THREAD()); // kill other threads in main-ractor and wait
2477 
2478  RB_VM_LOCK();
2479  {
2480  while (vm->ractor.cnt > 1) {
2481  RUBY_DEBUG_LOG("terminate_waiting:%d", vm->ractor.sync.terminate_waiting);
2482  vm->ractor.sync.terminate_waiting = true;
2483 
2484  // wait for 1sec
2485  rb_vm_ractor_blocking_cnt_inc(vm, cr, __FILE__, __LINE__);
2486  rb_del_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2487  rb_vm_cond_timedwait(vm, &vm->ractor.sync.terminate_cond, 1000 /* ms */);
2488  rb_add_running_thread(rb_ec_thread_ptr(cr->threads.running_ec));
2489  rb_vm_ractor_blocking_cnt_dec(vm, cr, __FILE__, __LINE__);
2490 
2491  ractor_terminal_interrupt_all(vm);
2492  }
2493  }
2494  RB_VM_UNLOCK();
2495 }
2496 
2498 rb_vm_main_ractor_ec(rb_vm_t *vm)
2499 {
2500  /* This code needs to carefully work around two bugs:
2501  * - Bug #20016: When M:N threading is enabled, running_ec is NULL if no thread is
2502  * actually currently running (as opposed to without M:N threading, when
2503  * running_ec will still point to the _last_ thread which ran)
2504  * - Bug #20197: If the main thread is sleeping, setting its postponed job
2505  * interrupt flag is pointless; it won't look at the flag until it stops sleeping
2506  * for some reason. It would be better to set the flag on the running ec, which
2507  * will presumably look at it soon.
2508  *
2509  * Solution: use running_ec if it's set, otherwise fall back to the main thread ec.
2510  * This is still susceptible to some rare race conditions (what if the last thread
2511  * to run just entered a long-running sleep?), but seems like the best balance of
2512  * robustness and complexity.
2513  */
2514  rb_execution_context_t *running_ec = vm->ractor.main_ractor->threads.running_ec;
2515  if (running_ec) { return running_ec; }
2516  return vm->ractor.main_thread->ec;
2517 }
2518 
2519 static VALUE
2520 ractor_moved_missing(int argc, VALUE *argv, VALUE self)
2521 {
2522  rb_raise(rb_eRactorMovedError, "can not send any methods to a moved object");
2523 }
2524 
2525 #ifndef USE_RACTOR_SELECTOR
2526 #define USE_RACTOR_SELECTOR 0
2527 #endif
2528 
2529 RUBY_SYMBOL_EXPORT_BEGIN
2530 void rb_init_ractor_selector(void);
2531 RUBY_SYMBOL_EXPORT_END
2532 
2533 void
2534 rb_init_ractor_selector(void)
2535 {
2536  rb_cRactorSelector = rb_define_class_under(rb_cRactor, "Selector", rb_cObject);
2537  rb_undef_alloc_func(rb_cRactorSelector);
2538 
2539  rb_define_singleton_method(rb_cRactorSelector, "new", ractor_selector_new , -1);
2540  rb_define_method(rb_cRactorSelector, "add", ractor_selector_add, 1);
2541  rb_define_method(rb_cRactorSelector, "remove", ractor_selector_remove, 1);
2542  rb_define_method(rb_cRactorSelector, "clear", ractor_selector_clear, 0);
2543  rb_define_method(rb_cRactorSelector, "empty?", ractor_selector_empty_p, 0);
2544  rb_define_method(rb_cRactorSelector, "wait", ractor_selector_wait, -1);
2545  rb_define_method(rb_cRactorSelector, "_wait", ractor_selector__wait, 4);
2546 }
2547 
2548 /*
2549  * Document-class: Ractor::ClosedError
2550  *
2551  * Raised when an attempt is made to send a message to a closed port,
2552  * or to retrieve a message from a closed and empty port.
2553  * Ports may be closed explicitly with Ractor#close_outgoing/close_incoming
2554  * and are closed implicitly when a Ractor terminates.
2555  *
2556  * r = Ractor.new { sleep(500) }
2557  * r.close_outgoing
2558  * r.take # Ractor::ClosedError
2559  *
2560  * ClosedError is a descendant of StopIteration, so the closing of the ractor will break
2561  * the loops without propagating the error:
2562  *
2563  * r = Ractor.new do
2564  * loop do
2565  * msg = receive # raises ClosedError and loop traps it
2566  * puts "Received: #{msg}"
2567  * end
2568  * puts "loop exited"
2569  * end
2570  *
2571  * 3.times{|i| r << i}
2572  * r.close_incoming
2573  * r.take
2574  * puts "Continue successfully"
2575  *
2576  * This will print:
2577  *
2578  * Received: 0
2579  * Received: 1
2580  * Received: 2
2581  * loop exited
2582  * Continue successfully
2583  */
2584 
2585 /*
2586  * Document-class: Ractor::RemoteError
2587  *
2588  * Raised on attempt to Ractor#take if there was an uncaught exception in the Ractor.
2589  * Its +cause+ will contain the original exception, and +ractor+ is the original ractor
2590  * it was raised in.
2591  *
2592  * r = Ractor.new { raise "Something weird happened" }
2593  *
2594  * begin
2595  * r.take
2596  * rescue => e
2597  * p e # => #<Ractor::RemoteError: thrown by remote Ractor.>
2598  * p e.ractor == r # => true
2599  * p e.cause # => #<RuntimeError: Something weird happened>
2600  * end
2601  *
2602  */
2603 
2604 /*
2605  * Document-class: Ractor::MovedError
2606  *
2607  * Raised on an attempt to access an object which was moved in Ractor#send or Ractor.yield.
2608  *
2609  * r = Ractor.new { sleep }
2610  *
2611  * ary = [1, 2, 3]
2612  * r.send(ary, move: true)
2613  * ary.inspect
2614  * # Ractor::MovedError (can not send any methods to a moved object)
2615  *
2616  */
2617 
2618 /*
2619  * Document-class: Ractor::MovedObject
2620  *
2621  * A special object which replaces any value that was moved to another ractor in Ractor#send
2622  * or Ractor.yield. Any attempt to access the object results in Ractor::MovedError.
2623  *
2624  * r = Ractor.new { receive }
2625  *
2626  * ary = [1, 2, 3]
2627  * r.send(ary, move: true)
2628  * p Ractor::MovedObject === ary
2629  * # => true
2630  * ary.inspect
2631  * # Ractor::MovedError (can not send any methods to a moved object)
2632  */
2633 
2634 // Main docs are in ractor.rb, but without this clause there are weird artifacts
2635 // in their rendering.
2636 /*
2637  * Document-class: Ractor
2638  *
2639  */
2640 
2641 void
2642 Init_Ractor(void)
2643 {
2644  rb_cRactor = rb_define_class("Ractor", rb_cObject);
2646 
2647  rb_eRactorError = rb_define_class_under(rb_cRactor, "Error", rb_eRuntimeError);
2648  rb_eRactorIsolationError = rb_define_class_under(rb_cRactor, "IsolationError", rb_eRactorError);
2649  rb_eRactorRemoteError = rb_define_class_under(rb_cRactor, "RemoteError", rb_eRactorError);
2650  rb_eRactorMovedError = rb_define_class_under(rb_cRactor, "MovedError", rb_eRactorError);
2651  rb_eRactorClosedError = rb_define_class_under(rb_cRactor, "ClosedError", rb_eStopIteration);
2652  rb_eRactorUnsafeError = rb_define_class_under(rb_cRactor, "UnsafeError", rb_eRactorError);
2653 
2654  rb_cRactorMovedObject = rb_define_class_under(rb_cRactor, "MovedObject", rb_cBasicObject);
2655  rb_undef_alloc_func(rb_cRactorMovedObject);
2656  rb_define_method(rb_cRactorMovedObject, "method_missing", ractor_moved_missing, -1);
2657 
2658  // override methods defined in BasicObject
2659  rb_define_method(rb_cRactorMovedObject, "__send__", ractor_moved_missing, -1);
2660  rb_define_method(rb_cRactorMovedObject, "!", ractor_moved_missing, -1);
2661  rb_define_method(rb_cRactorMovedObject, "==", ractor_moved_missing, -1);
2662  rb_define_method(rb_cRactorMovedObject, "!=", ractor_moved_missing, -1);
2663  rb_define_method(rb_cRactorMovedObject, "__id__", ractor_moved_missing, -1);
2664  rb_define_method(rb_cRactorMovedObject, "equal?", ractor_moved_missing, -1);
2665  rb_define_method(rb_cRactorMovedObject, "instance_eval", ractor_moved_missing, -1);
2666  rb_define_method(rb_cRactorMovedObject, "instance_exec", ractor_moved_missing, -1);
2667 
2668 #if USE_RACTOR_SELECTOR
2669  rb_init_ractor_selector();
2670 #endif
2671 }
2672 
2673 void
2674 rb_ractor_dump(void)
2675 {
2676  rb_vm_t *vm = GET_VM();
2677  rb_ractor_t *r = 0;
2678 
2679  ccan_list_for_each(&vm->ractor.set, r, vmlr_node) {
2680  if (r != vm->ractor.main_ractor) {
2681  fprintf(stderr, "r:%u (%s)\n", r->pub.id, ractor_status_str(r->status_));
2682  }
2683  }
2684 }
2685 
2686 VALUE
2688 {
2689  if (rb_ractor_main_p()) {
2690  return rb_stdin;
2691  }
2692  else {
2693  rb_ractor_t *cr = GET_RACTOR();
2694  return cr->r_stdin;
2695  }
2696 }
2697 
2698 VALUE
2700 {
2701  if (rb_ractor_main_p()) {
2702  return rb_stdout;
2703  }
2704  else {
2705  rb_ractor_t *cr = GET_RACTOR();
2706  return cr->r_stdout;
2707  }
2708 }
2709 
2710 VALUE
2712 {
2713  if (rb_ractor_main_p()) {
2714  return rb_stderr;
2715  }
2716  else {
2717  rb_ractor_t *cr = GET_RACTOR();
2718  return cr->r_stderr;
2719  }
2720 }
2721 
2722 void
2724 {
2725  if (rb_ractor_main_p()) {
2726  rb_stdin = in;
2727  }
2728  else {
2729  rb_ractor_t *cr = GET_RACTOR();
2730  RB_OBJ_WRITE(cr->pub.self, &cr->r_stdin, in);
2731  }
2732 }
2733 
2734 void
2736 {
2737  if (rb_ractor_main_p()) {
2738  rb_stdout = out;
2739  }
2740  else {
2741  rb_ractor_t *cr = GET_RACTOR();
2742  RB_OBJ_WRITE(cr->pub.self, &cr->r_stdout, out);
2743  }
2744 }
2745 
2746 void
2748 {
2749  if (rb_ractor_main_p()) {
2750  rb_stderr = err;
2751  }
2752  else {
2753  rb_ractor_t *cr = GET_RACTOR();
2754  RB_OBJ_WRITE(cr->pub.self, &cr->r_stderr, err);
2755  }
2756 }
2757 
2759 rb_ractor_hooks(rb_ractor_t *cr)
2760 {
2761  return &cr->pub.hooks;
2762 }
2763 
2765 
2766 // 2: stop search
2767 // 1: skip child
2768 // 0: continue
2769 
2770 enum obj_traverse_iterator_result {
2771  traverse_cont,
2772  traverse_skip,
2773  traverse_stop,
2774 };
2775 
2776 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_enter_func)(VALUE obj);
2777 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_leave_func)(VALUE obj);
2778 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_final_func)(VALUE obj);
2779 
2780 static enum obj_traverse_iterator_result null_leave(VALUE obj);
2781 
2783  rb_obj_traverse_enter_func enter_func;
2784  rb_obj_traverse_leave_func leave_func;
2785 
2786  st_table *rec;
2787  VALUE rec_hash;
2788 };
2789 
2790 
2792  bool stop;
2793  struct obj_traverse_data *data;
2794 };
2795 
2796 static int obj_traverse_i(VALUE obj, struct obj_traverse_data *data);
2797 
2798 static int
2799 obj_hash_traverse_i(VALUE key, VALUE val, VALUE ptr)
2800 {
2802 
2803  if (obj_traverse_i(key, d->data)) {
2804  d->stop = true;
2805  return ST_STOP;
2806  }
2807 
2808  if (obj_traverse_i(val, d->data)) {
2809  d->stop = true;
2810  return ST_STOP;
2811  }
2812 
2813  return ST_CONTINUE;
2814 }
2815 
2816 static void
2817 obj_traverse_reachable_i(VALUE obj, void *ptr)
2818 {
2820 
2821  if (obj_traverse_i(obj, d->data)) {
2822  d->stop = true;
2823  }
2824 }
2825 
2826 static struct st_table *
2827 obj_traverse_rec(struct obj_traverse_data *data)
2828 {
2829  if (UNLIKELY(!data->rec)) {
2830  data->rec_hash = rb_ident_hash_new();
2831  data->rec = RHASH_ST_TABLE(data->rec_hash);
2832  }
2833  return data->rec;
2834 }
2835 
2836 static int
2837 obj_traverse_ivar_foreach_i(ID key, VALUE val, st_data_t ptr)
2838 {
2840 
2841  if (obj_traverse_i(val, d->data)) {
2842  d->stop = true;
2843  return ST_STOP;
2844  }
2845 
2846  return ST_CONTINUE;
2847 }
2848 
2849 static int
2850 obj_traverse_i(VALUE obj, struct obj_traverse_data *data)
2851 {
2852  if (RB_SPECIAL_CONST_P(obj)) return 0;
2853 
2854  switch (data->enter_func(obj)) {
2855  case traverse_cont: break;
2856  case traverse_skip: return 0; // skip children
2857  case traverse_stop: return 1; // stop search
2858  }
2859 
2860  if (UNLIKELY(st_insert(obj_traverse_rec(data), obj, 1))) {
2861  // already traversed
2862  return 0;
2863  }
2864 
2865  struct obj_traverse_callback_data d = {
2866  .stop = false,
2867  .data = data,
2868  };
2869  rb_ivar_foreach(obj, obj_traverse_ivar_foreach_i, (st_data_t)&d);
2870  if (d.stop) return 1;
2871 
2872  switch (BUILTIN_TYPE(obj)) {
2873  // no child node
2874  case T_STRING:
2875  case T_FLOAT:
2876  case T_BIGNUM:
2877  case T_REGEXP:
2878  case T_FILE:
2879  case T_SYMBOL:
2880  case T_MATCH:
2881  break;
2882 
2883  case T_OBJECT:
2884  /* Instance variables already traversed. */
2885  break;
2886 
2887  case T_ARRAY:
2888  {
2889  for (int i = 0; i < RARRAY_LENINT(obj); i++) {
2890  VALUE e = rb_ary_entry(obj, i);
2891  if (obj_traverse_i(e, data)) return 1;
2892  }
2893  }
2894  break;
2895 
2896  case T_HASH:
2897  {
2898  if (obj_traverse_i(RHASH_IFNONE(obj), data)) return 1;
2899 
2900  struct obj_traverse_callback_data d = {
2901  .stop = false,
2902  .data = data,
2903  };
2904  rb_hash_foreach(obj, obj_hash_traverse_i, (VALUE)&d);
2905  if (d.stop) return 1;
2906  }
2907  break;
2908 
2909  case T_STRUCT:
2910  {
2911  long len = RSTRUCT_LEN(obj);
2912  const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
2913 
2914  for (long i=0; i<len; i++) {
2915  if (obj_traverse_i(ptr[i], data)) return 1;
2916  }
2917  }
2918  break;
2919 
2920  case T_RATIONAL:
2921  if (obj_traverse_i(RRATIONAL(obj)->num, data)) return 1;
2922  if (obj_traverse_i(RRATIONAL(obj)->den, data)) return 1;
2923  break;
2924  case T_COMPLEX:
2925  if (obj_traverse_i(RCOMPLEX(obj)->real, data)) return 1;
2926  if (obj_traverse_i(RCOMPLEX(obj)->imag, data)) return 1;
2927  break;
2928 
2929  case T_DATA:
2930  case T_IMEMO:
2931  {
2932  struct obj_traverse_callback_data d = {
2933  .stop = false,
2934  .data = data,
2935  };
2936  RB_VM_LOCK_ENTER_NO_BARRIER();
2937  {
2938  rb_objspace_reachable_objects_from(obj, obj_traverse_reachable_i, &d);
2939  }
2940  RB_VM_LOCK_LEAVE_NO_BARRIER();
2941  if (d.stop) return 1;
2942  }
2943  break;
2944 
2945  // unreachable
2946  case T_CLASS:
2947  case T_MODULE:
2948  case T_ICLASS:
2949  default:
2950  rp(obj);
2951  rb_bug("unreachable");
2952  }
2953 
2954  if (data->leave_func(obj) == traverse_stop) {
2955  return 1;
2956  }
2957  else {
2958  return 0;
2959  }
2960 }
2961 
2963  rb_obj_traverse_final_func final_func;
2964  int stopped;
2965 };
2966 
2967 static int
2968 obj_traverse_final_i(st_data_t key, st_data_t val, st_data_t arg)
2969 {
2970  struct rb_obj_traverse_final_data *data = (void *)arg;
2971  if (data->final_func(key)) {
2972  data->stopped = 1;
2973  return ST_STOP;
2974  }
2975  return ST_CONTINUE;
2976 }
2977 
2978 // 0: traverse all
2979 // 1: stopped
2980 static int
2981 rb_obj_traverse(VALUE obj,
2982  rb_obj_traverse_enter_func enter_func,
2983  rb_obj_traverse_leave_func leave_func,
2984  rb_obj_traverse_final_func final_func)
2985 {
2986  struct obj_traverse_data data = {
2987  .enter_func = enter_func,
2988  .leave_func = leave_func,
2989  .rec = NULL,
2990  };
2991 
2992  if (obj_traverse_i(obj, &data)) return 1;
2993  if (final_func && data.rec) {
2994  struct rb_obj_traverse_final_data f = {final_func, 0};
2995  st_foreach(data.rec, obj_traverse_final_i, (st_data_t)&f);
2996  return f.stopped;
2997  }
2998  return 0;
2999 }
3000 
3001 static int
3002 frozen_shareable_p(VALUE obj, bool *made_shareable)
3003 {
3004  if (!RB_TYPE_P(obj, T_DATA)) {
3005  return true;
3006  }
3007  else if (RTYPEDDATA_P(obj)) {
3008  const rb_data_type_t *type = RTYPEDDATA_TYPE(obj);
3009  if (type->flags & RUBY_TYPED_FROZEN_SHAREABLE) {
3010  return true;
3011  }
3012  else if (made_shareable && rb_obj_is_proc(obj)) {
3013  // special path to make shareable Proc.
3014  rb_proc_ractor_make_shareable(obj);
3015  *made_shareable = true;
3016  VM_ASSERT(RB_OBJ_SHAREABLE_P(obj));
3017  return false;
3018  }
3019  }
3020 
3021  return false;
3022 }
3023 
3024 static enum obj_traverse_iterator_result
3025 make_shareable_check_shareable(VALUE obj)
3026 {
3027  VM_ASSERT(!SPECIAL_CONST_P(obj));
3028  bool made_shareable = false;
3029 
3030  if (rb_ractor_shareable_p(obj)) {
3031  return traverse_skip;
3032  }
3033  if (!frozen_shareable_p(obj, &made_shareable)) {
3034  if (made_shareable) {
3035  return traverse_skip;
3036  }
3037  else {
3038  rb_raise(rb_eRactorError, "can not make shareable object for %"PRIsVALUE, obj);
3039  }
3040  }
3041 
3042  if (!RB_OBJ_FROZEN_RAW(obj)) {
3043  rb_funcall(obj, idFreeze, 0);
3044 
3045  if (UNLIKELY(!RB_OBJ_FROZEN_RAW(obj))) {
3046  rb_raise(rb_eRactorError, "#freeze does not freeze object correctly");
3047  }
3048 
3049  if (RB_OBJ_SHAREABLE_P(obj)) {
3050  return traverse_skip;
3051  }
3052  }
3053 
3054  return traverse_cont;
3055 }
3056 
3057 static enum obj_traverse_iterator_result
3058 mark_shareable(VALUE obj)
3059 {
3061  return traverse_cont;
3062 }
3063 
3064 VALUE
3066 {
3067  rb_obj_traverse(obj,
3068  make_shareable_check_shareable,
3069  null_leave, mark_shareable);
3070  return obj;
3071 }
3072 
3073 VALUE
3075 {
3076  VALUE copy = ractor_copy(obj);
3077  return rb_ractor_make_shareable(copy);
3078 }
3079 
3080 VALUE
3081 rb_ractor_ensure_shareable(VALUE obj, VALUE name)
3082 {
3083  if (!rb_ractor_shareable_p(obj)) {
3084  VALUE message = rb_sprintf("cannot assign unshareable object to %"PRIsVALUE,
3085  name);
3086  rb_exc_raise(rb_exc_new_str(rb_eRactorIsolationError, message));
3087  }
3088  return obj;
3089 }
3090 
3091 void
3092 rb_ractor_ensure_main_ractor(const char *msg)
3093 {
3094  if (!rb_ractor_main_p()) {
3095  rb_raise(rb_eRactorIsolationError, "%s", msg);
3096  }
3097 }
3098 
3099 static enum obj_traverse_iterator_result
3100 shareable_p_enter(VALUE obj)
3101 {
3102  if (RB_OBJ_SHAREABLE_P(obj)) {
3103  return traverse_skip;
3104  }
3105  else if (RB_TYPE_P(obj, T_CLASS) ||
3106  RB_TYPE_P(obj, T_MODULE) ||
3107  RB_TYPE_P(obj, T_ICLASS)) {
3108  // TODO: remove it
3109  mark_shareable(obj);
3110  return traverse_skip;
3111  }
3112  else if (RB_OBJ_FROZEN_RAW(obj) &&
3113  frozen_shareable_p(obj, NULL)) {
3114  return traverse_cont;
3115  }
3116 
3117  return traverse_stop; // fail
3118 }
3119 
3120 bool
3121 rb_ractor_shareable_p_continue(VALUE obj)
3122 {
3123  if (rb_obj_traverse(obj,
3124  shareable_p_enter, null_leave,
3125  mark_shareable)) {
3126  return false;
3127  }
3128  else {
3129  return true;
3130  }
3131 }
3132 
3133 #if RACTOR_CHECK_MODE > 0
3134 void
3135 rb_ractor_setup_belonging(VALUE obj)
3136 {
3137  rb_ractor_setup_belonging_to(obj, rb_ractor_current_id());
3138 }
3139 
3140 static enum obj_traverse_iterator_result
3141 reset_belonging_enter(VALUE obj)
3142 {
3143  if (rb_ractor_shareable_p(obj)) {
3144  return traverse_skip;
3145  }
3146  else {
3147  rb_ractor_setup_belonging(obj);
3148  return traverse_cont;
3149  }
3150 }
3151 #endif
3152 
3153 static enum obj_traverse_iterator_result
3154 null_leave(VALUE obj)
3155 {
3156  return traverse_cont;
3157 }
3158 
3159 static VALUE
3160 ractor_reset_belonging(VALUE obj)
3161 {
3162 #if RACTOR_CHECK_MODE > 0
3163  rb_obj_traverse(obj, reset_belonging_enter, null_leave, NULL);
3164 #endif
3165  return obj;
3166 }
3167 
3168 
3170 
3171 // 2: stop search
3172 // 1: skip child
3173 // 0: continue
3174 
3176 static int obj_traverse_replace_i(VALUE obj, struct obj_traverse_replace_data *data);
3177 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_replace_enter_func)(VALUE obj, struct obj_traverse_replace_data *data);
3178 typedef enum obj_traverse_iterator_result (*rb_obj_traverse_replace_leave_func)(VALUE obj, struct obj_traverse_replace_data *data);
3179 
3181  rb_obj_traverse_replace_enter_func enter_func;
3182  rb_obj_traverse_replace_leave_func leave_func;
3183 
3184  st_table *rec;
3185  VALUE rec_hash;
3186 
3187  VALUE replacement;
3188  bool move;
3189 };
3190 
3192  bool stop;
3193  VALUE src;
3194  struct obj_traverse_replace_data *data;
3195 };
3196 
3197 static int
3198 obj_hash_traverse_replace_foreach_i(st_data_t key, st_data_t value, st_data_t argp, int error)
3199 {
3200  return ST_REPLACE;
3201 }
3202 
3203 static int
3204 obj_hash_traverse_replace_i(st_data_t *key, st_data_t *val, st_data_t ptr, int exists)
3205 {
3207  struct obj_traverse_replace_data *data = d->data;
3208 
3209  if (obj_traverse_replace_i(*key, data)) {
3210  d->stop = true;
3211  return ST_STOP;
3212  }
3213  else if (*key != data->replacement) {
3214  VALUE v = *key = data->replacement;
3215  RB_OBJ_WRITTEN(d->src, Qundef, v);
3216  }
3217 
3218  if (obj_traverse_replace_i(*val, data)) {
3219  d->stop = true;
3220  return ST_STOP;
3221  }
3222  else if (*val != data->replacement) {
3223  VALUE v = *val = data->replacement;
3224  RB_OBJ_WRITTEN(d->src, Qundef, v);
3225  }
3226 
3227  return ST_CONTINUE;
3228 }
3229 
3230 static int
3231 obj_iv_hash_traverse_replace_foreach_i(st_data_t _key, st_data_t _val, st_data_t _data, int _x)
3232 {
3233  return ST_REPLACE;
3234 }
3235 
3236 static int
3237 obj_iv_hash_traverse_replace_i(st_data_t * _key, st_data_t * val, st_data_t ptr, int exists)
3238 {
3240  struct obj_traverse_replace_data *data = d->data;
3241 
3242  if (obj_traverse_replace_i(*(VALUE *)val, data)) {
3243  d->stop = true;
3244  return ST_STOP;
3245  }
3246  else if (*(VALUE *)val != data->replacement) {
3247  VALUE v = *(VALUE *)val = data->replacement;
3248  RB_OBJ_WRITTEN(d->src, Qundef, v);
3249  }
3250 
3251  return ST_CONTINUE;
3252 }
3253 
3254 static struct st_table *
3255 obj_traverse_replace_rec(struct obj_traverse_replace_data *data)
3256 {
3257  if (UNLIKELY(!data->rec)) {
3258  data->rec_hash = rb_ident_hash_new();
3259  data->rec = RHASH_ST_TABLE(data->rec_hash);
3260  }
3261  return data->rec;
3262 }
3263 
3264 static void
3265 obj_refer_only_shareables_p_i(VALUE obj, void *ptr)
3266 {
3267  int *pcnt = (int *)ptr;
3268 
3269  if (!rb_ractor_shareable_p(obj)) {
3270  ++*pcnt;
3271  }
3272 }
3273 
3274 static int
3275 obj_refer_only_shareables_p(VALUE obj)
3276 {
3277  int cnt = 0;
3278  RB_VM_LOCK_ENTER_NO_BARRIER();
3279  {
3280  rb_objspace_reachable_objects_from(obj, obj_refer_only_shareables_p_i, &cnt);
3281  }
3282  RB_VM_LOCK_LEAVE_NO_BARRIER();
3283  return cnt == 0;
3284 }
3285 
3286 static int
3287 obj_traverse_replace_i(VALUE obj, struct obj_traverse_replace_data *data)
3288 {
3289  st_data_t replacement;
3290 
3291  if (RB_SPECIAL_CONST_P(obj)) {
3292  data->replacement = obj;
3293  return 0;
3294  }
3295 
3296  switch (data->enter_func(obj, data)) {
3297  case traverse_cont: break;
3298  case traverse_skip: return 0; // skip children
3299  case traverse_stop: return 1; // stop search
3300  }
3301 
3302  replacement = (st_data_t)data->replacement;
3303 
3304  if (UNLIKELY(st_lookup(obj_traverse_replace_rec(data), (st_data_t)obj, &replacement))) {
3305  data->replacement = (VALUE)replacement;
3306  return 0;
3307  }
3308  else {
3309  st_insert(obj_traverse_replace_rec(data), (st_data_t)obj, replacement);
3310  }
3311 
3312  if (!data->move) {
3313  obj = replacement;
3314  }
3315 
3316 #define CHECK_AND_REPLACE(v) do { \
3317  VALUE _val = (v); \
3318  if (obj_traverse_replace_i(_val, data)) { return 1; } \
3319  else if (data->replacement != _val) { RB_OBJ_WRITE(obj, &v, data->replacement); } \
3320 } while (0)
3321 
3322  if (UNLIKELY(FL_TEST_RAW(obj, FL_EXIVAR))) {
3323  struct gen_ivtbl *ivtbl;
3324  rb_ivar_generic_ivtbl_lookup(obj, &ivtbl);
3325 
3326  if (UNLIKELY(rb_shape_obj_too_complex(obj))) {
3328  .stop = false,
3329  .data = data,
3330  .src = obj,
3331  };
3332  rb_st_foreach_with_replace(
3333  ivtbl->as.complex.table,
3334  obj_iv_hash_traverse_replace_foreach_i,
3335  obj_iv_hash_traverse_replace_i,
3336  (st_data_t)&d
3337  );
3338  if (d.stop) return 1;
3339  }
3340  else {
3341  for (uint32_t i = 0; i < ivtbl->as.shape.numiv; i++) {
3342  if (!UNDEF_P(ivtbl->as.shape.ivptr[i])) {
3343  CHECK_AND_REPLACE(ivtbl->as.shape.ivptr[i]);
3344  }
3345  }
3346  }
3347  }
3348 
3349  switch (BUILTIN_TYPE(obj)) {
3350  // no child node
3351  case T_FLOAT:
3352  case T_BIGNUM:
3353  case T_REGEXP:
3354  case T_FILE:
3355  case T_SYMBOL:
3356  case T_MATCH:
3357  break;
3358  case T_STRING:
3359  rb_str_make_independent(obj);
3360  break;
3361 
3362  case T_OBJECT:
3363  {
3364  if (rb_shape_obj_too_complex(obj)) {
3366  .stop = false,
3367  .data = data,
3368  .src = obj,
3369  };
3370  rb_st_foreach_with_replace(
3371  ROBJECT_IV_HASH(obj),
3372  obj_iv_hash_traverse_replace_foreach_i,
3373  obj_iv_hash_traverse_replace_i,
3374  (st_data_t)&d
3375  );
3376  if (d.stop) return 1;
3377  }
3378  else {
3379  uint32_t len = ROBJECT_IV_COUNT(obj);
3380  VALUE *ptr = ROBJECT_IVPTR(obj);
3381 
3382  for (uint32_t i = 0; i < len; i++) {
3383  CHECK_AND_REPLACE(ptr[i]);
3384  }
3385  }
3386  }
3387  break;
3388 
3389  case T_ARRAY:
3390  {
3391  rb_ary_cancel_sharing(obj);
3392 
3393  for (int i = 0; i < RARRAY_LENINT(obj); i++) {
3394  VALUE e = rb_ary_entry(obj, i);
3395 
3396  if (obj_traverse_replace_i(e, data)) {
3397  return 1;
3398  }
3399  else if (e != data->replacement) {
3400  RARRAY_ASET(obj, i, data->replacement);
3401  }
3402  }
3403  RB_GC_GUARD(obj);
3404  }
3405  break;
3406  case T_HASH:
3407  {
3409  .stop = false,
3410  .data = data,
3411  .src = obj,
3412  };
3413  rb_hash_stlike_foreach_with_replace(obj,
3414  obj_hash_traverse_replace_foreach_i,
3415  obj_hash_traverse_replace_i,
3416  (VALUE)&d);
3417  if (d.stop) return 1;
3418  // TODO: rehash here?
3419 
3420  VALUE ifnone = RHASH_IFNONE(obj);
3421  if (obj_traverse_replace_i(ifnone, data)) {
3422  return 1;
3423  }
3424  else if (ifnone != data->replacement) {
3425  RHASH_SET_IFNONE(obj, data->replacement);
3426  }
3427  }
3428  break;
3429 
3430  case T_STRUCT:
3431  {
3432  long len = RSTRUCT_LEN(obj);
3433  const VALUE *ptr = RSTRUCT_CONST_PTR(obj);
3434 
3435  for (long i=0; i<len; i++) {
3436  CHECK_AND_REPLACE(ptr[i]);
3437  }
3438  }
3439  break;
3440 
3441  case T_RATIONAL:
3442  CHECK_AND_REPLACE(RRATIONAL(obj)->num);
3443  CHECK_AND_REPLACE(RRATIONAL(obj)->den);
3444  break;
3445  case T_COMPLEX:
3446  CHECK_AND_REPLACE(RCOMPLEX(obj)->real);
3447  CHECK_AND_REPLACE(RCOMPLEX(obj)->imag);
3448  break;
3449 
3450  case T_DATA:
3451  if (!data->move && obj_refer_only_shareables_p(obj)) {
3452  break;
3453  }
3454  else {
3455  rb_raise(rb_eRactorError, "can not %s %"PRIsVALUE" object.",
3456  data->move ? "move" : "copy", rb_class_of(obj));
3457  }
3458 
3459  case T_IMEMO:
3460  // not supported yet
3461  return 1;
3462 
3463  // unreachable
3464  case T_CLASS:
3465  case T_MODULE:
3466  case T_ICLASS:
3467  default:
3468  rp(obj);
3469  rb_bug("unreachable");
3470  }
3471 
3472  data->replacement = (VALUE)replacement;
3473 
3474  if (data->leave_func(obj, data) == traverse_stop) {
3475  return 1;
3476  }
3477  else {
3478  return 0;
3479  }
3480 }
3481 
3482 // 0: traverse all
3483 // 1: stopped
3484 static VALUE
3485 rb_obj_traverse_replace(VALUE obj,
3486  rb_obj_traverse_replace_enter_func enter_func,
3487  rb_obj_traverse_replace_leave_func leave_func,
3488  bool move)
3489 {
3490  struct obj_traverse_replace_data data = {
3491  .enter_func = enter_func,
3492  .leave_func = leave_func,
3493  .rec = NULL,
3494  .replacement = Qundef,
3495  .move = move,
3496  };
3497 
3498  if (obj_traverse_replace_i(obj, &data)) {
3499  return Qundef;
3500  }
3501  else {
3502  return data.replacement;
3503  }
3504 }
3505 
3506 struct RVALUE {
3507  VALUE flags;
3508  VALUE klass;
3509  VALUE v1;
3510  VALUE v2;
3511  VALUE v3;
3512 };
3513 
3514 static const VALUE fl_users = FL_USER1 | FL_USER2 | FL_USER3 |
3519 
3520 static void
3521 ractor_moved_bang(VALUE obj)
3522 {
3523  // invalidate src object
3524  struct RVALUE *rv = (void *)obj;
3525 
3526  rv->klass = rb_cRactorMovedObject;
3527  rv->v1 = 0;
3528  rv->v2 = 0;
3529  rv->v3 = 0;
3530  rv->flags = rv->flags & ~fl_users;
3531 
3532  if (BUILTIN_TYPE(obj) == T_OBJECT) ROBJECT_SET_SHAPE_ID(obj, ROOT_SHAPE_ID);
3533 
3534  // TODO: record moved location
3535 }
3536 
3537 static enum obj_traverse_iterator_result
3538 move_enter(VALUE obj, struct obj_traverse_replace_data *data)
3539 {
3540  if (rb_ractor_shareable_p(obj)) {
3541  data->replacement = obj;
3542  return traverse_skip;
3543  }
3544  else {
3545  VALUE moved = rb_obj_alloc(RBASIC_CLASS(obj));
3546  rb_shape_set_shape(moved, rb_shape_get_shape(obj));
3547  data->replacement = moved;
3548  return traverse_cont;
3549  }
3550 }
3551 
3552 void rb_replace_generic_ivar(VALUE clone, VALUE obj); // variable.c
3553 
3554 static enum obj_traverse_iterator_result
3555 move_leave(VALUE obj, struct obj_traverse_replace_data *data)
3556 {
3557  VALUE v = data->replacement;
3558  struct RVALUE *dst = (struct RVALUE *)v;
3559  struct RVALUE *src = (struct RVALUE *)obj;
3560 
3561  dst->flags = (dst->flags & ~fl_users) | (src->flags & fl_users);
3562 
3563  dst->v1 = src->v1;
3564  dst->v2 = src->v2;
3565  dst->v3 = src->v3;
3566 
3567  if (UNLIKELY(FL_TEST_RAW(obj, FL_EXIVAR))) {
3568  rb_replace_generic_ivar(v, obj);
3569  }
3570 
3571  // TODO: generic_ivar
3572 
3573  ractor_moved_bang(obj);
3574  return traverse_cont;
3575 }
3576 
3577 static VALUE
3578 ractor_move(VALUE obj)
3579 {
3580  VALUE val = rb_obj_traverse_replace(obj, move_enter, move_leave, true);
3581  if (!UNDEF_P(val)) {
3582  return val;
3583  }
3584  else {
3585  rb_raise(rb_eRactorError, "can not move the object");
3586  }
3587 }
3588 
3589 static enum obj_traverse_iterator_result
3590 copy_enter(VALUE obj, struct obj_traverse_replace_data *data)
3591 {
3592  if (rb_ractor_shareable_p(obj)) {
3593  data->replacement = obj;
3594  return traverse_skip;
3595  }
3596  else {
3597  data->replacement = rb_obj_clone(obj);
3598  return traverse_cont;
3599  }
3600 }
3601 
3602 static enum obj_traverse_iterator_result
3603 copy_leave(VALUE obj, struct obj_traverse_replace_data *data)
3604 {
3605  return traverse_cont;
3606 }
3607 
3608 static VALUE
3609 ractor_copy(VALUE obj)
3610 {
3611  VALUE val = rb_obj_traverse_replace(obj, copy_enter, copy_leave, false);
3612  if (!UNDEF_P(val)) {
3613  return val;
3614  }
3615  else {
3616  rb_raise(rb_eRactorError, "can not copy the object");
3617  }
3618 }
3619 
3620 // Ractor local storage
3621 
3623  const struct rb_ractor_local_storage_type *type;
3624  void *main_cache;
3625 };
3626 
3628  int cnt;
3629  int capa;
3630  rb_ractor_local_key_t *keys;
3631 } freed_ractor_local_keys;
3632 
3633 static int
3634 ractor_local_storage_mark_i(st_data_t key, st_data_t val, st_data_t dmy)
3635 {
3636  struct rb_ractor_local_key_struct *k = (struct rb_ractor_local_key_struct *)key;
3637  if (k->type->mark) (*k->type->mark)((void *)val);
3638  return ST_CONTINUE;
3639 }
3640 
3641 static enum rb_id_table_iterator_result
3642 idkey_local_storage_mark_i(VALUE val, void *dmy)
3643 {
3644  rb_gc_mark(val);
3645  return ID_TABLE_CONTINUE;
3646 }
3647 
3648 static void
3649 ractor_local_storage_mark(rb_ractor_t *r)
3650 {
3651  if (r->local_storage) {
3652  st_foreach(r->local_storage, ractor_local_storage_mark_i, 0);
3653 
3654  for (int i=0; i<freed_ractor_local_keys.cnt; i++) {
3655  rb_ractor_local_key_t key = freed_ractor_local_keys.keys[i];
3656  st_data_t val, k = (st_data_t)key;
3657  if (st_delete(r->local_storage, &k, &val) &&
3658  (key = (rb_ractor_local_key_t)k)->type->free) {
3659  (*key->type->free)((void *)val);
3660  }
3661  }
3662  }
3663 
3664  if (r->idkey_local_storage) {
3665  rb_id_table_foreach_values(r->idkey_local_storage, idkey_local_storage_mark_i, NULL);
3666  }
3667 }
3668 
3669 static int
3670 ractor_local_storage_free_i(st_data_t key, st_data_t val, st_data_t dmy)
3671 {
3672  struct rb_ractor_local_key_struct *k = (struct rb_ractor_local_key_struct *)key;
3673  if (k->type->free) (*k->type->free)((void *)val);
3674  return ST_CONTINUE;
3675 }
3676 
3677 static void
3678 ractor_local_storage_free(rb_ractor_t *r)
3679 {
3680  if (r->local_storage) {
3681  st_foreach(r->local_storage, ractor_local_storage_free_i, 0);
3682  st_free_table(r->local_storage);
3683  }
3684 
3685  if (r->idkey_local_storage) {
3686  rb_id_table_free(r->idkey_local_storage);
3687  }
3688 }
3689 
3690 static void
3691 rb_ractor_local_storage_value_mark(void *ptr)
3692 {
3693  rb_gc_mark((VALUE)ptr);
3694 }
3695 
3696 static const struct rb_ractor_local_storage_type ractor_local_storage_type_null = {
3697  NULL,
3698  NULL,
3699 };
3700 
3702  NULL,
3703  ruby_xfree,
3704 };
3705 
3706 static const struct rb_ractor_local_storage_type ractor_local_storage_type_value = {
3707  rb_ractor_local_storage_value_mark,
3708  NULL,
3709 };
3710 
3713 {
3715  key->type = type ? type : &ractor_local_storage_type_null;
3716  key->main_cache = (void *)Qundef;
3717  return key;
3718 }
3719 
3722 {
3723  return rb_ractor_local_storage_ptr_newkey(&ractor_local_storage_type_value);
3724 }
3725 
3726 void
3727 rb_ractor_local_storage_delkey(rb_ractor_local_key_t key)
3728 {
3729  RB_VM_LOCK_ENTER();
3730  {
3731  if (freed_ractor_local_keys.cnt == freed_ractor_local_keys.capa) {
3732  freed_ractor_local_keys.capa = freed_ractor_local_keys.capa ? freed_ractor_local_keys.capa * 2 : 4;
3733  REALLOC_N(freed_ractor_local_keys.keys, rb_ractor_local_key_t, freed_ractor_local_keys.capa);
3734  }
3735  freed_ractor_local_keys.keys[freed_ractor_local_keys.cnt++] = key;
3736  }
3737  RB_VM_LOCK_LEAVE();
3738 }
3739 
3740 static bool
3741 ractor_local_ref(rb_ractor_local_key_t key, void **pret)
3742 {
3743  if (rb_ractor_main_p()) {
3744  if (!UNDEF_P((VALUE)key->main_cache)) {
3745  *pret = key->main_cache;
3746  return true;
3747  }
3748  else {
3749  return false;
3750  }
3751  }
3752  else {
3753  rb_ractor_t *cr = GET_RACTOR();
3754 
3755  if (cr->local_storage && st_lookup(cr->local_storage, (st_data_t)key, (st_data_t *)pret)) {
3756  return true;
3757  }
3758  else {
3759  return false;
3760  }
3761  }
3762 }
3763 
3764 static void
3765 ractor_local_set(rb_ractor_local_key_t key, void *ptr)
3766 {
3767  rb_ractor_t *cr = GET_RACTOR();
3768 
3769  if (cr->local_storage == NULL) {
3770  cr->local_storage = st_init_numtable();
3771  }
3772 
3773  st_insert(cr->local_storage, (st_data_t)key, (st_data_t)ptr);
3774 
3775  if (rb_ractor_main_p()) {
3776  key->main_cache = ptr;
3777  }
3778 }
3779 
3780 VALUE
3782 {
3783  void *val;
3784  if (ractor_local_ref(key, &val)) {
3785  return (VALUE)val;
3786  }
3787  else {
3788  return Qnil;
3789  }
3790 }
3791 
3792 bool
3794 {
3795  if (ractor_local_ref(key, (void **)val)) {
3796  return true;
3797  }
3798  else {
3799  return false;
3800  }
3801 }
3802 
3803 void
3805 {
3806  ractor_local_set(key, (void *)val);
3807 }
3808 
3809 void *
3811 {
3812  void *ret;
3813  if (ractor_local_ref(key, &ret)) {
3814  return ret;
3815  }
3816  else {
3817  return NULL;
3818  }
3819 }
3820 
3821 void
3823 {
3824  ractor_local_set(key, ptr);
3825 }
3826 
3827 #define DEFAULT_KEYS_CAPA 0x10
3828 
3829 void
3830 rb_ractor_finish_marking(void)
3831 {
3832  for (int i=0; i<freed_ractor_local_keys.cnt; i++) {
3833  ruby_xfree(freed_ractor_local_keys.keys[i]);
3834  }
3835  freed_ractor_local_keys.cnt = 0;
3836  if (freed_ractor_local_keys.capa > DEFAULT_KEYS_CAPA) {
3837  freed_ractor_local_keys.capa = DEFAULT_KEYS_CAPA;
3838  REALLOC_N(freed_ractor_local_keys.keys, rb_ractor_local_key_t, DEFAULT_KEYS_CAPA);
3839  }
3840 }
3841 
3842 static VALUE
3843 ractor_local_value(rb_execution_context_t *ec, VALUE self, VALUE sym)
3844 {
3845  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
3846  ID id = rb_check_id(&sym);
3847  struct rb_id_table *tbl = cr->idkey_local_storage;
3848  VALUE val;
3849 
3850  if (id && tbl && rb_id_table_lookup(tbl, id, &val)) {
3851  return val;
3852  }
3853  else {
3854  return Qnil;
3855  }
3856 }
3857 
3858 static VALUE
3859 ractor_local_value_set(rb_execution_context_t *ec, VALUE self, VALUE sym, VALUE val)
3860 {
3861  rb_ractor_t *cr = rb_ec_ractor_ptr(ec);
3862  ID id = SYM2ID(rb_to_symbol(sym));
3863  struct rb_id_table *tbl = cr->idkey_local_storage;
3864 
3865  if (tbl == NULL) {
3866  tbl = cr->idkey_local_storage = rb_id_table_create(2);
3867  }
3868  rb_id_table_insert(tbl, id, val);
3869  return val;
3870 }
3871 
3872 #include "ractor.rbinc"
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition: assert.h:219
#define RUBY_ATOMIC_CAS(var, oldval, newval)
Atomic compare-and-swap.
Definition: atomic.h:140
std::atomic< unsigned > rb_atomic_t
Type that is eligible for atomic operations.
Definition: atomic.h:69
#define RUBY_ATOMIC_FETCH_ADD(var, val)
Atomically replaces the value pointed by var with the result of addition of val to the old value of v...
Definition: atomic.h:93
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:685
static VALUE RB_OBJ_FROZEN_RAW(VALUE obj)
This is an implementation detail of RB_OBJ_FROZEN().
Definition: fl_type.h:883
@ RUBY_FL_SHAREABLE
This flag has something to do with Ractor.
Definition: fl_type.h:266
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:980
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:1012
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition: class.c:2635
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
Definition: class.c:2142
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition: class.c:2424
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition: value_type.h:59
#define T_FILE
Old name of RUBY_T_FILE.
Definition: value_type.h:62
#define FL_EXIVAR
Old name of RUBY_FL_EXIVAR.
Definition: fl_type.h:66
#define FL_USER3
Old name of RUBY_FL_USER3.
Definition: fl_type.h:74
#define REALLOC_N
Old name of RB_REALLOC_N.
Definition: memory.h:398
#define ALLOC
Old name of RB_ALLOC.
Definition: memory.h:395
#define T_STRING
Old name of RUBY_T_STRING.
Definition: value_type.h:78
#define FL_USER7
Old name of RUBY_FL_USER7.
Definition: fl_type.h:78
#define FL_USER10
Old name of RUBY_FL_USER10.
Definition: fl_type.h:81
#define Qundef
Old name of RUBY_Qundef.
#define FL_USER6
Old name of RUBY_FL_USER6.
Definition: fl_type.h:77
#define T_FLOAT
Old name of RUBY_T_FLOAT.
Definition: value_type.h:64
#define T_IMEMO
Old name of RUBY_T_IMEMO.
Definition: value_type.h:67
#define FL_USER1
Old name of RUBY_FL_USER1.
Definition: fl_type.h:72
#define ID2SYM
Old name of RB_ID2SYM.
Definition: symbol.h:44
#define T_BIGNUM
Old name of RUBY_T_BIGNUM.
Definition: value_type.h:57
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define T_STRUCT
Old name of RUBY_T_STRUCT.
Definition: value_type.h:79
#define FL_USER19
Old name of RUBY_FL_USER19.
Definition: fl_type.h:90
#define SYM2ID
Old name of RB_SYM2ID.
Definition: symbol.h:45
#define FL_USER12
Old name of RUBY_FL_USER12.
Definition: fl_type.h:83
#define T_DATA
Old name of RUBY_T_DATA.
Definition: value_type.h:60
#define FL_USER11
Old name of RUBY_FL_USER11.
Definition: fl_type.h:82
#define FL_USER15
Old name of RUBY_FL_USER15.
Definition: fl_type.h:86
#define T_MODULE
Old name of RUBY_T_MODULE.
Definition: value_type.h:70
#define FL_USER8
Old name of RUBY_FL_USER8.
Definition: fl_type.h:79
#define FL_USER14
Old name of RUBY_FL_USER14.
Definition: fl_type.h:85
#define FL_USER17
Old name of RUBY_FL_USER17.
Definition: fl_type.h:88
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition: value_type.h:76
#define T_ICLASS
Old name of RUBY_T_ICLASS.
Definition: value_type.h:66
#define T_HASH
Old name of RUBY_T_HASH.
Definition: value_type.h:65
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition: fl_type.h:132
#define FL_USER18
Old name of RUBY_FL_USER18.
Definition: fl_type.h:89
#define FL_USER2
Old name of RUBY_FL_USER2.
Definition: fl_type.h:73
#define FL_USER9
Old name of RUBY_FL_USER9.
Definition: fl_type.h:80
#define Qtrue
Old name of RUBY_Qtrue.
#define FL_USER13
Old name of RUBY_FL_USER13.
Definition: fl_type.h:84
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition: value_type.h:56
#define T_OBJECT
Old name of RUBY_T_OBJECT.
Definition: value_type.h:75
#define NIL_P
Old name of RB_NIL_P.
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition: value_type.h:80
#define T_MATCH
Old name of RUBY_T_MATCH.
Definition: value_type.h:69
#define FL_USER16
Old name of RUBY_FL_USER16.
Definition: fl_type.h:87
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition: value_type.h:58
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition: value_type.h:85
#define FL_USER5
Old name of RUBY_FL_USER5.
Definition: fl_type.h:76
#define FL_USER4
Old name of RUBY_FL_USER4.
Definition: fl_type.h:75
#define FL_SET_RAW
Old name of RB_FL_SET_RAW.
Definition: fl_type.h:130
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition: value_type.h:77
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3627
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:676
int rb_typeddata_is_kind_of(VALUE obj, const rb_data_type_t *data_type)
Checks if the given object is of given kind.
Definition: error.c:1353
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:1088
VALUE rb_eRuntimeError
RuntimeError exception.
Definition: error.c:1401
VALUE rb_eStopIteration
StopIteration exception.
Definition: enumerator.c:181
VALUE rb_exc_new_str(VALUE etype, VALUE str)
Identical to rb_exc_new_cstr(), except it takes a Ruby's string instead of C's.
Definition: error.c:1454
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1404
VALUE rb_ensure(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*e_proc)(VALUE), VALUE data2)
An equivalent to ensure clause.
Definition: eval.c:1045
VALUE rb_obj_alloc(VALUE klass)
Allocates an instance of the given class.
Definition: object.c:2091
VALUE rb_cRactor
Ractor class.
Definition: ractor.c:23
VALUE rb_stdin
STDIN constant.
Definition: io.c:201
VALUE rb_stderr
STDERR constant.
Definition: io.c:201
static VALUE rb_class_of(VALUE obj)
Object to class mapping function.
Definition: globals.h:172
VALUE rb_cBasicObject
BasicObject class.
Definition: object.c:64
VALUE rb_obj_clone(VALUE obj)
Produces a shallow copy of the given object.
Definition: object.c:519
VALUE rb_stdout
STDOUT constant.
Definition: io.c:201
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition: gc.h:615
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition: gc.h:603
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
Definition: encoding.h:768
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
Definition: encoding.c:1013
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
Definition: encoding.h:417
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition: vm_eval.c:1099
void rb_gc_mark(VALUE obj)
Marks an object.
Definition: gc.c:2094
VALUE rb_gc_disable(void)
Disables GC.
Definition: gc.c:3566
VALUE rb_gc_start(void)
Identical to rb_gc(), except the return value.
Definition: gc.c:3363
VALUE rb_gc_enable(void)
(Re-) enables GC.
Definition: gc.c:3532
VALUE rb_ary_new(void)
Allocates a new, empty array.
Definition: array.c:747
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
Definition: array.c:1384
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
Definition: array.c:753
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
Definition: array.c:1737
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition: proc.c:119
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
Definition: string.h:1670
VALUE rb_str_new_frozen(VALUE str)
Creates a frozen copy of the string, if necessary.
Definition: string.c:1446
VALUE rb_mutex_new(void)
Creates a mutex.
Definition: thread_sync.c:191
void rb_unblock_function_t(void *)
This is the type of UBFs.
Definition: thread.h:336
void rb_thread_check_ints(void)
Checks for interrupts.
Definition: thread.c:1416
VALUE rb_mutex_unlock(VALUE mutex)
Releases the mutex.
Definition: thread_sync.c:505
VALUE rb_mutex_lock(VALUE mutex)
Attempts to lock the mutex.
Definition: thread_sync.c:432
void rb_thread_sleep(int sec)
Blocks for the given period of time.
Definition: thread.c:1439
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition: variable.c:1859
void rb_ivar_foreach(VALUE obj, int(*func)(ID name, VALUE val, st_data_t arg), st_data_t arg)
Iterates over an object's instance variables.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition: vm_method.c:1286
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition: symbol.c:1117
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition: symbol.c:823
VALUE rb_to_symbol(VALUE name)
Identical to rb_intern_str(), except it generates a dynamic symbol if necessary.
Definition: string.c:12460
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition: io.h:2
int len
Length of the buffer.
Definition: io.h:8
const struct rb_ractor_local_storage_type rb_ractor_local_storage_type_free
A type of ractor-local storage that destructs itself using ruby_xfree.
Definition: ractor.c:3701
VALUE rb_ractor_make_shareable_copy(VALUE obj)
Identical to rb_ractor_make_shareable(), except it returns a (deep) copy of the passed one instead of...
Definition: ractor.c:3074
struct rb_ractor_local_key_struct * rb_ractor_local_key_t
(Opaque) struct that holds a ractor-local storage key.
Definition: ractor.h:42
void rb_ractor_local_storage_ptr_set(rb_ractor_local_key_t key, void *ptr)
Identical to rb_ractor_local_storage_value_set() except the parameter type.
Definition: ractor.c:3822
rb_ractor_local_key_t rb_ractor_local_storage_ptr_newkey(const struct rb_ractor_local_storage_type *type)
Extended version of rb_ractor_local_storage_value_newkey().
Definition: ractor.c:3712
VALUE rb_ractor_stderr(void)
Queries the standard error of the current Ractor that is calling this function.
Definition: ractor.c:2711
VALUE rb_ractor_stdin(void)
Queries the standard input of the current Ractor that is calling this function.
Definition: ractor.c:2687
static bool rb_ractor_shareable_p(VALUE obj)
Queries if multiple Ractors can share the passed object or not.
Definition: ractor.h:249
void rb_ractor_stderr_set(VALUE io)
Assigns an IO to the standard error of the Ractor that is calling this function.
Definition: ractor.c:2747
void rb_ractor_local_storage_value_set(rb_ractor_local_key_t key, VALUE val)
Associates the passed value to the passed key.
Definition: ractor.c:3804
void * rb_ractor_local_storage_ptr(rb_ractor_local_key_t key)
Identical to rb_ractor_local_storage_value() except the return type.
Definition: ractor.c:3810
bool rb_ractor_local_storage_value_lookup(rb_ractor_local_key_t key, VALUE *val)
Queries the key.
Definition: ractor.c:3793
VALUE rb_ractor_stdout(void)
Queries the standard output of the current Ractor that is calling this function.
Definition: ractor.c:2699
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
Definition: ractor.h:235
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
Definition: ractor.c:3065
rb_ractor_local_key_t rb_ractor_local_storage_value_newkey(void)
Issues a new key.
Definition: ractor.c:3721
void rb_ractor_stdout_set(VALUE io)
Assigns an IO to the standard output of the Ractor that is calling this function.
Definition: ractor.c:2735
void rb_ractor_stdin_set(VALUE io)
Assigns an IO to the standard input of the Ractor that is calling this function.
Definition: ractor.c:2723
VALUE rb_ractor_local_storage_value(rb_ractor_local_key_t key)
Queries the key.
Definition: ractor.c:3781
#define RB_NOGVL_UBF_ASYNC_SAFE
Passing this flag to rb_nogvl() indicates that the passed UBF is async-signal-safe.
Definition: thread.h:60
#define RB_NOGVL_INTR_FAIL
Passing this flag to rb_nogvl() prevents it from checking interrupts.
Definition: thread.h:48
void * rb_nogvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2, int flags)
Identical to rb_thread_call_without_gvl(), except it additionally takes "flags" that change the behav...
Definition: thread.c:1521
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition: sprintf.c:1217
VALUE rb_yield(VALUE val)
Yields the block.
Definition: vm_eval.c:1354
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition: memory.h:162
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition: cxxanyargs.hpp:56
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
Definition: cxxanyargs.hpp:432
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition: rarray.h:281
static void RARRAY_ASET(VALUE ary, long i, VALUE v)
Assigns an object in an array.
Definition: rarray.h:386
#define RARRAY_AREF(a, i)
Definition: rarray.h:403
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
Definition: rarray.h:52
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition: rbasic.h:150
#define DATA_PTR(obj)
Convenient getter macro.
Definition: rdata.h:67
#define RHASH_SET_IFNONE(h, ifnone)
Destructively updates the default value of the hash.
Definition: rhash.h:92
#define RHASH_IFNONE(h)
Definition: rhash.h:59
static VALUE * ROBJECT_IVPTR(VALUE obj)
Queries the instance variables.
Definition: robject.h:136
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition: rstring.h:416
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition: rstring.h:89
static long RSTRUCT_LEN(VALUE st)
Returns the number of struct members.
Definition: rstruct.h:94
static bool RTYPEDDATA_P(VALUE obj)
Checks whether the passed object is RTypedData or RData.
Definition: rtypeddata.h:579
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition: rtypeddata.h:449
static const struct rb_data_type_struct * RTYPEDDATA_TYPE(VALUE obj)
Queries for the type of given object.
Definition: rtypeddata.h:602
#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:497
static bool RB_SPECIAL_CONST_P(VALUE obj)
Checks if the given object is of enum ruby_special_consts.
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
Definition: rtypeddata.h:200
Type that defines a ractor-local storage.
Definition: ractor.h:21
void(* free)(void *ptr)
A function to destruct a ractor-local storage.
Definition: ractor.h:37
void(* mark)(void *ptr)
A function to mark a ractor-local storage.
Definition: ractor.h:29
Definition: st.h:79
Definition: string.c:8257
void rb_native_mutex_lock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_lock.
void rb_native_cond_initialize(rb_nativethread_cond_t *cond)
Fills the passed condition variable with an initial value.
void rb_native_cond_broadcast(rb_nativethread_cond_t *cond)
Signals a condition variable.
void rb_native_mutex_initialize(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_initialize.
void rb_native_mutex_unlock(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_unlock.
void rb_native_mutex_destroy(rb_nativethread_lock_t *lock)
Just another name of rb_nativethread_lock_destroy.
void rb_native_cond_destroy(rb_nativethread_cond_t *cond)
Destroys the passed condition variable.
void rb_native_cond_signal(rb_nativethread_cond_t *cond)
Signals a condition variable.
void rb_native_cond_wait(rb_nativethread_cond_t *cond, rb_nativethread_lock_t *mutex)
Waits for the passed condition variable to be signalled.
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
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:376
void ruby_xfree(void *ptr)
Deallocates a storage instance.
Definition: gc.c:4264