qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH v5 2/5] coroutine: introduce coroutines


From: Andreas Färber
Subject: Re: [Qemu-devel] [PATCH v5 2/5] coroutine: introduce coroutines
Date: Sat, 25 Jun 2011 16:03:36 +0200

Am 12.06.2011 um 22:46 schrieb Stefan Hajnoczi:

From: Kevin Wolf <address@hidden>

Asynchronous code is becoming very complex.  At the same time
synchronous code is growing because it is convenient to write.
Sometimes duplicate code paths are even added, one synchronous and the
other asynchronous.  This patch introduces coroutines which allow code
that looks synchronous but is asynchronous under the covers.

A coroutine has its own stack and is therefore able to preserve state
across blocking operations, which traditionally require callback
functions and manual marshalling of parameters.

Creating and starting a coroutine is easy:

 coroutine = qemu_coroutine_create(my_coroutine);
 qemu_coroutine_enter(coroutine, my_data);

The coroutine then executes until it returns or yields:

 void coroutine_fn my_coroutine(void *opaque) {
     MyData *my_data = opaque;

     /* do some work */

     qemu_coroutine_yield();

     /* do some more work */
 }

Yielding switches control back to the caller of qemu_coroutine_enter().
This is typically used to switch back to the main thread's event loop
after issuing an asynchronous I/O request.  The request callback will
then invoke qemu_coroutine_enter() once more to switch back to the
coroutine.

Note that if coroutines are used only from threads which hold the global mutex they will never execute concurrently. This makes programming with coroutines easier than with threads. Race conditions cannot occur since only one coroutine may be active at any time. Other coroutines can only
run across yield.

This coroutines implementation is based on the gtk-vnc implementation
written by Anthony Liguori <address@hidden> but it has been
significantly rewritten by Kevin Wolf <address@hidden> to use
setjmp()/longjmp() instead of the more expensive swapcontext() and by
Paolo Bonzini <address@hidden> for Windows Fibers support.

Signed-off-by: Kevin Wolf <address@hidden>
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
Makefile.objs        |    7 ++
coroutine-ucontext.c | 229 +++++++++++++++++++++++++++++++++++++++++ +++++++++
coroutine-win32.c    |   92 ++++++++++++++++++++
qemu-coroutine-int.h |   48 +++++++++++
qemu-coroutine.c     |   75 ++++++++++++++++
qemu-coroutine.h     |   95 +++++++++++++++++++++
trace-events         |    5 +
7 files changed, 551 insertions(+), 0 deletions(-)
create mode 100644 coroutine-ucontext.c
create mode 100644 coroutine-win32.c
create mode 100644 qemu-coroutine-int.h
create mode 100644 qemu-coroutine.c
create mode 100644 qemu-coroutine.h

diff --git a/trace-events b/trace-events
index e0e9574..e0a4d83 100644
--- a/trace-events
+++ b/trace-events
@@ -386,3 +386,8 @@ disable xen_unmap_block(void* addr, unsigned long size) "%p, size %#lx"

# exec.c
disable qemu_put_ram_ptr(void* addr) "%p"
+
+# qemu-coroutine.c
+disable qemu_coroutine_enter(void *from, void *to, void *opaque) "from %p to %p opaque %p"
+disable qemu_coroutine_yield(void *from, void *to) "from %p to %p"
+disable qemu_coroutine_terminate(void *co) "self %p"

This hunk no longer applies.

Andreas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]