All of lore.kernel.org
 help / color / mirror / Atom feed
From: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
To: Kevin Wolf <kwolf@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>,
	qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	sheepdog@lists.wpkg.org, Liu Yuan <namei.unix@gmail.com>
Subject: [Qemu-devel] [PATCH v3 06/10] coroutine: add co_aio_sleep_ns() to allow sleep in block drivers
Date: Thu, 25 Jul 2013 17:32:01 +0900	[thread overview]
Message-ID: <1374741125-31859-7-git-send-email-morita.kazutaka@lab.ntt.co.jp> (raw)
In-Reply-To: <1374741125-31859-1-git-send-email-morita.kazutaka@lab.ntt.co.jp>

This helper function behaves similarly to co_sleep_ns(), but the
sleeping coroutine will be resumed when using qemu_aio_wait().

Signed-off-by: MORITA Kazutaka <morita.kazutaka@lab.ntt.co.jp>
---
 include/block/coroutine.h |  8 ++++++++
 qemu-coroutine-sleep.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)

diff --git a/include/block/coroutine.h b/include/block/coroutine.h
index 377805a..23ea6e9 100644
--- a/include/block/coroutine.h
+++ b/include/block/coroutine.h
@@ -210,6 +210,14 @@ void qemu_co_rwlock_unlock(CoRwlock *lock);
 void coroutine_fn co_sleep_ns(QEMUClock *clock, int64_t ns);
 
 /**
+ * Yield the coroutine for a given duration
+ *
+ * Behaves similarly to co_sleep_ns(), but the sleeping coroutine will be
+ * resumed when using qemu_aio_wait().
+ */
+void coroutine_fn co_aio_sleep_ns(int64_t ns);
+
+/**
  * Yield until a file descriptor becomes readable
  *
  * Note that this function clobbers the handlers for the file descriptor.
diff --git a/qemu-coroutine-sleep.c b/qemu-coroutine-sleep.c
index 169ce5c..3955347 100644
--- a/qemu-coroutine-sleep.c
+++ b/qemu-coroutine-sleep.c
@@ -13,6 +13,7 @@
 
 #include "block/coroutine.h"
 #include "qemu/timer.h"
+#include "qemu/thread.h"
 
 typedef struct CoSleepCB {
     QEMUTimer *ts;
@@ -37,3 +38,49 @@ void coroutine_fn co_sleep_ns(QEMUClock *clock, int64_t ns)
     qemu_del_timer(sleep_cb.ts);
     qemu_free_timer(sleep_cb.ts);
 }
+
+typedef struct CoAioSleepCB {
+    QEMUBH *bh;
+    int64_t ns;
+    Coroutine *co;
+} CoAioSleepCB;
+
+static void co_aio_sleep_cb(void *opaque)
+{
+    CoAioSleepCB *aio_sleep_cb = opaque;
+
+    qemu_coroutine_enter(aio_sleep_cb->co, NULL);
+}
+
+static void *sleep_thread(void *opaque)
+{
+    CoAioSleepCB *aio_sleep_cb = opaque;
+    struct timespec req = {
+        .tv_sec = aio_sleep_cb->ns / 1000000000,
+        .tv_nsec = aio_sleep_cb->ns % 1000000000,
+    };
+    struct timespec rem;
+
+    while (nanosleep(&req, &rem) < 0 && errno == EINTR) {
+        req = rem;
+    }
+
+    qemu_bh_schedule(aio_sleep_cb->bh);
+
+    return NULL;
+}
+
+void coroutine_fn co_aio_sleep_ns(int64_t ns)
+{
+    CoAioSleepCB aio_sleep_cb = {
+        .ns = ns,
+        .co = qemu_coroutine_self(),
+    };
+    QemuThread thread;
+
+    aio_sleep_cb.bh = qemu_bh_new(co_aio_sleep_cb, &aio_sleep_cb);
+    qemu_thread_create(&thread, sleep_thread, &aio_sleep_cb,
+                       QEMU_THREAD_DETACHED);
+    qemu_coroutine_yield();
+    qemu_bh_delete(aio_sleep_cb.bh);
+}
-- 
1.8.1.3.566.gaa39828

  parent reply	other threads:[~2013-07-25  8:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-25  8:31 [Qemu-devel] [PATCH v3 00/10] sheepdog: reconnect server after connection failure MORITA Kazutaka
2013-07-25  8:31 ` [Qemu-devel] [PATCH v3 01/10] ignore SIGPIPE in qemu-img and qemu-io MORITA Kazutaka
2013-07-25  8:31 ` [Qemu-devel] [PATCH v3 02/10] iov: handle EOF in iov_send_recv MORITA Kazutaka
2013-07-25  8:31 ` [Qemu-devel] [PATCH v3 03/10] sheepdog: check return values of qemu_co_recv/send correctly MORITA Kazutaka
2013-07-25  8:46   ` Liu Yuan
2013-07-25  8:53     ` [Qemu-devel] [sheepdog] " MORITA Kazutaka
2013-07-25  8:56     ` [Qemu-devel] " Liu Yuan
2013-07-25  8:31 ` [Qemu-devel] [PATCH v3 04/10] sheepdog: handle vdi objects in resend_aio_req MORITA Kazutaka
2013-07-25  8:32 ` [Qemu-devel] [PATCH v3 05/10] sheepdog: reload inode outside of resend_aioreq MORITA Kazutaka
2013-07-25  8:32 ` MORITA Kazutaka [this message]
2013-07-25  8:32 ` [Qemu-devel] [PATCH v3 07/10] sheepdog: try to reconnect to sheepdog after network error MORITA Kazutaka
2013-07-25  9:13   ` Liu Yuan
2013-07-25 12:53     ` [Qemu-devel] [sheepdog] " MORITA Kazutaka
2013-07-25 13:20       ` Liu Yuan
2013-07-26  5:53         ` MORITA Kazutaka
2013-07-25  8:32 ` [Qemu-devel] [PATCH v3 08/10] sheepdog: make add_aio_request and send_aioreq void functions MORITA Kazutaka
2013-07-25  8:32 ` [Qemu-devel] [PATCH v3 09/10] sheepdog: cancel aio requests if possible MORITA Kazutaka
2013-07-25  9:04   ` Liu Yuan
2013-07-25 12:54     ` [Qemu-devel] [sheepdog] " MORITA Kazutaka
2013-07-25  8:32 ` [Qemu-devel] [PATCH v3 10/10] sheepdog: check simultaneous create in resend_aioreq MORITA Kazutaka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1374741125-31859-7-git-send-email-morita.kazutaka@lab.ntt.co.jp \
    --to=morita.kazutaka@lab.ntt.co.jp \
    --cc=kwolf@redhat.com \
    --cc=namei.unix@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=sheepdog@lists.wpkg.org \
    --cc=stefanha@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.