diff mbox

[v2,3/7] glib-compat.h: add new thread API emulation on top of pre-2.31 API

Message ID 1399041361-30496-4-git-send-email-mjt@msgid.tls.msk.ru
State New
Headers show

Commit Message

Michael Tokarev May 2, 2014, 2:35 p.m. UTC
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, re-defining the base
types (GCond and GMutex) to be pointers.

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 <mjt@tls.msk.ru>
---
 coroutine-gthread.c   |   30 +++++---------
 include/glib-compat.h |  106 +++++++++++++++++++++++++++++++++++++++++++++++++
 trace/simple.c        |   50 ++++++-----------------
 3 files changed, 129 insertions(+), 57 deletions(-)
diff mbox

Patch

diff --git a/coroutine-gthread.c b/coroutine-gthread.c
index a61efe0..a4813d9 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 GMutex coroutine_lock;
+static GCond 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;
 
@@ -121,21 +110,22 @@  static void __attribute__((constructor)) coroutine_init(void)
     }
 #endif
 
-    init_coroutine_cond();
+    g_cond_init_static(&coroutine_cond);
+    g_mutex_init_static(&coroutine_lock);
 }
 
 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 +167,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 8d25900..34a7967 100644
--- a/include/glib-compat.h
+++ b/include/glib-compat.h
@@ -5,6 +5,7 @@ 
  *
  * Authors:
  *  Anthony Liguori   <aliguori@us.ibm.com>
+ *  Michael Tokarev   <mjt@tls.msk.ru>
  *
  * This work is licensed under the terms of the GNU GPL, version 2 or later.
  * See the COPYING file in the top-level directory.
@@ -36,4 +37,109 @@  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.
+ * We redefine GMutex to GMutex* and create new _init()/_clear()
+ * primitives using old _new()/_free() primitives.
+ * The tricky part is to re-define the types.
+ * g_forward_compat_ prefix is used for inline functions.
+ * This is done as both inline function and a macro because original
+ * glib symbols which we redefine were actually macros.
+ */
+
+#define GMutex GMutex *
+#define GCond  GCond *
+
+static inline void g_mutex_init(GMutex *mutex)
+{
+  *mutex = g_mutex_new();
+}
+
+static inline void g_mutex_clear(GMutex *mutex)
+{
+  g_mutex_free(*mutex);
+  *mutex = NULL;
+}
+
+#define g_mutex_init_static(mutex) g_mutex_init(mutex)
+#define g_mutex_clear_static(mutex) g_mutex_clear(mutex)
+
+static inline void g_forward_compat_mutex_lock(GMutex *mutex)
+{
+  g_mutex_lock(*mutex);
+}
+#undef g_mutex_lock
+#define g_mutex_lock(mutex) g_forward_compat_mutex_lock(mutex)
+
+static inline gboolean g_forward_compat_mutex_trylock(GMutex *mutex)
+{
+  return g_mutex_trylock(*mutex);
+}
+#undef g_mutex_trylock
+#define g_mutex_trylock(mutex) g_forward_compat_mutex_trylock(mutex)
+
+static inline void g_forward_compat_mutex_unlock(GMutex *mutex)
+{
+  g_mutex_unlock(*mutex);
+}
+#undef g_mutex_unlock
+#define g_mutex_unlock(mutex) g_forward_compat_mutex_unlock(mutex)
+
+
+static inline void g_cond_init(GCond *cond)
+{
+  *cond = g_cond_new();
+}
+
+static inline void g_cond_clear(GCond *cond)
+{
+  g_cond_free(*cond);
+  *cond = NULL;
+}
+
+#define g_cond_init_static(cond) g_cond_init(cond)
+#define g_cond_clear_static(cond) g_cond_clear(cond)
+
+static inline void g_forward_compat_cond_wait(GCond *cond, GMutex *mutex)
+{
+  g_cond_wait(*cond, *mutex);
+}
+#undef g_cond_wait
+#define g_cond_wait(cond, mutex) g_forward_compat_cond_wait(cond, mutex)
+
+static inline void g_forward_compat_cond_broadcast(GCond *cond)
+{
+  g_cond_broadcast(*cond);
+}
+#undef g_cond_broadcast
+#define g_cond_broadcast(cond) g_forward_compat_cond_broadcast(cond)
+
+static inline void g_forward_compat_cond_signal(GCond *cond)
+{
+  g_cond_signal(*cond);
+}
+#undef g_cond_signal
+#define g_cond_signal(cond) g_forward_compat_cond_signal(cond)
+
+
+/* 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 /* glib 2.31 */
+
+#define g_cond_init_static(cond) do{}while(0)
+#define g_cond_clear_static(cond) do{}while(0)
+#define g_mutex_init_static(cond) do{}while(0)
+#define g_mutex_clear_static(cond) do{}while(0)
+
+#endif /* glib 2.31 */
+
 #endif
diff --git a/trace/simple.c b/trace/simple.c
index aaa010e..ae7edeb 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 GCond trace_available_cond;
+static GCond trace_empty_cond;
 
 static bool trace_available;
 static bool trace_writeout_enabled;
@@ -150,26 +131,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)
@@ -397,11 +378,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);
@@ -414,10 +391,9 @@  bool trace_backend_init(const char *events, const char *file)
 {
     GThread *thread;
 
-#if !GLIB_CHECK_VERSION(2, 31, 0)
-    trace_available_cond = g_cond_new();
-    trace_empty_cond = g_cond_new();
-#endif
+    g_cond_init_static(&trace_available_cond);
+    g_cond_init_static(&trace_empty_cond);
+    g_mutex_init_static(&trace_lock);
 
     thread = trace_thread_create(writeout_thread);
     if (!thread) {