|
| 1 | +#include "um.h" |
| 2 | +#include <stdatomic.h> |
| 3 | +#include <linux/futex.h> |
| 4 | + |
| 5 | +struct sync_ctx { |
| 6 | + struct um *machine; |
| 7 | + uint32_t *futex; |
| 8 | +}; |
| 9 | + |
| 10 | +void um_futex_wait(struct um *machine, uint32_t *futex, uint32_t expect) { |
| 11 | + struct um_op *op = um_op_idle_checkout(machine, OP_SYNCHRONIZE); |
| 12 | + struct io_uring_sqe *sqe = um_get_sqe(machine, op); |
| 13 | + __s32 result = 0; |
| 14 | + |
| 15 | + // submit futex_wait |
| 16 | + io_uring_prep_futex_wait( |
| 17 | + sqe, (uint32_t *)futex, expect, FUTEX_BITSET_MATCH_ANY, |
| 18 | + FUTEX2_SIZE_U32, 0 |
| 19 | + ); |
| 20 | + um_await_op(machine, op, &result, NULL); |
| 21 | + if (result != -EAGAIN) |
| 22 | + um_raise_on_error_result(result); |
| 23 | +} |
| 24 | + |
| 25 | +void um_futex_wake(struct um *machine, uint32_t *futex, uint32_t num_waiters) { |
| 26 | + struct um_op *op = um_op_idle_checkout(machine, OP_SYNCHRONIZE); |
| 27 | + struct io_uring_sqe *sqe = um_get_sqe(machine, op); |
| 28 | + __s32 result = 0; |
| 29 | + |
| 30 | + // submit futex_wait |
| 31 | + io_uring_prep_futex_wake( |
| 32 | + sqe, (uint32_t *)futex, num_waiters, FUTEX_BITSET_MATCH_ANY, |
| 33 | + FUTEX2_SIZE_U32, 0 |
| 34 | + ); |
| 35 | + um_await_op(machine, op, &result, NULL); |
| 36 | + um_raise_on_error_result(result); |
| 37 | +} |
| 38 | + |
| 39 | +#define LOCKED 1 |
| 40 | +#define UNLOCKED 0 |
| 41 | + |
| 42 | +void um_mutex_init(uint32_t *futex) { |
| 43 | + *futex = UNLOCKED; |
| 44 | +} |
| 45 | + |
| 46 | +void um_mutex_lock(struct um *machine, uint32_t *futex) { |
| 47 | + while (*futex == LOCKED) { |
| 48 | + um_futex_wait(machine, futex, LOCKED); |
| 49 | + } |
| 50 | + *futex = LOCKED; |
| 51 | +} |
| 52 | + |
| 53 | +void um_mutex_unlock(struct um *machine, uint32_t *futex) { |
| 54 | + *futex = UNLOCKED; |
| 55 | + // Wake up 1 waiting fiber |
| 56 | + um_futex_wake(machine, futex, 1); |
| 57 | +} |
| 58 | + |
| 59 | +VALUE synchronize_begin(VALUE arg) { |
| 60 | + struct sync_ctx *ctx = (struct sync_ctx *)arg; |
| 61 | + um_mutex_lock(ctx->machine, ctx->futex); |
| 62 | + return rb_yield(Qnil); |
| 63 | +} |
| 64 | + |
| 65 | +VALUE synchronize_ensure(VALUE arg) { |
| 66 | + struct sync_ctx *ctx = (struct sync_ctx *)arg; |
| 67 | + um_mutex_unlock(ctx->machine, ctx->futex); |
| 68 | + return Qnil; |
| 69 | +} |
| 70 | + |
| 71 | +VALUE um_mutex_synchronize(struct um *machine, uint32_t *futex) { |
| 72 | + struct sync_ctx ctx = { .machine = machine, .futex = futex }; |
| 73 | + return rb_ensure(synchronize_begin, (VALUE)&ctx, synchronize_ensure, (VALUE)&ctx); |
| 74 | +} |
0 commit comments