uringmachine 0.8.2 → 0.11

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.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/TODO.md +0 -1
  4. data/examples/bm_side_running.rb +83 -0
  5. data/examples/bm_sqlite.rb +1 -1
  6. data/ext/um/um.c +66 -4
  7. data/ext/um/um.h +36 -0
  8. data/ext/um/um_class.c +6 -0
  9. data/ext/um/um_const.c +36 -0
  10. data/ext/um/um_ext.c +2 -0
  11. data/ext/um/um_stream.c +344 -0
  12. data/ext/um/um_stream_class.c +140 -0
  13. data/ext/um/um_utils.c +4 -0
  14. data/lib/uringmachine/actor.rb +1 -1
  15. data/lib/uringmachine/version.rb +1 -1
  16. data/lib/uringmachine.rb +35 -17
  17. data/test/test_fiber.rb +23 -3
  18. data/test/test_stream.rb +133 -0
  19. data/test/test_um.rb +109 -2
  20. data/uringmachine.gemspec +0 -2
  21. data/vendor/liburing/.github/workflows/{build.yml → ci.yml} +107 -42
  22. data/vendor/liburing/.gitignore +1 -0
  23. data/vendor/liburing/CHANGELOG +10 -0
  24. data/vendor/liburing/README +5 -0
  25. data/vendor/liburing/configure +1 -1
  26. data/vendor/liburing/examples/Makefile +1 -0
  27. data/vendor/liburing/examples/helpers.c +25 -0
  28. data/vendor/liburing/examples/helpers.h +13 -0
  29. data/vendor/liburing/examples/io_uring-test.c +3 -0
  30. data/vendor/liburing/examples/proxy.c +1 -1
  31. data/vendor/liburing/examples/reg-wait.c +41 -6
  32. data/vendor/liburing/examples/send-zerocopy.c +79 -32
  33. data/vendor/liburing/examples/zcrx.c +436 -0
  34. data/vendor/liburing/liburing.spec +1 -1
  35. data/vendor/liburing/src/Makefile +0 -1
  36. data/vendor/liburing/src/arch/generic/syscall.h +2 -2
  37. data/vendor/liburing/src/arch/syscall-defs.h +2 -2
  38. data/vendor/liburing/src/include/liburing/io_uring.h +101 -17
  39. data/vendor/liburing/src/include/liburing.h +179 -59
  40. data/vendor/liburing/src/int_flags.h +4 -1
  41. data/vendor/liburing/src/liburing-ffi.map +14 -2
  42. data/vendor/liburing/src/liburing.map +9 -2
  43. data/vendor/liburing/src/queue.c +35 -30
  44. data/vendor/liburing/src/register.c +46 -15
  45. data/vendor/liburing/src/sanitize.c +6 -9
  46. data/vendor/liburing/src/setup.c +37 -71
  47. data/vendor/liburing/src/syscall.c +2 -2
  48. data/vendor/liburing/test/232c93d07b74.c +1 -0
  49. data/vendor/liburing/test/Makefile +9 -0
  50. data/vendor/liburing/test/accept-test.c +1 -0
  51. data/vendor/liburing/test/cmd-discard.c +16 -8
  52. data/vendor/liburing/test/connect.c +11 -7
  53. data/vendor/liburing/test/epwait.c +420 -0
  54. data/vendor/liburing/test/eventfd-ring.c +30 -5
  55. data/vendor/liburing/test/fallocate.c +1 -1
  56. data/vendor/liburing/test/fixed-hugepage.c +10 -7
  57. data/vendor/liburing/test/fixed-seg.c +187 -0
  58. data/vendor/liburing/test/helpers.c +121 -0
  59. data/vendor/liburing/test/helpers.h +13 -0
  60. data/vendor/liburing/test/init-mem.c +2 -0
  61. data/vendor/liburing/test/io_uring_passthrough.c +78 -62
  62. data/vendor/liburing/test/iopoll-overflow.c +5 -4
  63. data/vendor/liburing/test/iopoll.c +20 -10
  64. data/vendor/liburing/test/iowait.c +141 -0
  65. data/vendor/liburing/test/nvme.h +2 -0
  66. data/vendor/liburing/test/pipe-bug.c +11 -5
  67. data/vendor/liburing/test/pipe-eof.c +11 -1
  68. data/vendor/liburing/test/read-inc-file.c +150 -0
  69. data/vendor/liburing/test/read-write.c +21 -14
  70. data/vendor/liburing/test/recv-bundle-short-ooo.c +435 -0
  71. data/vendor/liburing/test/recv-multishot.c +2 -2
  72. data/vendor/liburing/test/reg-wait.c +449 -120
  73. data/vendor/liburing/test/regbuf-clone.c +53 -0
  74. data/vendor/liburing/test/resize-rings.c +25 -2
  75. data/vendor/liburing/test/rsrc_tags.c +67 -14
  76. data/vendor/liburing/test/send-zerocopy.c +52 -130
  77. data/vendor/liburing/test/sendmsg_iov_clean.c +216 -0
  78. data/vendor/liburing/test/socket-nb.c +158 -0
  79. data/vendor/liburing/test/sqwait.c +9 -11
  80. data/vendor/liburing/test/timeout.c +198 -0
  81. data/vendor/liburing/test/vec-regbuf.c +609 -0
  82. data/vendor/liburing/test/wait-timeout.c +1 -1
  83. data/vendor/liburing/test/wq-aff.c +5 -1
  84. data/vendor/liburing/test/zcrx.c +928 -0
  85. metadata +16 -32
  86. data/vendor/liburing/.github/workflows/codespell.yml +0 -25
  87. data/vendor/liburing/.github/workflows/shellcheck.yml +0 -20
