From patchwork Sat Mar 3 14:52:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 7075 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 9B08223EB0 for ; Sat, 3 Mar 2012 14:53:01 +0000 (UTC) Received: from mail-iy0-f180.google.com (mail-iy0-f180.google.com [209.85.210.180]) by fiordland.canonical.com (Postfix) with ESMTP id 43657A1860E for ; Sat, 3 Mar 2012 14:53:01 +0000 (UTC) Received: by iage36 with SMTP id e36so4825182iag.11 for ; Sat, 03 Mar 2012 06:53:00 -0800 (PST) Received: from mr.google.com ([10.50.159.228]) by 10.50.159.228 with SMTP id xf4mr1941744igb.0.1330786380785 (num_hops = 1); Sat, 03 Mar 2012 06:53:00 -0800 (PST) MIME-Version: 1.0 Received: by 10.50.159.228 with SMTP id xf4mr1602691igb.0.1330786380671; Sat, 03 Mar 2012 06:53:00 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.53.18 with SMTP id k18csp7686ibg; Sat, 3 Mar 2012 06:53:00 -0800 (PST) Received: by 10.180.83.97 with SMTP id p1mr3385433wiy.19.1330786379469; Sat, 03 Mar 2012 06:52:59 -0800 (PST) Received: from mnementh.archaic.org.uk (mnementh.archaic.org.uk. [81.2.115.146]) by mx.google.com with ESMTPS id g51si1843876wee.104.2012.03.03.06.52.58 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 03 Mar 2012 06:52:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 81.2.115.146 as permitted sender) client-ip=81.2.115.146; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of pm215@archaic.org.uk designates 81.2.115.146 as permitted sender) smtp.mail=pm215@archaic.org.uk Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.72) (envelope-from ) id 1S3qKO-0002Pb-1s; Sat, 03 Mar 2012 14:52:56 +0000 From: Peter Maydell To: qemu-devel@nongnu.org Cc: patches@linaro.org Subject: [PATCH] coroutine-gthread.c: Avoid threading APIs deprecated in GLib 2.31 Date: Sat, 3 Mar 2012 14:52:56 +0000 Message-Id: <1330786376-9248-1-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.2.5 X-Gm-Message-State: ALoCoQkHBPGduoKyFSivbw+vVo+IVBfmPciU5M41p0+R+aw5eHmR43A6jEVLxZOYXjtmdUqwxdUn The GLib threading APIs were revamped in GLib 2.31 and a number of the old interfaces were deprecated, which means they provoke compilation warnings (errors if -Werror) now. Add support for the new interfaces while retaining the old ones so we can still compile on older versions of GLib too. Signed-off-by: Peter Maydell --- In particular, this fixes compilation failure on ARM hosts running Ubuntu Precise. Seems kinda ugly to me, suggestions for improvement welcomed. coroutine-gthread.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 87 insertions(+), 9 deletions(-) diff --git a/coroutine-gthread.c b/coroutine-gthread.c index 662801b..30c24c9 100644 --- a/coroutine-gthread.c +++ b/coroutine-gthread.c @@ -26,13 +26,93 @@ typedef struct { Coroutine base; GThread *thread; bool runnable; + bool free_on_thread_exit; CoroutineAction action; } CoroutineGThread; -static GCond *coroutine_cond; static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT; + +/* 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 + * thread exit / coroutine key update using the free_on_thread_exit + * field. + */ +static void coroutine_destroy_notify(gpointer data) +{ + CoroutineGThread *co = data; + if (co && co->free_on_thread_exit) { + g_free(co); + } +} + +static GPrivate coroutine_key = G_PRIVATE_INIT(coroutine_destroy_notify); + +static inline CoroutineGThread *get_coroutine_key(void) +{ + return g_private_get(&coroutine_key); +} + +static inline void set_coroutine_key(CoroutineGThread *co, + bool free_on_thread_exit) +{ + /* Unlike g_static_private_set() this does not call the GDestroyNotify + * if the previous value of the key was NULL. Fortunately we only need + * the GDestroyNotify in the non-NULL key case. + */ + co->free_on_thread_exit = free_on_thread_exit; + g_private_replace(&coroutine_key, co); +} + +static inline GThread *create_thread(GThreadFunc func, gpointer data) +{ + return g_thread_new("coroutine", func, 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; +static inline CoroutineGThread *get_coroutine_key(void) +{ + return g_static_private_get(&coroutine_key); +} + +static inline void set_coroutine_key(CoroutineGThread *co, + bool free_on_thread_exit) +{ + g_static_private_set(&coroutine_key, co, + free_on_thread_exit ? (GDestroyNotify)g_free : NULL); +} + +static inline GThread *create_thread(GThreadFunc func, gpointer data) +{ + return g_thread_create_full(func, data, 0, TRUE, TRUE, + G_THREAD_PRIORITY_NORMAL, NULL); +} + +#endif + + static void __attribute__((constructor)) coroutine_init(void) { if (!g_thread_supported()) { @@ -44,7 +124,7 @@ static void __attribute__((constructor)) coroutine_init(void) #endif } - coroutine_cond = g_cond_new(); + init_coroutine_cond(); } static void coroutine_wait_runnable_locked(CoroutineGThread *co) @@ -65,7 +145,7 @@ static gpointer coroutine_thread(gpointer opaque) { CoroutineGThread *co = opaque; - g_static_private_set(&coroutine_key, co, NULL); + set_coroutine_key(co, false); coroutine_wait_runnable(co); co->base.entry(co->base.entry_arg); qemu_coroutine_switch(&co->base, co->base.caller, COROUTINE_TERMINATE); @@ -77,8 +157,7 @@ Coroutine *qemu_coroutine_new(void) CoroutineGThread *co; co = g_malloc0(sizeof(*co)); - co->thread = g_thread_create_full(coroutine_thread, co, 0, TRUE, TRUE, - G_THREAD_PRIORITY_NORMAL, NULL); + co->thread = create_thread(coroutine_thread, co); if (!co->thread) { g_free(co); return NULL; @@ -117,12 +196,11 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *qemu_coroutine_self(void) { - CoroutineGThread *co = g_static_private_get(&coroutine_key); - + CoroutineGThread *co = get_coroutine_key(); if (!co) { co = g_malloc0(sizeof(*co)); co->runnable = true; - g_static_private_set(&coroutine_key, co, (GDestroyNotify)g_free); + set_coroutine_key(co, true); } return &co->base; @@ -130,7 +208,7 @@ Coroutine *qemu_coroutine_self(void) bool qemu_in_coroutine(void) { - CoroutineGThread *co = g_static_private_get(&coroutine_key); + CoroutineGThread *co = get_coroutine_key(); return co && co->base.caller; }