|
| 1 | +#include "um.h" |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +VALUE cAsyncOp; |
| 5 | + |
| 6 | +VALUE SYM_timeout; |
| 7 | + |
| 8 | +static void AsyncOp_mark(void *ptr) { |
| 9 | + struct um_async_op *async_op = ptr; |
| 10 | + rb_gc_mark_movable(async_op->self); |
| 11 | + rb_gc_mark_movable(async_op->machine->self); |
| 12 | +} |
| 13 | + |
| 14 | +static void AsyncOp_compact(void *ptr) { |
| 15 | + struct um_async_op *async_op = ptr; |
| 16 | + async_op->self = rb_gc_location(async_op->self); |
| 17 | +} |
| 18 | + |
| 19 | +static size_t AsyncOp_size(const void *ptr) { |
| 20 | + return sizeof(struct um_async_op); |
| 21 | +} |
| 22 | + |
| 23 | +static void AsyncOp_free(void *ptr) { |
| 24 | + struct um_async_op *async_op = ptr; |
| 25 | + um_op_free(async_op->machine, async_op->op); |
| 26 | + free(ptr); |
| 27 | +} |
| 28 | + |
| 29 | +static const rb_data_type_t AsyncOp_type = { |
| 30 | + "UringMachine::AsyncOp", |
| 31 | + {AsyncOp_mark, AsyncOp_free, AsyncOp_size, AsyncOp_compact}, |
| 32 | + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED |
| 33 | +}; |
| 34 | + |
| 35 | +static VALUE AsyncOp_allocate(VALUE klass) { |
| 36 | + struct um_async_op *async_op = malloc(sizeof(struct um_async_op)); |
| 37 | + return TypedData_Wrap_Struct(klass, &AsyncOp_type, async_op); |
| 38 | +} |
| 39 | + |
| 40 | +inline struct um_async_op *AsyncOp_data(VALUE self) { |
| 41 | + return RTYPEDDATA_DATA(self); |
| 42 | +} |
| 43 | + |
| 44 | +VALUE AsyncOp_initialize(VALUE self) { |
| 45 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 46 | + memset(async_op, 0, sizeof(struct um_async_op)); |
| 47 | + async_op->self = self; |
| 48 | + return self; |
| 49 | +} |
| 50 | + |
| 51 | +void um_async_op_set(VALUE self, struct um *machine, struct um_op *op) { |
| 52 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 53 | + async_op->machine = machine; |
| 54 | + async_op->op = op; |
| 55 | +} |
| 56 | + |
| 57 | +inline void raise_on_missing_op(struct um_async_op *async_op) { |
| 58 | + if (!async_op->op) |
| 59 | + rb_raise(rb_eRuntimeError, "Missing op"); |
| 60 | +} |
| 61 | + |
| 62 | +inline int async_op_is_done(struct um_async_op *async_op) { |
| 63 | + return (async_op->op->flags & OP_F_COMPLETED); |
| 64 | +} |
| 65 | + |
| 66 | +VALUE AsyncOp_kind(VALUE self) { |
| 67 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 68 | + raise_on_missing_op(async_op); |
| 69 | + |
| 70 | + switch(async_op->op->kind) { |
| 71 | + case OP_TIMEOUT: |
| 72 | + return SYM_timeout; |
| 73 | + default: |
| 74 | + rb_raise(rb_eRuntimeError, "Invalid op kind"); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +VALUE AsyncOp_done_p(VALUE self) { |
| 79 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 80 | + raise_on_missing_op(async_op); |
| 81 | + |
| 82 | + return async_op_is_done(async_op) ? Qtrue : Qfalse; |
| 83 | +} |
| 84 | + |
| 85 | +VALUE AsyncOp_result(VALUE self) { |
| 86 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 87 | + raise_on_missing_op(async_op); |
| 88 | + |
| 89 | + return async_op_is_done(async_op) ? INT2NUM(async_op->op->result.res) : Qnil; |
| 90 | +} |
| 91 | + |
| 92 | +VALUE AsyncOp_cancelled_p(VALUE self) { |
| 93 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 94 | + raise_on_missing_op(async_op); |
| 95 | + |
| 96 | + if (!async_op_is_done(async_op)) return Qnil; |
| 97 | + |
| 98 | + return (async_op->op->result.res == -ECANCELED) ? Qtrue : Qfalse; |
| 99 | +} |
| 100 | + |
| 101 | +VALUE AsyncOp_await(VALUE self) { |
| 102 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 103 | + raise_on_missing_op(async_op); |
| 104 | + |
| 105 | + if (async_op_is_done(async_op)) |
| 106 | + return INT2NUM(async_op->op->result.res); |
| 107 | + |
| 108 | + return um_async_op_await(async_op); |
| 109 | +} |
| 110 | + |
| 111 | +VALUE AsyncOp_cancel(VALUE self) { |
| 112 | + struct um_async_op *async_op = AsyncOp_data(self); |
| 113 | + raise_on_missing_op(async_op); |
| 114 | + |
| 115 | + if (!async_op_is_done(async_op)) |
| 116 | + um_async_op_cancel(async_op); |
| 117 | + |
| 118 | + return self; |
| 119 | +} |
| 120 | + |
| 121 | +void Init_AsyncOp(void) { |
| 122 | + cAsyncOp = rb_define_class_under(cUM, "AsyncOp", rb_cObject); |
| 123 | + rb_define_alloc_func(cAsyncOp, AsyncOp_allocate); |
| 124 | + |
| 125 | + rb_define_method(cAsyncOp, "initialize", AsyncOp_initialize, 0); |
| 126 | + rb_define_method(cAsyncOp, "kind", AsyncOp_kind, 0); |
| 127 | + rb_define_method(cAsyncOp, "done?", AsyncOp_done_p, 0); |
| 128 | + rb_define_method(cAsyncOp, "result", AsyncOp_result, 0); |
| 129 | + rb_define_method(cAsyncOp, "cancelled?", AsyncOp_cancelled_p, 0); |
| 130 | + |
| 131 | + rb_define_method(cAsyncOp, "await", AsyncOp_await, 0); |
| 132 | + rb_define_method(cAsyncOp, "join", AsyncOp_await, 0); |
| 133 | + rb_define_method(cAsyncOp, "cancel", AsyncOp_cancel, 0); |
| 134 | + |
| 135 | + SYM_timeout = ID2SYM(rb_intern("timeout")); |
| 136 | +} |
0 commit comments