|
| 1 | +#include "iowatcher.h" |
| 2 | + |
| 3 | +#ifdef EVENT_IO_URING |
| 4 | +#include "hplatform.h" |
| 5 | +#include "hdef.h" |
| 6 | +#include "hevent.h" |
| 7 | + |
| 8 | +#include <liburing.h> |
| 9 | +#include <poll.h> |
| 10 | + |
| 11 | +#define IO_URING_ENTRIES 1024 |
| 12 | +#define IO_URING_CANCEL_TAG ((void*)(uintptr_t)-1) |
| 13 | + |
| 14 | +typedef struct io_uring_ctx_s { |
| 15 | + struct io_uring ring; |
| 16 | + int nfds; |
| 17 | +} io_uring_ctx_t; |
| 18 | + |
| 19 | +int iowatcher_init(hloop_t* loop) { |
| 20 | + if (loop->iowatcher) return 0; |
| 21 | + io_uring_ctx_t* ctx; |
| 22 | + HV_ALLOC_SIZEOF(ctx); |
| 23 | + int ret = io_uring_queue_init(IO_URING_ENTRIES, &ctx->ring, 0); |
| 24 | + if (ret < 0) { |
| 25 | + HV_FREE(ctx); |
| 26 | + return ret; |
| 27 | + } |
| 28 | + ctx->nfds = 0; |
| 29 | + loop->iowatcher = ctx; |
| 30 | + return 0; |
| 31 | +} |
| 32 | + |
| 33 | +int iowatcher_cleanup(hloop_t* loop) { |
| 34 | + if (loop->iowatcher == NULL) return 0; |
| 35 | + io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; |
| 36 | + io_uring_queue_exit(&ctx->ring); |
| 37 | + HV_FREE(loop->iowatcher); |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +static struct io_uring_sqe* io_uring_get_sqe_safe(struct io_uring* ring) { |
| 42 | + struct io_uring_sqe* sqe = io_uring_get_sqe(ring); |
| 43 | + if (sqe == NULL) { |
| 44 | + // SQ is full, flush pending submissions and retry |
| 45 | + io_uring_submit(ring); |
| 46 | + sqe = io_uring_get_sqe(ring); |
| 47 | + } |
| 48 | + return sqe; |
| 49 | +} |
| 50 | + |
| 51 | +int iowatcher_add_event(hloop_t* loop, int fd, int events) { |
| 52 | + if (loop->iowatcher == NULL) { |
| 53 | + int ret = iowatcher_init(loop); |
| 54 | + if (ret < 0) { |
| 55 | + return ret; |
| 56 | + } |
| 57 | + } |
| 58 | + io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; |
| 59 | + hio_t* io = loop->ios.ptr[fd]; |
| 60 | + |
| 61 | + unsigned poll_mask = 0; |
| 62 | + // pre events |
| 63 | + if (io->events & HV_READ) { |
| 64 | + poll_mask |= POLLIN; |
| 65 | + } |
| 66 | + if (io->events & HV_WRITE) { |
| 67 | + poll_mask |= POLLOUT; |
| 68 | + } |
| 69 | + // now events |
| 70 | + if (events & HV_READ) { |
| 71 | + poll_mask |= POLLIN; |
| 72 | + } |
| 73 | + if (events & HV_WRITE) { |
| 74 | + poll_mask |= POLLOUT; |
| 75 | + } |
| 76 | + |
| 77 | + struct io_uring_sqe* sqe; |
| 78 | + if (io->events != 0) { |
| 79 | + // Cancel the existing poll request first |
| 80 | + sqe = io_uring_get_sqe_safe(&ctx->ring); |
| 81 | + if (sqe == NULL) return -1; |
| 82 | + io_uring_prep_poll_remove(sqe, (uint64_t)fd); |
| 83 | + io_uring_sqe_set_data(sqe, IO_URING_CANCEL_TAG); |
| 84 | + } else { |
| 85 | + ctx->nfds++; |
| 86 | + } |
| 87 | + |
| 88 | + // Add poll for the combined events |
| 89 | + sqe = io_uring_get_sqe_safe(&ctx->ring); |
| 90 | + if (sqe == NULL) return -1; |
| 91 | + io_uring_prep_poll_add(sqe, fd, poll_mask); |
| 92 | + io_uring_sqe_set_data(sqe, (void*)(uintptr_t)fd); |
| 93 | + |
| 94 | + io_uring_submit(&ctx->ring); |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | +int iowatcher_del_event(hloop_t* loop, int fd, int events) { |
| 99 | + io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; |
| 100 | + if (ctx == NULL) return 0; |
| 101 | + hio_t* io = loop->ios.ptr[fd]; |
| 102 | + |
| 103 | + // Calculate remaining events |
| 104 | + unsigned poll_mask = 0; |
| 105 | + // pre events |
| 106 | + if (io->events & HV_READ) { |
| 107 | + poll_mask |= POLLIN; |
| 108 | + } |
| 109 | + if (io->events & HV_WRITE) { |
| 110 | + poll_mask |= POLLOUT; |
| 111 | + } |
| 112 | + // now events |
| 113 | + if (events & HV_READ) { |
| 114 | + poll_mask &= ~POLLIN; |
| 115 | + } |
| 116 | + if (events & HV_WRITE) { |
| 117 | + poll_mask &= ~POLLOUT; |
| 118 | + } |
| 119 | + |
| 120 | + // Cancel existing poll |
| 121 | + struct io_uring_sqe* sqe = io_uring_get_sqe_safe(&ctx->ring); |
| 122 | + if (sqe == NULL) return -1; |
| 123 | + io_uring_prep_poll_remove(sqe, (uint64_t)fd); |
| 124 | + io_uring_sqe_set_data(sqe, IO_URING_CANCEL_TAG); |
| 125 | + |
| 126 | + if (poll_mask == 0) { |
| 127 | + ctx->nfds--; |
| 128 | + } else { |
| 129 | + // Re-add with remaining events |
| 130 | + sqe = io_uring_get_sqe_safe(&ctx->ring); |
| 131 | + if (sqe == NULL) return -1; |
| 132 | + io_uring_prep_poll_add(sqe, fd, poll_mask); |
| 133 | + io_uring_sqe_set_data(sqe, (void*)(uintptr_t)fd); |
| 134 | + } |
| 135 | + |
| 136 | + io_uring_submit(&ctx->ring); |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +int iowatcher_poll_events(hloop_t* loop, int timeout) { |
| 141 | + io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; |
| 142 | + if (ctx == NULL) return 0; |
| 143 | + if (ctx->nfds == 0) return 0; |
| 144 | + |
| 145 | + struct __kernel_timespec ts; |
| 146 | + struct __kernel_timespec* tp = NULL; |
| 147 | + if (timeout != INFINITE) { |
| 148 | + ts.tv_sec = timeout / 1000; |
| 149 | + ts.tv_nsec = (timeout % 1000) * 1000000LL; |
| 150 | + tp = &ts; |
| 151 | + } |
| 152 | + |
| 153 | + struct io_uring_cqe* cqe; |
| 154 | + int ret; |
| 155 | + if (tp) { |
| 156 | + ret = io_uring_wait_cqe_timeout(&ctx->ring, &cqe, tp); |
| 157 | + } else { |
| 158 | + ret = io_uring_wait_cqe(&ctx->ring, &cqe); |
| 159 | + } |
| 160 | + if (ret < 0) { |
| 161 | + if (ret == -ETIME || ret == -EINTR) { |
| 162 | + return 0; |
| 163 | + } |
| 164 | + perror("io_uring_wait_cqe"); |
| 165 | + return ret; |
| 166 | + } |
| 167 | + |
| 168 | + int nevents = 0; |
| 169 | + int sqe_queued = 0; |
| 170 | + unsigned nready = io_uring_cq_ready(&ctx->ring); |
| 171 | + unsigned i; |
| 172 | + for (i = 0; i < nready; ++i) { |
| 173 | + if (io_uring_peek_cqe(&ctx->ring, &cqe) != 0) break; |
| 174 | + void* data = io_uring_cqe_get_data(cqe); |
| 175 | + if (data == IO_URING_CANCEL_TAG) { |
| 176 | + io_uring_cqe_seen(&ctx->ring, cqe); |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + int fd = (int)(uintptr_t)data; |
| 181 | + if (fd < 0 || fd >= loop->ios.maxsize) { |
| 182 | + io_uring_cqe_seen(&ctx->ring, cqe); |
| 183 | + continue; |
| 184 | + } |
| 185 | + hio_t* io = loop->ios.ptr[fd]; |
| 186 | + if (io == NULL) { |
| 187 | + io_uring_cqe_seen(&ctx->ring, cqe); |
| 188 | + continue; |
| 189 | + } |
| 190 | + |
| 191 | + if (cqe->res < 0) { |
| 192 | + // Poll request failed: notify registered events, or both if none registered |
| 193 | + io->revents |= (io->events ? io->events : HV_RDWR); |
| 194 | + EVENT_PENDING(io); |
| 195 | + ++nevents; |
| 196 | + } else { |
| 197 | + int revents = cqe->res; |
| 198 | + if (revents & (POLLIN | POLLHUP | POLLERR)) { |
| 199 | + io->revents |= HV_READ; |
| 200 | + } |
| 201 | + if (revents & (POLLOUT | POLLHUP | POLLERR)) { |
| 202 | + io->revents |= HV_WRITE; |
| 203 | + } |
| 204 | + if (io->revents) { |
| 205 | + EVENT_PENDING(io); |
| 206 | + ++nevents; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + io_uring_cqe_seen(&ctx->ring, cqe); |
| 211 | + |
| 212 | + // io_uring POLL_ADD is one-shot, re-arm for the same events |
| 213 | + unsigned remask = 0; |
| 214 | + if (io->events & HV_READ) remask |= POLLIN; |
| 215 | + if (io->events & HV_WRITE) remask |= POLLOUT; |
| 216 | + if (remask) { |
| 217 | + struct io_uring_sqe* sqe = io_uring_get_sqe_safe(&ctx->ring); |
| 218 | + if (sqe) { |
| 219 | + io_uring_prep_poll_add(sqe, fd, remask); |
| 220 | + io_uring_sqe_set_data(sqe, (void*)(uintptr_t)fd); |
| 221 | + sqe_queued = 1; |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + if (sqe_queued) { |
| 227 | + io_uring_submit(&ctx->ring); |
| 228 | + } |
| 229 | + |
| 230 | + return nevents; |
| 231 | +} |
| 232 | +#endif |
0 commit comments