From 21f9e79acf37d05e068e155f4f977d04628204de Mon Sep 17 00:00:00 2001
From: Colomban Wendling <ban@herbesfolles.org>
Date: Sun, 12 Oct 2014 14:52:05 +0200
Subject: [PATCH] debugger: Fix GCond usage

g_cond_wait() and the likes unlock the passed in mutex during the wait
and re-lock it before returning.  However, the code here used to pass
in an unlocked mutex.

This used to work because GLib threading implementation was tolerant
and allowed unlocking an unlocked mutex, but GLib 2.42 has a new and
less tolerant implementation that would abort in such situation.

Fix this by properly locking the passed in mutex.

The implementation here also removes the second mutex only used for the
condition as the one used to protect the loop body can very well be
used and actually makes sense as they protect the same thing.

Doing so requires to properly wait for the thread to quit before
destroying the mutex, but this probably should be done anyways to avoid
forcefully killing the thread when the application quits.
---
 debugger/src/dconfig.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/debugger/src/dconfig.c b/debugger/src/dconfig.c
index 97e7bab..e5bb55c 100644
--- a/debugger/src/dconfig.c
+++ b/debugger/src/dconfig.c
@@ -270,11 +270,9 @@ static void save_to_keyfile(GKeyFile *keyfile)
 static gpointer saving_thread_func(gpointer data)
 {
 	GTimeVal interval;
-	GMutex *m = g_mutex_new();
+	g_mutex_lock(change_config_mutex);
 	do
 	{
-		g_mutex_lock(change_config_mutex);
-		
 		if (
 			panel_config_changed ||
 			(debug_config_changed && DEBUG_STORE_PLUGIN == dstore)
@@ -309,14 +307,12 @@ static gpointer saving_thread_func(gpointer data)
 		
 			debug_config_changed = FALSE;
 		}
-		
-		g_mutex_unlock(change_config_mutex);
 
 		g_get_current_time(&interval);
 		g_time_val_add(&interval, SAVING_INTERVAL);
 	}
-	while (!g_cond_timed_wait(cond, m, &interval));
-	g_mutex_free(m);
+	while (!g_cond_timed_wait(cond, change_config_mutex, &interval));
+	g_mutex_unlock(change_config_mutex);
 	
 	return NULL;
 }
@@ -471,7 +467,7 @@ void config_init(void)
 void config_destroy(void)
 {
 	g_cond_signal(cond);
-	/* ??? g_thread_join(saving_thread); */	
+	g_thread_join(saving_thread);
 	
 	g_mutex_free(change_config_mutex);
 	g_cond_free(cond);
