From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:57302) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QVNHt-0001Dg-Hb for qemu-devel@nongnu.org; Sat, 11 Jun 2011 08:27:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QVNHr-00008K-Ky for qemu-devel@nongnu.org; Sat, 11 Jun 2011 08:27:37 -0400 Received: from mail-ww0-f53.google.com ([74.125.82.53]:48573) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QVNHr-00008F-Ct for qemu-devel@nongnu.org; Sat, 11 Jun 2011 08:27:35 -0400 Received: by wwj40 with SMTP id 40so3213149wwj.10 for ; Sat, 11 Jun 2011 05:27:33 -0700 (PDT) From: Bastien ROUCARIES Date: Sat, 11 Jun 2011 14:27:24 +0200 References: <1307641266-7726-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> In-Reply-To: <1307641266-7726-1-git-send-email-aneesh.kumar@linux.vnet.ibm.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-15" Content-Transfer-Encoding: quoted-printable Message-Id: <201106111427.28579.roucaries.bastien@gmail.com> Subject: Re: [Qemu-devel] [PATCH] coroutine: Implement coroutines using gthread List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@us.ibm.com, "Aneesh Kumar K.V" Le jeudi 9 juin 2011 19:41:06, Aneesh Kumar K.V a =E9crit : > On platforms that doesn't support makecontext use gthread > based coroutine implementation. Why not using one of the existing lib of coroutine or improving it ? Could you give some hints ? Why not use http://cvs.schmorp.de/libcoro/coro.h what is the base of the pe= rl coroutine lib and thus well tested on a lot of=20 plateform? Bastien >=20 > Signed-off-by: Aneesh Kumar K.V > --- >=20 > NOTE: Tested on linux with force compliation of coroutine-gthread.c >=20 > Makefile.objs | 5 ++ > configure | 18 +++++ > coroutine-gthread.c | 172 > +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 > insertions(+), 0 deletions(-) > create mode 100644 coroutine-gthread.c >=20 > diff --git a/Makefile.objs b/Makefile.objs > index 0f1d7df..d354d3c 100644 > --- a/Makefile.objs > +++ b/Makefile.objs > @@ -13,9 +13,14 @@ oslib-obj-$(CONFIG_POSIX) +=3D oslib-posix.o > qemu-thread-posix.o > ####################################################################### # > coroutines > coroutine-obj-y =3D qemu-coroutine.o qemu-coroutine-lock.o > +ifeq ($(CONFIG_UCONTEXT_COROUTINE),y) > coroutine-obj-$(CONFIG_POSIX) +=3D coroutine-ucontext.o > +else > +coroutine-obj-$(CONFIG_POSIX) +=3D coroutine-gthread.o > +endif > coroutine-obj-$(CONFIG_WIN32) +=3D coroutine-win32.o >=20 > + > ####################################################################### > # block-obj-y is code used by both qemu system emulation and qemu-img >=20 > diff --git a/configure b/configure > index 980914a..529d8c4 100755 > --- a/configure > +++ b/configure > @@ -2568,6 +2568,20 @@ if test "$trace_backend" =3D "dtrace"; then > fi >=20 > ########################################## > +# check if we have makecontext > + > +ucontext_coroutine=3Dno > +cat > $TMPC << EOF > +#include > +int main(void) { makecontext(0, 0, 0); } > +EOF > +if compile_prog "" "" ; then > + ucontext_coroutine=3Dyes > +fi > + > + > + > +########################################## > # End of CC checks > # After here, no more $cc or $ld runs >=20 > @@ -3031,6 +3045,10 @@ if test "$rbd" =3D "yes" ; then > echo "CONFIG_RBD=3Dy" >> $config_host_mak > fi >=20 > +if test "$ucontext_coroutine" =3D "yes" ; then > + echo "CONFIG_UCONTEXT_COROUTINE=3Dy" >> $config_host_mak > +fi > + > # USB host support > case "$usb" in > linux) > diff --git a/coroutine-gthread.c b/coroutine-gthread.c > new file mode 100644 > index 0000000..37e5a16 > --- /dev/null > +++ b/coroutine-gthread.c > @@ -0,0 +1,172 @@ > +/* > + * > + * Copyright (C) 2006 Anthony Liguori > + * Copyright (C) 2011 Aneesh Kumar K.V > + * > + * This library is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.0 of the License, or (at your option) any later version. > + * > + * This library is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with this library; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA=20 > 02110-1301 USA + */ > + > +#include "qemu-common.h" > +#include "qemu-coroutine-int.h" > +#include > + > +typedef struct { > + Coroutine qemu_co; > + GThread *thread; > + gboolean runnable; > +} CoroutineGthread; > + > +typedef struct { > + /** Currently executing coroutine */ > + CoroutineGthread *current; > + > + /** The default coroutine */ > + CoroutineGthread leader; > +} CoroutineThreadState; > + > +static GCond *run_cond; > +static GMutex *run_lock; > +static pthread_key_t thread_state_key; > + > +static void qemu_coroutine_thread_cleanup(void *opaque) > +{ > + CoroutineThreadState *s =3D opaque; > + > + qemu_free(s); > +} > + > +static void __attribute__((constructor)) coroutine_system_init(void) > +{ > + int ret; > + if (!g_thread_supported()) { > + g_thread_init(NULL); > + } > + run_cond =3D g_cond_new(); > + run_lock =3D g_mutex_new(); > + > + ret =3D pthread_key_create(&thread_state_key, > qemu_coroutine_thread_cleanup); + if (ret !=3D 0) { > + fprintf(stderr, "unable to create leader key: %s\n", > strerror(errno)); + abort(); > + } > +} > + > +static CoroutineThreadState *coroutine_get_thread_state(void) > +{ > + CoroutineThreadState *s =3D pthread_getspecific(thread_state_key); > + > + if (!s) { > + s =3D qemu_mallocz(sizeof(*s)); > + s->current =3D &s->leader; > + pthread_setspecific(thread_state_key, s); > + } > + return s; > +} > + > +static gpointer coroutine_thread(gpointer opaque) > +{ > + CoroutineGthread *co =3D opaque; > + CoroutineThreadState *s =3D coroutine_get_thread_state(); > + > + s->current =3D co; > + > + /* Wait for somebody make it runnable */ > + g_mutex_lock(run_lock); > + while (!co->runnable) { > + g_cond_wait(run_cond, run_lock); > + } > + g_mutex_unlock(run_lock); > + /* > + * run the coroutine function > + * Coroutines can run in parallel. > + */ > + co->qemu_co.entry(co->qemu_co.entry_arg); > + > + /* Now yield with terminating status */ > + qemu_coroutine_switch(&co->qemu_co, > + co->qemu_co.caller, COROUTINE_TERMINATE); > + return NULL; > +} > + > +Coroutine *qemu_coroutine_new(void) > +{ > + CoroutineGthread *co; > + if (run_cond =3D=3D NULL) { > + abort(); > + } > + co =3D qemu_mallocz(sizeof(*co)); > + co->runnable =3D FALSE; > + co->thread =3D g_thread_create_full(coroutine_thread, co, 0, > + FALSE, TRUE, > + G_THREAD_PRIORITY_NORMAL, > + NULL); > + if (co->thread =3D=3D NULL) { > + qemu_free(co); > + return NULL; > + } > + co->qemu_co.caller =3D NULL; > + return &co->qemu_co; > +} > + > +Coroutine *qemu_coroutine_self(void) > +{ > + CoroutineThreadState *s =3D coroutine_get_thread_state(); > + > + return &s->current->qemu_co; > +} > + > +CoroutineAction qemu_coroutine_switch(Coroutine *qemu_co_from, > + Coroutine *qemu_co_to, > + CoroutineAction action) > +{ > + CoroutineGthread *from =3D DO_UPCAST(CoroutineGthread, qemu_co, > qemu_co_from); + CoroutineGthread *to =3D DO_UPCAST(CoroutineGthread, > qemu_co, qemu_co_to); + > + /* Wakeup the runnable to coroutine */ > + g_mutex_lock(run_lock); > + to->qemu_co.caller =3D qemu_co_from; > + from->runnable =3D FALSE; > + to->runnable =3D TRUE; > + g_cond_broadcast(run_cond); > + g_mutex_unlock(run_lock); > + > + /* Don't wait if we are going to terminate */ > + if (action =3D=3D COROUTINE_TERMINATE) { > + return action; > + } > + > + /* Now wait for somebody to make from runnable */ > + g_mutex_lock(run_lock); > + while (!from->runnable) { > + g_cond_wait(run_cond, run_lock); > + } > + g_mutex_unlock(run_lock); > + return action; > +} > + > +bool qemu_in_coroutine(void) > +{ > + CoroutineThreadState *s =3D pthread_getspecific(thread_state_key); > + > + return s && s->current->qemu_co.caller; > +} > + > +void qemu_coroutine_delete(Coroutine *qemu_co) > +{ > + CoroutineGthread *co =3D DO_UPCAST(CoroutineGthread, qemu_co, qemu_c= o); > + qemu_free(co); > + return; > +} > +