Received: from eggs.gnu.org ([140.186.70.92]:32933) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qm2Se-0003Yg-VH for qemu-devel@nongnu.org; Wed, 27 Jul 2011 07:39:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qm2Sd-0006NE-CW for qemu-devel@nongnu.org; Wed, 27 Jul 2011 07:39:36 -0400 Received: from e4.ny.us.ibm.com ([32.97.182.144]:35828) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qm2Sd-0006N2-9P for qemu-devel@nongnu.org; Wed, 27 Jul 2011 07:39:35 -0400 Received: from d01relay05.pok.ibm.com (d01relay05.pok.ibm.com [9.56.227.237]) by e4.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id p6RBH3SH022956 for ; Wed, 27 Jul 2011 07:17:03 -0400 Received: from d01av02.pok.ibm.com (d01av02.pok.ibm.com [9.56.224.216]) by d01relay05.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p6RBdY60161390 for ; Wed, 27 Jul 2011 07:39:34 -0400 Received: from d01av02.pok.ibm.com (loopback [127.0.0.1]) by d01av02.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p6RBdUl7024140 for ; Wed, 27 Jul 2011 08:39:33 -0300 From: "Aneesh Kumar K.V" In-Reply-To: <4E2FE28D.5060206@redhat.com> References: <1311672077-4592-1-git-send-email-stefanha@linux.vnet.ibm.com> <8739hs5a6n.fsf@skywalker.in.ibm.com> <4E2FE28D.5060206@redhat.com> Date: Wed, 27 Jul 2011 17:09:20 +0530 Message-ID: <87tya83qc7.fsf@skywalker.in.ibm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Subject: Re: [Qemu-devel] [PATCH v8 0/5] Coroutines for better asynchronous programming List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: Blue Swirl , Anthony Liguori , Venkateswararao Jujjuri , Stefan Hajnoczi , qemu-devel@nongnu.org On Wed, 27 Jul 2011 12:03:57 +0200, Kevin Wolf wrote: > Am 27.07.2011 11:45, schrieb Aneesh Kumar K.V: > > On Tue, 26 Jul 2011 10:21:12 +0100, Stefan Hajnoczi wrote: > >> QEMU is event-driven and suffers when blocking operations are performed because > >> VM execution may be stopped until the operation completes. Therefore many > >> operations that could block are performed asynchronously and a callback is > >> invoked when the operation has completed. This allows QEMU to continue > >> executing while the operation is pending. > >> > >> The downside to callbacks is that they split up code into many smaller > >> functions, each of which is a single step in a state machine that quickly > >> becomes complex and hard to understand. Callback functions also result in lots > >> of noise as variables are packed and unpacked into temporary structs that pass > >> state to the callback function. > >> > >> This patch series introduces coroutines as a solution for writing asynchronous > >> code while still having a nice sequential control flow. The semantics are > >> explained in the second patch. The fourth patch adds automated tests. > >> > >> A nice feature of coroutines is that it is relatively easy to take synchronous > >> code and lift it into a coroutine to make it asynchronous. Work has been done > >> to move qcow2 request processing into coroutines and thereby make it > >> asynchronous (today qcow2 will perform synchronous metadata accesses). This > >> qcow2 work is still ongoing and not quite ready for mainline yet. > >> > >> v8: > >> * Bisectability: introduce gthread implementation before ucontext/fibers > > > > Can we also get CoMutex and CoQueue patches also merged. I use them in > > the VirtFS series. > > > > http://repo.or.cz/w/qemu/kevin.git/shortlog/refs/heads/coroutine-devel > > http://repo.or.cz/w/qemu/v9fs.git/commit/318ef0b9b01cd296f8c30d8288139b9bed859892 > > I introduce these in my block coroutine patches. I posted a RFC last > week and the first "real" patch series yesterday, so I hope they get > review and can be merged into master soon. > Can you review the patch that add CoRWlock ? http://article.gmane.org/gmane.comp.emulators.qemu/105402 Message-id:1307382497-3737-2-git-send-email-aneesh.kumar@linux.vnet.ibm.com commit 8c787d8b81aca1f4f7be45adb67b9e1a6dde7f1f Author: Aneesh Kumar K.V Date: Tue May 24 22:14:04 2011 +0530 coroutine: Add CoRwlock support Signed-off-by: Aneesh Kumar K.V diff --git a/qemu-coroutine-lock.c b/qemu-coroutine-lock.c index 5071fb8..5ecaa94 100644 --- a/qemu-coroutine-lock.c +++ b/qemu-coroutine-lock.c @@ -115,3 +115,47 @@ void qemu_co_mutex_unlock(CoMutex *mutex) trace_qemu_co_mutex_unlock_return(mutex, self); } + +void qemu_co_rwlock_init(CoRwlock *lock) +{ + memset(lock, 0, sizeof(*lock)); + qemu_co_queue_init(&lock->queue); +} + +void qemu_co_rwlock_rdlock(CoRwlock *lock) +{ + while (lock->writer) { + qemu_co_queue_wait(&lock->queue); + } + lock->reader++; +} + +void qemu_co_rwlock_unlock(CoRwlock *lock) +{ + assert(qemu_in_coroutine()); + if (lock->writer) { + lock->writer--;; + assert(lock->writer == 0); + while (!qemu_co_queue_empty(&lock->queue)) { + /* + * Wakeup every body. This will include some + * writers too. + */ + qemu_co_queue_next(&lock->queue); + } + } else { + lock->reader--; + assert(lock->reader >= 0); + /* Wakeup only one waiting writer */ + qemu_co_queue_next(&lock->queue); + } +} + +void qemu_co_rwlock_wrlock(CoRwlock *lock) +{ + while (lock->writer || lock->reader) { + qemu_co_queue_wait(&lock->queue); + } + lock->writer++; + assert(lock->writer == 1); +} diff --git a/qemu-coroutine.h b/qemu-coroutine.h index 71cfa5a..a9735fb 100644 --- a/qemu-coroutine.h +++ b/qemu-coroutine.h @@ -17,6 +17,7 @@ #include #include "qemu-queue.h" +#include "qemu-thread.h" /** * Coroutines are a mechanism for stack switching and can be used for @@ -114,4 +115,15 @@ void qemu_co_mutex_init(CoMutex *mutex); void qemu_co_mutex_lock(CoMutex *mutex); void qemu_co_mutex_unlock(CoMutex *mutex); +typedef struct CoRwlock { + bool writer; + int reader; + CoQueue queue; +} CoRwlock; + +void qemu_co_rwlock_init(CoRwlock *lock); +void qemu_co_rwlock_rdlock(CoRwlock *lock); +void qemu_co_rwlock_wrlock(CoRwlock *lock); +void qemu_co_rwlock_unlock(CoRwlock *lock); + #endif /* QEMU_COROUTINE_H */