glib >= 2.31.0 Threads and Mutexes



Hello list!

There was a change in the threads and mutexes API in glib starting with version 2.31, see http://git.gnome.org/browse/glib/tree/NEWS. Especially g_thread_init(), g_thread_create(), g_mutex_new() and g_mutex_free() are gone.  I replaced those as follows:

- made g_thread_init conditional, as demonstrated in https://bugs.freedesktop.org/show_bug.cgi?id=42036.
- conditionally replaced g_thread_create() with g_thread_new(). NetworkManager never catches errors and threads seem always joinable now, so the change is straight forward replace and satisfy g_thread_new() signature
- g_mutex_new() replaced by static declarations of GMutex-es and (conditionally) passing their references where g_mutex_new() used to be called
- g_mutex_free() removed (conditionally)

I came across this problem while building Gnome3 using jhbuild, so this patch should be against git HEAD (or its equivalent given a central repository).

Kind regards

Martin
  

 
diff --git a/libnm-glib/libnm_glib.c b/libnm-glib/libnm_glib.c
index 1d75578..8da58b9 100644
--- a/libnm-glib/libnm_glib.c
+++ b/libnm-glib/libnm_glib.c
@@ -32,6 +32,9 @@
 
 #define	DBUS_NO_SERVICE_ERROR			"org.freedesktop.DBus.Error.ServiceDoesNotExist"
 
+#if GLIB_CHECK_VERSION (2, 31, 0)
+static GMutex callbacks_lock_mutex;
+#endif
 
 struct libnm_glib_ctx
 {
@@ -453,8 +456,10 @@ _libnm_glib_ctx_free (libnm_glib_ctx *ctx)
 		ctx->dbus_con = NULL;
 	}
 
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	if (ctx->callbacks_lock)
 		g_mutex_free (ctx->callbacks_lock);
+#endif
 
 	g_slist_foreach (ctx->callbacks, (GFunc)g_free, NULL);
 	g_slist_free (ctx->callbacks);
@@ -477,8 +482,12 @@ _libnm_glib_ctx_new (void)
 		goto error;
 	if (!(ctx->g_main_loop = g_main_loop_new (ctx->g_main_ctx, FALSE)))
 		goto error;
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	if (!(ctx->callbacks_lock = g_mutex_new ()))
 		goto error;
+#else
+	ctx->callbacks_lock = &callbacks_lock_mutex;
+#endif
 	ctx->dbus_watch_interval = 1000;
 
 	return ctx;
@@ -495,14 +504,20 @@ libnm_glib_init (void)
 	libnm_glib_ctx	*ctx = NULL;
 
 	g_type_init ();
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	if (!g_thread_supported ())
 		g_thread_init (NULL);
+#endif
 	dbus_g_thread_init ();
 
 	if (!(ctx = _libnm_glib_ctx_new ()))
 		return NULL;
 
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	ctx->thread = g_thread_create (_libnm_glib_dbus_worker, ctx, TRUE, NULL);
+#else
+	ctx->thread = g_thread_new ("_libnm_glib_dbus_worker", _libnm_glib_dbus_worker, ctx);
+#endif
 	if (!ctx->thread)
 		goto error;	
 
diff --git a/src/main.c b/src/main.c
index b7c0fd5..0718e91 100644
--- a/src/main.c
+++ b/src/main.c
@@ -563,8 +563,10 @@ main (int argc, char *argv[])
 	umask (022);
 
 	g_type_init ();
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	if (!g_thread_supported ())
 		g_thread_init (NULL);
+#endif
 	dbus_g_thread_init ();
 
 #ifndef HAVE_DBUS_GLIB_DISABLE_LEGACY_PROP_ACCESS
diff --git a/src/nm-policy-hostname.c b/src/nm-policy-hostname.c
index 4fe69c5..0ee1cf9 100644
--- a/src/nm-policy-hostname.c
+++ b/src/nm-policy-hostname.c
@@ -33,6 +33,11 @@
 
 /************************************************************************/
 
