farce 0.4.0-x86-linux-gnu → 0.4.1-x86-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/farce/reactor.c CHANGED
@@ -345,6 +345,9 @@ void fs_watch_io(fs_state *s, fs_wait *w, int fd, int events, int group, long in
345
345
  }
346
346
  }
347
347
 
348
+ // Cancel the outstanding futex operation before its caller releases the word.
349
+ static void fs_cancel_futex(VALUE self, fs_wait *w);
350
+
348
351
  // Finish a wait and remove its watches and ready-queue entry.
349
352
  // A pending receive must stop using the buffer before cleanup can continue.
350
353
  // Clear Ruby references and keep the fiber's slot available for its next wait.
@@ -352,6 +355,12 @@ VALUE fs_retire(VALUE self, VALUE token) {
352
355
  fs_state *s = fs_get(self); fs_wait *w = fs_lookup(s, token);
353
356
  if (!w) return Qnil;
354
357
  if (w->receive_pending) fs_cancel_receive(self, w);
358
+ if (w->futex_pending) {
359
+ // Logical cancellation must not discard the lookup required by the CQE.
360
+ // Suppress resumption first, then consume the original completion.
361
+ w->status = 2;
362
+ fs_cancel_futex(self, w);
363
+ }
355
364
  ready_remove(s, w);
356
365
  fs_watch *watch = w->watches;
357
366
  while (watch) {
@@ -381,6 +390,56 @@ VALUE fs_retire(VALUE self, VALUE token) {
381
390
  return Qnil;
382
391
  }
383
392
 
393
+ // Select asynchronous futex parking only for an installed, live native ring.
394
+ // External schedulers require a later integration instead of a duck-typed hook.
395
+ bool containers_futex_scheduler_supported(VALUE scheduler) {
396
+ #ifdef FARCE_HAVE_URING_FUTEX
397
+ if (NIL_P(scheduler) || !rb_typeddata_is_kind_of(scheduler, &fs_type)) return false;
398
+ fs_state *s = fs_get(scheduler);
399
+ return s->driver == FS_URING_DRIVER && s->ring_live && s->futex_supported &&
400
+ !s->closed && !s->stopping && s->owner == rb_thread_current() &&
401
+ s->active == rb_fiber_current() && rb_fiber_scheduler_get() == scheduler &&
402
+ containers_futex_available();
403
+ #else
404
+ return false;
405
+ #endif
406
+ }
407
+
408
+ typedef struct {
409
+ VALUE self, token, timeout;
410
+ containers_futex_t *word;
411
+ } fs_futex_wait;
412
+
413
+ // Submit and park using one suspension. The surrounding ensure retains the word
414
+ // until the kernel request has completed, even if timer setup or parking raises.
415
+ static VALUE futex_wait_body(VALUE data) {
416
+ fs_futex_wait *wait = (fs_futex_wait *)data;
417
+ fs_state *s = fs_get(wait->self);
418
+ fs_wait *w = fs_lookup(s, wait->token);
419
+ int error = fs_driver_futex(s, w, wait->word);
420
+ if (error) rb_syserr_fail(error, "scheduler futex submission");
421
+ return rb_funcall(wait->self, rb_intern("wait_with_timeout"), 3,
422
+ wait->token, wait->timeout, Qfalse);
423
+ }
424
+
425
+ static VALUE futex_wait_cleanup(VALUE data) {
426
+ fs_futex_wait *wait = (fs_futex_wait *)data;
427
+ return fs_retire(wait->self, wait->token);
428
+ }
429
+
430
+ bool containers_futex_scheduler_wait(VALUE scheduler, containers_futex_t *word, VALUE timeout) {
431
+ if (!containers_futex_scheduler_supported(scheduler)) return true;
432
+ fs_futex_wait wait = {scheduler, fs_arm(scheduler), timeout, word};
433
+ return RTEST(rb_ensure(futex_wait_body, (VALUE)&wait, futex_wait_cleanup, (VALUE)&wait));
434
+ }
435
+
436
+ // Expose the backend capability privately so native-path validation cannot pass
437
+ // by silently exercising a descriptor fallback.
438
+ static VALUE futex_supported(VALUE self) {
439
+ fs_state *s = fs_get(self);
440
+ return !s->closed && !s->stopping && s->futex_supported && containers_futex_available() ? Qtrue : Qfalse;
441
+ }
442
+
384
443
  // Switch from the root to a ready fiber.
385
444
  // If a newly started child finishes, return control to its waiting parent
386
445
  // before returning to the root dispatcher.
@@ -600,6 +659,27 @@ static int snapshot_fallback(st_data_t key, st_data_t value, st_data_t arg) {
600
659
  // Wake-pipe events ask the dispatcher to return to Ruby policy code.
601
660
  static void accept_event(VALUE self, fs_event *e) {
602
661
  fs_state *s = fs_get(self);
662
+ if (e->generation & FS_FUTEX) {
663
+ fs_wait *w = fs_lookup(s, ULL2NUM(e->generation & ~FS_OPERATION));
664
+ if (!w) return;
665
+ w->futex_pending = 0;
666
+ int error = e->error < 0 ? -e->error : 0;
667
+ if (error == ENOSYS || error == EOPNOTSUPP) s->futex_supported = 0;
668
+ if (!w->status) {
669
+ if (error == ENOSYS || error == EOPNOTSUPP) {
670
+ // Retry with the now-disabled capability, rather than reporting
671
+ // a timeout to consumers that have not reached their deadline.
672
+ fs_resume(self, w->token, Qtrue, Qnil);
673
+ } else {
674
+ // Early notification is reported as EAGAIN. Other interruptions
675
+ // also return to the container's condition/deadline check.
676
+ if (error != EAGAIN && error != EINTR && error != ECANCELED)
677
+ w->native_error = error;
678
+ fs_resume(self, w->token, Qtrue, Qnil);
679
+ }
680
+ }
681
+ return;
682
+ }
603
683
  if (e->generation & FS_RECEIVE) {
604
684
  fs_wait *w = fs_lookup(s, ULL2NUM(e->generation & ~FS_RECEIVE));
605
685
  if (!w) return;
@@ -660,36 +740,44 @@ static void poll_ready(VALUE self) {
660
740
  if (s->poll_error || s->selected_pending) s->policy_pending = 1;
661
741
  }
662
742
 
663
- // Cancel an io_uring receive and wait until the original operation has finished.
664
- // Keep the caller's buffer locked throughout, because a cancellation reply alone
665
- // does not prove the kernel has stopped writing to it.
666
- void fs_cancel_receive(VALUE self, fs_wait *w) {
743
+ // Cancel an operation and consume its original CQE before releasing storage.
744
+ // Cancellation acknowledgements use zero userdata, so a delayed acknowledgement
745
+ // cannot refer to an operation record or a subsequently reused suspension.
746
+ static void cancel_operation(VALUE self, fs_wait *w, uint64_t kind, int *pending) {
667
747
  #ifdef FARCE_URING
668
748
  fs_state *s = fs_get(self);
669
749
  struct io_uring_sqe *entry = io_uring_get_sqe(&s->ring);
670
750
  if (!entry) {
671
751
  int result;
672
752
  do { result = io_uring_submit(&s->ring); } while (result == -EINTR);
673
- if (result < 0) rb_bug("cannot submit pending scheduler receive");
753
+ if (result < 0) rb_bug("cannot submit pending scheduler operation");
674
754
  entry = io_uring_get_sqe(&s->ring);
675
755
  if (!entry) rb_bug("cannot reserve scheduler cancellation");
676
756
  }
677
- io_uring_prep_cancel64(entry, FS_RECEIVE | NUM2ULL(w->token), 0);
757
+ io_uring_prep_cancel64(entry, kind | NUM2ULL(w->token), 0);
678
758
  entry->user_data = 0;
679
- // Wait for the original receive CQE, not just the cancellation CQE. No Ruby
680
- // fiber runs here, and the caller's buffer remains locked throughout.
681
- while (w->receive_pending) {
759
+ // Do not run fibers here. The caller's buffer or futex word stays alive
760
+ // until the original completion, rather than just the cancellation reply.
761
+ while (*pending) {
682
762
  int result = io_uring_submit_and_wait(&s->ring, 1);
683
- if (result < 0 && result != -EINTR) rb_bug("cannot acknowledge scheduler receive cancellation");
763
+ if (result < 0 && result != -EINTR) rb_bug("cannot acknowledge scheduler cancellation");
684
764
  s->timeout = 0;
685
765
  fs_driver_poll(s);
686
- if (s->poll_error && s->poll_error != EINTR) rb_bug("cannot drain scheduler receive cancellation");
766
+ if (s->poll_error && s->poll_error != EINTR) rb_bug("cannot drain scheduler cancellation");
687
767
  for (int i = 0; i < s->event_count; i++) accept_event(self, &s->events[i]);
688
768
  rearm_events(s);
689
769
  }
690
770
  #endif
691
771
  }
692
772
 
773
+ void fs_cancel_receive(VALUE self, fs_wait *w) {
774
+ cancel_operation(self, w, FS_RECEIVE, &w->receive_pending);
775
+ }
776
+
777
+ static void fs_cancel_futex(VALUE self, fs_wait *w) {
778
+ cancel_operation(self, w, FS_FUTEX, &w->futex_pending);
779
+ }
780
+
693
781
  // Call a helper for every active wait, skipping empty slots and idle fibers.
694
782
  static void each_wait(fs_state *s, int (*callback)(st_data_t, st_data_t, st_data_t), st_data_t arg) {
695
783
  for (uint32_t i = 0; i < s->slot_count; i++) {
@@ -836,6 +924,7 @@ void farce_init_fiber_scheduler(VALUE internal) {
836
924
  rb_define_private_method(klass, "retire_wait", fs_retire, 1);
837
925
  rb_define_private_method(klass, "register_select", register_select, 3);
838
926
  rb_define_private_method(klass, "ready?", ready, 0);
927
+ rb_define_private_method(klass, "futex_supported?", futex_supported, 0);
839
928
  rb_define_private_method(klass, "pending?", pending, 0);
840
929
  rb_define_private_method(klass, "pending_count", pending_count, 0);
841
930
  rb_define_private_method(klass, "wakeup", wakeup, 0);
data/ext/farce/reactor.h CHANGED
@@ -1,6 +1,7 @@
1
1
  #ifndef FARCE_FIBER_REACTOR_H
2
2
  #define FARCE_FIBER_REACTOR_H
3
3
  #include "containers.h"
4
+ #include "futex.h"
4
5
  #include <ruby/io.h>
5
6
  #include <ruby/io/buffer.h>
6
7
  #include <ruby/thread.h>
@@ -21,6 +22,8 @@
21
22
  #define FS_WRITE 4
22
23
  #define FS_EVENTS 1024
23
24
  #define FS_RECEIVE (UINT64_C(1) << 63)
25
+ #define FS_FUTEX (UINT64_C(1) << 62)
26
+ #define FS_OPERATION (FS_RECEIVE | FS_FUTEX)
24
27
  #define FS_SLOT_BITS 20
25
28
  #define FS_SLOT_MASK ((1U << FS_SLOT_BITS) - 1)
26
29
 
@@ -45,7 +48,7 @@ struct fs_reg {
45
48
  struct fs_wait {
46
49
  VALUE token, fiber, value, error, groups, ios;
47
50
  uint32_t slot;
48
- int status, events, selected, queued, native_error, receive_pending;
51
+ int status, events, selected, queued, native_error, receive_pending, futex_pending;
49
52
  fs_wait *ready_next, *ready_prev;
50
53
  fs_watch *watches;
51
54
  fs_watch first_watch;
@@ -62,7 +65,7 @@ typedef struct {
62
65
  size_t pending;
63
66
  uint64_t serial, generation, suspensions;
64
67
  unsigned immediate;
65
- int selected_pending, receive_disabled;
68
+ int selected_pending, receive_disabled, futex_supported;
66
69
  int driver, descriptor, wake[2], closed;
67
70
  pthread_mutex_t wake_mutex;
68
71
  #ifdef FARCE_URING
@@ -85,6 +88,7 @@ static inline fs_state *fs_get(VALUE self) {
85
88
  int fs_driver_init(fs_state *s, int driver);
86
89
  int fs_driver_update(fs_state *s, fs_reg *reg, int events);
87
90
  int fs_driver_receive(fs_state *s, fs_wait *w, int fd, void *buffer, size_t size);
91
+ int fs_driver_futex(fs_state *s, fs_wait *w, containers_futex_t *word);
88
92
  void fs_cancel_receive(VALUE self, fs_wait *w);
89
93
  void fs_driver_close(fs_state *s);
90
94
  void *fs_driver_poll(void *data);
data/ext/farce/signal.c CHANGED
@@ -1,21 +1,16 @@
1
1
  #include "containers.h"
2
- #include "ruby/io.h"
2
+ #include "native_wait.h"
3
3
 
4
- #include <errno.h>
5
- #include <fcntl.h>
6
4
  #include <math.h>
7
5
  #include <stdint.h>
8
6
  #include <time.h>
9
- #include <unistd.h>
10
7
 
11
8
  static VALUE cSignal;
12
9
 
13
10
  typedef struct containers_signal_waiter containers_signal_waiter_t;
14
11
 
15
12
  struct containers_signal_waiter {
16
- int read_fd;
17
- int write_fd;
18
- bool notified;
13
+ containers_waiter_t notification;
19
14
  containers_signal_waiter_t *next;
20
15
  };
21
16
 
@@ -66,34 +61,13 @@ containers_signal_parse_timeout(VALUE value)
66
61
  return timeout;
67
62
  }
68
63
 
69
- static void
70
- containers_signal_set_fd_flags(int fd)
71
- {
72
- #ifdef _WIN32
73
- (void)fd;
74
- #else
75
- int flags = fcntl(fd, F_GETFL, 0);
76
- if (flags >= 0) (void)fcntl(fd, F_SETFL, flags | O_NONBLOCK);
77
- flags = fcntl(fd, F_GETFD, 0);
78
- if (flags >= 0) (void)fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
79
- #endif
80
- }
81
-
82
- /* Called with signal->lock held. Each waiter has a separate descriptor, so a
83
- * single broadcast makes every scheduled Fiber or native thread runnable. */
64
+ /* Called with signal->lock held. The notification remains latched while
65
+ * the waiting caller selects its parking backend outside the state mutex. */
84
66
  static void
85
67
  containers_signal_notify_waiters_locked(containers_signal_t *signal)
86
68
  {
87
- unsigned char byte = 1;
88
69
  for (containers_signal_waiter_t *waiter = signal->waiters; waiter; waiter = waiter->next) {
89
- if (waiter->notified) continue;
90
- ssize_t result;
91
- do {
92
- result = write(waiter->write_fd, &byte, 1);
93
- } while (result < 0 && errno == EINTR);
94
- if (result == 1 || (result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) {
95
- waiter->notified = true;
96
- }
70
+ (void)containers_waiter_notify(&waiter->notification);
97
71
  }
98
72
  }
99
73
 
@@ -112,7 +86,7 @@ containers_signal_memsize(const void *pointer)
112
86
  }
113
87
 
114
88
  static const rb_data_type_t containers_signal_type = {
115
- .wrap_struct_name = "Ractor::Containers::Signal",
89
+ .wrap_struct_name = "Farce::Internal::Signal",
116
90
  .function = {
117
91
  .dmark = NULL,
118
92
  .dfree = containers_signal_free,
@@ -184,26 +158,14 @@ containers_signal_new(void)
184
158
  return containers_signal_initialize(signal);
185
159
  }
186
160
 
187
- static bool
188
- containers_signal_wait_for_descriptor(int fd, containers_signal_timeout_t *timeout)
189
- {
190
- VALUE wait_timeout = Qnil;
191
- if (timeout->finite) {
192
- double remaining = timeout->deadline - containers_signal_monotonic_now();
193
- if (remaining <= 0) return false;
194
- wait_timeout = DBL2NUM(remaining);
195
- }
196
-
197
- return containers_wait_for_readable(fd, wait_timeout);
198
- }
199
-
200
161
  static VALUE
201
162
  containers_signal_wait_body(VALUE opaque)
202
163
  {
203
164
  containers_signal_wait_context_t *context = (containers_signal_wait_context_t *)opaque;
204
- return containers_signal_wait_for_descriptor(context->waiter.read_fd, context->timeout)
205
- ? Qtrue
206
- : Qfalse;
165
+ return containers_waiter_wait(
166
+ &context->waiter.notification, &context->signal->lock,
167
+ context->timeout->finite, context->timeout->deadline, NULL
168
+ ) ? Qtrue : Qfalse;
207
169
  }
208
170
 
209
171
  static VALUE
@@ -220,8 +182,7 @@ containers_signal_wait_cleanup(VALUE opaque)
220
182
  }
221
183
  }
222
184
  pthread_mutex_unlock(&context->signal->lock);
223
- close(context->waiter.read_fd);
224
- close(context->waiter.write_fd);
185
+ containers_waiter_close(&context->waiter.notification);
225
186
  return Qnil;
226
187
  }
227
188
 
@@ -232,24 +193,12 @@ containers_signal_wait_once(
232
193
  containers_signal_timeout_t *timeout
233
194
  )
234
195
  {
235
- int descriptors[2];
236
- if (pipe(descriptors) != 0) {
237
- pthread_mutex_unlock(&signal->lock);
238
- rb_sys_fail("pipe");
239
- }
240
- containers_signal_set_fd_flags(descriptors[0]);
241
- containers_signal_set_fd_flags(descriptors[1]);
242
-
243
196
  containers_signal_wait_context_t context = {
244
197
  .signal = signal,
245
- .waiter = {
246
- .read_fd = descriptors[0],
247
- .write_fd = descriptors[1],
248
- .notified = false,
249
- .next = signal->waiters,
250
- },
198
+ .waiter = {.next = signal->waiters},
251
199
  .timeout = timeout,
252
200
  };
201
+ containers_waiter_init(&context.waiter.notification);
253
202
  signal->waiters = &context.waiter;
254
203
  pthread_mutex_unlock(&signal->lock);
255
204
  return RTEST(rb_ensure(
@@ -330,6 +279,10 @@ containers_signal_wait(int argc, VALUE *argv, VALUE self)
330
279
  pthread_mutex_unlock(&signal->lock);
331
280
  return ULL2NUM(generation);
332
281
  }
282
+ if (timeout.finite && timeout.deadline <= containers_signal_monotonic_now()) {
283
+ pthread_mutex_unlock(&signal->lock);
284
+ return containers_signal_timeout_result();
285
+ }
333
286
  bool awakened = containers_signal_wait_once(signal, &timeout);
334
287
  pthread_mutex_lock(&signal->lock);
335
288
  if (!awakened && signal->generation == observed) {
data/ext/farce/tree_map.c CHANGED
@@ -1,4 +1,5 @@
1
1
  #include "containers.h"
2
+ #include "native_wait.h"
2
3
  #include "transaction.h"
3
4
  #include <ruby/atomic.h>
4
5
  #include <ruby/fiber/scheduler.h>
@@ -558,9 +559,7 @@ tc_map_store_unlocked(VALUE self, tc_map *map, VALUE key, VALUE value,
558
559
  typedef struct tc_core_map_waiter tc_core_map_waiter;
559
560
 
560
561
  struct tc_core_map_waiter {
561
- int read_fd;
562
- int write_fd;
563
- bool notified;
562
+ containers_waiter_t notification;
564
563
  tc_core_map_waiter *next;
565
564
  };
566
565
 
@@ -787,40 +786,14 @@ typedef struct {
787
786
  tc_core_map_waiter waiter;
788
787
  } tc_core_map_fiber_wait;
789
788
 
790
- static void
791
- tc_core_map_set_fd_flags(int fd)
792
- {
793
- #ifdef _WIN32
794
- (void)fd;
795
- #else
796
- int flags = fcntl(fd, F_GETFL, 0);
797
- if (flags >= 0) (void)fcntl(fd, F_SETFL, flags | O_NONBLOCK);
798
- flags = fcntl(fd, F_GETFD, 0);
799
- if (flags >= 0) (void)fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
800
- #endif
801
- }
802
-
803
789
  /* Called with core->lock held. The waiter remains linked until its ensure
804
790
  * cleanup so an interrupt cannot leave a dangling stack pointer behind. */
805
791
  static void
806
792
  tc_core_map_notify_one_locked(tc_core_map *core)
807
793
  {
808
- unsigned char byte = 1;
809
-
810
- for (tc_core_map_waiter *waiter = core->waiters;
811
- waiter != NULL;
812
- waiter = waiter->next) {
813
- ssize_t result;
814
-
815
- if (waiter->notified) continue;
816
- do {
817
- result = write(waiter->write_fd, &byte, 1);
818
- } while (result < 0 && errno == EINTR);
819
- if (result == 1 ||
820
- (result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) {
821
- waiter->notified = true;
822
- return;
823
- }
794
+ for (tc_core_map_waiter *waiter = core->waiters; waiter; waiter = waiter->next) {
795
+ if (waiter->notification.notified) continue;
796
+ if (containers_waiter_notify(&waiter->notification)) return;
824
797
  }
825
798
  }
826
799
 
@@ -828,7 +801,9 @@ static VALUE
828
801
  tc_core_map_fiber_wait_body(VALUE opaque)
829
802
  {
830
803
  tc_core_map_fiber_wait *wait = (tc_core_map_fiber_wait *)opaque;
831
- return containers_wait_for_readable(wait->waiter.read_fd, Qnil) ? Qtrue : Qfalse;
804
+ return containers_waiter_wait(
805
+ &wait->waiter.notification, &wait->core->lock, false, 0, NULL
806
+ ) ? Qtrue : Qfalse;
832
807
  }
833
808
 
834
809
  static VALUE
@@ -851,9 +826,9 @@ tc_core_map_fiber_wait_cleanup(VALUE opaque)
851
826
  }
852
827
  /* Notification makes a waiter runnable, but does not reserve ownership.
853
828
  * Always pass the baton if a notified waiter leaves while ownerless,
854
- * including cancellation after rb_io_wait returned but before the lock
829
+ * including cancellation after a wait returned but before the lock
855
830
  * acquisition loop claimed ownership. */
856
- if (wait->waiter.notified &&
831
+ if (wait->waiter.notification.notified &&
857
832
  NIL_P(wait->core->owner_fiber) &&
858
833
  !tc_core_map_publication_busy(
859
834
  RUBY_ATOMIC_LOAD(wait->core->publication_state)
@@ -862,8 +837,7 @@ tc_core_map_fiber_wait_cleanup(VALUE opaque)
862
837
  }
863
838
  pthread_mutex_unlock(&wait->core->lock);
864
839
 
865
- close(wait->waiter.read_fd);
866
- close(wait->waiter.write_fd);
840
+ containers_waiter_close(&wait->waiter.notification);
867
841
  return Qnil;
868
842
  }
869
843
 
@@ -871,24 +845,11 @@ tc_core_map_fiber_wait_cleanup(VALUE opaque)
871
845
  static void
872
846
  tc_core_map_wait_once(tc_core_map *core)
873
847
  {
874
- int descriptors[2];
875
-
876
- if (pipe(descriptors) != 0) {
877
- pthread_mutex_unlock(&core->lock);
878
- rb_sys_fail("pipe");
879
- }
880
- tc_core_map_set_fd_flags(descriptors[0]);
881
- tc_core_map_set_fd_flags(descriptors[1]);
882
-
883
848
  tc_core_map_fiber_wait wait = {
884
849
  .core = core,
885
- .waiter = {
886
- .read_fd = descriptors[0],
887
- .write_fd = descriptors[1],
888
- .notified = false,
889
- .next = NULL,
890
- },
850
+ .waiter = {.next = NULL},
891
851
  };
852
+ containers_waiter_init(&wait.waiter.notification);
892
853
 
893
854
  if (core->last_waiter != NULL) core->last_waiter->next = &wait.waiter;
894
855
  else core->waiters = &wait.waiter;
@@ -904,8 +865,8 @@ tc_core_map_wait_once(tc_core_map *core)
904
865
  }
905
866
 
906
867
  /* CRuby needs logical Fiber ownership rather than pthread ownership: multiple
907
- * scheduled Fibers can contend on the same native thread. Descriptor waits
908
- * let rb_io_wait park only the current Fiber while its owner resumes. */
868
+ * scheduled Fibers can contend on the same native thread. Native notifications
869
+ * park only the current Fiber while its owner resumes. */
909
870
  static void
910
871
  tc_core_map_lock(VALUE self, tc_core_map *core)
911
872
  {
@@ -40,7 +40,7 @@ containers_marker_size(const void *pointer)
40
40
  }
41
41
 
42
42
  static const rb_data_type_t containers_pin_marker_type = {
43
- .wrap_struct_name = "Ractor::Containers pin marker",
43
+ .wrap_struct_name = "Farce pin marker",
44
44
  .function = {
45
45
  .dmark = containers_marker_mark,
46
46
  .dfree = RUBY_TYPED_DEFAULT_FREE,
@@ -1,6 +1,18 @@
1
- #include "ruby.h"
1
+ #include "futex.h"
2
2
 
3
3
  void farce_init_fiber_scheduler(VALUE internal) {
4
4
  (void)internal;
5
5
  rb_raise(rb_eLoadError, "native Farce fiber scheduling requires epoll or kqueue; use the select implementation");
6
6
  }
7
+
8
+ bool containers_futex_scheduler_supported(VALUE scheduler) {
9
+ (void)scheduler;
10
+ return false;
11
+ }
12
+
13
+ bool containers_futex_scheduler_wait(VALUE scheduler, containers_futex_t *word, VALUE timeout) {
14
+ (void)scheduler;
15
+ (void)word;
16
+ (void)timeout;
17
+ return true;
18
+ }
data/ext/farce/vector.c CHANGED
@@ -1,15 +1,13 @@
1
1
  #include "containers.h"
2
+ #include "native_wait.h"
2
3
  #include "transaction.h"
3
- #include "ruby/io.h"
4
4
  #include "ruby/fiber/scheduler.h"
5
5
 
6
6
  #include <errno.h>
7
- #include <fcntl.h>
8
7
  #include <math.h>
9
8
  #include <stdint.h>
10
9
  #include <stdlib.h>
11
10
  #include <time.h>
12
- #include <unistd.h>
13
11
 
14
12
  #define VECTOR_INITIAL_CAPACITY 16
15
13
  static VALUE cVector;
@@ -19,9 +17,7 @@ static VALUE vector_transaction_marker;
19
17
  typedef struct vector_waiter vector_waiter_t;
20
18
 
21
19
  struct vector_waiter {
22
- int read_fd;
23
- int write_fd;
24
- bool notified;
20
+ containers_waiter_t notification;
25
21
  vector_waiter_t *next;
26
22
  };
27
23
 
@@ -85,7 +81,7 @@ vector_memsize(const void *pointer)
85
81
  }
86
82
 
87
83
  static const rb_data_type_t vector_type = {
88
- .wrap_struct_name = "Ractor::Containers::Vector",
84
+ .wrap_struct_name = "Farce::Internal::Vector",
89
85
  .function = {
90
86
  .dmark = vector_mark,
91
87
  .dfree = vector_free,
@@ -214,35 +210,14 @@ vector_timeout_keyword(VALUE keywords)
214
210
  return timeout;
215
211
  }
216
212
 
217
- static void
218
- vector_set_fd_flags(int fd)
219
- {
220
- #ifdef _WIN32
221
- (void)fd;
222
- #else
223
- int flags = fcntl(fd, F_GETFL, 0);
224
- if (flags >= 0) (void)fcntl(fd, F_SETFL, flags | O_NONBLOCK);
225
- flags = fcntl(fd, F_GETFD, 0);
226
- if (flags >= 0) (void)fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
227
- #endif
228
- }
229
-
230
- /* Called with vector->lock held. Each waiter has an independent pipe, so a
231
- * transition wakes every waiting thread or scheduled fiber. */
213
+ /* Called with vector->lock held. A transition wakes every registered
214
+ * thread or fiber without sharing a consumable notification. */
232
215
  static void
233
216
  vector_notify_waiters_locked(vector_t *vector)
234
217
  {
235
- unsigned char byte = 1;
236
218
  vector->generation++;
237
219
  for (vector_waiter_t *waiter = vector->waiters; waiter; waiter = waiter->next) {
238
- if (waiter->notified) continue;
239
- ssize_t result;
240
- do {
241
- result = write(waiter->write_fd, &byte, 1);
242
- } while (result < 0 && errno == EINTR);
243
- if (result == 1 || (result < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))) {
244
- waiter->notified = true;
245
- }
220
+ (void)containers_waiter_notify(&waiter->notification);
246
221
  }
247
222
  }
248
223
 
@@ -259,23 +234,14 @@ typedef struct {
259
234
  vector_timeout_t *timeout;
260
235
  } vector_wait_context_t;
261
236
 
262
- static bool
263
- vector_wait_for_descriptor(int fd, vector_timeout_t *timeout)
264
- {
265
- VALUE wait_timeout = Qnil;
266
- if (timeout->finite) {
267
- double remaining = timeout->deadline - vector_monotonic_now();
268
- if (remaining <= 0) return false;
269
- wait_timeout = DBL2NUM(remaining);
270
- }
271
- return containers_wait_for_readable(fd, wait_timeout);
272
- }
273
-
274
237
  static VALUE
275
238
  vector_wait_body(VALUE opaque)
276
239
  {
277
240
  vector_wait_context_t *context = (vector_wait_context_t *)opaque;
278
- return vector_wait_for_descriptor(context->waiter.read_fd, context->timeout) ? Qtrue : Qfalse;
241
+ return containers_waiter_wait(
242
+ &context->waiter.notification, &context->vector->lock,
243
+ context->timeout->finite, context->timeout->deadline, NULL
244
+ ) ? Qtrue : Qfalse;
279
245
  }
280
246
 
281
247
  static VALUE
@@ -292,8 +258,7 @@ vector_wait_cleanup(VALUE opaque)
292
258
  }
293
259
  }
294
260
  pthread_mutex_unlock(&context->vector->lock);
295
- close(context->waiter.read_fd);
296
- close(context->waiter.write_fd);
261
+ containers_waiter_close(&context->waiter.notification);
297
262
  return Qnil;
298
263
  }
299
264
 
@@ -303,24 +268,12 @@ vector_wait_cleanup(VALUE opaque)
303
268
  static bool
304
269
  vector_wait_once(vector_t *vector, vector_timeout_t *timeout)
305
270
  {
306
- int descriptors[2];
307
- if (pipe(descriptors) != 0) {
308
- pthread_mutex_unlock(&vector->lock);
309
- rb_sys_fail("pipe");
310
- }
311
- vector_set_fd_flags(descriptors[0]);
312
- vector_set_fd_flags(descriptors[1]);
313
-
314
271
  vector_wait_context_t context = {
315
272
  .vector = vector,
316
- .waiter = {
317
- .read_fd = descriptors[0],
318
- .write_fd = descriptors[1],
319
- .notified = false,
320
- .next = vector->waiters,
321
- },
273
+ .waiter = {.next = vector->waiters},
322
274
  .timeout = timeout,
323
275
  };
276
+ containers_waiter_init(&context.waiter.notification);
324
277
  vector->waiters = &context.waiter;
325
278
  pthread_mutex_unlock(&vector->lock);
326
279
  return RTEST(rb_ensure(vector_wait_body, (VALUE)&context, vector_wait_cleanup, (VALUE)&context));
data/ext/farce/weak_map.c CHANGED
@@ -250,7 +250,7 @@ weak_map_memsize(const void *pointer)
250
250
  }
251
251
 
252
252
  static const rb_data_type_t weak_map_type = {
253
- .wrap_struct_name = "Ractor::Containers::WeakMap",
253
+ .wrap_struct_name = "Farce::Internal::WeakMap",
254
254
  .function = {
255
255
  .dmark = weak_map_mark,
256
256
  .dfree = weak_map_free,
Binary file
Binary file
data/lib/farce/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
 
5
5
  module Farce
6
6
  # @return [String] the current version of Farce
7
- VERSION = "0.4.0"
7
+ VERSION = "0.4.1"
8
8
  end