polyphony 0.75 → 0.79
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.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +1 -1
- data/CHANGELOG.md +16 -0
- data/Gemfile.lock +1 -1
- data/examples/core/pingpong.rb +7 -4
- data/examples/core/trap1.rb +21 -0
- data/examples/core/trap2.rb +14 -0
- data/ext/polyphony/backend_common.c +14 -7
- data/ext/polyphony/backend_common.h +1 -1
- data/ext/polyphony/backend_io_uring.c +52 -32
- data/ext/polyphony/backend_libev.c +38 -17
- data/ext/polyphony/fiber.c +28 -28
- data/ext/polyphony/polyphony.c +5 -12
- data/ext/polyphony/polyphony.h +12 -9
- data/ext/polyphony/queue.c +82 -6
- data/ext/polyphony/thread.c +6 -2
- data/ext/test_eintr.c +50 -0
- data/lib/polyphony/core/debug.rb +146 -0
- data/lib/polyphony/extensions/fiber.rb +16 -8
- data/lib/polyphony/version.rb +1 -1
- data/test/helper.rb +2 -1
- data/test/stress.rb +1 -1
- data/test/test_fiber.rb +6 -4
- data/test/test_io.rb +7 -7
- data/test/test_queue.rb +103 -1
- data/test/test_signal.rb +57 -0
- data/test/test_supervise.rb +27 -0
- data/test/test_thread.rb +1 -1
- data/test/test_timer.rb +1 -1
- data/test/test_trace.rb +102 -24
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34ed5a7685a8986f1fe5fd6cd714ee049633952746ecb8bced1aee73553c6c99
|
4
|
+
data.tar.gz: 03ded408e8849a921d0cfbbe1a232c5cea83cfbd58710efade4900f6240f511c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91fe5a0979c0158315e942bba88f563a47926e78ad3da0ff8e483ee91aff1482af1f2fc3e3aa4cacd49e787531627bfc3295d45439e99d8400b4736b1a749443
|
7
|
+
data.tar.gz: 3c1eaaccec661c4f3e4c07853fbaaaab19222837d5ac25b4098856bd1b1367833cbc35a58602fb9c30ff8e7cd63d04f79206bf07438a1e842eb86871c8036063
|
data/.github/workflows/test.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## 0.79 2022-02-19
|
2
|
+
|
3
|
+
- Overhaul trace events system (#73)
|
4
|
+
|
5
|
+
## 0.78 2022-02-16
|
6
|
+
|
7
|
+
- Fix Polyphony::Queue API compatibility (#72)
|
8
|
+
|
9
|
+
## 0.77 2022-02-07
|
10
|
+
|
11
|
+
- Fix behaviour of signal traps (#71)
|
12
|
+
|
13
|
+
## 0.76 2022-02-06
|
14
|
+
|
15
|
+
- Comment out `Backend_close` API (#70)
|
16
|
+
|
1
17
|
## 0.75 2022-02-04
|
2
18
|
|
3
19
|
- Fix handling of MoveOn on main fiber of forked process
|
data/Gemfile.lock
CHANGED
data/examples/core/pingpong.rb
CHANGED
@@ -3,18 +3,21 @@
|
|
3
3
|
require 'bundler/setup'
|
4
4
|
require 'polyphony'
|
5
5
|
|
6
|
-
|
6
|
+
require 'polyphony/core/debug'
|
7
|
+
Polyphony::Trace.start_event_firehose(STDOUT)
|
8
|
+
|
9
|
+
pong = spin_loop(:pong) do
|
7
10
|
msg, ping = receive
|
8
11
|
puts msg
|
9
12
|
ping << 'pong'
|
10
13
|
end
|
11
14
|
|
12
|
-
ping = spin do
|
13
|
-
|
15
|
+
ping = spin(:ping) do
|
16
|
+
1.times do
|
14
17
|
pong << ['ping', Fiber.current]
|
15
18
|
msg = receive
|
16
19
|
puts msg
|
17
20
|
end
|
18
21
|
end
|
19
22
|
|
20
|
-
ping.await
|
23
|
+
ping.await
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
pid = Process.pid
|
4
|
+
fork do
|
5
|
+
sleep 1
|
6
|
+
Process.kill('SIGINT', pid)
|
7
|
+
# sleep 10
|
8
|
+
# Process.kill(-9, pid)
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'bundler/setup'
|
12
|
+
require 'polyphony'
|
13
|
+
|
14
|
+
Thread.backend.trace_proc = proc { |*e| STDOUT.orig_write("#{e.inspect}\n") }
|
15
|
+
trap('SIGINT') { STDOUT.orig_write("* recv SIGINT\n") }
|
16
|
+
# trap('SIGCHLD') { STDOUT.orig_write("* recv SIGCHLD\n") }
|
17
|
+
STDOUT.orig_write("* pre gets\n")
|
18
|
+
# STDIN.wait_readable
|
19
|
+
s = gets
|
20
|
+
p s
|
21
|
+
STDOUT.orig_write("* post gets\n")
|
@@ -65,7 +65,7 @@ VALUE backend_base_switch_fiber(VALUE backend, struct Backend_base *base) {
|
|
65
65
|
|
66
66
|
base->switch_count++;
|
67
67
|
if (SHOULD_TRACE(base))
|
68
|
-
TRACE(base, 3,
|
68
|
+
TRACE(base, 3, SYM_block, current_fiber, CALLER());
|
69
69
|
|
70
70
|
while (1) {
|
71
71
|
next = runqueue_shift(&base->runqueue);
|
@@ -96,8 +96,6 @@ VALUE backend_base_switch_fiber(VALUE backend, struct Backend_base *base) {
|
|
96
96
|
if (next.fiber == Qnil) return Qnil;
|
97
97
|
|
98
98
|
// run next fiber
|
99
|
-
COND_TRACE(base, 3, SYM_fiber_run, next.fiber, next.value);
|
100
|
-
|
101
99
|
rb_ivar_set(next.fiber, ID_ivar_runnable, Qnil);
|
102
100
|
RB_GC_GUARD(next.fiber);
|
103
101
|
RB_GC_GUARD(next.value);
|
@@ -112,7 +110,7 @@ void backend_base_schedule_fiber(VALUE thread, VALUE backend, struct Backend_bas
|
|
112
110
|
if (rb_fiber_alive_p(fiber) != Qtrue) return;
|
113
111
|
already_runnable = rb_ivar_get(fiber, ID_ivar_runnable) != Qnil;
|
114
112
|
|
115
|
-
COND_TRACE(base,
|
113
|
+
COND_TRACE(base, 5, SYM_schedule, fiber, value, prioritize ? Qtrue : Qfalse, CALLER());
|
116
114
|
|
117
115
|
runqueue = rb_ivar_get(fiber, ID_ivar_parked) == Qtrue ? &base->parked_runqueue : &base->runqueue;
|
118
116
|
|
@@ -244,15 +242,24 @@ inline VALUE backend_await(struct Backend_base *backend) {
|
|
244
242
|
VALUE ret;
|
245
243
|
backend->pending_count++;
|
246
244
|
ret = Thread_switch_fiber(rb_thread_current());
|
245
|
+
|
246
|
+
// run next fiber
|
247
|
+
COND_TRACE(backend, 4, SYM_unblock, rb_fiber_current(), ret, CALLER());
|
248
|
+
|
247
249
|
backend->pending_count--;
|
248
250
|
RB_GC_GUARD(ret);
|
249
251
|
return ret;
|
250
252
|
}
|
251
253
|
|
252
|
-
inline VALUE backend_snooze() {
|
254
|
+
inline VALUE backend_snooze(struct Backend_base *backend) {
|
253
255
|
VALUE ret;
|
254
|
-
|
255
|
-
|
256
|
+
VALUE fiber = rb_fiber_current();
|
257
|
+
VALUE thread = rb_thread_current();
|
258
|
+
Fiber_make_runnable(fiber, Qnil);
|
259
|
+
ret = Thread_switch_fiber(thread);
|
260
|
+
|
261
|
+
COND_TRACE(backend, 4, SYM_unblock, fiber, ret, CALLER());
|
262
|
+
|
256
263
|
return ret;
|
257
264
|
}
|
258
265
|
|
@@ -80,7 +80,7 @@ void fptr_finalize(rb_io_t *fptr);
|
|
80
80
|
|
81
81
|
struct backend_stats backend_get_stats(VALUE self);
|
82
82
|
VALUE backend_await(struct Backend_base *backend);
|
83
|
-
VALUE backend_snooze();
|
83
|
+
VALUE backend_snooze(struct Backend_base *backend);
|
84
84
|
|
85
85
|
// macros for doing read loops
|
86
86
|
#define READ_LOOP_PREPARE_STR() { \
|
@@ -191,10 +191,14 @@ void io_uring_backend_poll(Backend_t *backend) {
|
|
191
191
|
io_uring_submit(&backend->ring);
|
192
192
|
}
|
193
193
|
|
194
|
+
wait_cqe:
|
194
195
|
backend->base.currently_polling = 1;
|
195
196
|
rb_thread_call_without_gvl(io_uring_backend_poll_without_gvl, (void *)&poll_ctx, RUBY_UBF_IO, 0);
|
196
197
|
backend->base.currently_polling = 0;
|
197
|
-
if (poll_ctx.result < 0)
|
198
|
+
if (poll_ctx.result < 0) {
|
199
|
+
if (poll_ctx.result == -EINTR && runqueue_empty_p(&backend->base.runqueue)) goto wait_cqe;
|
200
|
+
return;
|
201
|
+
}
|
198
202
|
|
199
203
|
io_uring_backend_handle_completion(poll_ctx.cqe, backend);
|
200
204
|
io_uring_cqe_seen(&backend->ring, poll_ctx.cqe);
|
@@ -212,12 +216,12 @@ inline VALUE Backend_poll(VALUE self, VALUE blocking) {
|
|
212
216
|
io_uring_submit(&backend->ring);
|
213
217
|
}
|
214
218
|
|
215
|
-
COND_TRACE(&backend->base, 2,
|
219
|
+
COND_TRACE(&backend->base, 2, SYM_enter_poll, rb_fiber_current());
|
216
220
|
|
217
221
|
if (is_blocking) io_uring_backend_poll(backend);
|
218
222
|
io_uring_backend_handle_ready_cqes(backend);
|
219
223
|
|
220
|
-
COND_TRACE(&backend->base, 2,
|
224
|
+
COND_TRACE(&backend->base, 2, SYM_leave_poll, rb_fiber_current());
|
221
225
|
|
222
226
|
return self;
|
223
227
|
}
|
@@ -1017,39 +1021,39 @@ VALUE Backend_wait_io(VALUE self, VALUE io, VALUE write) {
|
|
1017
1021
|
return self;
|
1018
1022
|
}
|
1019
1023
|
|
1020
|
-
VALUE Backend_close(VALUE self, VALUE io) {
|
1021
|
-
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
1024
|
+
// VALUE Backend_close(VALUE self, VALUE io) {
|
1025
|
+
// Backend_t *backend;
|
1026
|
+
// rb_io_t *fptr;
|
1027
|
+
// VALUE underlying_io = rb_ivar_get(io, ID_ivar_io);
|
1028
|
+
// VALUE resume_value = Qnil;
|
1029
|
+
// op_context_t *ctx;
|
1030
|
+
// struct io_uring_sqe *sqe;
|
1031
|
+
// int result;
|
1032
|
+
// int completed;
|
1029
1033
|
|
1030
|
-
|
1031
|
-
|
1032
|
-
|
1034
|
+
// if (underlying_io != Qnil) io = underlying_io;
|
1035
|
+
// GetBackend(self, backend);
|
1036
|
+
// GetOpenFile(io, fptr);
|
1033
1037
|
|
1034
|
-
|
1038
|
+
// if (fptr->fd < 0) return Qnil;
|
1035
1039
|
|
1036
|
-
|
1040
|
+
// io_unset_nonblock(fptr, io);
|
1037
1041
|
|
1038
|
-
|
1039
|
-
|
1040
|
-
|
1041
|
-
|
1042
|
-
|
1043
|
-
|
1044
|
-
|
1045
|
-
|
1042
|
+
// ctx = context_store_acquire(&backend->store, OP_CLOSE);
|
1043
|
+
// sqe = io_uring_get_sqe(&backend->ring);
|
1044
|
+
// io_uring_prep_close(sqe, fptr->fd);
|
1045
|
+
// result = io_uring_backend_defer_submit_and_await(backend, sqe, ctx, &resume_value);
|
1046
|
+
// completed = context_store_release(&backend->store, ctx);
|
1047
|
+
// RAISE_IF_EXCEPTION(resume_value);
|
1048
|
+
// if (!completed) return resume_value;
|
1049
|
+
// RB_GC_GUARD(resume_value);
|
1046
1050
|
|
1047
|
-
|
1051
|
+
// if (result < 0) rb_syserr_fail(-result, strerror(-result));
|
1048
1052
|
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1052
|
-
}
|
1053
|
+
// fptr_finalize(fptr);
|
1054
|
+
// // fptr->fd = -1;
|
1055
|
+
// return io;
|
1056
|
+
// }
|
1053
1057
|
|
1054
1058
|
inline struct __kernel_timespec double_to_timespec(double duration) {
|
1055
1059
|
double duration_integral;
|
@@ -1104,7 +1108,7 @@ VALUE Backend_timer_loop(VALUE self, VALUE interval) {
|
|
1104
1108
|
if (!completed) return resume_value;
|
1105
1109
|
}
|
1106
1110
|
else {
|
1107
|
-
resume_value = backend_snooze();
|
1111
|
+
resume_value = backend_snooze(&backend->base);
|
1108
1112
|
RAISE_IF_EXCEPTION(resume_value);
|
1109
1113
|
}
|
1110
1114
|
|
@@ -1587,6 +1591,22 @@ VALUE Backend_trace_proc_set(VALUE self, VALUE block) {
|
|
1587
1591
|
return self;
|
1588
1592
|
}
|
1589
1593
|
|
1594
|
+
VALUE Backend_snooze(VALUE self) {
|
1595
|
+
VALUE ret;
|
1596
|
+
VALUE fiber = rb_fiber_current();
|
1597
|
+
Backend_t *backend;
|
1598
|
+
GetBackend(self, backend);
|
1599
|
+
|
1600
|
+
Fiber_make_runnable(fiber, Qnil);
|
1601
|
+
ret = Thread_switch_fiber(rb_thread_current());
|
1602
|
+
|
1603
|
+
COND_TRACE(&backend->base, 4, SYM_unblock, rb_fiber_current(), ret, CALLER());
|
1604
|
+
|
1605
|
+
RAISE_IF_EXCEPTION(ret);
|
1606
|
+
RB_GC_GUARD(ret);
|
1607
|
+
return ret;
|
1608
|
+
}
|
1609
|
+
|
1590
1610
|
void Backend_park_fiber(VALUE self, VALUE fiber) {
|
1591
1611
|
Backend_t *backend;
|
1592
1612
|
GetBackend(self, backend);
|
@@ -1640,7 +1660,7 @@ void Init_Backend() {
|
|
1640
1660
|
rb_define_method(cBackend, "wait_io", Backend_wait_io, 2);
|
1641
1661
|
rb_define_method(cBackend, "waitpid", Backend_waitpid, 1);
|
1642
1662
|
rb_define_method(cBackend, "write", Backend_write_m, -1);
|
1643
|
-
rb_define_method(cBackend, "close", Backend_close, 1);
|
1663
|
+
// rb_define_method(cBackend, "close", Backend_close, 1);
|
1644
1664
|
|
1645
1665
|
SYM_io_uring = ID2SYM(rb_intern("io_uring"));
|
1646
1666
|
SYM_send = ID2SYM(rb_intern("send"));
|
@@ -168,11 +168,16 @@ inline VALUE Backend_poll(VALUE self, VALUE blocking) {
|
|
168
168
|
|
169
169
|
backend->base.poll_count++;
|
170
170
|
|
171
|
-
COND_TRACE(&backend->base, 2,
|
171
|
+
COND_TRACE(&backend->base, 2, SYM_enter_poll, rb_fiber_current());
|
172
|
+
|
173
|
+
ev_run:
|
172
174
|
backend->base.currently_polling = 1;
|
175
|
+
errno = 0;
|
173
176
|
ev_run(backend->ev_loop, blocking == Qtrue ? EVRUN_ONCE : EVRUN_NOWAIT);
|
174
177
|
backend->base.currently_polling = 0;
|
175
|
-
|
178
|
+
if (errno == EINTR && runqueue_empty_p(&backend->base.runqueue)) goto ev_run;
|
179
|
+
|
180
|
+
COND_TRACE(&backend->base, 2, SYM_leave_poll, rb_fiber_current());
|
176
181
|
|
177
182
|
return self;
|
178
183
|
}
|
@@ -300,7 +305,7 @@ VALUE Backend_read(VALUE self, VALUE io, VALUE str, VALUE length, VALUE to_eof,
|
|
300
305
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
301
306
|
}
|
302
307
|
else {
|
303
|
-
switchpoint_result = backend_snooze();
|
308
|
+
switchpoint_result = backend_snooze(&backend->base);
|
304
309
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
305
310
|
|
306
311
|
if (n == 0) break; // EOF
|
@@ -370,7 +375,7 @@ VALUE Backend_read_loop(VALUE self, VALUE io, VALUE maxlen) {
|
|
370
375
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
371
376
|
}
|
372
377
|
else {
|
373
|
-
switchpoint_result = backend_snooze();
|
378
|
+
switchpoint_result = backend_snooze(&backend->base);
|
374
379
|
|
375
380
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
376
381
|
|
@@ -423,7 +428,7 @@ VALUE Backend_feed_loop(VALUE self, VALUE io, VALUE receiver, VALUE method) {
|
|
423
428
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
424
429
|
}
|
425
430
|
else {
|
426
|
-
switchpoint_result = backend_snooze();
|
431
|
+
switchpoint_result = backend_snooze(&backend->base);
|
427
432
|
|
428
433
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
429
434
|
|
@@ -478,7 +483,7 @@ VALUE Backend_write(VALUE self, VALUE io, VALUE str) {
|
|
478
483
|
}
|
479
484
|
|
480
485
|
if (watcher.fiber == Qnil) {
|
481
|
-
switchpoint_result = backend_snooze();
|
486
|
+
switchpoint_result = backend_snooze(&backend->base);
|
482
487
|
|
483
488
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
484
489
|
}
|
@@ -553,7 +558,7 @@ VALUE Backend_writev(VALUE self, VALUE io, int argc, VALUE *argv) {
|
|
553
558
|
}
|
554
559
|
}
|
555
560
|
if (watcher.fiber == Qnil) {
|
556
|
-
switchpoint_result = backend_snooze();
|
561
|
+
switchpoint_result = backend_snooze(&backend->base);
|
557
562
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
558
563
|
}
|
559
564
|
|
@@ -606,7 +611,7 @@ VALUE Backend_accept(VALUE self, VALUE server_socket, VALUE socket_class) {
|
|
606
611
|
else {
|
607
612
|
VALUE socket;
|
608
613
|
rb_io_t *fp;
|
609
|
-
switchpoint_result = backend_snooze();
|
614
|
+
switchpoint_result = backend_snooze(&backend->base);
|
610
615
|
|
611
616
|
if (TEST_EXCEPTION(switchpoint_result)) {
|
612
617
|
close(fd); // close fd since we're raising an exception
|
@@ -664,7 +669,7 @@ VALUE Backend_accept_loop(VALUE self, VALUE server_socket, VALUE socket_class) {
|
|
664
669
|
}
|
665
670
|
else {
|
666
671
|
rb_io_t *fp;
|
667
|
-
switchpoint_result = backend_snooze();
|
672
|
+
switchpoint_result = backend_snooze(&backend->base);
|
668
673
|
|
669
674
|
if (TEST_EXCEPTION(switchpoint_result)) {
|
670
675
|
close(fd); // close fd since we're raising an exception
|
@@ -722,7 +727,7 @@ VALUE Backend_connect(VALUE self, VALUE sock, VALUE host, VALUE port) {
|
|
722
727
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
723
728
|
}
|
724
729
|
else {
|
725
|
-
switchpoint_result = backend_snooze();
|
730
|
+
switchpoint_result = backend_snooze(&backend->base);
|
726
731
|
|
727
732
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
728
733
|
}
|
@@ -769,7 +774,7 @@ VALUE Backend_send(VALUE self, VALUE io, VALUE str, VALUE flags) {
|
|
769
774
|
}
|
770
775
|
|
771
776
|
if (watcher.fiber == Qnil) {
|
772
|
-
switchpoint_result = backend_snooze();
|
777
|
+
switchpoint_result = backend_snooze(&backend->base);
|
773
778
|
|
774
779
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
775
780
|
}
|
@@ -875,7 +880,7 @@ VALUE Backend_splice(VALUE self, VALUE src, VALUE dest, VALUE maxlen) {
|
|
875
880
|
}
|
876
881
|
|
877
882
|
if (watcher.ctx.fiber == Qnil) {
|
878
|
-
switchpoint_result = backend_snooze();
|
883
|
+
switchpoint_result = backend_snooze(&backend->base);
|
879
884
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
880
885
|
}
|
881
886
|
|
@@ -930,7 +935,7 @@ VALUE Backend_splice_to_eof(VALUE self, VALUE src, VALUE dest, VALUE maxlen) {
|
|
930
935
|
}
|
931
936
|
|
932
937
|
if (watcher.ctx.fiber == Qnil) {
|
933
|
-
switchpoint_result = backend_snooze();
|
938
|
+
switchpoint_result = backend_snooze(&backend->base);
|
934
939
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
935
940
|
}
|
936
941
|
|
@@ -1004,7 +1009,7 @@ VALUE Backend_splice(VALUE self, VALUE src, VALUE dest, VALUE maxlen) {
|
|
1004
1009
|
}
|
1005
1010
|
|
1006
1011
|
if (watcher.fiber == Qnil) {
|
1007
|
-
switchpoint_result = backend_snooze();
|
1012
|
+
switchpoint_result = backend_snooze(&backend->base);
|
1008
1013
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
1009
1014
|
}
|
1010
1015
|
|
@@ -1084,7 +1089,7 @@ VALUE Backend_splice_to_eof(VALUE self, VALUE src, VALUE dest, VALUE maxlen) {
|
|
1084
1089
|
|
1085
1090
|
done:
|
1086
1091
|
if (watcher.fiber == Qnil) {
|
1087
|
-
switchpoint_result = backend_snooze();
|
1092
|
+
switchpoint_result = backend_snooze(&backend->base);
|
1088
1093
|
if (TEST_EXCEPTION(switchpoint_result)) goto error;
|
1089
1094
|
}
|
1090
1095
|
|
@@ -1166,7 +1171,7 @@ noreturn VALUE Backend_timer_loop(VALUE self, VALUE interval) {
|
|
1166
1171
|
RAISE_IF_EXCEPTION(resume_value);
|
1167
1172
|
}
|
1168
1173
|
else {
|
1169
|
-
resume_value = backend_snooze();
|
1174
|
+
resume_value = backend_snooze(&backend->base);
|
1170
1175
|
RAISE_IF_EXCEPTION(resume_value);
|
1171
1176
|
}
|
1172
1177
|
|
@@ -1525,7 +1530,7 @@ VALUE Backend_splice_chunks(VALUE self, VALUE src, VALUE dest, VALUE prefix, VAL
|
|
1525
1530
|
}
|
1526
1531
|
|
1527
1532
|
if (watcher.ctx.fiber == Qnil) {
|
1528
|
-
result = backend_snooze();
|
1533
|
+
result = backend_snooze(&backend->base);
|
1529
1534
|
if (TEST_EXCEPTION(result)) goto error;
|
1530
1535
|
}
|
1531
1536
|
RB_GC_GUARD(str);
|
@@ -1559,6 +1564,22 @@ VALUE Backend_trace_proc_set(VALUE self, VALUE block) {
|
|
1559
1564
|
return self;
|
1560
1565
|
}
|
1561
1566
|
|
1567
|
+
VALUE Backend_snooze(VALUE self) {
|
1568
|
+
VALUE ret;
|
1569
|
+
VALUE fiber = rb_fiber_current();
|
1570
|
+
Backend_t *backend;
|
1571
|
+
GetBackend(self, backend);
|
1572
|
+
|
1573
|
+
Fiber_make_runnable(fiber, Qnil);
|
1574
|
+
ret = Thread_switch_fiber(rb_thread_current());
|
1575
|
+
|
1576
|
+
COND_TRACE(&backend->base, 4, SYM_unblock, rb_fiber_current(), ret, CALLER());
|
1577
|
+
|
1578
|
+
RAISE_IF_EXCEPTION(ret);
|
1579
|
+
RB_GC_GUARD(ret);
|
1580
|
+
return ret;
|
1581
|
+
}
|
1582
|
+
|
1562
1583
|
void Backend_park_fiber(VALUE self, VALUE fiber) {
|
1563
1584
|
Backend_t *backend;
|
1564
1585
|
GetBackend(self, backend);
|
data/ext/polyphony/fiber.c
CHANGED
@@ -10,13 +10,13 @@ VALUE SYM_running;
|
|
10
10
|
VALUE SYM_runnable;
|
11
11
|
VALUE SYM_waiting;
|
12
12
|
|
13
|
-
VALUE
|
14
|
-
VALUE
|
15
|
-
VALUE
|
16
|
-
VALUE
|
17
|
-
VALUE
|
18
|
-
VALUE
|
19
|
-
VALUE
|
13
|
+
VALUE SYM_spin;
|
14
|
+
VALUE SYM_enter_poll;
|
15
|
+
VALUE SYM_leave_poll;
|
16
|
+
VALUE SYM_unblock;
|
17
|
+
VALUE SYM_schedule;
|
18
|
+
VALUE SYM_block;
|
19
|
+
VALUE SYM_terminate;
|
20
20
|
|
21
21
|
static VALUE Fiber_safe_transfer(int argc, VALUE *argv, VALUE self) {
|
22
22
|
VALUE arg = (argc == 0) ? Qnil : argv[0];
|
@@ -97,7 +97,7 @@ VALUE Fiber_receive(VALUE self) {
|
|
97
97
|
mailbox = rb_funcall(cQueue, ID_new, 0);
|
98
98
|
rb_ivar_set(self, ID_ivar_mailbox, mailbox);
|
99
99
|
}
|
100
|
-
return Queue_shift(mailbox);
|
100
|
+
return Queue_shift(0, 0, mailbox);
|
101
101
|
}
|
102
102
|
|
103
103
|
VALUE Fiber_mailbox(VALUE self) {
|
@@ -157,24 +157,24 @@ void Init_Fiber() {
|
|
157
157
|
rb_global_variable(&SYM_runnable);
|
158
158
|
rb_global_variable(&SYM_waiting);
|
159
159
|
|
160
|
-
ID_ivar_auto_watcher
|
161
|
-
ID_ivar_mailbox
|
162
|
-
ID_ivar_result
|
163
|
-
ID_ivar_waiting_fibers
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
rb_global_variable(&
|
174
|
-
rb_global_variable(&
|
175
|
-
rb_global_variable(&
|
176
|
-
rb_global_variable(&
|
177
|
-
rb_global_variable(&
|
178
|
-
rb_global_variable(&
|
179
|
-
rb_global_variable(&
|
160
|
+
ID_ivar_auto_watcher = rb_intern("@auto_watcher");
|
161
|
+
ID_ivar_mailbox = rb_intern("@mailbox");
|
162
|
+
ID_ivar_result = rb_intern("@result");
|
163
|
+
ID_ivar_waiting_fibers = rb_intern("@waiting_fibers");
|
164
|
+
|
165
|
+
SYM_spin = ID2SYM(rb_intern("spin"));
|
166
|
+
SYM_enter_poll = ID2SYM(rb_intern("enter_poll"));
|
167
|
+
SYM_leave_poll = ID2SYM(rb_intern("leave_poll"));
|
168
|
+
SYM_unblock = ID2SYM(rb_intern("unblock"));
|
169
|
+
SYM_schedule = ID2SYM(rb_intern("schedule"));
|
170
|
+
SYM_block = ID2SYM(rb_intern("block"));
|
171
|
+
SYM_terminate = ID2SYM(rb_intern("terminate"));
|
172
|
+
|
173
|
+
rb_global_variable(&SYM_spin);
|
174
|
+
rb_global_variable(&SYM_enter_poll);
|
175
|
+
rb_global_variable(&SYM_leave_poll);
|
176
|
+
rb_global_variable(&SYM_unblock);
|
177
|
+
rb_global_variable(&SYM_schedule);
|
178
|
+
rb_global_variable(&SYM_block);
|
179
|
+
rb_global_variable(&SYM_terminate);
|
180
180
|
}
|
data/ext/polyphony/polyphony.c
CHANGED
@@ -27,14 +27,7 @@ ID ID_W;
|
|
27
27
|
ID ID_RW;
|
28
28
|
|
29
29
|
VALUE Polyphony_snooze(VALUE self) {
|
30
|
-
|
31
|
-
VALUE fiber = rb_fiber_current();
|
32
|
-
|
33
|
-
Fiber_make_runnable(fiber, Qnil);
|
34
|
-
ret = Thread_switch_fiber(rb_thread_current());
|
35
|
-
RAISE_IF_EXCEPTION(ret);
|
36
|
-
RB_GC_GUARD(ret);
|
37
|
-
return ret;
|
30
|
+
return Backend_snooze(BACKEND());
|
38
31
|
}
|
39
32
|
|
40
33
|
static VALUE Polyphony_suspend(VALUE self) {
|
@@ -125,9 +118,9 @@ VALUE Polyphony_backend_write(int argc, VALUE *argv, VALUE self) {
|
|
125
118
|
return Backend_write_m(argc, argv, BACKEND());
|
126
119
|
}
|
127
120
|
|
128
|
-
VALUE Polyphony_backend_close(VALUE self, VALUE io) {
|
129
|
-
|
130
|
-
}
|
121
|
+
// VALUE Polyphony_backend_close(VALUE self, VALUE io) {
|
122
|
+
// return Backend_close(BACKEND(), io);
|
123
|
+
// }
|
131
124
|
|
132
125
|
void Init_Polyphony() {
|
133
126
|
mPolyphony = rb_define_module("Polyphony");
|
@@ -153,7 +146,7 @@ void Init_Polyphony() {
|
|
153
146
|
rb_define_singleton_method(mPolyphony, "backend_wait_io", Polyphony_backend_wait_io, 2);
|
154
147
|
rb_define_singleton_method(mPolyphony, "backend_waitpid", Polyphony_backend_waitpid, 1);
|
155
148
|
rb_define_singleton_method(mPolyphony, "backend_write", Polyphony_backend_write, -1);
|
156
|
-
rb_define_singleton_method(mPolyphony, "backend_close", Polyphony_backend_close, 1);
|
149
|
+
// rb_define_singleton_method(mPolyphony, "backend_close", Polyphony_backend_close, 1);
|
157
150
|
rb_define_singleton_method(mPolyphony, "backend_verify_blocking_mode", Backend_verify_blocking_mode, 2);
|
158
151
|
|
159
152
|
rb_define_global_function("snooze", Polyphony_snooze, 0);
|
data/ext/polyphony/polyphony.h
CHANGED
@@ -61,20 +61,20 @@ extern ID ID_switch_fiber;
|
|
61
61
|
extern ID ID_to_s;
|
62
62
|
extern ID ID_transfer;
|
63
63
|
|
64
|
-
extern VALUE
|
65
|
-
extern VALUE
|
66
|
-
extern VALUE
|
67
|
-
extern VALUE
|
68
|
-
extern VALUE
|
69
|
-
extern VALUE
|
70
|
-
extern VALUE
|
64
|
+
extern VALUE SYM_spin;
|
65
|
+
extern VALUE SYM_enter_poll;
|
66
|
+
extern VALUE SYM_leave_poll;
|
67
|
+
extern VALUE SYM_unblock;
|
68
|
+
extern VALUE SYM_schedule;
|
69
|
+
extern VALUE SYM_block;
|
70
|
+
extern VALUE SYM_terminate;
|
71
71
|
|
72
72
|
VALUE Fiber_auto_watcher(VALUE self);
|
73
73
|
void Fiber_make_runnable(VALUE fiber, VALUE value);
|
74
74
|
|
75
75
|
VALUE Queue_push(VALUE self, VALUE value);
|
76
76
|
VALUE Queue_unshift(VALUE self, VALUE value);
|
77
|
-
VALUE Queue_shift(VALUE self);
|
77
|
+
VALUE Queue_shift(int argc,VALUE *argv, VALUE self);
|
78
78
|
VALUE Queue_shift_all(VALUE self);
|
79
79
|
|
80
80
|
void Runqueue_push(VALUE self, VALUE fiber, VALUE value, int reschedule);
|
@@ -114,18 +114,21 @@ VALUE Backend_wait_event(VALUE self, VALUE raise);
|
|
114
114
|
VALUE Backend_wait_io(VALUE self, VALUE io, VALUE write);
|
115
115
|
VALUE Backend_waitpid(VALUE self, VALUE pid);
|
116
116
|
VALUE Backend_write_m(int argc, VALUE *argv, VALUE self);
|
117
|
-
VALUE Backend_close(VALUE self, VALUE io);
|
117
|
+
// VALUE Backend_close(VALUE self, VALUE io);
|
118
118
|
|
119
119
|
VALUE Backend_poll(VALUE self, VALUE blocking);
|
120
120
|
VALUE Backend_wait_event(VALUE self, VALUE raise_on_exception);
|
121
121
|
VALUE Backend_wakeup(VALUE self);
|
122
122
|
VALUE Backend_run_idle_tasks(VALUE self);
|
123
123
|
VALUE Backend_switch_fiber(VALUE self);
|
124
|
+
|
124
125
|
void Backend_schedule_fiber(VALUE thread, VALUE self, VALUE fiber, VALUE value, int prioritize);
|
125
126
|
void Backend_unschedule_fiber(VALUE self, VALUE fiber);
|
126
127
|
void Backend_park_fiber(VALUE self, VALUE fiber);
|
127
128
|
void Backend_unpark_fiber(VALUE self, VALUE fiber);
|
128
129
|
|
130
|
+
VALUE Backend_snooze(VALUE self);
|
131
|
+
|
129
132
|
void Thread_schedule_fiber(VALUE thread, VALUE fiber, VALUE value);
|
130
133
|
void Thread_schedule_fiber_with_priority(VALUE thread, VALUE fiber, VALUE value);
|
131
134
|
VALUE Thread_switch_fiber(VALUE thread);
|