Skip to content

Commit

Permalink
Combine submit and wait_cqe into single syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Jul 18, 2023
1 parent 2c1c007 commit 9903df2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
7 changes: 0 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@

## Roadmap for Polyphony 2

- io_uring
- Use playground.c to find out why we when submitting and waiting for
completion in single syscall signals seem to be blocked until the syscall
returns. Is this a bug in io_uring/liburing?

-----------------------------------------------------

- allow backend selection at runtime?
- Debugging
- Eat your own dogfood: need a good tool to check what's going on when some
Expand Down
26 changes: 22 additions & 4 deletions ext/polyphony/backend_io_uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,28 @@ VALUE Backend_post_fork(VALUE self) {
typedef struct poll_context {
struct io_uring *ring;
struct io_uring_cqe *cqe;
int pending_sqes;
int result;
} poll_context_t;

// This function combines the functionality of io_uring_wait_cqe() and io_uring_submit_and_wait()
static inline int io_uring_submit_and_wait_cqe(struct io_uring *ring,
struct io_uring_cqe **cqe_ptr)
{
if (!__io_uring_peek_cqe(ring, cqe_ptr, NULL) && *cqe_ptr) {
io_uring_submit(ring);
return 0;
}

*cqe_ptr = NULL;
return io_uring_submit_and_wait(ring, 1);
}

void *io_uring_backend_poll_without_gvl(void *ptr) {
poll_context_t *ctx = (poll_context_t *)ptr;
ctx->result = io_uring_wait_cqe(ctx->ring, &ctx->cqe);
ctx->result = ctx->pending_sqes ?
io_uring_submit_and_wait_cqe(ctx->ring, &ctx->cqe) :
io_uring_wait_cqe(ctx->ring, &ctx->cqe);
return NULL;
}

Expand Down Expand Up @@ -238,7 +254,7 @@ inline void io_uring_backend_defer_submit(Backend_t *backend) {
void io_uring_backend_poll(Backend_t *backend) {
poll_context_t poll_ctx;
poll_ctx.ring = &backend->ring;
if (backend->pending_sqes) io_uring_backend_immediate_submit(backend);
poll_ctx.pending_sqes = backend->pending_sqes;

wait_cqe:
backend->base.currently_polling = 1;
Expand All @@ -249,8 +265,10 @@ void io_uring_backend_poll(Backend_t *backend) {
return;
}

io_uring_backend_handle_completion(poll_ctx.cqe, backend);
io_uring_cqe_seen(&backend->ring, poll_ctx.cqe);
if (poll_ctx.cqe) {
io_uring_backend_handle_completion(poll_ctx.cqe, backend);
io_uring_cqe_seen(&backend->ring, poll_ctx.cqe);
}
}

inline VALUE Backend_poll(VALUE self, VALUE blocking) {
Expand Down

0 comments on commit 9903df2

Please sign in to comment.