coroutine: Implement coroutines using gthread
PATCH 1/1 coroutine: Implement coroutines using gthread
On platforms that doesn't support makecontext use gthread based coroutine implementation.
---
NOTE: Tested on linux with force compliation of coroutine-gthread.c
Makefile.objs | 5 ++ configure | 18 +++++ coroutine-gthread.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 0 deletions(-) create mode 100644 coroutine-gthread.c
Makefile.objs +5 -0
@@ -13,9 +13,14 @@ oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o ####################################################################### # coroutines coroutine-obj-y = qemu-coroutine.o qemu-coroutine-lock.o +ifeq ($(CONFIG_UCONTEXT_COROUTINE),y) coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o +else +coroutine-obj-$(CONFIG_POSIX) += coroutine-gthread.o +endif coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o + ####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-img
configure +18 -0
@@ -2568,6 +2568,20 @@ if test "$trace_backend" = "dtrace"; then fi ########################################## +# check if we have makecontext + +ucontext_coroutine=no +cat > $TMPC << EOF +#include <ucontext.h> +int main(void) { makecontext(0, 0, 0); } +EOF +if compile_prog "" "" ; then + ucontext_coroutine=yes +fi + + + +########################################## # End of CC checks # After here, no more $cc or $ld runs
@@ -3031,6 +3045,10 @@ if test "$rbd" = "yes" ; then echo "CONFIG_RBD=y" >> $config_host_mak fi +if test "$ucontext_coroutine" = "yes" ; then + echo "CONFIG_UCONTEXT_COROUTINE=y" >> $config_host_mak +fi + # USB host support case "$usb" in linux)
coroutine-gthread.c new file +172 -0
172 lines not on this page · raw message
Signature · 1 line
1.7.4.1
Quoted 75 lines · Aneesh Kumar K.V
On platforms that doesn't support makecontext use gthread based coroutine implementation.
Signed-off-by Aneesh Kumar K.V <[email protected]>---
NOTE: Tested on linux with force compliation of
coroutine-gthread.cMakefile.objs | 5 ++
configure | 18 +++++
coroutine-gthread.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++3 files changed, 195 insertions(+), 0 deletions(-)create mode 100644
coroutine-gthread.cMakefile.objs +0
-0@@ -13,9 +13,14 @@ oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o ####################################################################### # coroutinescoroutine-obj-y = qemu-coroutine.o qemu-coroutine-lock.o +ifeq ($(CONFIG_UCONTEXT_COROUTINE),y) coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o
Makefile.objs +3
-0+else +coroutine-obj-$(CONFIG_POSIX) += coroutine-gthread.o +endifcoroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o
+
######################################################################## block-obj-y is code used by both qemu system emulation and qemu-imgconfigure +0
-0@@ -2568,6 +2568,20 @@ if test "$trace_backend" = "dtrace"; thenfi
##########################################
configure +14
-0+# check if we have makecontext + +ucontext_coroutine=no +cat > $TMPC << EOF +#include <ucontext.h> +int main(void) { makecontext(0, 0, 0); } +EOF +if compile_prog "" "" ; then + ucontext_coroutine=yes +fi + + + +########################################### End of CC checks
# After here, no more $cc or $ld runsconfigure +0
-0@@ -3031,6 +3045,10 @@ if test "$rbd" = "yes" ; thenecho "
CONFIG_RBD=y" >> $config_host_mak
ficonfigure +4
-0+if test "$ucontext_coroutine" = "yes" ; then + echo "CONFIG_UCONTEXT_COROUTINE=y" >> $config_host_mak +fi +# USB host support
case "$usb" in
linux)coroutine-gthread.c new file +0
-0
coroutine-gthread.c line 1
Quoted 1 line
coroutine-gthread.c new file +1
-0@@ -0,0 +1,172 @@ +/*
A summary of the purpose of this file would be nice.
Quoted 7 lines
coroutine-gthread.c new file +7
-0+* +* Copyright (C) 2006 Anthony Liguori <[email protected]> +* Copyright (C) 2011 Aneesh Kumar K.V <[email protected]> +* +* 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
coroutine-gthread.c line 18
Quoted 10 lines
coroutine-gthread.c new file +10
-0+* 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 02110-1301 USA
The URL is preferred over the postal address:
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
coroutine-gthread.c line 23
Quoted 5 lines
coroutine-gthread.c new file +5
-0+*/ + +#include "qemu-common.h" +#include "qemu-coroutine-int.h" +#include <glib.h>
It's good practice to include system headers first unless you are explicitly setting a magic #define to affect the system header.
Quoted 8 lines
coroutine-gthread.c new file +8
-0+ +typedef struct { + Coroutine qemu_co; + GThread *thread; + gboolean runnable; +}CoroutineGthread ; + +typedef struct {
coroutine-gthread.c line 41
Quoted 10 lines
coroutine-gthread.c new file +10
-0+ /** 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;
Why pthread_key_t instead of GStaticPrivate?
I've followed the main control flow once but am going to review in more detail to make sure.
Stefan
Quoted 99 lines · Stefan Hajnoczi
Quoted 75 lines · Aneesh Kumar K.V
On platforms that doesn't support makecontext use gthread based coroutine implementation.
Signed-off-by Aneesh Kumar K.V <[email protected]>---
NOTE: Tested on linux with force compliation of
coroutine-gthread.cMakefile.objs | 5 ++
configure | 18 +++++
coroutine-gthread.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++3 files changed, 195 insertions(+), 0 deletions(-)create mode 100644
coroutine-gthread.cMakefile.objs +0
-0@@ -13,9 +13,14 @@ oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o ####################################################################### # coroutinescoroutine-obj-y = qemu-coroutine.o qemu-coroutine-lock.o +ifeq ($(CONFIG_UCONTEXT_COROUTINE),y) coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o
Makefile.objs +3
-0+else +coroutine-obj-$(CONFIG_POSIX) += coroutine-gthread.o +endifcoroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o
+
######################################################################## block-obj-y is code used by both qemu system emulation and qemu-imgconfigure +0
-0@@ -2568,6 +2568,20 @@ if test "$trace_backend" = "dtrace"; thenfi
##########################################
configure +14
-0+# check if we have makecontext + +ucontext_coroutine=no +cat > $TMPC << EOF +#include <ucontext.h> +int main(void) { makecontext(0, 0, 0); } +EOF +if compile_prog "" "" ; then + ucontext_coroutine=yes +fi + + + +########################################### End of CC checks
# After here, no more $cc or $ld runsconfigure +0
-0@@ -3031,6 +3045,10 @@ if test "$rbd" = "yes" ; thenecho "
CONFIG_RBD=y" >> $config_host_mak
ficonfigure +4
-0+if test "$ucontext_coroutine" = "yes" ; then + echo "CONFIG_UCONTEXT_COROUTINE=y" >> $config_host_mak +fi +# USB host support
case "$usb" in
linux)coroutine-gthread.c new file +0
-0coroutine-gthread.c line 1
Quoted 1 line
coroutine-gthread.c new file +1
-0@@ -0,0 +1,172 @@ +/* A summary of the purpose of this file would be nice.
Quoted 7 lines
coroutine-gthread.c new file +7
-0+* +* Copyright (C) 2006 Anthony Liguori <[email protected]> +* Copyright (C) 2011 Aneesh Kumar K.V <[email protected]> +* +* 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 coroutine-gthread.c line 18
Quoted 10 lines
coroutine-gthread.c new file +10
-0+* 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 02110-1301 USA
Quoted 5 lines
The URL is preferred over the postal address:
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
<http://www.gnu.org/licenses/>.
inherited that header from gtk-vnc version of original file
Quoted 6 lines
coroutine-gthread.c line 23
Quoted 6 lines
coroutine-gthread.c new file +5
-0+*/ + +#include "qemu-common.h" +#include "qemu-coroutine-int.h" +#include <glib.h>
Will fix.
Quoted 16 lines
It's good practice to include system headers first unless you are explicitly setting a magic #define to affect the system header.
Quoted 12 lines
coroutine-gthread.c new file +12
-0+ +typedef struct { + Coroutine qemu_co; + GThread *thread; + gboolean runnable; +}CoroutineGthread ; + +typedef struct { + /** Currently executing coroutine */ + CoroutineGthread *current; + + /** The default coroutine */
Quoted 8 lines
coroutine-gthread.c line 41
Quoted 6 lines
coroutine-gthread.c new file +6
-0+ CoroutineGthread leader; +} CoroutineThreadState; + +static GCond *run_cond; +static GMutex *run_lock; +static pthread_key_t thread_state_key;Why
pthread_key_tinstead of GStaticPrivate?
No specific reason other than I was not aware of GStaticPrivate.
Quoted 4 lines
I've followed the main control flow once but am going to review in more detail to make sure.
Thanks.
-aneesh
Quoted 141 lines · Aneesh Kumar K.V
Quoted 99 lines · Stefan Hajnoczi
Quoted 75 lines · Aneesh Kumar K.V
On platforms that doesn't support makecontext use gthread based coroutine implementation.
Signed-off-by Aneesh Kumar K.V <[email protected]>---
NOTE: Tested on linux with force compliation of
coroutine-gthread.cMakefile.objs | 5 ++
configure | 18 +++++
coroutine-gthread.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++3 files changed, 195 insertions(+), 0 deletions(-)create mode 100644
coroutine-gthread.cMakefile.objs +0
-0@@ -13,9 +13,14 @@ oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o ####################################################################### # coroutinescoroutine-obj-y = qemu-coroutine.o qemu-coroutine-lock.o +ifeq ($(CONFIG_UCONTEXT_COROUTINE),y) coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o
Makefile.objs +3
-0+else +coroutine-obj-$(CONFIG_POSIX) += coroutine-gthread.o +endifcoroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o
+
######################################################################## block-obj-y is code used by both qemu system emulation and qemu-imgconfigure +0
-0@@ -2568,6 +2568,20 @@ if test "$trace_backend" = "dtrace"; thenfi
##########################################
configure +14
-0+# check if we have makecontext + +ucontext_coroutine=no +cat > $TMPC << EOF +#include <ucontext.h> +int main(void) { makecontext(0, 0, 0); } +EOF +if compile_prog "" "" ; then + ucontext_coroutine=yes +fi + + + +########################################### End of CC checks
# After here, no more $cc or $ld runsconfigure +0
-0@@ -3031,6 +3045,10 @@ if test "$rbd" = "yes" ; thenecho "
CONFIG_RBD=y" >> $config_host_mak
ficonfigure +4
-0+if test "$ucontext_coroutine" = "yes" ; then + echo "CONFIG_UCONTEXT_COROUTINE=y" >> $config_host_mak +fi +# USB host support
case "$usb" in
linux)coroutine-gthread.c new file +0
-0coroutine-gthread.c line 1
Quoted 1 line
coroutine-gthread.c new file +1
-0@@ -0,0 +1,172 @@ +/* A summary of the purpose of this file would be nice.
Quoted 7 lines
coroutine-gthread.c new file +7
-0+* +* Copyright (C) 2006 Anthony Liguori <[email protected]> +* Copyright (C) 2011 Aneesh Kumar K.V <[email protected]> +* +* 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 coroutine-gthread.c line 18
Quoted 10 lines
coroutine-gthread.c new file +10
-0+* 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 02110-1301 USA Quoted 5 lines
The URL is preferred over the postal address:
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
<http://www.gnu.org/licenses/>.inherited that header from gtk-vnc version of original file
Quoted 6 lines
coroutine-gthread.c line 23
Quoted 6 lines
coroutine-gthread.c new file +5
-0+*/ + +#include "qemu-common.h" +#include "qemu-coroutine-int.h" +#include <glib.h> Will fix.
Quoted 16 lines
It's good practice to include system headers first unless you are explicitly setting a magic #define to affect the system header.
Quoted 12 lines
coroutine-gthread.c new file +12
-0+ +typedef struct { + Coroutine qemu_co; + GThread *thread; + gboolean runnable; +}CoroutineGthread ; + +typedef struct { + /** Currently executing coroutine */ + CoroutineGthread *current; + + /** The default coroutine */Quoted 8 lines
coroutine-gthread.c line 41
Quoted 6 lines
coroutine-gthread.c new file +6
-0+ CoroutineGthread leader; +} CoroutineThreadState; + +static GCond *run_cond; +static GMutex *run_lock; +static pthread_key_t thread_state_key;Why
pthread_key_tinstead of GStaticPrivate?
Quoted 1 line
No specific reason other than I was not aware of GStaticPrivate.
It would be worth using that to avoid mixing GThread and pthreads unnecessarily. I still need to look at the rest of this patch so don't worry about sending v2 yet.
Stefan
Quoted 2 lines · Aneesh Kumar K.V
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 perl coroutine lib and thus well tested on a lot of plateform?
Bastien
Quoted 247 lines
Signed-off-by Aneesh Kumar K.V <[email protected]>---
NOTE: Tested on linux with force compliation of
coroutine-gthread.cMakefile.objs | 5 ++ configure | 18 +++++ coroutine-gthread.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+), 0 deletions(-) create mode 100644 coroutine-gthread.cMakefile.objs +0
-0@@ -13,9 +13,14 @@ oslib-obj-$(CONFIG_POSIX) += oslib-posix.oqemu-thread-posix.o ####################################################################### # coroutines
coroutine-obj-y = qemu-coroutine.o qemu-coroutine-lock.oMakefile.objs +5
-0+ifeq ($(CONFIG_UCONTEXT_COROUTINE),y) coroutine-obj-$(CONFIG_POSIX) += coroutine-ucontext.o +else +coroutine-obj-$(CONFIG_POSIX) += coroutine-gthread.o +endif coroutine-obj-$(CONFIG_WIN32) += coroutine-win32.o + ####################################################################### # block-obj-y is code used by both qemu system emulation and qemu-imgconfigure +18
-0@@ -2568,6 +2568,20 @@ if test "$trace_backend" = "dtrace"; then fi ########################################## +# check if we have makecontext + +ucontext_coroutine=no +cat > $TMPC << EOF +#include <ucontext.h> +int main(void) { makecontext(0, 0, 0); } +EOF +if compile_prog "" "" ; then + ucontext_coroutine=yes +fi + + + +########################################## # End of CC checks # After here, no more $cc or $ld runs@@ -3031,6 +3045,10 @@ if test "$rbd" = "yes" ; then echo "CONFIG_RBD=y" >> $config_host_mak fi +if test "$ucontext_coroutine" = "yes" ; then + echo "CONFIG_UCONTEXT_COROUTINE=y" >> $config_host_mak +fi + # USB host support case "$usb" in linux)coroutine-gthread.c new file +18
-0@@ -0,0 +1,172 @@ +/* +* +* Copyright (C) 2006 Anthony Liguori <[email protected]> +* Copyright (C) 2011 Aneesh Kumar K.V <[email protected]> +* +* 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 02110-1301 USA + */
coroutine-gthread.c new file +40
-0+ +#include "qemu-common.h" +#include "qemu-coroutine-int.h" +#include <glib.h> + +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 = opaque; + +qemu_free (s); +} + +static void __attribute__((constructor))coroutine_system_init (void ) +{ +int ret; +if (!g_thread_supported ()) { +g_thread_init (NULL ); + } + run_cond =g_cond_new (); + run_lock =g_mutex_new (); + + ret = pthread_key_create(&thread_state_key,
qemu_coroutine_thread_cleanup); + if (ret != 0) {
+ fprintf(stderr, "unable to create leader key: %s\n",
strerror(errno)); +abort();coroutine-gthread.c new file +72
-0+ } +} + +static CoroutineThreadState *coroutine_get_thread_state (void ) +{ +CoroutineThreadState *s =pthread_getspecific (thread_state_key); + +if (!s) { + s =qemu_mallocz (sizeof (*s)); + s->current = &s->leader; +pthread_setspecific (thread_state_key, s); + } +return s; +} + +static gpointer coroutine_thread (gpointer opaque) +{ +CoroutineGthread *co = opaque; +CoroutineThreadState *s =coroutine_get_thread_state (); + + s->current = 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 ==NULL ) { +abort (); + } + co =qemu_mallocz (sizeof (*co)); + co->runnable = FALSE; + co->thread =g_thread_create_full (coroutine_thread, co,0 , + FALSE, TRUE, +G_THREAD_PRIORITY_NORMAL , +NULL ); +if (co->thread ==NULL ) { +qemu_free (co); +return NULL ; + } + co->qemu_co.caller =NULL ; +return &co->qemu_co; +} + +Coroutine *qemu_coroutine_self (void ) +{ +CoroutineThreadState *s =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 = DO_UPCAST(CoroutineGthread, qemu_co,
qemu_co_from); + CoroutineGthread *to = DO_UPCAST(CoroutineGthread,qemu_co,qemu_co_to); +coroutine-gthread.c new file +36
-0+/* Wakeup the runnable to coroutine */ +g_mutex_lock (run_lock); + to->qemu_co.caller = qemu_co_from; + from->runnable = FALSE; + to->runnable = TRUE; +g_cond_broadcast (run_cond); +g_mutex_unlock (run_lock); + +/* Don't wait if we are going to terminate */ +if (action ==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 =pthread_getspecific (thread_state_key); + +return s && s->current->qemu_co.caller; +} + +void qemu_coroutine_delete (Coroutine *qemu_co) +{ +CoroutineGthread *co =DO_UPCAST (CoroutineGthread, qemu_co, qemu_co); +qemu_free (co); +return ; +} +
Quoted 10 lines · Bastien ROUCARIES
Quoted 2 lines · Aneesh Kumar K.V
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 perl coroutine lib and thus well tested on a lot of plateform?
libcoro provides a different API where you need to know which coroutine called you in order to yield back to it later. Since the common use-case is to transfer control to a coroutine and then have it yield back, this caller information is very handy and avoids littering code with explicit caller variables.
We have ucontext, Win32 Fibers, and now GThread implementations of coroutines. I think it's not worth switching to libcoro because it is just a little different with its own quirks and limitations, not clearly better. This code actually started out from the gtk-vnc coroutines implementation, so we did start by reusing code... :)
Stefan