io-event 1.6.5 → 1.9.0
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
- checksums.yaml.gz.sig +0 -0
- data/ext/extconf.rb +26 -13
- data/ext/io/event/{selector/array.h → array.h} +74 -18
- data/ext/io/event/event.c +9 -26
- data/ext/io/event/event.h +2 -19
- data/ext/io/event/fiber.c +63 -0
- data/ext/io/event/fiber.h +23 -0
- data/ext/io/event/interrupt.c +19 -27
- data/ext/io/event/interrupt.h +2 -19
- data/ext/io/event/{selector/list.h → list.h} +20 -2
- data/ext/io/event/profiler.c +505 -0
- data/ext/io/event/profiler.h +8 -0
- data/ext/io/event/selector/epoll.c +41 -43
- data/ext/io/event/selector/epoll.h +2 -19
- data/ext/io/event/selector/kqueue.c +34 -41
- data/ext/io/event/selector/kqueue.h +2 -19
- data/ext/io/event/selector/pidfd.c +2 -19
- data/ext/io/event/selector/selector.c +65 -102
- data/ext/io/event/selector/selector.h +51 -46
- data/ext/io/event/selector/uring.c +130 -47
- data/ext/io/event/selector/uring.h +2 -19
- data/ext/io/event/time.c +35 -0
- data/ext/io/event/time.h +17 -0
- data/lib/io/event/debug/selector.rb +44 -4
- data/lib/io/event/interrupt.rb +2 -2
- data/lib/io/event/native.rb +11 -0
- data/lib/io/event/priority_heap.rb +13 -14
- data/lib/io/event/profiler.rb +18 -0
- data/lib/io/event/selector/nonblock.rb +6 -2
- data/lib/io/event/selector/select.rb +25 -5
- data/lib/io/event/selector.rb +15 -5
- data/lib/io/event/support.rb +23 -2
- data/lib/io/event/timers.rb +42 -4
- data/lib/io/event/version.rb +4 -2
- data/lib/io/event.rb +5 -11
- data/license.md +4 -1
- data/readme.md +22 -4
- data/releases.md +57 -0
- data.tar.gz.sig +0 -0
- metadata +20 -11
- metadata.gz.sig +0 -0
|
@@ -1,65 +1,13 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
// in the Software without restriction, including without limitation the rights
|
|
6
|
-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
// furnished to do so, subject to the following conditions:
|
|
9
|
-
//
|
|
10
|
-
// The above copyright notice and this permission notice shall be included in
|
|
11
|
-
// all copies or substantial portions of the Software.
|
|
12
|
-
//
|
|
13
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
// THE SOFTWARE.
|
|
1
|
+
// Released under the MIT License.
|
|
2
|
+
// Copyright, 2021-2025, by Samuel Williams.
|
|
20
3
|
|
|
21
4
|
#include "selector.h"
|
|
5
|
+
|
|
22
6
|
#include <fcntl.h>
|
|
7
|
+
#include <stdlib.h>
|
|
23
8
|
|
|
24
9
|
static const int DEBUG = 0;
|
|
25
10
|
|
|
26
|
-
static ID id_transfer, id_alive_p;
|
|
27
|
-
|
|
28
|
-
VALUE IO_Event_Selector_fiber_transfer(VALUE fiber, int argc, VALUE *argv) {
|
|
29
|
-
// TODO Consider introducing something like `rb_fiber_scheduler_transfer(...)`.
|
|
30
|
-
#ifdef HAVE__RB_FIBER_TRANSFER
|
|
31
|
-
if (RTEST(rb_obj_is_fiber(fiber))) {
|
|
32
|
-
if (RTEST(rb_fiber_alive_p(fiber))) {
|
|
33
|
-
return rb_fiber_transfer(fiber, argc, argv);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return Qnil;
|
|
37
|
-
}
|
|
38
|
-
#endif
|
|
39
|
-
if (RTEST(rb_funcall(fiber, id_alive_p, 0))) {
|
|
40
|
-
return rb_funcallv(fiber, id_transfer, argc, argv);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return Qnil;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
#ifndef HAVE__RB_FIBER_RAISE
|
|
47
|
-
static ID id_raise;
|
|
48
|
-
|
|
49
|
-
VALUE IO_Event_Selector_fiber_raise(VALUE fiber, int argc, VALUE *argv) {
|
|
50
|
-
return rb_funcallv(fiber, id_raise, argc, argv);
|
|
51
|
-
}
|
|
52
|
-
#endif
|
|
53
|
-
|
|
54
|
-
#ifndef HAVE_RB_FIBER_CURRENT
|
|
55
|
-
static ID id_current;
|
|
56
|
-
|
|
57
|
-
static VALUE rb_fiber_current() {
|
|
58
|
-
return rb_funcall(rb_cFiber, id_current, 0);
|
|
59
|
-
}
|
|
60
|
-
#endif
|
|
61
|
-
|
|
62
|
-
|
|
63
11
|
#ifndef HAVE_RB_IO_DESCRIPTOR
|
|
64
12
|
static ID id_fileno;
|
|
65
13
|
|
|
@@ -136,17 +84,6 @@ static VALUE IO_Event_Selector_nonblock(VALUE class, VALUE io)
|
|
|
136
84
|
}
|
|
137
85
|
|
|
138
86
|
void Init_IO_Event_Selector(VALUE IO_Event_Selector) {
|
|
139
|
-
id_transfer = rb_intern("transfer");
|
|
140
|
-
id_alive_p = rb_intern("alive?");
|
|
141
|
-
|
|
142
|
-
#ifndef HAVE__RB_FIBER_RAISE
|
|
143
|
-
id_raise = rb_intern("raise");
|
|
144
|
-
#endif
|
|
145
|
-
|
|
146
|
-
#ifndef HAVE_RB_FIBER_CURRENT
|
|
147
|
-
id_current = rb_intern("current");
|
|
148
|
-
#endif
|
|
149
|
-
|
|
150
87
|
#ifndef HAVE_RB_IO_DESCRIPTOR
|
|
151
88
|
id_fileno = rb_intern("fileno");
|
|
152
89
|
#endif
|
|
@@ -156,10 +93,29 @@ void Init_IO_Event_Selector(VALUE IO_Event_Selector) {
|
|
|
156
93
|
rb_Process_Status = rb_const_get_at(rb_mProcess, rb_intern("Status"));
|
|
157
94
|
rb_gc_register_mark_object(rb_Process_Status);
|
|
158
95
|
#endif
|
|
159
|
-
|
|
96
|
+
|
|
160
97
|
rb_define_singleton_method(IO_Event_Selector, "nonblock", IO_Event_Selector_nonblock, 1);
|
|
161
98
|
}
|
|
162
99
|
|
|
100
|
+
void IO_Event_Selector_initialize(struct IO_Event_Selector *backend, VALUE self, VALUE loop) {
|
|
101
|
+
RB_OBJ_WRITE(self, &backend->self, self);
|
|
102
|
+
RB_OBJ_WRITE(self, &backend->loop, loop);
|
|
103
|
+
|
|
104
|
+
backend->waiting = NULL;
|
|
105
|
+
backend->ready = NULL;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
VALUE IO_Event_Selector_loop_resume(struct IO_Event_Selector *backend, VALUE fiber, int argc, VALUE *argv) {
|
|
109
|
+
return IO_Event_Fiber_transfer(fiber, argc, argv);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
VALUE IO_Event_Selector_loop_yield(struct IO_Event_Selector *backend)
|
|
113
|
+
{
|
|
114
|
+
// TODO Why is this assertion failing in async?
|
|
115
|
+
// RUBY_ASSERT(backend->loop != IO_Event_Fiber_current());
|
|
116
|
+
return IO_Event_Fiber_transfer(backend->loop, 0, NULL);
|
|
117
|
+
}
|
|
118
|
+
|
|
163
119
|
struct wait_and_transfer_arguments {
|
|
164
120
|
int argc;
|
|
165
121
|
VALUE *argv;
|
|
@@ -172,24 +128,35 @@ static void queue_pop(struct IO_Event_Selector *backend, struct IO_Event_Selecto
|
|
|
172
128
|
if (waiting->head) {
|
|
173
129
|
waiting->head->tail = waiting->tail;
|
|
174
130
|
} else {
|
|
131
|
+
// We must have been at the head of the queue:
|
|
175
132
|
backend->waiting = waiting->tail;
|
|
176
133
|
}
|
|
177
134
|
|
|
178
135
|
if (waiting->tail) {
|
|
179
136
|
waiting->tail->head = waiting->head;
|
|
180
137
|
} else {
|
|
138
|
+
// We must have been at the tail of the queue:
|
|
181
139
|
backend->ready = waiting->head;
|
|
182
140
|
}
|
|
141
|
+
|
|
142
|
+
waiting->head = NULL;
|
|
143
|
+
waiting->tail = NULL;
|
|
183
144
|
}
|
|
184
145
|
|
|
185
146
|
static void queue_push(struct IO_Event_Selector *backend, struct IO_Event_Selector_Queue *waiting) {
|
|
147
|
+
assert(waiting->head == NULL);
|
|
148
|
+
assert(waiting->tail == NULL);
|
|
149
|
+
|
|
186
150
|
if (backend->waiting) {
|
|
151
|
+
// If there was an item in the queue already, we shift it along:
|
|
187
152
|
backend->waiting->head = waiting;
|
|
188
153
|
waiting->tail = backend->waiting;
|
|
189
154
|
} else {
|
|
155
|
+
// If the queue was empty, we update the tail too:
|
|
190
156
|
backend->ready = waiting;
|
|
191
157
|
}
|
|
192
158
|
|
|
159
|
+
// We always push to the front/head:
|
|
193
160
|
backend->waiting = waiting;
|
|
194
161
|
}
|
|
195
162
|
|
|
@@ -200,7 +167,7 @@ static VALUE wait_and_transfer(VALUE _arguments) {
|
|
|
200
167
|
int argc = arguments->argc - 1;
|
|
201
168
|
VALUE *argv = arguments->argv + 1;
|
|
202
169
|
|
|
203
|
-
return
|
|
170
|
+
return IO_Event_Selector_loop_resume(arguments->backend, fiber, argc, argv);
|
|
204
171
|
}
|
|
205
172
|
|
|
206
173
|
static VALUE wait_and_transfer_ensure(VALUE _arguments) {
|
|
@@ -219,9 +186,11 @@ VALUE IO_Event_Selector_resume(struct IO_Event_Selector *backend, int argc, VALU
|
|
|
219
186
|
.head = NULL,
|
|
220
187
|
.tail = NULL,
|
|
221
188
|
.flags = IO_EVENT_SELECTOR_QUEUE_FIBER,
|
|
222
|
-
.fiber =
|
|
189
|
+
.fiber = IO_Event_Fiber_current()
|
|
223
190
|
};
|
|
224
191
|
|
|
192
|
+
RB_OBJ_WRITTEN(backend->self, Qundef, waiting.fiber);
|
|
193
|
+
|
|
225
194
|
queue_push(backend, &waiting);
|
|
226
195
|
|
|
227
196
|
struct wait_and_transfer_arguments arguments = {
|
|
@@ -241,7 +210,7 @@ static VALUE wait_and_raise(VALUE _arguments) {
|
|
|
241
210
|
int argc = arguments->argc - 1;
|
|
242
211
|
VALUE *argv = arguments->argv + 1;
|
|
243
212
|
|
|
244
|
-
return
|
|
213
|
+
return IO_Event_Fiber_raise(fiber, argc, argv);
|
|
245
214
|
}
|
|
246
215
|
|
|
247
216
|
VALUE IO_Event_Selector_raise(struct IO_Event_Selector *backend, int argc, VALUE *argv)
|
|
@@ -252,9 +221,11 @@ VALUE IO_Event_Selector_raise(struct IO_Event_Selector *backend, int argc, VALUE
|
|
|
252
221
|
.head = NULL,
|
|
253
222
|
.tail = NULL,
|
|
254
223
|
.flags = IO_EVENT_SELECTOR_QUEUE_FIBER,
|
|
255
|
-
.fiber =
|
|
224
|
+
.fiber = IO_Event_Fiber_current()
|
|
256
225
|
};
|
|
257
226
|
|
|
227
|
+
RB_OBJ_WRITTEN(backend->self, Qundef, waiting.fiber);
|
|
228
|
+
|
|
258
229
|
queue_push(backend, &waiting);
|
|
259
230
|
|
|
260
231
|
struct wait_and_transfer_arguments arguments = {
|
|
@@ -267,42 +238,49 @@ VALUE IO_Event_Selector_raise(struct IO_Event_Selector *backend, int argc, VALUE
|
|
|
267
238
|
return rb_ensure(wait_and_raise, (VALUE)&arguments, wait_and_transfer_ensure, (VALUE)&arguments);
|
|
268
239
|
}
|
|
269
240
|
|
|
270
|
-
void
|
|
241
|
+
void IO_Event_Selector_ready_push(struct IO_Event_Selector *backend, VALUE fiber)
|
|
271
242
|
{
|
|
272
243
|
struct IO_Event_Selector_Queue *waiting = malloc(sizeof(struct IO_Event_Selector_Queue));
|
|
244
|
+
assert(waiting);
|
|
273
245
|
|
|
274
246
|
waiting->head = NULL;
|
|
275
247
|
waiting->tail = NULL;
|
|
276
248
|
waiting->flags = IO_EVENT_SELECTOR_QUEUE_INTERNAL;
|
|
277
|
-
|
|
249
|
+
|
|
250
|
+
RB_OBJ_WRITE(backend->self, &waiting->fiber, fiber);
|
|
278
251
|
|
|
279
252
|
queue_push(backend, waiting);
|
|
280
253
|
}
|
|
281
254
|
|
|
282
255
|
static inline
|
|
283
|
-
void
|
|
256
|
+
void IO_Event_Selector_ready_pop(struct IO_Event_Selector *backend, struct IO_Event_Selector_Queue *ready)
|
|
284
257
|
{
|
|
285
|
-
if (DEBUG) fprintf(stderr, "
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
258
|
+
if (DEBUG) fprintf(stderr, "IO_Event_Selector_ready_pop -> %p\n", (void*)ready->fiber);
|
|
259
|
+
|
|
260
|
+
VALUE fiber = ready->fiber;
|
|
261
|
+
|
|
262
|
+
if (ready->flags & IO_EVENT_SELECTOR_QUEUE_INTERNAL) {
|
|
263
|
+
// This means that the fiber was added to the ready queue by the selector itself, and we need to transfer control to it, but before we do that, we need to remove it from the queue, as there is no expectation that returning from `transfer` will remove it.
|
|
290
264
|
queue_pop(backend, ready);
|
|
291
265
|
free(ready);
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
266
|
+
} else if (ready->flags & IO_EVENT_SELECTOR_QUEUE_FIBER) {
|
|
267
|
+
// This means the fiber added itself to the ready queue, and we need to transfer control back to it. Transferring control back to the fiber will call `queue_pop` and remove it from the queue.
|
|
268
|
+
} else {
|
|
269
|
+
rb_raise(rb_eRuntimeError, "Unknown queue type!");
|
|
296
270
|
}
|
|
271
|
+
|
|
272
|
+
IO_Event_Selector_loop_resume(backend, fiber, 0, NULL);
|
|
297
273
|
}
|
|
298
274
|
|
|
299
|
-
int
|
|
275
|
+
int IO_Event_Selector_ready_flush(struct IO_Event_Selector *backend)
|
|
300
276
|
{
|
|
301
277
|
int count = 0;
|
|
302
278
|
|
|
279
|
+
// During iteration of the queue, the same item may be re-queued. If we don't handle this correctly, we may end up in an infinite loop. So, to avoid this situation, we keep note of the current head of the queue and break the loop if we reach the same item again.
|
|
280
|
+
|
|
303
281
|
// Get the current tail and head of the queue:
|
|
304
282
|
struct IO_Event_Selector_Queue *waiting = backend->waiting;
|
|
305
|
-
if (DEBUG) fprintf(stderr, "
|
|
283
|
+
if (DEBUG) fprintf(stderr, "IO_Event_Selector_ready_flush waiting = %p\n", waiting);
|
|
306
284
|
|
|
307
285
|
// Process from head to tail in order:
|
|
308
286
|
// During this, more items may be appended to tail.
|
|
@@ -311,25 +289,10 @@ int IO_Event_Selector_queue_flush(struct IO_Event_Selector *backend)
|
|
|
311
289
|
struct IO_Event_Selector_Queue *ready = backend->ready;
|
|
312
290
|
|
|
313
291
|
count += 1;
|
|
314
|
-
|
|
292
|
+
IO_Event_Selector_ready_pop(backend, ready);
|
|
315
293
|
|
|
316
294
|
if (ready == waiting) break;
|
|
317
295
|
}
|
|
318
296
|
|
|
319
297
|
return count;
|
|
320
298
|
}
|
|
321
|
-
|
|
322
|
-
void IO_Event_Selector_elapsed_time(struct timespec* start, struct timespec* stop, struct timespec *duration)
|
|
323
|
-
{
|
|
324
|
-
if ((stop->tv_nsec - start->tv_nsec) < 0) {
|
|
325
|
-
duration->tv_sec = stop->tv_sec - start->tv_sec - 1;
|
|
326
|
-
duration->tv_nsec = stop->tv_nsec - start->tv_nsec + 1000000000;
|
|
327
|
-
} else {
|
|
328
|
-
duration->tv_sec = stop->tv_sec - start->tv_sec;
|
|
329
|
-
duration->tv_nsec = stop->tv_nsec - start->tv_nsec;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
void IO_Event_Selector_current_time(struct timespec *time) {
|
|
334
|
-
clock_gettime(CLOCK_MONOTONIC, time);
|
|
335
|
-
}
|
|
@@ -1,22 +1,5 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
// of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
// in the Software without restriction, including without limitation the rights
|
|
6
|
-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
// copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
// furnished to do so, subject to the following conditions:
|
|
9
|
-
//
|
|
10
|
-
// The above copyright notice and this permission notice shall be included in
|
|
11
|
-
// all copies or substantial portions of the Software.
|
|
12
|
-
//
|
|
13
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
// THE SOFTWARE.
|
|
1
|
+
// Released under the MIT License.
|
|
2
|
+
// Copyright, 2021-2025, by Samuel Williams.
|
|
20
3
|
|
|
21
4
|
#pragma once
|
|
22
5
|
|
|
@@ -24,6 +7,9 @@
|
|
|
24
7
|
#include <ruby/thread.h>
|
|
25
8
|
#include <ruby/io.h>
|
|
26
9
|
|
|
10
|
+
#include "../time.h"
|
|
11
|
+
#include "../fiber.h"
|
|
12
|
+
|
|
27
13
|
#ifdef HAVE_RUBY_IO_BUFFER_H
|
|
28
14
|
#include <ruby/io/buffer.h>
|
|
29
15
|
#include <ruby/fiber/scheduler.h>
|
|
@@ -33,8 +19,6 @@
|
|
|
33
19
|
#define RUBY_FIBER_SCHEDULER_VERSION 1
|
|
34
20
|
#endif
|
|
35
21
|
|
|
36
|
-
#include <time.h>
|
|
37
|
-
|
|
38
22
|
#ifdef HAVE_SYS_WAIT_H
|
|
39
23
|
#include <sys/wait.h>
|
|
40
24
|
#endif
|
|
@@ -56,14 +40,6 @@ static inline int IO_Event_try_again(int error) {
|
|
|
56
40
|
return error == EAGAIN || error == EWOULDBLOCK;
|
|
57
41
|
}
|
|
58
42
|
|
|
59
|
-
VALUE IO_Event_Selector_fiber_transfer(VALUE fiber, int argc, VALUE *argv);
|
|
60
|
-
|
|
61
|
-
#ifdef HAVE__RB_FIBER_RAISE
|
|
62
|
-
#define IO_Event_Selector_fiber_raise(fiber, argc, argv) rb_fiber_raise(fiber, argc, argv)
|
|
63
|
-
#else
|
|
64
|
-
VALUE IO_Event_Selector_fiber_raise(VALUE fiber, int argc, VALUE *argv);
|
|
65
|
-
#endif
|
|
66
|
-
|
|
67
43
|
#ifdef HAVE_RB_IO_DESCRIPTOR
|
|
68
44
|
#define IO_Event_Selector_io_descriptor(io) rb_io_descriptor(io)
|
|
69
45
|
#else
|
|
@@ -94,28 +70,27 @@ struct IO_Event_Selector_Queue {
|
|
|
94
70
|
VALUE fiber;
|
|
95
71
|
};
|
|
96
72
|
|
|
73
|
+
// The internal state of the event selector.
|
|
74
|
+
// The event selector is responsible for managing the scheduling of fibers, as well as selecting for events.
|
|
97
75
|
struct IO_Event_Selector {
|
|
76
|
+
VALUE self;
|
|
98
77
|
VALUE loop;
|
|
99
78
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// Append to waiting.
|
|
79
|
+
// The ready queue is a list of fibers that are ready to be resumed from the event loop fiber.
|
|
80
|
+
// Append to waiting (front/head of queue).
|
|
103
81
|
struct IO_Event_Selector_Queue *waiting;
|
|
104
|
-
// Process from ready.
|
|
82
|
+
// Process from ready (back/tail of queue).
|
|
105
83
|
struct IO_Event_Selector_Queue *ready;
|
|
106
84
|
};
|
|
107
85
|
|
|
108
|
-
|
|
109
|
-
void IO_Event_Selector_initialize(struct IO_Event_Selector *backend, VALUE loop) {
|
|
110
|
-
backend->loop = loop;
|
|
111
|
-
backend->waiting = NULL;
|
|
112
|
-
backend->ready = NULL;
|
|
113
|
-
}
|
|
86
|
+
void IO_Event_Selector_initialize(struct IO_Event_Selector *backend, VALUE self, VALUE loop);
|
|
114
87
|
|
|
115
88
|
static inline
|
|
116
89
|
void IO_Event_Selector_mark(struct IO_Event_Selector *backend) {
|
|
90
|
+
rb_gc_mark_movable(backend->self);
|
|
117
91
|
rb_gc_mark_movable(backend->loop);
|
|
118
92
|
|
|
93
|
+
// Walk backwards through the ready queue:
|
|
119
94
|
struct IO_Event_Selector_Queue *ready = backend->ready;
|
|
120
95
|
while (ready) {
|
|
121
96
|
rb_gc_mark_movable(ready->fiber);
|
|
@@ -125,6 +100,7 @@ void IO_Event_Selector_mark(struct IO_Event_Selector *backend) {
|
|
|
125
100
|
|
|
126
101
|
static inline
|
|
127
102
|
void IO_Event_Selector_compact(struct IO_Event_Selector *backend) {
|
|
103
|
+
backend->self = rb_gc_location(backend->self);
|
|
128
104
|
backend->loop = rb_gc_location(backend->loop);
|
|
129
105
|
|
|
130
106
|
struct IO_Event_Selector_Queue *ready = backend->ready;
|
|
@@ -134,20 +110,49 @@ void IO_Event_Selector_compact(struct IO_Event_Selector *backend) {
|
|
|
134
110
|
}
|
|
135
111
|
}
|
|
136
112
|
|
|
113
|
+
// Transfer control from the event loop to a user fiber.
|
|
114
|
+
// This is used to transfer control to a user fiber when it may proceed.
|
|
115
|
+
// Strictly speaking, it's not a scheduling operation (does not schedule the current fiber).
|
|
116
|
+
VALUE IO_Event_Selector_loop_resume(struct IO_Event_Selector *backend, VALUE fiber, int argc, VALUE *argv);
|
|
117
|
+
|
|
118
|
+
// Transfer from a user fiber back to the event loop.
|
|
119
|
+
// This is used to transfer control back to the event loop in order to wait for events.
|
|
120
|
+
// Strictly speaking, it's not a scheduling operation (does not schedule the current fiber).
|
|
121
|
+
VALUE IO_Event_Selector_loop_yield(struct IO_Event_Selector *backend);
|
|
122
|
+
|
|
123
|
+
// Resume a specific fiber. This is a scheduling operation.
|
|
124
|
+
// The first argument is the fiber, the rest are the arguments to the resume.
|
|
125
|
+
//
|
|
126
|
+
// The implementation has two possible strategies:
|
|
127
|
+
// 1. Add the current fiber to the ready queue and transfer control to the target fiber.
|
|
128
|
+
// 2. Schedule the target fiber to be resumed by the event loop later on.
|
|
129
|
+
//
|
|
130
|
+
// We currently only implement the first strategy.
|
|
137
131
|
VALUE IO_Event_Selector_resume(struct IO_Event_Selector *backend, int argc, VALUE *argv);
|
|
132
|
+
|
|
133
|
+
// Raise an exception on a specific fiber.
|
|
134
|
+
// The first argument is the fiber, the rest are the arguments to the exception.
|
|
135
|
+
//
|
|
136
|
+
// The implementation has two possible strategies:
|
|
137
|
+
// 1. Add the current fiber to the ready queue and transfer control to the target fiber.
|
|
138
|
+
// 2. Schedule the target fiber to be resumed by the event loop with an exception later on.
|
|
139
|
+
//
|
|
140
|
+
// We currently only implement the first strategy.
|
|
138
141
|
VALUE IO_Event_Selector_raise(struct IO_Event_Selector *backend, int argc, VALUE *argv);
|
|
139
142
|
|
|
143
|
+
// Yield control to the event loop. This is a scheduling operation.
|
|
144
|
+
//
|
|
145
|
+
// The implementation adds the current fiber to the ready queue and transfers control to the event loop.
|
|
140
146
|
static inline
|
|
141
147
|
VALUE IO_Event_Selector_yield(struct IO_Event_Selector *backend)
|
|
142
148
|
{
|
|
143
149
|
return IO_Event_Selector_resume(backend, 1, &backend->loop);
|
|
144
150
|
}
|
|
145
151
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
void
|
|
150
|
-
void IO_Event_Selector_current_time(struct timespec *time);
|
|
152
|
+
// Append a specific fiber to the ready queue.
|
|
153
|
+
// The fiber can be an actual fiber or an object that responds to `alive?` and `transfer`.
|
|
154
|
+
// The implementation will transfer control to the fiber later on.
|
|
155
|
+
void IO_Event_Selector_ready_push(struct IO_Event_Selector *backend, VALUE fiber);
|
|
151
156
|
|
|
152
|
-
|
|
153
|
-
|
|
157
|
+
// Flush the ready queue by transferring control one at a time.
|
|
158
|
+
int IO_Event_Selector_ready_flush(struct IO_Event_Selector *backend);
|