qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Anthony Liguori <aliguori@amazon.com>
Subject: [Qemu-devel] [PATCH 3/3] glib: add compat wrapper for GStaticMutex
Date: Mon,  3 Feb 2014 14:31:50 +0100	[thread overview]
Message-ID: <1391434310-9990-4-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1391434310-9990-1-git-send-email-stefanha@redhat.com>

Avoid duplicating ifdefs for the deprecated GStaticMutex API by
introducing compat_g_static_mutex_*() in glib-compat.h.

GStaticMutex users should use the compat API to avoid dealing with
deprecation.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 coroutine-gthread.c   | 13 +++++++------
 include/glib-compat.h | 19 +++++++++++++++++++
 trace/simple.c        | 26 +++++++++-----------------
 3 files changed, 35 insertions(+), 23 deletions(-)

diff --git a/coroutine-gthread.c b/coroutine-gthread.c
index 695c113..28272f8 100644
--- a/coroutine-gthread.c
+++ b/coroutine-gthread.c
@@ -30,7 +30,7 @@ typedef struct {
     CoroutineAction action;
 } CoroutineGThread;
 
-static GStaticMutex coroutine_lock = G_STATIC_MUTEX_INIT;
+DEFINE_COMPAT_G_STATIC_MUTEX(coroutine_lock);
 
 /* GLib 2.31 and beyond deprecated various parts of the thread API,
  * but the new interfaces are not available in older GLib versions
@@ -123,15 +123,16 @@ static void __attribute__((constructor)) coroutine_init(void)
 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,
+                    compat_g_static_mutex_get_mutex(&coroutine_lock));
     }
 }
 
 static void coroutine_wait_runnable(CoroutineGThread *co)
 {
-    g_static_mutex_lock(&coroutine_lock);
+    compat_g_static_mutex_lock(&coroutine_lock);
     coroutine_wait_runnable_locked(co);
-    g_static_mutex_unlock(&coroutine_lock);
+    compat_g_static_mutex_unlock(&coroutine_lock);
 }
 
 static gpointer coroutine_thread(gpointer opaque)
@@ -173,7 +174,7 @@ 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);
+    compat_g_static_mutex_lock(&coroutine_lock);
     from->runnable = false;
     from->action = action;
     to->runnable = true;
@@ -183,7 +184,7 @@ CoroutineAction qemu_coroutine_switch(Coroutine *from_,
     if (action != COROUTINE_TERMINATE) {
         coroutine_wait_runnable_locked(from);
     }
-    g_static_mutex_unlock(&coroutine_lock);
+    compat_g_static_mutex_unlock(&coroutine_lock);
     return from->action;
 }
 
diff --git a/include/glib-compat.h b/include/glib-compat.h
index ea965df..48fa95c 100644
--- a/include/glib-compat.h
+++ b/include/glib-compat.h
@@ -49,4 +49,23 @@ static inline GThread *g_thread_new(const gchar *unused,
 }
 #endif
 
+/* GStaticMutex was deprecated in 2.32.0.  Use the compat_g_static_mutex_*()
+ * wrappers to hide the GStaticMutex/GMutex distinction.
+ */
+#if GLIB_CHECK_VERSION(2, 32, 0)
+#define DEFINE_COMPAT_G_STATIC_MUTEX(name) \
+static GMutex name
+
+#define compat_g_static_mutex_lock(m) g_mutex_lock(m)
+#define compat_g_static_mutex_unlock(m) g_mutex_unlock(m)
+#define compat_g_static_mutex_get_mutex(m) (m)
+#else
+#define DEFINE_COMPAT_G_STATIC_MUTEX(name) \
+static GStaticMutex name = G_STATIC_MUTEX_INIT
+
+#define compat_g_static_mutex_lock(m) g_static_mutex_lock(m)
+#define compat_g_static_mutex_unlock(m) g_static_mutex_unlock(m)
+#define compat_g_static_mutex_get_mutex(m) g_static_mutex_get_mutex(m)
+#endif
+
 #endif
diff --git a/trace/simple.c b/trace/simple.c
index 8e83e59..dc58afe 100644
--- a/trace/simple.c
+++ b/trace/simple.c
@@ -41,17 +41,7 @@
  * 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
+DEFINE_COMPAT_G_STATIC_MUTEX(trace_lock);
 
 /* g_cond_new() was deprecated in glib 2.31 but we still need to support it */
 #if GLIB_CHECK_VERSION(2, 31, 0)
@@ -151,26 +141,28 @@ static bool get_trace_record(unsigned int idx, TraceRecord **recordptr)
  */
 static void flush_trace_file(bool wait)
 {
-    lock_trace_lock();
+    compat_g_static_mutex_lock(&trace_lock);
     trace_available = true;
     g_cond_signal(trace_available_cond);
 
     if (wait) {
-        g_cond_wait(trace_empty_cond, get_trace_lock_mutex());
+        g_cond_wait(trace_empty_cond,
+                    compat_g_static_mutex_get_mutex(&trace_lock));
     }
 
-    unlock_trace_lock();
+    compat_g_static_mutex_unlock(&trace_lock);
 }
 
 static void wait_for_trace_records_available(void)
 {
-    lock_trace_lock();
+    compat_g_static_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_wait(trace_available_cond,
+                    compat_g_static_mutex_get_mutex(&trace_lock));
     }
     trace_available = false;
-    unlock_trace_lock();
+    compat_g_static_mutex_unlock(&trace_lock);
 }
 
 static gpointer writeout_thread(gpointer opaque)
-- 
1.8.5.3

  parent reply	other threads:[~2014-02-03 13:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-03 13:31 [Qemu-devel] [PATCH 0/3] glib: move compat functions into glib-compat.h Stefan Hajnoczi
2014-02-03 13:31 ` [Qemu-devel] [PATCH 1/3] glib: move g_poll() replacement " Stefan Hajnoczi
2014-02-03 13:31 ` [Qemu-devel] [PATCH 2/3] glib: add g_thread_new() compat function Stefan Hajnoczi
2014-02-03 13:31 ` Stefan Hajnoczi [this message]
2014-02-14 12:31 ` [Qemu-devel] [PATCH 0/3] glib: move compat functions into glib-compat.h Stefan Hajnoczi
2014-05-02  9:35 ` Stefan Hajnoczi
  -- strict thread matches above, loose matches on Subject: below --
2014-05-02 11:08 [Qemu-devel] [PATCH 3/3] glib: add compat wrapper for GStaticMutex Michael Tokarev
2014-05-05 12:11 ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1391434310-9990-4-git-send-email-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=aliguori@amazon.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).