From patchwork Fri Apr 15 11:32:00 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 610924 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qmb4w0t8qz9t5C for ; Fri, 15 Apr 2016 21:33:04 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=WlGIfuUM; dkim-atps=neutral Received: from localhost ([::1]:60475 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar1zi-00077N-0y for incoming@patchwork.ozlabs.org; Fri, 15 Apr 2016 07:33:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55309) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar1z4-0005my-GA for qemu-devel@nongnu.org; Fri, 15 Apr 2016 07:32:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ar1z2-0008Qc-W0 for qemu-devel@nongnu.org; Fri, 15 Apr 2016 07:32:22 -0400 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:35490) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar1z2-0008Py-Kn for qemu-devel@nongnu.org; Fri, 15 Apr 2016 07:32:20 -0400 Received: by mail-wm0-x244.google.com with SMTP id a140so5415345wma.2 for ; Fri, 15 Apr 2016 04:32:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=QZ7X8qz/WVsWOaZ1klMb9w9UqvCSduIalyXxY6ftYMw=; b=WlGIfuUMZUAFMTsyzM/Y+6bR91jfpOM4JhWz4KTvtODabCSku9BdvJt5gUdE0zlpJQ u7nI355c+dv1rnZ7Qm1SY9iKuDQ1ZLQisj9iD7EqABqbI74HrlgDlf/SDClwx1GYQSQU aZY0QztKfiY+bxZCWggmTAwuzVQUOUZM/+UOHsax15de9A+U5s0acvRjPGMEwwbTDarS 90ULsLXGu+WbwZLXNBeC70p4wOAbPKZb+9zs0CXyEwoJfNUJ7Cmu/VhJtK4W/6gyyDap xzI0r/xksL/Ky32Q/PpNnZ3oWOciCGNY9095JxmgqnK5IoIjOT/JiZpkI2XT/JwkfWbG uGZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=QZ7X8qz/WVsWOaZ1klMb9w9UqvCSduIalyXxY6ftYMw=; b=B46KC31ULD6jInEXqb//Mq6htg+MHsSQynBXHx0tl2ZgtqvDLzY9Bme0KSAI90CBlR d1RUKEXe54VtEx5g1ddjUPBhuwkMbznBC5oQ9gArPVlLfsdIJclMIzwp36aHnHSyQHY0 DkIAmz4QXvPNG3M6rhHKA+1ffehrIa71tmQHrcvlK6ZBwcuquZ8YxbNhr/dD129GRj/T K2O3BmBKr0Yablb6lpq0UUZC02/E65l4hCfWQWSuEWM1jmWSJWmOxUzqYi5/JPBfiuvg l3Pdx91eHNanEM9U8IDKJCZBMf4lee8GkE51hCNVg6vBAZ7/Xyb3udv0WPa2f5O53N9T w68A== X-Gm-Message-State: AOPr4FXh1UaZpP1sJmL94yA0HuUBwh3W+yaIn9f7B5LNlJxkuPW7IiIPV1pMhmdJu98+Pw== X-Received: by 10.28.63.195 with SMTP id m186mr3691172wma.25.1460719940080; Fri, 15 Apr 2016 04:32:20 -0700 (PDT) Received: from donizetti.redhat.com (94-39-141-76.adsl-ull.clienti.tiscali.it. [94.39.141.76]) by smtp.gmail.com with ESMTPSA id qt3sm15217493wjc.32.2016.04.15.04.32.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Apr 2016 04:32:19 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Fri, 15 Apr 2016 13:32:00 +0200 Message-Id: <1460719926-12950-6-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1460719926-12950-1-git-send-email-pbonzini@redhat.com> References: <1460719926-12950-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::244 Subject: [Qemu-devel] [PATCH 05/11] coroutine-lock: reschedule coroutine on the AioContext it was running on X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, berto@igalia.com, famz@redhat.com, stefanha@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Paolo Bonzini --- 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; }