Class: Event::Backend::URing
- Inherits:
-
Object
- Object
- Event::Backend::URing
- Defined in:
- ext/event/backend/uring.c
Instance Method Summary collapse
- #initialize(loop) ⇒ Object constructor
- #io_read(fiber, io, buffer, offset, length) ⇒ Object
- #io_wait(fiber, io, events) ⇒ Object
- #io_write(fiber, io, buffer, offset, length) ⇒ Object
- #select(duration) ⇒ Object
Constructor Details
#initialize(loop) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'ext/event/backend/uring.c', line 83
VALUE Event_Backend_URing_initialize(VALUE self, VALUE loop) {
struct Event_Backend_URing *data = NULL;
TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
data->loop = loop;
data->ring = xmalloc(sizeof(struct io_uring));
int result = io_uring_queue_init(URING_ENTRIES, data->ring, 0);
if (result == -1) {
rb_sys_fail("io_uring_queue_init");
}
return self;
}
|
Instance Method Details
#io_read(fiber, io, buffer, offset, length) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'ext/event/backend/uring.c', line 183
VALUE Event_Backend_URing_io_read(VALUE self, VALUE fiber, VALUE io, VALUE buffer, VALUE offset, VALUE length) {
struct Event_Backend_URing *data = NULL;
TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
resize_to_capacity(buffer, NUM2SIZET(offset), NUM2SIZET(length));
int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
struct io_uring_sqe *sqe = io_uring_get_sqe(data->ring);
struct iovec iovecs[1];
iovecs[0].iov_base = RSTRING_PTR(buffer) + NUM2SIZET(offset);
iovecs[0].iov_len = NUM2SIZET(length);
io_uring_prep_readv(sqe, descriptor, iovecs, 1, 0);
io_uring_sqe_set_data(sqe, (void*)fiber);
io_uring_submit(data->ring);
int result = NUM2INT(rb_funcall(data->loop, id_transfer, 0));
if (result < 0) {
rb_syserr_fail(-result, strerror(-result));
}
resize_to_fit(buffer, NUM2SIZET(offset), (size_t)result);
return INT2NUM(result);
}
|
#io_wait(fiber, io, events) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'ext/event/backend/uring.c', line 99
VALUE Event_Backend_URing_io_wait(VALUE self, VALUE fiber, VALUE io, VALUE events) {
struct Event_Backend_URing *data = NULL;
TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
struct io_uring_sqe *sqe = io_uring_get_sqe(data->ring);
int mask = NUM2INT(events);
short flags = 0;
if (mask & READABLE) {
flags |= POLL_IN;
}
if (mask & PRIORITY) {
flags |= POLL_PRI;
}
if (mask & WRITABLE) {
flags |= POLL_OUT;
}
// fprintf(stderr, "poll_add(%p, %d, %d)\n", sqe, descriptor, flags);
io_uring_prep_poll_add(sqe, descriptor, flags);
io_uring_sqe_set_data(sqe, (void*)fiber);
io_uring_submit(data->ring);
// fprintf(stderr, "count = %d, errno = %d\n", count, errno);
rb_funcall(data->loop, id_transfer, 0);
return Qnil;
}
|
#io_write(fiber, io, buffer, offset, length) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'ext/event/backend/uring.c', line 211
VALUE Event_Backend_URing_io_write(VALUE self, VALUE fiber, VALUE io, VALUE buffer, VALUE offset, VALUE length) {
struct Event_Backend_URing *data = NULL;
TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
if ((size_t)RSTRING_LEN(buffer) < NUM2SIZET(offset) + NUM2SIZET(length)) {
rb_raise(rb_eRuntimeError, "invalid offset/length exceeds bounds of buffer");
}
int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
struct io_uring_sqe *sqe = io_uring_get_sqe(data->ring);
struct iovec iovecs[1];
iovecs[0].iov_base = RSTRING_PTR(buffer) + NUM2SIZET(offset);
iovecs[0].iov_len = NUM2SIZET(length);
io_uring_prep_writev(sqe, descriptor, iovecs, 1, 0);
io_uring_sqe_set_data(sqe, (void*)fiber);
io_uring_submit(data->ring);
int result = NUM2INT(rb_funcall(data->loop, id_transfer, 0));
if (result < 0) {
rb_syserr_fail(-result, strerror(-result));
}
return INT2NUM(result);
}
|
#select(duration) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'ext/event/backend/uring.c', line 239
VALUE Event_Backend_URing_select(VALUE self, VALUE duration) {
struct Event_Backend_URing *data = NULL;
TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
struct io_uring_cqe *cqes[URING_MAX_EVENTS];
struct __kernel_timespec storage;
if (duration != Qnil) {
int result = io_uring_wait_cqe_timeout(data->ring, cqes, make_timeout(duration, &storage));
if (result == -ETIME) {
// Timeout.
} else if (result < 0) {
rb_syserr_fail(-result, strerror(-result));
}
}
int count = io_uring_peek_batch_cqe(data->ring, cqes, URING_MAX_EVENTS);
if (count == -1) {
rb_sys_fail("io_uring_peek_batch_cqe");
}
for (int i = 0; i < count; i += 1) {
VALUE fiber = (VALUE)io_uring_cqe_get_data(cqes[i]);
VALUE result = INT2NUM(cqes[i]->res);
io_uring_cqe_seen(data->ring, cqes[i]);
rb_funcall(fiber, id_transfer, 1, result);
}
return INT2NUM(count);
}
|