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 18:50:11 +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/coroutine-ucontext.c b/coroutine-ucontext.c
new file mode 100644
index 0000000..bcea2bd
--- /dev/null
+++ b/coroutine-ucontext.c

+static Coroutine *coroutine_new(void)
+{
+    const size_t stack_size = 4 << 20;
+    CoroutineUContext *co;
+    ucontext_t old_uc, uc;
+    jmp_buf old_env;
+    union cc_arg arg;
+
+ /* The ucontext functions preserve signal masks which incurs a system call + * overhead. setjmp()/longjmp() does not preserve signal masks but only + * works on the current stack. Since we need a way to create and switch to + * a new stack, use the ucontext functions for that but setjmp()/longjmp()
+     * for everything else.
+     */
+
+    if (getcontext(&uc) == -1) {
+        return NULL;

We figured out: on Darwin/ppc64 we run into this code path, with errno = ENOTSUP.
Same for Darwin/ppc, v10.5.8.

+    }
+
+    co = qemu_mallocz(sizeof(*co));
+    co->stack = qemu_malloc(stack_size);
+    co->base.entry_arg = &old_env; /* stash away our jmp_buf */
+
+    uc.uc_link = &old_uc;
+    uc.uc_stack.ss_sp = co->stack;
+    uc.uc_stack.ss_size = stack_size;
+    uc.uc_stack.ss_flags = 0;
+
+    arg.p = co;
+
+    makecontext(&uc, (void (*)(void))coroutine_trampoline,
+                2, arg.i[0], arg.i[1]);
+
+    /* swapcontext() in, longjmp() back out */
+    if (!setjmp(old_env)) {
+        swapcontext(&old_uc, &uc);
+    }
+    return &co->base;
+}
+
+Coroutine *qemu_coroutine_new(void)
+{
+    CoroutineThreadState *s = coroutine_get_thread_state();
+    Coroutine *co;
+
+    co = QLIST_FIRST(&s->pool);
+    if (co) {
+        QLIST_REMOVE(co, pool_next);
+        s->pool_size--;
+    } else {
+        co = coroutine_new();

Return value is not checked ...

+    }
+    return co;

... and passed through.

+}

Caller in test-coroutine.c doesn't check either. Maybe better abort instead of returning NULL?

Andreas



reply via email to

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