+#if GLIB_CHECK_VERSION (2, 31, 0)
+static GMutex lock_mutex_v4;
+static GMutex lock_mutex_v6;
+#endif
+
 struct HostnameThread {
 	GThread *thread;
 
@@ -106,7 +111,9 @@ hostname_thread_free (HostnameThread *ht)
 
 	nm_log_dbg (LOGD_DNS, "(%p) freeing reverse-lookup thread", ht);
 
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	g_mutex_free (ht->lock);
+#endif
 	memset (ht, 0, sizeof (HostnameThread));
 	g_free (ht);
 }
@@ -123,7 +130,11 @@ hostname4_thread_new (guint32 ip4_addr,
 	ht = g_malloc0 (sizeof (HostnameThread));
 	g_assert (ht);
 
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	ht->lock = g_mutex_new ();
+#else
+	ht->lock = &lock_mutex_v4;
+#endif
 	ht->callback = callback;
 	ht->user_data = user_data;
 
@@ -132,7 +143,11 @@ hostname4_thread_new (guint32 ip4_addr,
 	ht->addr = (struct sockaddr *) &ht->addr4;
 	ht->addr_size = sizeof (ht->addr4);
 
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	ht->thread = g_thread_create (hostname_thread_worker, ht, FALSE, NULL);
+#else
+	ht->thread = g_thread_new ("hostname_thread_worker", hostname_thread_worker, ht);
+#endif
 	if (!ht->thread) {
 		hostname_thread_free (ht);
 		return NULL;
@@ -158,7 +173,11 @@ hostname6_thread_new (const struct in6_addr *ip6_addr,
 	ht = g_malloc0 (sizeof (HostnameThread));
 	g_assert (ht);
 
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	ht->lock = g_mutex_new ();
+#else
+	ht->lock = &lock_mutex_v6;
+#endif
 	ht->callback = callback;
 	ht->user_data = user_data;
 
@@ -167,7 +186,11 @@ hostname6_thread_new (const struct in6_addr *ip6_addr,
 	ht->addr = (struct sockaddr *) &ht->addr6;
 	ht->addr_size = sizeof (ht->addr6);
 
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 	ht->thread = g_thread_create (hostname_thread_worker, ht, FALSE, NULL);
+#else
+	ht->thread = g_thread_new ("hostname_thread_worker", hostname_thread_worker, ht);
+#endif
 	if (!ht->thread) {
 		hostname_thread_free (ht);
 		return NULL;
diff --git a/src/wimax/iwmxsdk.c b/src/wimax/iwmxsdk.c
index 9c3a78b..24abdd8 100644
--- a/src/wimax/iwmxsdk.c
+++ b/src/wimax/iwmxsdk.c
@@ -39,6 +39,11 @@
 #include "logging/nm-logging.h"
 #include "iwmxsdk.h"
 
+#if GLIB_CHECK_VERSION (2, 31, 0)
+static GMutex status_mutex;
+static GMutex connect_mutex;
+#endif
+
 static WIMAX_API_DEVICE_ID g_api;
 static GStaticMutex add_remove_mutex = G_STATIC_MUTEX_INIT;
 
@@ -1289,10 +1294,14 @@ static struct wmxsdk *wmxsdk_new(void)
 		g_static_mutex_init(&wmxsdk->network_mutex);
 
 		wmxsdk->status = WIMAX_API_DEVICE_STATUS_UnInitialized;
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 		wmxsdk->status_mutex = g_mutex_new();
-		g_assert(wmxsdk->status_mutex);
-
 		wmxsdk->connect_mutex = g_mutex_new();
+#else
+		wmxsdk->status_mutex = &status_mutex;
+		wmxsdk->connect_mutex = &connect_mutex;
+#endif
+		g_assert(wmxsdk->status_mutex);
 		g_assert(wmxsdk->connect_mutex);
 	}
 	return wmxsdk;
@@ -1307,8 +1316,10 @@ struct wmxsdk *wmxsdk_ref(struct wmxsdk *wmxsdk)
 void wmxsdk_unref(struct wmxsdk *wmxsdk)
 {
 	if (g_atomic_int_dec_and_test(&wmxsdk->refcount)) {
+#if !GLIB_CHECK_VERSION (2, 31, 0)
 		g_mutex_free(wmxsdk->status_mutex);
 		g_mutex_free(wmxsdk->connect_mutex);
+#endif
 		memset(wmxsdk, 0, sizeof(*wmxsdk));
 		free(wmxsdk);
 	}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]