qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: stefanha@redhat.com, famz@redhat.com, kwolf@redhat.com, berto@igalia.com
Subject: [Qemu-devel] [PATCH 05/11] coroutine-lock: reschedule coroutine on the AioContext it was running on
Date: Fri, 15 Apr 2016 13:32:00 +0200	[thread overview]
Message-ID: <1460719926-12950-6-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1460719926-12950-1-git-send-email-pbonzini@redhat.com>

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/block/aio.h        |  5 +++++
 include/qemu/coroutine.h   | 18 ++++++++++++++++++
 iothread.c                 | 16 ++++++++++++++++
 stubs/iothread.c           | 11 +++++++++++
 tests/iothread.c           | 16 ++++++++++++++++
 trace-events               |  3 ++-
 util/qemu-coroutine-lock.c | 13 ++++++++++---
 7 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/include/block/aio.h b/include/block/aio.h
index 0a344c3..7fa909d 100644
--- a/include/block/aio.h
+++ b/include/block/aio.h
@@ -450,6 +450,11 @@ static inline bool aio_node_check(AioContext *ctx, bool is_external)
 void aio_co_schedule(AioContext *ctx, struct Coroutine *co);
 
 /**
+ * Return the AioContext whose event loop runs in the current I/O thread.
+ */
+AioContext *qemu_get_current_aio_context(void);
+
+/**
  * @ctx: the aio context
  *
  * Return whether we are running in the I/O thread that manages @ctx.
diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h
index bb23be0..179ab0b 100644
--- a/include/qemu/coroutine.h
+++ b/include/qemu/coroutine.h
@@ -73,6 +73,24 @@ Coroutine *qemu_coroutine_create(CoroutineEntry *entry);
 void qemu_coroutine_enter(Coroutine *coroutine, void *opaque);
 
 /**
+ * Wake up a suspended coroutine in the current thread.
+ *
+ * This function is used by the implementation of qemu_coroutine_wake.
+ * It arranges for @co to restart at the next yield point of the
+ * current coroutine.
+ */
+void qemu_coroutine_queue_next(Coroutine *co);
+
+/**
+ * Wake up a suspended coroutine
+ *
+ * This function is used by the synchronization primitives to wake up
+ * a sleeping coroutine.  For single-threaded scenarios it can simply
+ * call qemu_coroutine_queue_next.
+ */
+void qemu_coroutine_wake(AioContext *ctx, Coroutine *co);
+
+/**
  * Transfer control back to a coroutine's caller
  *
  * This function does not return until the coroutine is re-entered using
diff --git a/iothread.c b/iothread.c
index f66ec95..986d125 100644
--- a/iothread.c
+++ b/iothread.c
@@ -21,6 +21,7 @@
 #include "qemu/error-report.h"
 #include "qemu/rcu.h"
 #include "qemu/main-loop.h"
+#include "qemu/coroutine.h"
 
 typedef ObjectClass IOThreadClass;
 
@@ -31,11 +32,26 @@ typedef ObjectClass IOThreadClass;
 
 static __thread IOThread *my_iothread;
 
+void qemu_get_current_aio_context(void)
+{
+    return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
+}
+
 bool aio_context_in_iothread(AioContext *ctx)
 {
     return ctx == (my_iothread ? my_iothread->ctx : qemu_get_aio_context());
 }
 
+void qemu_coroutine_wake(AioContext *ctx, Coroutine *co)
+{
+    if (ctx != qemu_get_current_aio_context()) {
+        aio_co_schedule(ctx, co);
+        return;
+    }
+
+    qemu_coroutine_queue_next(co);
+}
+
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;
diff --git a/stubs/iothread.c b/stubs/iothread.c
index 6c02323..fb3e5e3 100644
--- a/stubs/iothread.c
+++ b/stubs/iothread.c
@@ -1,8 +1,19 @@
 #include "qemu/osdep.h"
 #include "block/aio.h"
 #include "qemu/main-loop.h"
+#include "qemu/coroutine.h"
+
+AioContext *qemu_get_current_aio_context(void)
+{
+    return qemu_get_aio_context();
+}
 
 bool aio_context_in_iothread(AioContext *ctx)
 {
     return ctx == qemu_get_aio_context();
 }
+
+void qemu_coroutine_wake(AioContext *ctx, Coroutine *co)
+{
+    qemu_coroutine_queue_next(co);
+}
diff --git a/tests/iothread.c b/tests/iothread.c
index 00ab316..e0d61b5 100644
--- a/tests/iothread.c
+++ b/tests/iothread.c
@@ -16,6 +16,7 @@
 #include "qapi/error.h"
 #include "block/aio.h"
 #include "qemu/main-loop.h"
+#include "qemu/coroutine.h"
 #include "qemu/rcu.h"
 #include "iothread.h"
 
@@ -30,11 +31,26 @@ struct IOThread {
 
 static __thread IOThread *my_iothread;
 
+AioContext *qemu_get_current_aio_context(void)
+{
+    return my_iothread ? my_iothread->ctx : qemu_get_aio_context();
+}
+
 bool aio_context_in_iothread(AioContext *ctx)
 {
     return ctx == (my_iothread ? my_iothread->ctx : qemu_get_aio_context());
 }
 
+void qemu_coroutine_wake(AioContext *ctx, Coroutine *co)
+{
+    if (ctx != qemu_get_current_aio_context()) {
+        aio_co_schedule(ctx, co);
+        return;
+    }
+
+    qemu_coroutine_queue_next(co);
+}
+
 static void *iothread_run(void *opaque)
 {
     IOThread *iothread = opaque;
diff --git a/trace-events b/trace-events
index 78b042c..be17ba8 100644
--- a/trace-events
+++ b/trace-events
@@ -982,8 +982,9 @@ qemu_coroutine_yield(void *from, void *to) "from %p to %p"
 qemu_coroutine_terminate(void *co) "self %p"
 
 # qemu-coroutine-lock.c
+qemu_coroutine_queue_next(void *from, void *nxt) "%p->%p"
 qemu_co_queue_run_restart(void *co) "co %p"
-qemu_co_queue_next(void *nxt) "next %p"
+qemu_co_queue_next(void *ctx, void *nxt) "context %p next %p"
 qemu_co_mutex_lock_entry(void *mutex, void *self) "mutex %p self %p"
 qemu_co_mutex_lock_return(void *mutex, void *self) "mutex %p self %p"
 qemu_co_mutex_unlock_entry(void *mutex, void *self) "mutex %p self %p"
diff --git a/util/qemu-coroutine-lock.c b/util/qemu-coroutine-lock.c
index 1f46970..0af14ab 100644
--- a/util/qemu-coroutine-lock.c
+++ b/util/qemu-coroutine-lock.c
@@ -27,8 +27,16 @@
 #include "qemu/coroutine.h"
 #include "qemu/coroutine_int.h"
 #include "qemu/queue.h"
+#include "block/aio.h"
 #include "trace.h"
 
+void qemu_coroutine_queue_next(Coroutine *co)
+{
+    Coroutine *self = qemu_coroutine_self();
+    trace_qemu_coroutine_queue_next(self, co);
+    QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, co, co_queue_next);
+}
+
 void qemu_co_queue_init(CoQueue *queue)
 {
     QSIMPLEQ_INIT(&queue->entries);
@@ -37,6 +45,7 @@ void qemu_co_queue_init(CoQueue *queue)
 void coroutine_fn qemu_co_queue_wait(CoQueue *queue)
 {
     Coroutine *self = qemu_coroutine_self();
+    self->ctx = qemu_get_current_aio_context();
     QSIMPLEQ_INSERT_TAIL(&queue->entries, self, co_queue_next);
     qemu_coroutine_yield();
     assert(qemu_in_coroutine());
@@ -63,7 +72,6 @@ void qemu_co_queue_run_restart(Coroutine *co)
 
 static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
 {
-    Coroutine *self = qemu_coroutine_self();
     Coroutine *next;
 
     if (QSIMPLEQ_EMPTY(&queue->entries)) {
@@ -72,8 +80,7 @@ static bool qemu_co_queue_do_restart(CoQueue *queue, bool single)
 
     while ((next = QSIMPLEQ_FIRST(&queue->entries)) != NULL) {
         QSIMPLEQ_REMOVE_HEAD(&queue->entries, co_queue_next);
-        QSIMPLEQ_INSERT_TAIL(&self->co_queue_wakeup, next, co_queue_next);
-        trace_qemu_co_queue_next(next);
+        qemu_coroutine_wake(next->ctx, next);
         if (single) {
             break;
         }
-- 
2.5.5

  parent reply	other threads:[~2016-04-15 11:32 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-15 11:31 [Qemu-devel] [RFC PATCH resend 00/11] Make CoMutex/CoQueue/CoRwlock thread-safe Paolo Bonzini
2016-04-15 11:31 ` [Qemu-devel] [PATCH 01/11] coroutine: use QSIMPLEQ instead of QTAILQ Paolo Bonzini
2016-04-19 13:45   ` Stefan Hajnoczi
2016-04-15 11:31 ` [Qemu-devel] [PATCH 02/11] throttle-groups: restart throttled requests from coroutine context Paolo Bonzini
2016-04-19 13:49   ` Stefan Hajnoczi
2016-04-15 11:31 ` [Qemu-devel] [PATCH 03/11] coroutine: delete qemu_co_enter_next Paolo Bonzini
2016-04-19 13:49   ` Stefan Hajnoczi
2016-04-15 11:31 ` [Qemu-devel] [PATCH 04/11] aio: introduce aio_co_schedule Paolo Bonzini
2016-04-19 14:31   ` Stefan Hajnoczi
2016-05-17 14:57     ` Paolo Bonzini
2016-05-26 19:19       ` Stefan Hajnoczi
2016-04-29  5:11   ` Fam Zheng
2016-05-17 14:38     ` Paolo Bonzini
2016-04-15 11:32 ` Paolo Bonzini [this message]
2016-04-15 11:32 ` [Qemu-devel] [PATCH 06/11] coroutine-lock: make CoMutex thread-safe Paolo Bonzini
2016-04-29  6:26   ` Fam Zheng
2016-05-17 15:34     ` Paolo Bonzini
2016-04-15 11:32 ` [Qemu-devel] [PATCH 07/11] coroutine-lock: add limited spinning to CoMutex Paolo Bonzini
2016-04-15 11:32 ` [Qemu-devel] [PATCH 08/11] test-aio-multithread: add performance comparison with thread-based mutexes Paolo Bonzini
2016-04-29  6:52   ` Fam Zheng
2016-05-12 16:49     ` Paolo Bonzini
2016-04-15 11:32 ` [Qemu-devel] [PATCH 09/11] coroutine-lock: place CoMutex before CoQueue in header Paolo Bonzini
2016-04-15 11:32 ` [Qemu-devel] [PATCH 10/11] coroutine-lock: add mutex argument to CoQueue APIs Paolo Bonzini
2016-04-15 11:32 ` [Qemu-devel] [PATCH 11/11] coroutine-lock: make CoRwlock thread-safe and fair Paolo Bonzini
2016-04-26 10:54 ` [Qemu-devel] [RFC PATCH resend 00/11] Make CoMutex/CoQueue/CoRwlock thread-safe Stefan Hajnoczi
2016-04-27 15:42   ` Stefan Hajnoczi
2016-05-17 15:34   ` Paolo Bonzini

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=1460719926-12950-6-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=berto@igalia.com \
    --cc=famz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).