From 57a1d211179279727d5afa21a7feba2d249d6867 Mon Sep 17 00:00:00 2001 From: Michael Tokarev Date: Thu, 8 May 2014 12:30:46 +0400 Subject: [PATCH] glib-compat.h: add new thread API emulation on top of pre-2.31 API Thread API changed in glib-2.31 significantly. Before that version, conditionals and mutexes were only allocated dynamically, using _new()/_free() interface. in 2.31 and up, they're allocated statically as regular variables, and old interface is deprecated. (Note: glib docs says the new interface is available since version 2.32, but it was actually introduced in version 2.31). Create the new interface using old primitives, by providing non-opaque definitions of the base types (GCond and GMutex) using GOnces. Replace #ifdeffery around GCond and GMutex in trace/simple.c and coroutine-gthread.c too because it does not work anymore with the new glib-compat.h. Signed-off-by: Michael Tokarev Reviewed-by: Stefan Hajnoczi [Use GOnce to support lazy initialization; introduce CompatGMutex and CompatGCond. - Paolo] Signed-off-by: Paolo Bonzini (cherry picked from commit 86946a2d835614050b90bc8e5c82982fe45deff2) --- coroutine-gthread.c | 29 ++++-------- include/glib-compat.h | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ trace/simple.c | 50 +++++---------------- 3 files changed, 138 insertions(+), 60 deletions(-) diff --git a/coroutine-gthread.c b/coroutine-gthread.c index a61efe0..6bd6d6b 100644 --- a/coroutine-gthread.c +++ b/coroutine-gthread.c @@ -30,20 +30,14 @@ typedef struct { CoroutineAction action; } CoroutineGThread; -static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT; +static CompatGMutex coroutine_lock; +static CompatGCond coroutine_cond; /* GLib 2.31 and beyond deprecated various parts of the thread API, * but the new interfaces are not available in older GLib versions * so we have to cope with both. */ #if GLIB_CHECK_VERSION(2, 31, 0) -/* Default zero-initialisation is sufficient for 2.31+ GCond */ -static GCond the_coroutine_cond; -static GCond *coroutine_cond = &the_coroutine_cond; -static inline void init_coroutine_cond(void) -{ -} - /* Awkwardly, the GPrivate API doesn't provide a way to update the * GDestroyNotify handler for the coroutine key dynamically. So instead * we track whether or not the CoroutineGThread should be freed on @@ -84,11 +78,6 @@ static inline GThread *create_thread(GThreadFunc func, gpointer data) #else /* Handle older GLib versions */ -static GCond *coroutine_cond; -static inline void init_coroutine_cond(void) -{ - coroutine_cond = g_cond_new(); -} static GStaticPrivate coroutine_key = G_STATIC_PRIVATE_INIT; @@ -120,22 +109,20 @@ static void __attribute__((constructor)) coroutine_init(void) g_thread_init(NULL); } #endif - - init_coroutine_cond(); } static void coroutine_wait_runnable_locked(CoroutineGThread *co) { while (!co->runnable) { - g_cond_wait(coroutine_cond, g_static_mutex_get_mutex(&coroutine_lock)); + g_cond_wait(&coroutine_cond, &coroutine_lock); } } static void coroutine_wait_runnable(CoroutineGThread *co) { - g_static_mutex_lock(&coroutine_lock); + g_mutex_lock(&coroutine_lock); coroutine_wait_runnable_locked(co); - g_static_mutex_unlock(&coroutine_lock); + g_mutex_unlock(&coroutine_lock); } static gpointer coroutine_thread(gpointer opaque) @@ -177,17 +164,17 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_, CoroutineGThread *from = DO_UPCAST(CoroutineGThread, base, from_); CoroutineGThread *to = DO_UPCAST(CoroutineGThread, base, to_); - g_static_mutex_lock(&coroutine_lock); + g_mutex_lock(&coroutine_lock); from->runnable = false; from->action = action; to->runnable = true; to->action = action; - g_cond_broadcast(coroutine_cond); + g_cond_broadcast(&coroutine_cond); if (action != COROUTINE_TERMINATE) { coroutine_wait_runnable_locked(from); } - g_static_mutex_unlock(&coroutine_lock); + g_mutex_unlock(&coroutine_lock); return from->action; } diff --git a/include/glib-compat.h b/include/glib-compat.h index 1280fb2..4ae0671 100644 --- a/include/glib-compat.h +++ b/include/glib-compat.h @@ -5,6 +5,8 @@ * * Authors: * Anthony Liguori + * Michael Tokarev + * Paolo Bonzini * * This work is licensed under the terms of the GNU GPL, version 2 or later. * See the COPYING file in the top-level directory. @@ -43,4 +45,121 @@ static inline gint g_poll(GPollFD *fds, guint nfds, gint timeout) } #endif +#if !GLIB_CHECK_VERSION(2, 31, 0) +/* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate + * GStaticMutex, but it didn't work with condition variables). + * + * Our implementation uses GOnce to fake a static implementation that does + * not require separate initialization. + * We need to rename the types to avoid passing our CompatGMutex/CompatGCond + * by mistake to a function that expects GMutex/GCond. However, for ease + * of use we keep the GLib function names. GLib uses macros for the + * implementation, we use inline functions instead and undefine the macros. + */ + +typedef struct CompatGMutex { + GOnce once; +} CompatGMutex; + +typedef struct CompatGCond { + GOnce once; +} CompatGCond; + +static inline gpointer do_g_mutex_new(gpointer unused) +{ + return (gpointer) g_mutex_new(); +} + +static inline void g_mutex_init(CompatGMutex *mutex) +{ + mutex->once = (GOnce) G_ONCE_INIT; +} + +static inline void g_mutex_clear(CompatGMutex *mutex) +{ + assert(mutex->once.status != G_ONCE_STATUS_PROGRESS); + if (mutex->once.retval) { + g_mutex_free((GMutex *) mutex->once.retval); + } + mutex->once = (GOnce) G_ONCE_INIT; +} + +static inline void (g_mutex_lock)(CompatGMutex *mutex) +{ + g_once(&mutex->once, do_g_mutex_new, NULL); + g_mutex_lock((GMutex *) mutex->once.retval); +} +#undef g_mutex_lock + +static inline gboolean (g_mutex_trylock)(CompatGMutex *mutex) +{ + g_once(&mutex->once, do_g_mutex_new, NULL); + return g_mutex_trylock((GMutex *) mutex->once.retval); +} +#undef g_mutex_trylock + + +static inline void (g_mutex_unlock)(CompatGMutex *mutex) +{ + g_mutex_unlock((GMutex *) mutex->once.retval); +} +#undef g_mutex_unlock + +static inline gpointer do_g_cond_new(gpointer unused) +{ + return (gpointer) g_cond_new(); +} + +static inline void g_cond_init(CompatGCond *cond) +{ + cond->once = (GOnce) G_ONCE_INIT; +} + +static inline void g_cond_clear(CompatGCond *cond) +{ + assert(cond->once.status != G_ONCE_STATUS_PROGRESS); + if (cond->once.retval) { + g_cond_free((GCond *) cond->once.retval); + } + cond->once = (GOnce) G_ONCE_INIT; +} + +static inline void (g_cond_wait)(CompatGCond *cond, CompatGMutex *mutex) +{ + assert(mutex->once.status != G_ONCE_STATUS_PROGRESS); + g_once(&cond->once, do_g_cond_new, NULL); + g_cond_wait((GCond *) cond->once.retval, (GMutex *) mutex->once.retval); +} +#undef g_cond_wait + +static inline void (g_cond_broadcast)(CompatGCond *cond) +{ + g_once(&cond->once, do_g_cond_new, NULL); + g_cond_broadcast((GCond *) cond->once.retval); +} +#undef g_cond_broadcast + +static inline void (g_cond_signal)(CompatGCond *cond) +{ + g_once(&cond->once, do_g_cond_new, NULL); + g_cond_signal((GCond *) cond->once.retval); +} +#undef g_cond_signal + + +/* before 2.31 there was no g_thread_new() */ +static inline GThread *g_thread_new(const char *name, + GThreadFunc func, gpointer data) +{ + GThread *thread = g_thread_create(func, data, TRUE, NULL); + if (!thread) { + g_error("creating thread"); + } + return thread; +} +#else +#define CompatGMutex GMutex +#define CompatGCond GCond +#endif /* glib 2.31 */ + #endif diff --git a/trace/simple.c b/trace/simple.c index 1584bf7..8fc96fe 100644 --- a/trace/simple.c +++ b/trace/simple.c @@ -40,28 +40,9 @@ * Trace records are written out by a dedicated thread. The thread waits for * records to become available, writes them out, and then waits again. */ -#if GLIB_CHECK_VERSION(2, 32, 0) -static GMutex trace_lock; -#define lock_trace_lock() g_mutex_lock(&trace_lock) -#define unlock_trace_lock() g_mutex_unlock(&trace_lock) -#define get_trace_lock_mutex() (&trace_lock) -#else -static GStaticMutex trace_lock = G_STATIC_MUTEX_INIT; -#define lock_trace_lock() g_static_mutex_lock(&trace_lock) -#define unlock_trace_lock() g_static_mutex_unlock(&trace_lock) -#define get_trace_lock_mutex() g_static_mutex_get_mutex(&trace_lock) -#endif - -/* g_cond_new() was deprecated in glib 2.31 but we still need to support it */ -#if GLIB_CHECK_VERSION(2, 31, 0) -static GCond the_trace_available_cond; -static GCond the_trace_empty_cond; -static GCond *trace_available_cond = &the_trace_available_cond; -static GCond *trace_empty_cond = &the_trace_empty_cond; -#else -static GCond *trace_available_cond; -static GCond *trace_empty_cond; -#endif +static CompatGMutex trace_lock; +static CompatGCond trace_available_cond; +static CompatGCond trace_empty_cond; static bool trace_available; static bool trace_writeout_enabled; @@ -151,26 +132,26 @@ static bool get_trace_record(unsigned int idx, TraceRecord **recordptr) */ static void flush_trace_file(bool wait) { - lock_trace_lock(); + g_mutex_lock(&trace_lock); trace_available = true; - g_cond_signal(trace_available_cond); + g_cond_signal(&trace_available_cond); if (wait) { - g_cond_wait(trace_empty_cond, get_trace_lock_mutex()); + g_cond_wait(&trace_empty_cond, &trace_lock); } - unlock_trace_lock(); + g_mutex_unlock(&trace_lock); } static void wait_for_trace_records_available(void) { - lock_trace_lock(); + g_mutex_lock(&trace_lock); while (!(trace_available && trace_writeout_enabled)) { - g_cond_signal(trace_empty_cond); - g_cond_wait(trace_available_cond, get_trace_lock_mutex()); + g_cond_signal(&trace_empty_cond); + g_cond_wait(&trace_available_cond, &trace_lock); } trace_available = false; - unlock_trace_lock(); + g_mutex_unlock(&trace_lock); } static gpointer writeout_thread(gpointer opaque) @@ -399,11 +380,7 @@ static GThread *trace_thread_create(GThreadFunc fn) pthread_sigmask(SIG_SETMASK, &set, &oldset); #endif -#if GLIB_CHECK_VERSION(2, 31, 0) thread = g_thread_new("trace-thread", fn, NULL); -#else - thread = g_thread_create(fn, NULL, FALSE, NULL); -#endif #ifndef _WIN32 pthread_sigmask(SIG_SETMASK, &oldset, NULL); @@ -418,11 +395,6 @@ bool trace_backend_init(const char *events, const char *file) trace_pid = getpid(); -#if !GLIB_CHECK_VERSION(2, 31, 0) - trace_available_cond = g_cond_new(); - trace_empty_cond = g_cond_new(); -#endif - thread = trace_thread_create(writeout_thread); if (!thread) { fprintf(stderr, "warning: unable to initialize simple trace backend\n");