@@ -0,0 +1,344 @@
1
+ #include "um.h"
2
+
3
+ static inline void stream_check_truncate_buffer(struct um_stream *stream) {
4
+ if ((stream->pos == stream->len) && (stream->len >= 1 << 12)) {
5
+ rb_str_modify(stream->buffer);
6
+ rb_str_set_len(stream->buffer, 0);
7
+ stream->len = 0;
8
+ stream->pos = 0;
9
+ }
10
+ else if (stream->pos >= 1 << 12) {
11
+ rb_str_modify(stream->buffer);
12
+ char *base = RSTRING_PTR(stream->buffer);
13
+ int len_rest = stream->len - stream->pos;
14
+ memmove(base, base + stream->pos, len_rest);
15
+ rb_str_set_len(stream->buffer, len_rest);
16
+ stream->len = len_rest;
17
+ stream->pos = 0;
18
+ }
19
+ }
20
+
21
+ int stream_read_more(struct um_stream *stream) {
22
+ stream_check_truncate_buffer(stream);
23
+
24
+ size_t maxlen = 1 << 12;
25
+ size_t capa = rb_str_capacity(stream->buffer);
26
+ if (capa - stream->pos < maxlen)
27
+ rb_str_modify_expand(stream->buffer, maxlen - (capa - stream->pos));
28
+
29
+ rb_str_modify(stream->buffer);
30
+ char *ptr = RSTRING_PTR(stream->buffer) + stream->pos;
31
+ size_t ret = um_read_raw(stream->machine, stream->fd, ptr, maxlen);
32
+
33
+ if (ret == 0) {
34
+ stream->eof = 1;
35
+ return 0;
36
+ }
37
+
38
+ stream->len = stream->pos + ret;
39
+ rb_str_set_len(stream->buffer, stream->len);
40
+ return 1;
41
+ }
42
+
43
+ // ensure string can hold at least len bytes
44
+ static inline void str_expand(VALUE str, size_t len) {
45
+ size_t capa = rb_str_capacity(str);
46
+ if (capa < len + 1) rb_str_modify_expand(str, len + 1 - capa);
47
+ }
48
+
49
+ VALUE resp_get_line(struct um_stream *stream, VALUE out_buffer) {
50
+ char *start = RSTRING_PTR(stream->buffer) + stream->pos;
51
+ while (true) {
52
+ char * lf_ptr = memchr(start, '\r', stream->len - stream->pos);
53
+ if (lf_ptr) {
54
+ ulong len = lf_ptr - start;
55
+ stream->pos += len + 2;
56
+
57
+ if (NIL_P(out_buffer)) {
58
+ VALUE str = rb_str_new(start, len + 1);
59
+ rb_str_set_len(str, len);
60
+ RSTRING_PTR(str)[len] = 0;
61
+ return str;
62
+ }
63
+
64
+ str_expand(out_buffer, len + 1);
65
+ char *dest_ptr = RSTRING_PTR(out_buffer);
66
+ memcpy(dest_ptr, start, len);
67
+ dest_ptr[len] = 0; // add null at end
68
+ rb_str_set_len(out_buffer, len);
69
+ return out_buffer;
70
+ }
71
+
72
+ if (stream_read_more(stream))
73
+ // buffer ptr and pos may have changed after reading
74
+ start = RSTRING_PTR(stream->buffer) + stream->pos;
75
+ else
76
+ return Qnil;
77
+ }
78
+ }
79
+
80
+ VALUE resp_get_string(struct um_stream *stream, ulong len, VALUE out_buffer) {
81
+ ulong read_len = len + 2;
82
+
83
+ while (stream->len - stream->pos < read_len)
84
+ if (!stream_read_more(stream)) return Qnil;
85
+
86
+ char *start = RSTRING_PTR(stream->buffer) + stream->pos;
87
+ stream->pos += read_len;
88
+
89
+ if (NIL_P(out_buffer)) return rb_utf8_str_new(start, len);
90
+
91
+ str_expand(out_buffer, len + 1);
92
+ char *dest_ptr = RSTRING_PTR(out_buffer);
93
+ memcpy(dest_ptr, start, len);
94
+ dest_ptr[len] = 0; // add null at end
95
+ rb_str_set_len(out_buffer, len);
96
+ return out_buffer;
97
+ }
98
+
99
+ inline ulong resp_parse_length_field(const char *ptr, int len) {
100
+ return strtoul(ptr + 1, NULL, 10);
101
+ }
102
+
103
+ VALUE resp_decode_hash(struct um_stream *stream, VALUE out_buffer, ulong len) {
104
+ VALUE hash = rb_hash_new();
105
+
106
+ for (ulong i = 0; i < len; i++) {
107
+ VALUE key = resp_decode(stream, out_buffer);
108
+ VALUE value = resp_decode(stream, out_buffer);
109
+ rb_hash_aset(hash, key, value);
110
+ RB_GC_GUARD(key);
111
+ RB_GC_GUARD(value);
112
+ }
113
+
114
+ RB_GC_GUARD(hash);
115
+ return hash;
116
+ }
117
+
118
+ VALUE resp_decode_array(struct um_stream *stream, VALUE out_buffer, ulong len) {
119
+ VALUE array = rb_ary_new2(len);
120
+
121
+ for (ulong i = 0; i < len; i++) {
122
+ VALUE value = resp_decode(stream, out_buffer);
123
+ rb_ary_push(array, value);
124
+ RB_GC_GUARD(value);
125
+ }
126
+
127
+ RB_GC_GUARD(array);
128
+ return array;
129
+ }
130
+
131
+ static inline VALUE resp_decode_simple_string(char *ptr, ulong len) {
132
+ return rb_str_new(ptr + 1, len - 1);
133
+ }
134
+
135
+ static inline VALUE resp_decode_string(struct um_stream *stream, VALUE out_buffer, ulong len) {
136
+ return resp_get_string(stream, len, out_buffer);
137
+ }
138
+
139
+ static inline VALUE resp_decode_string_with_encoding(struct um_stream *stream, VALUE out_buffer, ulong len) {
140
+ VALUE with_enc = resp_get_string(stream, len, out_buffer);
141
+ char *ptr = RSTRING_PTR(with_enc);
142
+ len = RSTRING_LEN(with_enc);
143
+ if ((len < 4) || (ptr[3] != ':')) return Qnil;
144
+
145
+ return rb_utf8_str_new(ptr + 4, len - 4);
146
+ }
147
+
148
+ static inline VALUE resp_decode_integer(char *ptr) {
149
+ long value = strtol(ptr + 1, NULL, 10);
150
+ return LONG2NUM(value);
151
+ }
152
+
153
+ static inline VALUE resp_decode_float(char *ptr) {
154
+ double value = strtod(ptr + 1, NULL);
155
+ return DBL2NUM(value);
156
+ }
157
+
158
+ static inline VALUE resp_decode_simple_error(char *ptr, ulong len) {
159
+ static ID ID_new = 0;
160
+ if (!ID_new) ID_new = rb_intern("new");
161
+
162
+ VALUE msg = rb_str_new(ptr + 1, len - 1);
163
+ VALUE err = rb_funcall(rb_eRuntimeError, ID_new, 1, msg);
164
+ RB_GC_GUARD(msg);
165
+ return err;
166
+ }
167
+
168
+ static inline VALUE resp_decode_error(struct um_stream *stream, VALUE out_buffer, ulong len) {
169
+ static ID ID_new = 0;
170
+ if (!ID_new) ID_new = rb_intern("new");
171
+
172
+ VALUE msg = resp_decode_string(stream, out_buffer, len);
173
+ VALUE err = rb_funcall(rb_eRuntimeError, ID_new, 1, msg);
174
+ RB_GC_GUARD(msg);
175
+ return err;
176
+ }
177
+
178
+ VALUE resp_decode(struct um_stream *stream, VALUE out_buffer) {
179
+ VALUE msg = resp_get_line(stream, out_buffer);
180
+ if (msg == Qnil) return Qnil;
181
+
182
+ char *ptr = RSTRING_PTR(msg);
183
+ ulong len = RSTRING_LEN(msg);
184
+ ulong data_len;
185
+ if (len == 0) return Qnil;
186
+
187
+ switch (ptr[0]) {
188
+ case '%': // hash
189
+ case '|': // attributes hash
190
+ data_len = resp_parse_length_field(ptr, len);
191
+ return resp_decode_hash(stream, out_buffer, data_len);
192
+
193
+ case '*': // array
194
+ case '~': // set
195
+ case '>': // pub/sub push
196
+ data_len = resp_parse_length_field(ptr, len);
197
+ return resp_decode_array(stream, out_buffer, data_len);
198
+
199
+ case '+': // simple string
200
+ return resp_decode_simple_string(ptr, len);
201
+ case '$': // string
202
+ data_len = resp_parse_length_field(ptr, len);
203
+ return resp_decode_string(stream, out_buffer, data_len);
204
+ case '=': // string with encoding
205
+ data_len = resp_parse_length_field(ptr, len);
206
+ return resp_decode_string_with_encoding(stream, out_buffer, data_len);
207
+
208
+ case '_': // null
209
+ return Qnil;
210
+ case '#': // boolean
211
+ return (len > 1) && (ptr[1] == 't') ? Qtrue : Qfalse;
212
+
213
+ case ':': // integer
214
+ return resp_decode_integer(ptr);
215
+ case '(': // big integer
216
+ rb_raise(rb_eRuntimeError, "Big integers are not supported");
217
+ case ',': // float
218
+ return resp_decode_float(ptr);
219
+
220
+ case '-': // simple error
221
+ return resp_decode_simple_error(ptr, len);
222
+ case '!': // error
223
+ data_len = resp_parse_length_field(ptr, len);
224
+ return resp_decode_error(stream, out_buffer, data_len);
225
+ default:
226
+ rb_raise(rb_eRuntimeError, "Invalid character encountered");
227
+ }
228
+
229
+ RB_GC_GUARD(msg);
230
+ }
231
+
232
+ void write_buffer_init(struct um_write_buffer *buf, VALUE str) {
233
+ size_t capa = 1 << 12;
234
+ size_t len = RSTRING_LEN(str);
235
+ while (capa < len) capa += 1 << 12;
236
+
237
+ rb_str_resize(str, capa);
238
+ rb_str_set_len(str, len);
239
+ buf->str = str;
240
+ buf->capa = capa;
241
+ buf->len = len;
242
+ buf->ptr = RSTRING_PTR(str);
243
+ }
244
+
245
+ static inline void write_buffer_expand(struct um_write_buffer *buf, size_t newsize) {
246
+ if (buf->capa < newsize) {
247
+ size_t old_capa = buf->capa;
248
+ while (buf->capa < newsize) buf->capa += 1 << 12;
249
+ rb_str_modify_expand(buf->str, buf->capa - old_capa);
250
+ buf->ptr = RSTRING_PTR(buf->str);
251
+ }
252
+ }
253
+
254
+ static inline void write_buffer_append(struct um_write_buffer *buf, const char *ptr, size_t len) {
255
+ size_t total_len = buf->len + len;
256
+ write_buffer_expand(buf, total_len);
257
+
258
+ memcpy(buf->ptr + buf->len, ptr, len);
259
+ buf->len = total_len;
260
+ }
261
+
262
+ static inline void write_buffer_append_cstr(struct um_write_buffer *buf, const char *str) {
263
+ write_buffer_append(buf, str, strlen(str));
264
+ }
265
+
266
+ static inline void write_buffer_append_resp_bulk_string(struct um_write_buffer *buf, VALUE str) {
267
+ // leave enough place for prefix and postfix
268
+ size_t str_len = RSTRING_LEN(str);
269
+ size_t total_len = buf->len + str_len + 16;
270
+ write_buffer_expand(buf, total_len);
271
+
272
+
273
+ int prefix_len = sprintf(buf->ptr + buf->len, "$%ld\r\n", str_len);
274
+ const char *src = RSTRING_PTR(str);
275
+ memcpy(buf->ptr + buf->len + prefix_len, src, str_len);
276
+ buf->ptr[buf->len + prefix_len + str_len + 0] = '\r';
277
+ buf->ptr[buf->len + prefix_len + str_len + 1] = '\n';
278
+ buf->len += prefix_len + str_len + 2;
279
+ }
280
+
281
+ inline void write_buffer_update_len(struct um_write_buffer *buf) {
282
+ rb_str_set_len(buf->str, buf->len);
283
+ }
284
+
285
+ struct resp_encode_hash_ctx {
286
+ struct um_write_buffer *buf;
287
+ VALUE obj;
288
+ };
289
+
290
+ int resp_encode_hash_entry(VALUE key, VALUE value, VALUE arg) {
291
+ struct resp_encode_hash_ctx *ctx = (struct resp_encode_hash_ctx *)arg;
292
+
293
+ resp_encode(ctx->buf, key);
294
+ resp_encode(ctx->buf, value);
295
+ return 0;
296
+ }
297
+
298
+ void resp_encode(struct um_write_buffer *buf, VALUE obj) {
299
+ char tmp[60];
300
+
301
+ switch (TYPE(obj)) {
302
+ case T_NIL:
303
+ return write_buffer_append_cstr(buf, "_\r\n");
304
+ return;
305
+ case T_FALSE:
306
+ write_buffer_append_cstr(buf, "#f\r\n");
307
+ return;
308
+ case T_TRUE:
309
+ write_buffer_append_cstr(buf, "#t\r\n");
310
+ return;
311
+ case T_FIXNUM:
312
+ sprintf(tmp, ":%ld\r\n", NUM2LONG(obj));
313
+ write_buffer_append_cstr(buf, tmp);
314
+ return;
315
+ case T_FLOAT:
316
+ sprintf(tmp, ",%lg\r\n", NUM2DBL(obj));
317
+ write_buffer_append_cstr(buf, tmp);
318
+ return;
319
+ case T_STRING:
320
+ write_buffer_append_resp_bulk_string(buf, obj);
321
+ return;
322
+ case T_ARRAY:
323
+ {
324
+ ulong len = RARRAY_LEN(obj);
325
+ sprintf(tmp, "*%ld\r\n", len);
326
+ write_buffer_append_cstr(buf, tmp);
327
+ for (ulong i = 0; i < len; i++)
328
+ resp_encode(buf, rb_ary_entry(obj, i));
329
+ return;
330
+ }
331
+ case T_HASH:
332
+ {
333
+ ulong len = rb_hash_size_num(obj);
334
+ sprintf(tmp, "%%%ld\r\n", len);
335
+ write_buffer_append_cstr(buf, tmp);
336
+
337
+ struct resp_encode_hash_ctx ctx = { buf, obj };
338
+ rb_hash_foreach(obj, resp_encode_hash_entry, (VALUE)&ctx);
339
+ return;
340
+ }
341
+ default:
342
+ rb_raise(rb_eRuntimeError, "Can't encode object");
343
+ }
344
+ }
@@ -0,0 +1,140 @@
1
+ #include "um.h"
2
+
3
+ VALUE cStream;
4
+
5
+ static void Stream_mark(void *ptr) {
6
+ struct um_stream *stream = ptr;
7
+ rb_gc_mark_movable(stream->self);
8
+ rb_gc_mark_movable(stream->buffer);
9
+ }
10
+
11
+ static void Stream_compact(void *ptr) {
12
+ struct um_stream *stream = ptr;
13
+ stream->self = rb_gc_location(stream->self);
14
+ stream->buffer = rb_gc_location(stream->buffer);
15
+ }
16
+
17
+ static void Stream_free(void *ptr) {
18
+ free(ptr);
19
+ }
20
+
21
+ static size_t Stream_size(const void *ptr) {
22
+ return sizeof(struct um_stream);
23
+ }
24
+
25
+ static const rb_data_type_t Stream_type = {
26
+ "UringMachine::Stream",
27
+ {Stream_mark, Stream_free, Stream_size, Stream_compact},
28
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
29
+ };
30
+
31
+ static VALUE Stream_allocate(VALUE klass) {
32
+ struct um_stream *stream = ALLOC(struct um_stream);
33
+
34
+ return TypedData_Wrap_Struct(klass, &Stream_type, stream);
35
+ }
36
+
37
+ VALUE Stream_initialize(VALUE self, VALUE machine, VALUE fd) {
38
+ struct um_stream *stream = RTYPEDDATA_DATA(self);
39
+
40
+ stream->machine = RTYPEDDATA_DATA(machine);
41
+ stream->fd = NUM2ULONG(fd);
42
+ stream->buffer = rb_utf8_str_new_literal("");
43
+ rb_str_resize(stream->buffer, 1 << 16); // 64KB
44
+ rb_str_set_len(stream->buffer, 0);
45
+
46
+ stream->len = 0;
47
+ stream->pos = 0;
48
+ stream->eof = 0;
49
+
50
+ return self;
51
+ }
52
+
53
+ VALUE Stream_get_line(VALUE self) {
54
+ struct um_stream *stream = RTYPEDDATA_DATA(self);
55
+ if (unlikely(stream->eof)) return Qnil;
56
+
57
+ char *start = RSTRING_PTR(stream->buffer) + stream->pos;
58
+ while (true) {
59
+ char * lf_ptr = memchr(start, '\n', stream->len - stream->pos);
60
+ if (lf_ptr) {
61
+ ulong len = lf_ptr - start;
62
+ if (len && (start[len - 1] == '\r')) len -= 1;
63
+
64
+ VALUE str = rb_str_new(start, len);
65
+ stream->pos += lf_ptr - start + 1;
66
+ return str;
67
+ }
68
+
69
+ if (!stream_read_more(stream)) return Qnil;
70
+ }
71
+ }
72
+
73
+ VALUE Stream_get_string(VALUE self, VALUE len) {
74
+ struct um_stream *stream = RTYPEDDATA_DATA(self);
75
+ if (unlikely(stream->eof)) return Qnil;
76
+
77
+ ulong ulen = NUM2ULONG(len);
78
+
79
+ while (stream->len - stream->pos < ulen)
80
+ if (!stream_read_more(stream)) return Qnil;
81
+
82
+ char *start = RSTRING_PTR(stream->buffer) + stream->pos;
83
+ VALUE str = rb_utf8_str_new(start, ulen);
84
+ stream->pos += ulen;
85
+ return str;
86
+ }
87
+
88
+ VALUE Stream_resp_get_line(VALUE self) {
89
+ struct um_stream *stream = RTYPEDDATA_DATA(self);
90
+ if (unlikely(stream->eof)) return Qnil;
91
+
92
+ VALUE line = resp_get_line(stream, Qnil);
93
+ RB_GC_GUARD(line);
94
+ return line;
95
+ }
96
+
97
+ VALUE Stream_resp_get_string(VALUE self, VALUE len) {
98
+ struct um_stream *stream = RTYPEDDATA_DATA(self);
99
+ if (unlikely(stream->eof)) return Qnil;
100
+
101
+ VALUE str = resp_get_string(stream, NUM2ULONG(len), Qnil);
102
+ RB_GC_GUARD(str);
103
+ return str;
104
+ }
105
+
106
+ VALUE Stream_resp_decode(VALUE self) {
107
+ struct um_stream *stream = RTYPEDDATA_DATA(self);
108
+ if (unlikely(stream->eof)) return Qnil;
109
+
110
+ VALUE out_buffer = rb_utf8_str_new_literal("");
111
+ VALUE obj = resp_decode(stream, out_buffer);
112
+ RB_GC_GUARD(out_buffer);
113
+ return obj;
114
+ }
115
+
116
+ VALUE Stream_resp_encode(VALUE self, VALUE str, VALUE obj) {
117
+ struct um_write_buffer buf;
118
+ write_buffer_init(&buf, str);
119
+ rb_str_modify(str);
120
+ resp_encode(&buf, obj);
121
+ write_buffer_update_len(&buf);
122
+ return str;
123
+ }
124
+
125
+ void Init_Stream(void) {
126
+ VALUE cStream = rb_define_class_under(cUM, "Stream", rb_cObject);
127
+ rb_define_alloc_func(cStream, Stream_allocate);
128
+
129
+ rb_define_method(cStream, "initialize", Stream_initialize, 2);
130
+
131
+ rb_define_method(cStream, "get_line", Stream_get_line, 0);
132
+ rb_define_method(cStream, "get_string", Stream_get_string, 1);
133
+
134
+ rb_define_method(cStream, "resp_get_line", Stream_resp_get_line, 0);
135
+ rb_define_method(cStream, "resp_get_string", Stream_resp_get_string, 1);
136
+
137
+ rb_define_method(cStream, "resp_decode", Stream_resp_decode, 0);
138
+
139
+ rb_define_singleton_method(cStream, "resp_encode", Stream_resp_encode, 2);
140
+ }
data/ext/um/um_utils.c CHANGED
@@ -11,6 +11,10 @@ inline struct __kernel_timespec um_double_to_timespec(double value) {
11
11
  return ts;
12
12
  }
