Skip to contentSkip to conversation

yield_until_fd_readable: make it work with any AioContect

PATCH Superseded Dietmar Maurer 2 participants 3 messages Oct 21, 2019 – Oct 24, 2019

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 yield_until_fd_readable: make it work with any AioContect

Simply use qemu_get_current_aio_context().

---

Changelog for v3:

- use (IOHandler *) instead of ((void (*)(void *))
- coding style: fix max line length

Changelog for v2:

- use correct read handler in aio_set_fd_handler (instead of write handler)

 util/qemu-coroutine-io.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
util/qemu-coroutine-io.c +4 -17
index 44a8969a69..dbbe817464 100644
@@ -66,25 +66,12 @@ qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
     return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
 }
 
-typedef struct {
-    Coroutine *co;
-    int fd;
-} FDYieldUntilData;
-
-static void fd_coroutine_enter(void *opaque)
-{
-    FDYieldUntilData *data = opaque;
-    qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
-    qemu_coroutine_enter(data->co);
-}
-
 void coroutine_fn yield_until_fd_readable(int fd)
 {
-    FDYieldUntilData data;
-
     assert(qemu_in_coroutine());
-    data.co = qemu_coroutine_self();
-    data.fd = fd;
-    qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
+    AioContext *ctx = qemu_get_current_aio_context();
+    aio_set_fd_handler(ctx, fd, false, (IOHandler *)qemu_coroutine_enter,
+                       NULL, NULL, qemu_coroutine_self());
     qemu_coroutine_yield();
+    aio_set_fd_handler(ctx, fd, false, NULL, NULL, NULL, NULL);
 }
Signature · 1 line
2.20.1
#1 lore raw
Quoted 8 lines · Dietmar Maurer
util/qemu-coroutine-io.c +0 -7
-static void fd_coroutine_enter(void *opaque)
-{
-    FDYieldUntilData *data = opaque;
-    qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
-    qemu_coroutine_enter(data->co);
-}
-
 void coroutine_fn yield_until_fd_readable(int fd)

util/qemu-coroutine-io.c qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send) · line 74

Quoted 10 lines
util/qemu-coroutine-io.c +3 -5
 {
-    FDYieldUntilData data;
-
     assert(qemu_in_coroutine());
-    data.co = qemu_coroutine_self();
-    data.fd = fd;
-    qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
+    AioContext *ctx = qemu_get_current_aio_context();
+    aio_set_fd_handler(ctx, fd, false, (IOHandler *)qemu_coroutine_enter,
+                       NULL, NULL, qemu_coroutine_self());

This cast is unsafe. If qemu_coroutine_enter()'s prototype is changed there will be no compiler warning that the prototypes are now incompatible.

Please keep fd_coroutine_enter() so the code is type-safe.

#2 lore raw
Quoted 8 lines

util/qemu-coroutine-io.c qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send) · line 74

Quoted 2 lines
util/qemu-coroutine-io.c +2 -0
+    aio_set_fd_handler(ctx, fd, false, (IOHandler *)qemu_coroutine_enter,
+                       NULL, NULL, qemu_coroutine_self());

This cast is unsafe. If qemu_coroutine_enter()'s prototype is changed there will be no compiler warning that the prototypes are now incompatible.

Please keep fd_coroutine_enter() so the code is type-safe.

OK. Sent v4 with suggested changes.