[evolution-kolab] libekolabutil: initialize Camel only once, and thread-safe



commit ba7444be4ab3b8a6350f491e92262c4243998ae9
Author: Christian Hilberg <hilberg kernelconcepts de>
Date:   Fri Sep 7 11:53:20 2012 +0200

    libekolabutil: initialize Camel only once, and thread-safe
    
    * for thread safety, replaced static bool var
      with a G_ONCE
    * initialization of Camel is now done only
      the first time the init function is called,
      even in multithreaded environments

 src/libekolabutil/kolab-util-camel.c |   39 +++++++++++++++++++++------------
 1 files changed, 25 insertions(+), 14 deletions(-)
---
diff --git a/src/libekolabutil/kolab-util-camel.c b/src/libekolabutil/kolab-util-camel.c
index dd081f0..7387a3b 100644
--- a/src/libekolabutil/kolab-util-camel.c
+++ b/src/libekolabutil/kolab-util-camel.c
@@ -37,37 +37,48 @@
 #include "kolab-util-camel.h"
 
 /*----------------------------------------------------------------------------*/
-/* Camel init/shutdown */
+/* local statics */
 
-gboolean
-kolab_util_camel_init (GError **err)
+static gpointer
+util_camel_init_fn (gpointer data)
 {
-	static gboolean is_initialized = FALSE;
-
+	GError **err = (GError **) (&data);
 	g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
 
-	if (is_initialized == TRUE)
-		return TRUE;
-
-	/* GLib must be initialized first */
-	kolab_util_glib_init ();
-
 	/* init Camel subsystem */
 	if (camel_init (e_get_user_data_dir (), TRUE) != 0) {
 		g_set_error (err,
 		             KOLAB_CAMEL_ERROR,
 		             KOLAB_CAMEL_ERROR_GENERIC,
 		             _("Failed to initialize Camel subsystem"));
-		return FALSE;
+		return (gpointer) 1;
 	}
 
 	/* init the CamelProvider system */
 	camel_provider_init ();
 
-	is_initialized = TRUE;
-
 	g_debug ("%s: camel system initialized", __func__);
 
+	return NULL; /* ok */
+}
+
+/*----------------------------------------------------------------------------*/
+/* Camel init/shutdown */
+
+gboolean
+kolab_util_camel_init (GError **err)
+{
+	static GOnce my_once = G_ONCE_INIT;
+
+	g_return_val_if_fail (err == NULL || *err == NULL, FALSE);
+
+	g_once (&my_once,
+	        util_camel_init_fn,
+	        (gpointer) *err);
+
+	if (my_once.retval != NULL)
+		return FALSE;
+
 	return TRUE;
 }
 



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