13
13
 
14
+ inline double um_timestamp_to_double(__s64 tv_sec, __u32 tv_nsec) {
15
+ return (double)tv_sec + ((double)tv_nsec) / 1000000000;
16
+ }
17
+
14
18
  #define RAISE_EXCEPTION(e) rb_funcall(e, ID_invoke, 0);
15
19
 
16
20
  inline int um_value_is_exception_p(VALUE v) {
@@ -15,7 +15,7 @@ class UringMachine
15
15
  target = Object.new.extend(mod)
16
16
  mailbox = UM::Queue.new
17
17
  actor = Actor.new
18
- thread = Thread.new do
18
+ Thread.new do
19
19
  actor.run(machine, target, mailbox)
20
20
  end
21
21
  target.setup(*a, **k)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class UringMachine
4
- VERSION = '0.8.2'
4
+ VERSION = '0.11'
5
5
  end
data/lib/uringmachine.rb CHANGED
@@ -15,22 +15,11 @@ class UringMachine
15
15
  class Terminate < Exception
16
16
  end
17
17
 
18
- def spin(value = nil, fiber_class = Fiber, &block)
19
- f = fiber_class.new do |resume_value|
20
- f.set_result block.(resume_value)
21
- rescue Exception => e
22
- f.set_result e
23
- ensure
24
- f.mark_as_done
25
- # cleanup
26
- @@fiber_map.delete(f)
27
- self.notify_done_listeners(f)
28
- # transfer control to other fibers
29
- self.yield
30
- end
31
- self.schedule(f, value)
32
- @@fiber_map[f] = true
33
- f
18
+ def spin(value = nil, klass = Fiber, &block)
19
+ fiber = klass.new { |v| run_block_in_fiber(block, fiber, v) }
20
+ self.schedule(fiber, value)
21
+
22
+ @@fiber_map[fiber] = fiber
34
23
  end
35
24
 
36
25
  def join(*fibers)
@@ -63,6 +52,21 @@ class UringMachine
63
52
 
64
53
  private
65
54
 
55
+ def run_block_in_fiber(block, fiber, value)
56
+ ret = block.(value)
57
+ fiber.set_result(ret)
58
+ rescue Exception => e
59
+ fiber.set_result(e)
60
+ ensure
61
+ fiber.mark_as_done
62
+ # cleanup
63
+ @@fiber_map.delete(fiber)
64
+ self.notify_done_listeners(fiber)
65
+
66
+ # transfer control to UM scheduler
67
+ self.yield
68
+ end
69
+
66
70
  def notify_done_listeners(fiber)
67
71
  listeners = fiber.done_listeners
68
72
  return if !listeners
@@ -88,9 +92,23 @@ class UringMachine
88
92
  def add_done_listener(queue)
89
93
  (@done_listeners ||= []) << queue
90
94
  end
95
+
96
+ def mailbox
97
+ @mailbox ||= UM::Queue.new
98
+ end
91
99
  end
92
100
 
93
101
  class ::Fiber
94
- include UringMachine::FiberExtensions
102
+ include FiberExtensions
103
+ end
104
+
105
+ module ThreadExtensions
106
+ def machine
107
+ @machine ||= UM.new
108
+ end
109
+ end
110
+
111
+ class ::Thread
112
+ include ThreadExtensions
95
113
  end
96
114
  end
data/test/test_fiber.rb CHANGED
@@ -63,7 +63,7 @@ class FiberTerminateTest < UMBaseTest
63
63
  end
64
64
  end
65
65
 
66
- class JoinTest < UMBaseTest
66
+ class FiberJoinTest < UMBaseTest
67
67
  def test_join
68
68
  q = UM::Queue.new
69
69
  x = nil
@@ -151,14 +151,14 @@ class ScopeTest < UMBaseTest
151
151
 
152
152
  t0 = monotonic_clock
153
153
  machine.scope do
154
- f1 = machine.spin do
154
+ machine.spin do
155
155
  x1 = 1
156
156
  machine.sleep 0.01
157
157
  ensure
158
158
  x1 = 0
159
159
  end
160
160
 
161
- f2 = machine.spin do
161
+ machine.spin do
162
162
  x2 = 1
163
163
  machine.sleep 0.03
164
164
  ensure
@@ -172,3 +172,23 @@ class ScopeTest < UMBaseTest
172
172
  assert_equal 0, x2
173
173
  end
174
174
  end
175
+
176
+ class FiberMailboxTest < UMBaseTest
177
+ def test_fiber_mailbox
178
+ m = Fiber.current.mailbox
179
+ assert_kind_of UM::Queue, m
180
+
181
+ m2 = Fiber.current.mailbox
182
+ assert_equal m, m2
183
+ end
184
+ end
185
+
186
+ class ThreadMachineTest < UMBaseTest
187
+ def test_thread_machine
188
+ m = Thread.current.machine
189
+ assert_kind_of UM, m
190
+
191
+ m2 = Thread.current.machine
192
+ assert_equal m, m2
193
+ end
194
+ end