diff options
Diffstat (limited to 'uring.c')
-rw-r--r-- | uring.c | 188 |
1 files changed, 91 insertions, 97 deletions
@@ -11,7 +11,6 @@ struct uring_ev { struct io_uring uring; struct io_uring_params uring_params; - struct cfg *cfg; struct uring_task task; }; @@ -28,11 +27,11 @@ uint64_t sqe_count = 0; uint64_t cqe_count = 0; static struct io_uring_sqe * -get_sqe(struct cfg *cfg, struct uring_task *task) +get_sqe(struct uring_task *task) { struct io_uring_sqe *sqe; - assert_die(cfg && task, "invalid arguments"); + assert_die(task, "invalid arguments"); sqe = io_uring_get_sqe(&cfg->uev->uring); if (!sqe) { @@ -43,7 +42,7 @@ get_sqe(struct cfg *cfg, struct uring_task *task) } sqe_count++; - uring_task_get(cfg, task); + uring_task_get(task); return sqe; } @@ -89,9 +88,9 @@ uring_task_refdump(struct uring_task *task) * while the task is active. */ void -uring_task_destroy(struct cfg *cfg, struct uring_task *task) +uring_task_destroy(struct uring_task *task) { - assert_return(cfg && task); + assert_return(task); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -99,21 +98,21 @@ uring_task_destroy(struct cfg *cfg, struct uring_task *task) if (task->fd >= 0) { struct io_uring_sqe *sqe; - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); io_uring_prep_cancel(sqe, task, 0); io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CANCEL)); } task->dead = true; - uring_task_put(cfg, task); + uring_task_put(task); } void -uring_task_put(struct cfg *cfg, struct uring_task *task) +uring_task_put(struct uring_task *task) { struct uring_task *parent; - assert_return(cfg && task); + assert_return(task); debug(DBG_REF, "task %s (%p), refcount %u -> %u", task->name, task, task->refcount, task->refcount - 1); @@ -127,7 +126,7 @@ uring_task_put(struct cfg *cfg, struct uring_task *task) error("Negative refcount!"); if (task->fd >= 0) { - uring_task_close_fd(cfg, task); + uring_task_close_fd(task); /* We'll be called again once the fd is closed */ return; } @@ -138,14 +137,14 @@ uring_task_put(struct cfg *cfg, struct uring_task *task) if (parent) { debug(DBG_REF, "putting parent %s (%p)", parent->name, parent); - uring_task_put(cfg, parent); + uring_task_put(parent); } } void -uring_task_get(struct cfg *cfg, struct uring_task *task) +uring_task_get(struct uring_task *task) { - assert_return(cfg && task); + assert_return(task); debug(DBG_REF, "task %s (%p), refcount %u -> %u", task->name, task, task->refcount, task->refcount + 1); @@ -187,9 +186,9 @@ uring_task_set_fd(struct uring_task *task, int fd) } void -uring_task_close_fd(struct cfg *cfg, struct uring_task *task) +uring_task_close_fd(struct uring_task *task) { - assert_return(cfg && task); + assert_return(task); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -197,25 +196,25 @@ uring_task_close_fd(struct cfg *cfg, struct uring_task *task) if (task->fd < 0) return; - uring_close(cfg, task, task->fd); + uring_close(task, task->fd); task->fd = -1; } struct uring_task * -uring_parent(struct cfg *cfg) +uring_parent() { - assert_die(cfg && cfg->uev, "invalid arguments"); + assert_die(cfg->uev, "invalid arguments"); return &cfg->uev->task; } void -uring_task_init(struct cfg *cfg, struct uring_task *task, const char *name, +uring_task_init(struct uring_task *task, const char *name, struct uring_task *parent, void (*free)(struct uring_task *)) { static bool first = true; - assert_die(cfg && task && !empty_str(name) && free, "invalid arguments"); + assert_die(task && !empty_str(name) && free, "invalid arguments"); if (first) first = false; @@ -235,31 +234,31 @@ uring_task_init(struct cfg *cfg, struct uring_task *task, const char *name, "getting parent %s (%p), refcount %u", task->name, task, task->refcount, task->parent->name, task->parent, task->parent->refcount); - uring_task_get(cfg, task->parent); + uring_task_get(task->parent); } } void -uring_close(struct cfg *cfg, struct uring_task *task, int fd) +uring_close(struct uring_task *task, int fd) { struct io_uring_sqe *sqe; - assert_return(cfg && task && fd >= 0); + assert_return(task && fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); io_uring_prep_close(sqe, fd); io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_CLOSE)); } static void -uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_write_cb(struct uring_task *task, int res) { int r; - assert_return(cfg && task && task->tbuf && task->final_cb); + assert_return(task && task->tbuf && task->final_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -276,50 +275,50 @@ uring_tbuf_write_cb(struct cfg *cfg, struct uring_task *task, int res) goto finished; } - uring_write(cfg, task, task->tbuf->buf + task->tbuf->done, + uring_write(task, task->tbuf->buf + task->tbuf->done, task->tbuf->len - task->tbuf->done, uring_tbuf_write_cb); return; finished: - task->final_cb(cfg, task, r); + task->final_cb(task, r); } void -uring_tbuf_write(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) +uring_tbuf_write(struct uring_task *task, utask_cb_t final_cb) { - assert_return(cfg && task && task->fd >= 0 && task->tbuf && task->tbuf->len > 0); + assert_return(task && task->fd >= 0 && task->tbuf && task->tbuf->len > 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); task->tbuf->done = 0; task->final_cb = final_cb; - uring_write(cfg, task, &task->tbuf->buf, task->tbuf->len, uring_tbuf_write_cb); + uring_write(task, &task->tbuf->buf, task->tbuf->len, uring_tbuf_write_cb); } void -uring_write(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, utask_cb_t cb) +uring_write(struct uring_task *task, void *buf, size_t len, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && buf && len > 0 && cb && task->fd >= 0); + assert_return(task && buf && len > 0 && cb && task->fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_write(sqe, task->fd, buf, len, 0); io_uring_sqe_set_data(sqe, task); } static void -uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_read_until_cb(struct uring_task *task, int res) { int r; - assert_return(cfg && task && task->tbuf && task->final_cb && task->is_complete_cb); + assert_return(task && task->tbuf && task->final_cb && task->is_complete_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -330,7 +329,7 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) } task->tbuf->len += res; - r = task->is_complete_cb(cfg, task, res); + r = task->is_complete_cb(task, res); if (r < 0) { r = res; goto finished; @@ -345,20 +344,20 @@ uring_tbuf_read_until_cb(struct cfg *cfg, struct uring_task *task, int res) goto finished; } - uring_read_offset(cfg, task, task->tbuf->buf + task->tbuf->len, + uring_read_offset(task, task->tbuf->buf + task->tbuf->len, sizeof(task->tbuf->buf) - task->tbuf->len, task->tbuf->len, uring_tbuf_read_until_cb); return; finished: - task->final_cb(cfg, task, r); + task->final_cb(task, r); } void -uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, +uring_tbuf_read_until(struct uring_task *task, rutask_cb_t is_complete_cb, utask_cb_t final_cb) { - assert_return(cfg && task && task->fd >= 0 && task->tbuf && is_complete_cb && final_cb); + assert_return(task && task->fd >= 0 && task->tbuf && is_complete_cb && final_cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); @@ -366,14 +365,14 @@ uring_tbuf_read_until(struct cfg *cfg, struct uring_task *task, task->tbuf->len = 0; task->is_complete_cb = is_complete_cb; task->final_cb = final_cb; - uring_read(cfg, task, &task->tbuf->buf, sizeof(task->tbuf->buf), + uring_read(task, &task->tbuf->buf, sizeof(task->tbuf->buf), uring_tbuf_read_until_cb); } static int -uring_tbuf_eof(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_eof(struct uring_task *task, int res) { - assert_return(cfg && task && task->tbuf, -EINVAL); + assert_return(task && task->tbuf, -EINVAL); assert_task_alive_or(DBG_UR, task, return -EINTR); if (task->tbuf->len + 1 >= sizeof(task->tbuf->buf)) @@ -388,18 +387,18 @@ uring_tbuf_eof(struct cfg *cfg, struct uring_task *task, int res) } void -uring_tbuf_read_until_eof(struct cfg *cfg, struct uring_task *task, +uring_tbuf_read_until_eof(struct uring_task *task, utask_cb_t final_cb) { - assert_return(cfg && task && task->tbuf && final_cb); + assert_return(task && task->tbuf && final_cb); - uring_tbuf_read_until(cfg, task, uring_tbuf_eof, final_cb); + uring_tbuf_read_until(task, uring_tbuf_eof, final_cb); } static int -uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res) +uring_tbuf_have_data(struct uring_task *task, int res) { - assert_return(cfg && task, -EINVAL); + assert_return(task, -EINVAL); if (res < 0) return res; @@ -408,56 +407,56 @@ uring_tbuf_have_data(struct cfg *cfg, struct uring_task *task, int res) } void -uring_tbuf_read(struct cfg *cfg, struct uring_task *task, utask_cb_t final_cb) +uring_tbuf_read(struct uring_task *task, utask_cb_t final_cb) { - assert_return(cfg && task && final_cb); + assert_return(task && final_cb); - uring_tbuf_read_until(cfg, task, uring_tbuf_have_data, final_cb); + uring_tbuf_read_until(task, uring_tbuf_have_data, final_cb); } void -uring_read_offset(struct cfg *cfg, struct uring_task *task, void *buf, size_t len, off_t offset, utask_cb_t cb) +uring_read_offset(struct uring_task *task, void *buf, size_t len, off_t offset, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && buf && len > 0 && task->fd >= 0); + assert_return(task && buf && len > 0 && task->fd >= 0); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_read(sqe, task->fd, buf, len, offset); io_uring_sqe_set_data(sqe, task); } void -uring_openat(struct cfg *cfg, struct uring_task *task, const char *path, utask_cb_t cb) +uring_openat(struct uring_task *task, const char *path, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && !empty_str(path) && cb); + assert_return(task && !empty_str(path) && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_openat(sqe, AT_FDCWD, path, O_RDONLY | O_CLOEXEC, 0); io_uring_sqe_set_data(sqe, task); } void -uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) +uring_tbuf_recvmsg(struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && task->tbuf && cb); + assert_return(task && task->fd >= 0 && task->tbuf && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->tbuf->done = 0; task->tbuf->len = 0; task->tbuf->iov.iov_len = sizeof(task->tbuf->buf); @@ -468,16 +467,16 @@ uring_tbuf_recvmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) } void -uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) +uring_tbuf_sendmsg(struct uring_task *task, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && task->tbuf && cb); + assert_return(task && task->fd >= 0 && task->tbuf && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->tbuf->done = 0; task->tbuf->iov.iov_len = task->tbuf->len; task->tbuf->msg.msg_namelen = task->saddr.addrlen; @@ -487,32 +486,32 @@ uring_tbuf_sendmsg(struct cfg *cfg, struct uring_task *task, utask_cb_t cb) } void -uring_connect(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utask_cb_t cb) +uring_connect(struct uring_task *task, struct saddr *saddr, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && saddr && cb); + assert_return(task && task->fd >= 0 && saddr && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_connect(sqe, task->fd, (struct sockaddr *)&saddr->storage, saddr->addrlen); io_uring_sqe_set_data(sqe, task); } void -uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utask_cb_t cb) +uring_accept(struct uring_task *task, struct saddr *saddr, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && saddr && cb); + assert_return(task && task->fd >= 0 && saddr && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); saddr->addrlen = sizeof(saddr->storage); task->cb = cb; io_uring_prep_accept(sqe, task->fd, (struct sockaddr *)&saddr->storage, &saddr->addrlen, SOCK_CLOEXEC); @@ -520,27 +519,27 @@ uring_accept(struct cfg *cfg, struct uring_task *task, struct saddr *saddr, utas } void -uring_poll(struct cfg *cfg, struct uring_task *task, short poll_mask, utask_cb_t cb) +uring_poll(struct uring_task *task, short poll_mask, utask_cb_t cb) { struct io_uring_sqe *sqe; - assert_return(cfg && task && task->fd >= 0 && poll_mask && cb); + assert_return(task && task->fd >= 0 && poll_mask && cb); debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->cb = cb; io_uring_prep_poll_add(sqe, task->fd, poll_mask); io_uring_sqe_set_data(sqe, task); } void -uring_poll_cancel(struct cfg *cfg, struct uring_task *task) +uring_poll_cancel(struct uring_task *task) { struct io_uring_sqe *sqe; - assert_return(cfg && task); + assert_return(task); if (task->fd < 0) { /* not an error, no need to print error msg */ @@ -550,7 +549,7 @@ uring_poll_cancel(struct cfg *cfg, struct uring_task *task) debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - sqe = get_sqe(cfg, task); + sqe = get_sqe(task); task->dead = true; io_uring_prep_poll_remove(sqe, task); io_uring_sqe_set_data(sqe, (void *)((uintptr_t)task | CQE_TYPE_POLL_CANCEL)); @@ -567,7 +566,7 @@ uring_free(struct uring_task *task) task->name, task, task->fd, task->refcount); io_uring_queue_exit(&uev->uring); - uev->cfg->uev = NULL; + cfg->uev = NULL; xfree(uev); } @@ -580,26 +579,24 @@ uring_refdump(struct uring_ev *uev) } void -uring_delete(struct cfg *cfg) +uring_delete() { struct uring_task *task; - assert_return(cfg && cfg->uev); + assert_return(cfg->uev); task = &cfg->uev->task; debug(DBG_UR, "task %s (%p), fd %i, refcount %u", task->name, task, task->fd, task->refcount); - uring_task_put(cfg, task); + uring_task_put(task); } void -uring_init(struct cfg *cfg) +uring_init() { struct uring_ev *uev; - assert_return(cfg); - uev = zmalloc(sizeof(*uev)); if (!uev) die("malloc: %m"); @@ -610,16 +607,15 @@ uring_init(struct cfg *cfg) debug(DBG_UR, "uring initialized, features: 0x%08x", uev->uring_params.features); - uring_task_init(cfg, &uev->task, "io_uring", &cfg->task, uring_free); + uring_task_init(&uev->task, "io_uring", &cfg->task, uring_free); cfg->uev = uev; - uev->cfg = cfg; } static inline void -uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task, +uring_print_cqe(const char *type, struct uring_task *task, struct io_uring_cqe *cqe) { - assert_return(cfg && !empty_str(type) && task && cqe); + assert_return(!empty_str(type) && task && cqe); debug(DBG_UR, "got CQE " "(type: %s, res: %i (%s), task: %s (%p), fd: %i, cb: %p)", @@ -633,10 +629,8 @@ uring_print_cqe(struct cfg *cfg, const char *type, struct uring_task *task, } void -uring_event_loop(struct cfg *cfg) +uring_event_loop() { - assert_return(cfg); - while (true) { struct io_uring_cqe *cqe; unsigned nr, head; @@ -667,22 +661,22 @@ uring_event_loop(struct cfg *cfg) switch (cqe_type) { case CQE_TYPE_CANCEL: - uring_print_cqe(cfg, "cancel", task, cqe); + uring_print_cqe("cancel", task, cqe); do_cb = false; break; case CQE_TYPE_CLOSE: - uring_print_cqe(cfg, "close", task, cqe); + uring_print_cqe("close", task, cqe); do_cb = false; break; case CQE_TYPE_POLL_CANCEL: - uring_print_cqe(cfg, "poll_cancel", task, cqe); + uring_print_cqe("poll_cancel", task, cqe); do_cb = false; break; case CQE_TYPE_NORMAL: - uring_print_cqe(cfg, "standard", task, cqe); + uring_print_cqe("standard", task, cqe); do_cb = true; break; @@ -691,9 +685,9 @@ uring_event_loop(struct cfg *cfg) } if (do_cb && task->cb) - task->cb(cfg, task, cqe->res); + task->cb(task, cqe->res); - uring_task_put(cfg, task); + uring_task_put(task); if (exiting) return; |