[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 14/25] coroutine: Add co_yield_timeout()
From: |
Max Reitz |
Subject: |
[Qemu-devel] [PATCH 14/25] coroutine: Add co_yield_timeout() |
Date: |
Wed, 25 Feb 2015 13:08:27 -0500 |
Signed-off-by: Max Reitz <address@hidden>
---
include/block/coroutine.h | 6 ++++++
qemu-coroutine-sleep.c | 34 ++++++++++++++++++++++++++++++++++
2 files changed, 40 insertions(+)
diff --git a/include/block/coroutine.h b/include/block/coroutine.h
index 20c027a..a05aefb 100644
--- a/include/block/coroutine.h
+++ b/include/block/coroutine.h
@@ -216,4 +216,10 @@ void coroutine_fn co_aio_sleep_ns(AioContext *ctx,
QEMUClockType type,
*/
void coroutine_fn yield_until_fd_readable(int fd);
+/**
+ * Yield with a timeout given in nanoseconds. Returns true if the timeout
+ * expired.
+ */
+bool coroutine_fn co_yield_timeout(QEMUClockType type, int64_t ns);
+
#endif /* QEMU_COROUTINE_H */
diff --git a/qemu-coroutine-sleep.c b/qemu-coroutine-sleep.c
index 9abb7fd..9b33bf0 100644
--- a/qemu-coroutine-sleep.c
+++ b/qemu-coroutine-sleep.c
@@ -14,6 +14,7 @@
#include "block/coroutine.h"
#include "qemu/timer.h"
#include "block/aio.h"
+#include "qemu/main-loop.h"
typedef struct CoSleepCB {
QEMUTimer *ts;
@@ -39,3 +40,36 @@ void coroutine_fn co_aio_sleep_ns(AioContext *ctx,
QEMUClockType type,
timer_del(sleep_cb.ts);
timer_free(sleep_cb.ts);
}
+
+typedef struct CoTimeoutCB {
+ Coroutine *co;
+ bool expired;
+} CoTimeoutCB;
+
+static void co_timeout_cb(void *opaque)
+{
+ CoTimeoutCB *timeout_cb = opaque;
+
+ timeout_cb->expired = true;
+ qemu_coroutine_enter(timeout_cb->co, NULL);
+}
+
+bool coroutine_fn co_yield_timeout(QEMUClockType type, int64_t ns)
+{
+ QEMUTimer timer;
+ CoTimeoutCB timeout_cb = {
+ .co = qemu_coroutine_self()
+ };
+
+ if (!ns) {
+ return true;
+ }
+
+ aio_timer_init(qemu_get_aio_context(), &timer, type, SCALE_NS,
+ &co_timeout_cb, &timeout_cb);
+ timer_mod(&timer, qemu_clock_get_ns(type) + ns);
+ qemu_coroutine_yield();
+ timer_del(&timer);
+
+ return timeout_cb.expired;
+}
--
2.1.0
- [Qemu-devel] [PATCH 00/25] nbd: Several fixes, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 02/25] qemu-nbd: Detect unused partitions by system == 0, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 03/25] nbd: Fix nbd_establish_connection()'s return value, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 05/25] nbd: Avoid generic -EINVAL, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 08/25] nbd: Handle blk_getlength() failure, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 01/25] util/uri: Add overflow check to rfc3986_parse_port, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 09/25] qemu-nbd: fork() can fail, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 14/25] coroutine: Add co_yield_timeout(),
Max Reitz <=
- [Qemu-devel] [PATCH 06/25] nbd: Pass return value from nbd_handle_list(), Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 04/25] nbd: Fix response to invalid requests, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 15/25] coroutine-io: Return -errno in case of error, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 10/25] nbd: Fix potential signed overflow issues, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 07/25] nbd: Add "failed to open export" error message, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 12/25] nbd: Set block size to BDRV_SECTOR_SIZE, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 13/25] nbd: Enforce sector alignment, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 21/25] nbd: Drop unexpected data for NBD_OPT_LIST, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 11/25] qemu-nbd: Fix and improve input verification, Max Reitz, 2015/02/25
- [Qemu-devel] [PATCH 16/25] coroutine-io: Add I/O functions with timeout, Max Reitz, 2015/02/25