uringmachine 1.0.6 → 1.0.7
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/CHANGELOG.md +6 -0
- data/Gemfile +3 -3
- data/TODO.md +101 -0
- data/ext/um/um.h +14 -2
- data/ext/um/um_class.c +2 -0
- data/ext/um/um_io.c +2 -5
- data/ext/um/um_utils.c +1 -1
- data/lib/uringmachine/fiber_scheduler.rb +289 -114
- data/lib/uringmachine/version.rb +1 -1
- data/test/test_fiber.rb +33 -0
- data/test/test_fiber_scheduler.rb +32 -42
- data/test/test_io.rb +24 -3
- data/test/test_um.rb +14 -1
- metadata +489 -487
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4d5efd4247aaec8a40723d586c1a4ab2c7a252ab02b030f078fda60af8209d8a
|
|
4
|
+
data.tar.gz: 6826e46a5314fd332b1e485d69a4804d6a89f7405c06b8f367ca145f6fc69865
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1745b88d1ea9c71bacbebb09f23b0b95cff4bafce99de392718c7556475a64be1bb32d91b491b832a6353de65f4670081f365cc549303933af27c05fda85fe86
|
|
7
|
+
data.tar.gz: 59a22341f0cc6ae89e8fd55bb753d56a9b2de077c707afb778a9eadd07ab9fc898420c8898c023a68b2c4ee282bb5787d1cc14c8495f4cdba38f20a58ecb193f
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
|
@@ -4,11 +4,11 @@ gemspec
|
|
|
4
4
|
|
|
5
5
|
group :development do
|
|
6
6
|
gem 'localhost'
|
|
7
|
-
gem 'rake-compiler', '~>1.3.
|
|
8
|
-
gem 'minitest', '~>6.0
|
|
7
|
+
gem 'rake-compiler', '~>1.3.3'
|
|
8
|
+
gem 'minitest', '~>6.1.0'
|
|
9
9
|
gem 'benchmark'
|
|
10
10
|
gem 'benchmark-ips'
|
|
11
|
-
gem 'http_parser.rb', '~>0.8.
|
|
11
|
+
gem 'http_parser.rb', '~>0.8.1'
|
|
12
12
|
gem 'yard'
|
|
13
13
|
gem 'irb'
|
|
14
14
|
end
|
data/TODO.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
## File copying with IO
|
|
2
|
+
|
|
3
|
+
```ruby
|
|
4
|
+
# instead of:
|
|
5
|
+
io_foo.read_each { machine.write(some_other_fd, it) }
|
|
6
|
+
|
|
7
|
+
# or:
|
|
8
|
+
io_foo.read_each { io_bar.write(it) }
|
|
9
|
+
|
|
10
|
+
# add:
|
|
11
|
+
io_foo.copy_each(some_other_fd || io)
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Also, we can add a specialized case: sending chunks from a given IO:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
conn.http1_write_chunks_from_io(file_io)
|
|
18
|
+
```
|
|
19
|
+
|
|
1
20
|
## Character scanning in UM::IO
|
|
2
21
|
|
|
3
22
|
- Refactor character scanning
|
|
@@ -164,3 +183,85 @@ inline int test_char(char c, uint64 *bitmap) {
|
|
|
164
183
|
# ['1.1.1.1:80', '2.2.2.2:80']
|
|
165
184
|
tcp_connect_he(*addrs)
|
|
166
185
|
```
|
|
186
|
+
|
|
187
|
+
## Data-Oriented Design
|
|
188
|
+
|
|
189
|
+
see: https://www.youtube.com/watch?v=uI1Yx1CJAB0
|
|
190
|
+
|
|
191
|
+
The current UM implementation is a "forest of pointers". It is also quite
|
|
192
|
+
complex, to a large extent because the design has evolved over a long period of
|
|
193
|
+
time. The automatic buffers feature in particular has necessitated many changes
|
|
194
|
+
to the original design, and the code shows this.
|
|
195
|
+
|
|
196
|
+
Would it be possible to move to a data-oriented design where instead of pointers
|
|
197
|
+
we use indexes into dynamic arrays of:
|
|
198
|
+
|
|
199
|
+
- `um_op`
|
|
200
|
+
- `um_op_result`
|
|
201
|
+
- `um_segment`
|
|
202
|
+
- `um_buffer`
|
|
203
|
+
|
|
204
|
+
In and of itself, switching from pointers to indexes is not that hard. What's
|
|
205
|
+
more challenging is the fact that in UM things are happening concurrently and
|
|
206
|
+
asynchronously, which means different `um_op`s (for example) have different
|
|
207
|
+
lifetimes, so we can't just treat a dynamic array of `um_op` as something that
|
|
208
|
+
just grows. A `um_op` in the middle of the array might be freed up, so we need
|
|
209
|
+
to track lifetimes and free slots. We also need to have some kind of abstraction
|
|
210
|
+
for a linked list that's index-based, and at the same time we need to track free
|
|
211
|
+
slots with a free list.
|
|
212
|
+
|
|
213
|
+
We use the concept of a store. The store itself doesn't deal with tracking the
|
|
214
|
+
list head/tail. This is done at a level above.
|
|
215
|
+
|
|
216
|
+
```c
|
|
217
|
+
typedef uint32_t um_op_idx;
|
|
218
|
+
#define UM_OP_NIL = (uint32_t)-1;
|
|
219
|
+
|
|
220
|
+
struct um_op_store {
|
|
221
|
+
struct um_op *store;
|
|
222
|
+
size_t size;
|
|
223
|
+
size_t used_count;
|
|
224
|
+
um_op_idx free_head;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
void um_op_store_init(struct um_op_store *list, size_t initial_size) {
|
|
228
|
+
list->size = initial_size;
|
|
229
|
+
list->store = malloc(sizeof(um_op) * initial_size);
|
|
230
|
+
memset(list->store, 0, sizeof(um_op) * initial_size);
|
|
231
|
+
if (!(list->store)) rb_raise("Failed to allocate");
|
|
232
|
+
|
|
233
|
+
list->used_count = 0;
|
|
234
|
+
list->free_head = 0;
|
|
235
|
+
|
|
236
|
+
for (um_op_idx i = 0; i < (initial_size - 1); i++)
|
|
237
|
+
list->store[i].next = i + 1;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
void um_op_store_deinit(struct um_op_store *list) {
|
|
241
|
+
free(list->store);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
struct um_op *um_op_store_get(struct um_op_store *list, um_op_idx idx) {
|
|
245
|
+
// TODO: add checkss
|
|
246
|
+
return list->store + idx;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
um_op_idx um_op_store_alloc(struct um_op_store *list) {
|
|
250
|
+
if (list->free_head == UM_OP_NIL) {
|
|
251
|
+
// reallocate list with size * 2
|
|
252
|
+
// set free_head
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
um_op_idx idx = list->free_head;
|
|
256
|
+
list->free_head = list->store[idx].next;
|
|
257
|
+
list->store[idx].next = UM_OP_NIL;
|
|
258
|
+
list->used_count++;
|
|
259
|
+
return idx;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
void um_op_store_free(struct um_op_store *list, um_op_idx idx) {
|
|
263
|
+
list->store[idx].next = list->free_head;
|
|
264
|
+
list->free_head = idx;
|
|
265
|
+
list->used_count--;
|
|
266
|
+
}
|
|
267
|
+
```
|
data/ext/um/um.h
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
|
|
4
4
|
#include <ruby.h>
|
|
5
5
|
#include <liburing.h>
|
|
6
|
+
#include <ruby/io/buffer.h>
|
|
7
|
+
#include <ruby/fiber/scheduler.h>
|
|
6
8
|
|
|
7
9
|
// debugging
|
|
8
10
|
enum {
|
|
@@ -31,8 +33,18 @@ enum {
|
|
|
31
33
|
#define likely(cond) __builtin_expect(!!(cond), 1)
|
|
32
34
|
#endif
|
|
33
35
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
//RUBY_IO_BUFFER_VERSION >= 4
|
|
37
|
+
//RUBY_FIBER_SCHEDULER_VERSION >= 4
|
|
38
|
+
#if RUBY_IO_BUFFER_VERSION >= 4
|
|
39
|
+
#define IO_BUFFER_P(buffer) \
|
|
40
|
+
(TYPE(buffer) == RUBY_T_DATA) && ( \
|
|
41
|
+
rb_obj_is_instance_of(buffer, rb_cIOBufferStorage) || \
|
|
42
|
+
rb_obj_is_instance_of(buffer, rb_cIOBufferSlice) \
|
|
43
|
+
)
|
|
44
|
+
#else
|
|
45
|
+
#define IO_BUFFER_P(buffer) \
|
|
46
|
+
(TYPE(buffer) == RUBY_T_DATA) && rb_obj_is_instance_of(buffer, rb_cIOBuffer)
|
|
47
|
+
#endif
|
|
36
48
|
|
|
37
49
|
enum um_op_kind {
|
|
38
50
|
OP_UNDEFINED,
|
data/ext/um/um_class.c
CHANGED
|
@@ -1529,6 +1529,8 @@ void Init_UM(void) {
|
|
|
1529
1529
|
|
|
1530
1530
|
eUMError = rb_define_class_under(cUM, "Error", rb_eStandardError);
|
|
1531
1531
|
|
|
1532
|
+
rb_define_const(cUM, "RUBY_IO_BUFFER_VERSION", INT2NUM(RUBY_IO_BUFFER_VERSION));
|
|
1533
|
+
rb_define_const(cUM, "RUBY_FIBER_SCHEDULER_VERSION", INT2NUM(RUBY_FIBER_SCHEDULER_VERSION));
|
|
1532
1534
|
um_define_net_constants(cUM);
|
|
1533
1535
|
|
|
1534
1536
|
SYM_size = ID2SYM(rb_intern("size"));
|
data/ext/um/um_io.c
CHANGED
|
@@ -274,10 +274,7 @@ inline void io_shift_head(struct um_io *io) {
|
|
|
274
274
|
}
|
|
275
275
|
|
|
276
276
|
inline VALUE make_segment_io_buffer(struct um_segment *segment, size_t pos) {
|
|
277
|
-
return rb_io_buffer_new(
|
|
278
|
-
segment->ptr + pos, segment->len - pos,
|
|
279
|
-
RB_IO_BUFFER_LOCKED|RB_IO_BUFFER_READONLY
|
|
280
|
-
);
|
|
277
|
+
return rb_io_buffer_new(segment->ptr + pos, segment->len - pos, RB_IO_BUFFER_READONLY);
|
|
281
278
|
}
|
|
282
279
|
|
|
283
280
|
inline void io_skip(struct um_io *io, size_t inc, int safe_inc) {
|
|
@@ -309,7 +306,7 @@ inline void io_read_each(struct um_io *io) {
|
|
|
309
306
|
while (true) {
|
|
310
307
|
struct um_segment *next = current->next;
|
|
311
308
|
buffer = make_segment_io_buffer(current, pos);
|
|
312
|
-
rb_yield(buffer);
|
|
309
|
+
rb_yield(rb_io_buffer_lock(buffer));
|
|
313
310
|
rb_io_buffer_free_locked(buffer);
|
|
314
311
|
io_shift_head(io);
|
|
315
312
|
|
data/ext/um/um_utils.c
CHANGED
|
@@ -93,7 +93,7 @@ static inline void adjust_read_buffer_len(VALUE buffer, int result, ssize_t ofs)
|
|
|
93
93
|
}
|
|
94
94
|
|
|
95
95
|
inline void um_update_read_buffer(VALUE buffer, ssize_t buffer_offset, __s32 result) {
|
|
96
|
-
if (!result) return;
|
|
96
|
+
// if (!result) return;
|
|
97
97
|
|
|
98
98
|
adjust_read_buffer_len(buffer, result, buffer_offset);
|
|
99
99
|
}
|
|
@@ -77,10 +77,286 @@ class UringMachine
|
|
|
77
77
|
# concurrent applications in Ruby, in tight integration with the standard Ruby
|
|
78
78
|
# I/O and locking APIs.
|
|
79
79
|
class FiberScheduler
|
|
80
|
-
BLOCKING_OP_SUPPORT = ENV['BLOCKING_OP_SUPPORT']
|
|
81
80
|
|
|
81
|
+
# read/write methods for FiberScheduler interface v<=3
|
|
82
|
+
module V_LTE_3_Methods
|
|
83
|
+
# Reads from the given IO.
|
|
84
|
+
#
|
|
85
|
+
# @param io [IO] IO object
|
|
86
|
+
# @param buffer [IO::Buffer] read buffer
|
|
87
|
+
# @param length [Integer] read length
|
|
88
|
+
# @param offset [Integer] buffer offset
|
|
89
|
+
# @return [Integer] bytes read
|
|
90
|
+
def io_read(io, buffer, length, offset)
|
|
91
|
+
# p io_read: [io, buffer, length, offset]
|
|
92
|
+
length = buffer.size if length == 0
|
|
93
|
+
|
|
94
|
+
if (timeout = io.timeout)
|
|
95
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
96
|
+
@machine.read(io.fileno, buffer, length, offset)
|
|
97
|
+
rescue Errno::EINTR
|
|
98
|
+
retry
|
|
99
|
+
end
|
|
100
|
+
else
|
|
101
|
+
@machine.read(io.fileno, buffer, length, offset)
|
|
102
|
+
end
|
|
103
|
+
rescue Errno::EINTR
|
|
104
|
+
retry
|
|
105
|
+
rescue Errno => e
|
|
106
|
+
-e.errno
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Reads from the given IO at the given file offset
|
|
110
|
+
#
|
|
111
|
+
# @param io [IO] IO object
|
|
112
|
+
# @param buffer [IO::Buffer] read buffer
|
|
113
|
+
# @param from [Integer] read offset
|
|
114
|
+
# @param length [Integer] read length
|
|
115
|
+
# @param offset [Integer] buffer offset
|
|
116
|
+
# @return [Integer] bytes read
|
|
117
|
+
def io_pread(io, buffer, from, length, offset)
|
|
118
|
+
# p io_pread: [io, buffer, from, length, offset]
|
|
119
|
+
length = buffer.size if length == 0
|
|
120
|
+
|
|
121
|
+
if (timeout = io.timeout)
|
|
122
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
123
|
+
@machine.read(io.fileno, buffer, length, offset, from)
|
|
124
|
+
rescue Errno::EINTR
|
|
125
|
+
retry
|
|
126
|
+
end
|
|
127
|
+
else
|
|
128
|
+
@machine.read(io.fileno, buffer, length, offset, from)
|
|
129
|
+
end
|
|
130
|
+
rescue Errno::EINTR
|
|
131
|
+
retry
|
|
132
|
+
rescue Errno => e
|
|
133
|
+
-e.errno
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Writes to the given IO.
|
|
137
|
+
#
|
|
138
|
+
# @param io [IO] IO object
|
|
139
|
+
# @param buffer [IO::Buffer] write buffer
|
|
140
|
+
# @param length [Integer] write length
|
|
141
|
+
# @param offset [Integer] write offset
|
|
142
|
+
# @return [Integer] bytes written
|
|
143
|
+
def io_write(io, buffer, length, offset)
|
|
144
|
+
# p io_write: [io, buffer, length, offset]
|
|
145
|
+
length = buffer.size if length == 0
|
|
146
|
+
buffer = buffer.slice(offset) if offset > 0
|
|
147
|
+
|
|
148
|
+
if (timeout = io.timeout)
|
|
149
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
150
|
+
@machine.write(io.fileno, buffer, length)
|
|
151
|
+
rescue Errno::EINTR
|
|
152
|
+
retry
|
|
153
|
+
end
|
|
154
|
+
else
|
|
155
|
+
@machine.write(io.fileno, buffer, length)
|
|
156
|
+
end
|
|
157
|
+
rescue Errno::EINTR
|
|
158
|
+
retry
|
|
159
|
+
rescue Errno => e
|
|
160
|
+
-e.errno
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Writes to the given IO at the given file offset.
|
|
164
|
+
#
|
|
165
|
+
# @param io [IO] IO object
|
|
166
|
+
# @param buffer [IO::Buffer] write buffer
|
|
167
|
+
# @param from [Integer] file offset
|
|
168
|
+
# @param length [Integer] write length
|
|
169
|
+
# @param offset [Integer] buffer offset
|
|
170
|
+
# @return [Integer] bytes written
|
|
171
|
+
def io_pwrite(io, buffer, from, length, offset)
|
|
172
|
+
# p io_pwrite: [io, buffer, from, length, offset]
|
|
173
|
+
length = buffer.size if length == 0
|
|
174
|
+
buffer = buffer.slice(offset) if offset > 0
|
|
175
|
+
|
|
176
|
+
if (timeout = io.timeout)
|
|
177
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
178
|
+
@machine.write(io.fileno, buffer, length, from)
|
|
179
|
+
rescue Errno::EINTR
|
|
180
|
+
retry
|
|
181
|
+
end
|
|
182
|
+
else
|
|
183
|
+
@machine.write(io.fileno, buffer, length, from)
|
|
184
|
+
end
|
|
185
|
+
rescue Errno::EINTR
|
|
186
|
+
retry
|
|
187
|
+
rescue Errno => e
|
|
188
|
+
-e.errno
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Interrupts the given fiber with an exception.
|
|
192
|
+
#
|
|
193
|
+
# @param fiber [Fiber] fiber to interrupt
|
|
194
|
+
# @param exception [Exception] Exception
|
|
195
|
+
# @return [void]
|
|
196
|
+
def fiber_interrupt(fiber, exception)
|
|
197
|
+
@machine.schedule(fiber, exception)
|
|
198
|
+
@machine.wakeup
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# read/write methods for FiberScheduler interface v>=4
|
|
203
|
+
module V_GTE_4_Methods
|
|
204
|
+
# Reads from the given IO.
|
|
205
|
+
#
|
|
206
|
+
# @param io [IO] IO object
|
|
207
|
+
# @param buffer [IO::Buffer] read buffer
|
|
208
|
+
# @param offset [Integer] buffer offset
|
|
209
|
+
# @param length [Integer] read length
|
|
210
|
+
# @return [Integer] bytes read
|
|
211
|
+
def io_read(io, buffer, offset, length)
|
|
212
|
+
# p io_read: [io, buffer, length, offset]
|
|
213
|
+
return 0 if length == 0
|
|
214
|
+
|
|
215
|
+
if (timeout = io.timeout)
|
|
216
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
217
|
+
@machine.read(io.fileno, buffer, length, offset)
|
|
218
|
+
rescue Errno::EINTR
|
|
219
|
+
retry
|
|
220
|
+
end
|
|
221
|
+
else
|
|
222
|
+
@machine.read(io.fileno, buffer, length, offset)
|
|
223
|
+
end
|
|
224
|
+
rescue Errno::EINTR
|
|
225
|
+
retry
|
|
226
|
+
rescue Errno => e
|
|
227
|
+
-e.errno
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Reads from the given IO at the given file offset
|
|
231
|
+
#
|
|
232
|
+
# @param io [IO] IO object
|
|
233
|
+
# @param buffer [IO::Buffer] read buffer
|
|
234
|
+
# @param from [Integer] read offset
|
|
235
|
+
# @param offset [Integer] buffer offset
|
|
236
|
+
# @param length [Integer] read length
|
|
237
|
+
# @return [Integer] bytes read
|
|
238
|
+
def io_pread(io, buffer, from, offset, length)
|
|
239
|
+
# p io_pread: [io, buffer, from, length, offset]
|
|
240
|
+
return 0 if length == 0
|
|
241
|
+
|
|
242
|
+
if (timeout = io.timeout)
|
|
243
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
244
|
+
@machine.read(io.fileno, buffer, length, offset, from)
|
|
245
|
+
rescue Errno::EINTR
|
|
246
|
+
retry
|
|
247
|
+
end
|
|
248
|
+
else
|
|
249
|
+
@machine.read(io.fileno, buffer, length, offset, from)
|
|
250
|
+
end
|
|
251
|
+
rescue Errno::EINTR
|
|
252
|
+
retry
|
|
253
|
+
rescue Errno => e
|
|
254
|
+
-e.errno
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Writes to the given IO.
|
|
258
|
+
#
|
|
259
|
+
# @param io [IO] IO object
|
|
260
|
+
# @param buffer [IO::Buffer] write buffer
|
|
261
|
+
# @param offset [Integer] write offset
|
|
262
|
+
# @param length [Integer] write length
|
|
263
|
+
# @return [Integer] bytes written
|
|
264
|
+
def io_write(io, buffer, offset, length)
|
|
265
|
+
# p io_write: [io, buffer, length, offset]
|
|
266
|
+
return 0 if length == 0
|
|
267
|
+
|
|
268
|
+
buffer = buffer.slice(offset) if offset > 0
|
|
269
|
+
|
|
270
|
+
if (timeout = io.timeout)
|
|
271
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
272
|
+
@machine.write(io.fileno, buffer, length)
|
|
273
|
+
rescue Errno::EINTR
|
|
274
|
+
retry
|
|
275
|
+
end
|
|
276
|
+
else
|
|
277
|
+
@machine.write(io.fileno, buffer, length)
|
|
278
|
+
end
|
|
279
|
+
rescue Errno::EINTR
|
|
280
|
+
retry
|
|
281
|
+
rescue Errno => e
|
|
282
|
+
-e.errno
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# Writes to the given IO at the given file offset.
|
|
286
|
+
#
|
|
287
|
+
# @param io [IO] IO object
|
|
288
|
+
# @param buffer [IO::Buffer] write buffer
|
|
289
|
+
# @param from [Integer] file offset
|
|
290
|
+
# @param offset [Integer] buffer offset
|
|
291
|
+
# @param length [Integer] write length
|
|
292
|
+
# @return [Integer] bytes written
|
|
293
|
+
def io_pwrite(io, buffer, from, offset, length)
|
|
294
|
+
# p io_pwrite: [io, buffer, from, length, offset]
|
|
295
|
+
return 0 if length == 0
|
|
296
|
+
|
|
297
|
+
buffer = buffer.slice(offset) if offset > 0
|
|
298
|
+
|
|
299
|
+
if (timeout = io.timeout)
|
|
300
|
+
@machine.timeout(timeout, Timeout::Error) do
|
|
301
|
+
@machine.write(io.fileno, buffer, length, from)
|
|
302
|
+
rescue Errno::EINTR
|
|
303
|
+
retry
|
|
304
|
+
end
|
|
305
|
+
else
|
|
306
|
+
@machine.write(io.fileno, buffer, length, from)
|
|
307
|
+
end
|
|
308
|
+
rescue Errno::EINTR
|
|
309
|
+
retry
|
|
310
|
+
rescue Errno => e
|
|
311
|
+
-e.errno
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Interrupts the given fiber with an exception.
|
|
315
|
+
#
|
|
316
|
+
# @param target [Object] fiber target
|
|
317
|
+
# @param exception [Exception] Exception
|
|
318
|
+
# @return [void]
|
|
319
|
+
def fiber_interrupt(fiber, exception)
|
|
320
|
+
m = Thread.current.__um_fiber_interrupt_machine__ ||= UM.new(size: 4)
|
|
321
|
+
m.push(@fiber_interrupt_queue, [fiber, exception])
|
|
322
|
+
@machine.wakeup
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# Sets up a fiber interrupt watcher, which handles queued fiber_interrupt
|
|
326
|
+
# requests. In Ruby 4.1, when the fiber_interrupt hook is invoked, the
|
|
327
|
+
# fiber to interrupt is wrapped in a custom target class instance that
|
|
328
|
+
# hides the relevant fiber behind a minimal interface, so we can't
|
|
329
|
+
# directly schedule the fiber. Instead, we put the target on a queue and
|
|
330
|
+
# handle it asynchronously.
|
|
331
|
+
def setup_fiber_interrupt_watcher
|
|
332
|
+
@fiber_interrupt_queue = UM::Queue.new
|
|
333
|
+
@machine.spin do
|
|
334
|
+
loop do
|
|
335
|
+
target, exception = @machine.shift(@fiber_interrupt_queue)
|
|
336
|
+
break if target == :STOP
|
|
337
|
+
|
|
338
|
+
@machine.schedule(Fiber.current, nil)
|
|
339
|
+
target.raise(exception)
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# Thread extensions that allow caching of a UM instance for pushing to a
|
|
346
|
+
# fiber_interrupt queue.
|
|
347
|
+
module FiberSchedulerThreadExtensions
|
|
348
|
+
attr_accessor :__um_fiber_interrupt_machine__
|
|
349
|
+
end
|
|
82
350
|
|
|
351
|
+
if UM::RUBY_FIBER_SCHEDULER_VERSION < 4
|
|
352
|
+
include(V_LTE_3_Methods)
|
|
353
|
+
else
|
|
354
|
+
include(V_GTE_4_Methods)
|
|
355
|
+
::Thread.include(FiberSchedulerThreadExtensions)
|
|
356
|
+
end
|
|
357
|
+
|
|
83
358
|
# The blocking operation thread pool is shared by all fiber schedulers.
|
|
359
|
+
BLOCKING_OP_SUPPORT = ENV['BLOCKING_OP_SUPPORT']
|
|
84
360
|
DEFAULT_THREAD_POOL = BLOCKING_OP_SUPPORT && BlockingOperationThreadPool.new
|
|
85
361
|
|
|
86
362
|
# UringMachine instance associated with scheduler.
|
|
@@ -102,6 +378,7 @@ class UringMachine
|
|
|
102
378
|
@thread_pool = thread_pool
|
|
103
379
|
@fiber_map = ObjectSpace::WeakMap.new
|
|
104
380
|
@thread = Thread.current
|
|
381
|
+
setup_fiber_interrupt_watcher if respond_to?(:setup_fiber_interrupt_watcher)
|
|
105
382
|
end
|
|
106
383
|
|
|
107
384
|
# :nodoc:
|
|
@@ -130,6 +407,7 @@ class UringMachine
|
|
|
130
407
|
#
|
|
131
408
|
# @return [void]
|
|
132
409
|
def scheduler_close
|
|
410
|
+
@machine.push(@fiber_interrupt_queue, :STOP) if @fiber_interrupt_queue
|
|
133
411
|
join()
|
|
134
412
|
end
|
|
135
413
|
|
|
@@ -148,7 +426,6 @@ class UringMachine
|
|
|
148
426
|
end
|
|
149
427
|
|
|
150
428
|
if BLOCKING_OP_SUPPORT
|
|
151
|
-
|
|
152
429
|
# Runs the given operation in a separate thread, so as not to block other
|
|
153
430
|
# fibers.
|
|
154
431
|
#
|
|
@@ -257,114 +534,6 @@ class UringMachine
|
|
|
257
534
|
[unmap_fds(r, map_r), unmap_fds(w, map_w), unmap_fds(e, map_e)]
|
|
258
535
|
end
|
|
259
536
|
|
|
260
|
-
# Reads from the given IO.
|
|
261
|
-
#
|
|
262
|
-
# @param io [IO] IO object
|
|
263
|
-
# @param buffer [IO::Buffer] read buffer
|
|
264
|
-
# @param length [Integer] read length
|
|
265
|
-
# @param offset [Integer] buffer offset
|
|
266
|
-
# @return [Integer] bytes read
|
|
267
|
-
def io_read(io, buffer, length, offset)
|
|
268
|
-
# p io_read: [io, buffer, length, offset]
|
|
269
|
-
length = buffer.size if length == 0
|
|
270
|
-
|
|
271
|
-
if (timeout = io.timeout)
|
|
272
|
-
@machine.timeout(timeout, Timeout::Error) do
|
|
273
|
-
@machine.read(io.fileno, buffer, length, offset)
|
|
274
|
-
rescue Errno::EINTR
|
|
275
|
-
retry
|
|
276
|
-
end
|
|
277
|
-
else
|
|
278
|
-
@machine.read(io.fileno, buffer, length, offset)
|
|
279
|
-
end
|
|
280
|
-
rescue Errno::EINTR
|
|
281
|
-
retry
|
|
282
|
-
rescue Errno => e
|
|
283
|
-
-e.errno
|
|
284
|
-
end
|
|
285
|
-
|
|
286
|
-
# Reads from the given IO at the given file offset
|
|
287
|
-
#
|
|
288
|
-
# @param io [IO] IO object
|
|
289
|
-
# @param buffer [IO::Buffer] read buffer
|
|
290
|
-
# @param from [Integer] read offset
|
|
291
|
-
# @param length [Integer] read length
|
|
292
|
-
# @param offset [Integer] buffer offset
|
|
293
|
-
# @return [Integer] bytes read
|
|
294
|
-
def io_pread(io, buffer, from, length, offset)
|
|
295
|
-
# p io_pread: [io, buffer, from, length, offset]
|
|
296
|
-
length = buffer.size if length == 0
|
|
297
|
-
|
|
298
|
-
if (timeout = io.timeout)
|
|
299
|
-
@machine.timeout(timeout, Timeout::Error) do
|
|
300
|
-
@machine.read(io.fileno, buffer, length, offset, from)
|
|
301
|
-
rescue Errno::EINTR
|
|
302
|
-
retry
|
|
303
|
-
end
|
|
304
|
-
else
|
|
305
|
-
@machine.read(io.fileno, buffer, length, offset, from)
|
|
306
|
-
end
|
|
307
|
-
rescue Errno::EINTR
|
|
308
|
-
retry
|
|
309
|
-
rescue Errno => e
|
|
310
|
-
-e.errno
|
|
311
|
-
end
|
|
312
|
-
|
|
313
|
-
# Writes to the given IO.
|
|
314
|
-
#
|
|
315
|
-
# @param io [IO] IO object
|
|
316
|
-
# @param buffer [IO::Buffer] write buffer
|
|
317
|
-
# @param length [Integer] write length
|
|
318
|
-
# @param offset [Integer] write offset
|
|
319
|
-
# @return [Integer] bytes written
|
|
320
|
-
def io_write(io, buffer, length, offset)
|
|
321
|
-
# p io_write: [io, buffer, length, offset]
|
|
322
|
-
length = buffer.size if length == 0
|
|
323
|
-
buffer = buffer.slice(offset) if offset > 0
|
|
324
|
-
|
|
325
|
-
if (timeout = io.timeout)
|
|
326
|
-
@machine.timeout(timeout, Timeout::Error) do
|
|
327
|
-
@machine.write(io.fileno, buffer, length)
|
|
328
|
-
rescue Errno::EINTR
|
|
329
|
-
retry
|
|
330
|
-
end
|
|
331
|
-
else
|
|
332
|
-
@machine.write(io.fileno, buffer, length)
|
|
333
|
-
end
|
|
334
|
-
rescue Errno::EINTR
|
|
335
|
-
retry
|
|
336
|
-
rescue Errno => e
|
|
337
|
-
-e.errno
|
|
338
|
-
end
|
|
339
|
-
|
|
340
|
-
# Writes to the given IO at the given file offset.
|
|
341
|
-
#
|
|
342
|
-
# @param io [IO] IO object
|
|
343
|
-
# @param buffer [IO::Buffer] write buffer
|
|
344
|
-
# @param from [Integer] file offset
|
|
345
|
-
# @param length [Integer] write length
|
|
346
|
-
# @param offset [Integer] buffer offset
|
|
347
|
-
# @return [Integer] bytes written
|
|
348
|
-
def io_pwrite(io, buffer, from, length, offset)
|
|
349
|
-
# p io_pwrite: [io, buffer, from, length, offset]
|
|
350
|
-
length = buffer.size if length == 0
|
|
351
|
-
buffer = buffer.slice(offset) if offset > 0
|
|
352
|
-
|
|
353
|
-
if (timeout = io.timeout)
|
|
354
|
-
@machine.timeout(timeout, Timeout::Error) do
|
|
355
|
-
@machine.write(io.fileno, buffer, length, from)
|
|
356
|
-
rescue Errno::EINTR
|
|
357
|
-
retry
|
|
358
|
-
end
|
|
359
|
-
else
|
|
360
|
-
@machine.write(io.fileno, buffer, length, from)
|
|
361
|
-
end
|
|
362
|
-
rescue Errno::EINTR
|
|
363
|
-
retry
|
|
364
|
-
rescue Errno => e
|
|
365
|
-
-e.errno
|
|
366
|
-
end
|
|
367
|
-
|
|
368
537
|
# Closes the given fd.
|
|
369
538
|
#
|
|
370
539
|
# @param fd [Integer] file descriptor
|
|
@@ -395,9 +564,15 @@ class UringMachine
|
|
|
395
564
|
# @param exception [Exception] Exception
|
|
396
565
|
# @return [void]
|
|
397
566
|
def fiber_interrupt(fiber, exception)
|
|
398
|
-
|
|
399
|
-
|
|
567
|
+
if fiber.is_a?(Fiber)
|
|
568
|
+
@machine.schedule(fiber, exception)
|
|
569
|
+
else
|
|
570
|
+
m = Thread.current.__um_fiber_interrupt_machine__ ||= UM.new(size: 4)
|
|
571
|
+
# @machine.schedule(Fiber.current, exception)
|
|
572
|
+
m.push(@fiber_interrupt_queue, [fiber, exception])
|
|
573
|
+
end
|
|
400
574
|
@machine.wakeup
|
|
575
|
+
nil
|
|
401
576
|
end
|
|
402
577
|
|
|
403
578
|
# Resolves an hostname.
|
|
@@ -429,8 +604,8 @@ class UringMachine
|
|
|
429
604
|
# @return [void]
|
|
430
605
|
def p(o)
|
|
431
606
|
UM.debug(o.inspect)
|
|
432
|
-
UM.debug(caller[2..12].inspect)
|
|
433
|
-
UM.debug("")
|
|
607
|
+
# UM.debug(caller[2..12].inspect)
|
|
608
|
+
# UM.debug("")
|
|
434
609
|
end
|
|
435
610
|
|
|
436
611
|
# Maps the given ios to fds.
|
data/lib/uringmachine/version.rb
CHANGED