[libgda] GdaThreadProvider related corrections



commit 886dd0793cf0088cb0b2c281ee2aa365d4b03a1d
Author: Vivien Malerba <malerba gnome-db org>
Date:   Fri Jan 15 19:12:16 2010 +0100

    GdaThreadProvider related corrections

 libgda/gda-connection.c                     |   20 ++++--------
 libgda/thread-wrapper/gda-thread-provider.c |   43 +++++++++++++++++---------
 2 files changed, 35 insertions(+), 28 deletions(-)
---
diff --git a/libgda/gda-connection.c b/libgda/gda-connection.c
index 85479ff..6bae026 100644
--- a/libgda/gda-connection.c
+++ b/libgda/gda-connection.c
@@ -829,9 +829,12 @@ cnc_task_free (CncTask *task)
 GdaServerProvider *
 _gda_connection_get_internal_thread_provider (void)
 {
+	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+
+	g_static_mutex_lock (&mutex);
 	if (!_gda_thread_wrapper_provider)
 		_gda_thread_wrapper_provider = GDA_SERVER_PROVIDER (g_object_new (GDA_TYPE_THREAD_PROVIDER, NULL));
-
+	g_static_mutex_unlock (&mutex);
 	return _gda_thread_wrapper_provider;
 }
 
@@ -920,13 +923,8 @@ gda_connection_open_from_dsn (const gchar *dsn, const gchar *auth_string,
 
 		pinfo = gda_config_get_provider_info (dsn_info->provider);
 		if (pinfo) {
-			if (options & GDA_CONNECTION_OPTIONS_THREAD_SAFE) {
-				if (!_gda_thread_wrapper_provider)
-					_gda_thread_wrapper_provider =
-						GDA_SERVER_PROVIDER (g_object_new (GDA_TYPE_THREAD_PROVIDER, 
-										   NULL));
-				prov = _gda_thread_wrapper_provider;
-			}
+			if (options & GDA_CONNECTION_OPTIONS_THREAD_SAFE)
+				prov = _gda_connection_get_internal_thread_provider ();
 			else
 				prov = gda_config_get_provider (dsn_info->provider, error);
 		}
@@ -1075,11 +1073,7 @@ gda_connection_open_from_string (const gchar *provider_name, const gchar *cnc_st
 				tmp = g_strdup_printf ("%s;PROVIDER_NAME=%s", real_cnc, pinfo->id);
 				g_free (real_cnc);
 				real_cnc = tmp;
-				if (!_gda_thread_wrapper_provider)
-					_gda_thread_wrapper_provider =
-						GDA_SERVER_PROVIDER (g_object_new (GDA_TYPE_THREAD_PROVIDER, 
-										   NULL));
-				prov = _gda_thread_wrapper_provider;
+				prov = _gda_connection_get_internal_thread_provider ();
 			}
 			else
 				prov = gda_config_get_provider (provider_name ? provider_name : real_provider, error);
diff --git a/libgda/thread-wrapper/gda-thread-provider.c b/libgda/thread-wrapper/gda-thread-provider.c
index 2f708be..9208745 100644
--- a/libgda/thread-wrapper/gda-thread-provider.c
+++ b/libgda/thread-wrapper/gda-thread-provider.c
@@ -389,7 +389,10 @@ gda_thread_provider_open_connection (GdaServerProvider *provider, GdaConnection
 						 _("Provider does not support asynchronous connection open"));
                 return FALSE;
 	}
-		
+
+	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+	g_static_mutex_lock (&mutex);
+
 	/* test if connection has to be opened using a DSN or a connection string */
 	gchar *dsn, *auth_string, *cnc_string;
 	GdaConnectionOptions options;
@@ -417,14 +420,6 @@ gda_thread_provider_open_connection (GdaServerProvider *provider, GdaConnection
 					  data->prov_name);
 	}
 	g_assert (data);
-	
-	/* open sub connection */
-	GdaConnection *sub_cnc;
-	GError *error = NULL;
-	guint jid;
-	g_assert (data);
-	data->auth_string = auth_string;
-	data->options = options & (~GDA_CONNECTION_OPTIONS_THREAD_SAFE);
 
 	if (!wr) {
 		wr_created = TRUE;
@@ -432,11 +427,24 @@ gda_thread_provider_open_connection (GdaServerProvider *provider, GdaConnection
 		if (!wr) {
 			gda_connection_add_event_string (cnc, "%s", _("Multi threading is not supported or enabled"));
 			g_free (data);
+			g_static_mutex_unlock (&mutex);
 			return FALSE;
 		}
 	}
-	else
+	else {
+		/* here wr_created == FALSE */
 		g_object_ref (wr);
+		g_static_mutex_unlock (&mutex);
+	}
+	
+	/* open sub connection */
+	GdaConnection *sub_cnc;
+	GError *error = NULL;
+	guint jid;
+	g_assert (data);
+	data->auth_string = auth_string;
+	data->options = options & (~GDA_CONNECTION_OPTIONS_THREAD_SAFE);
+
 	jid = gda_thread_wrapper_execute (wr, (GdaThreadWrapperFunc) sub_thread_open_connection, data, NULL, NULL);
 	sub_cnc = gda_thread_wrapper_fetch_result (wr, TRUE, jid, &error);
 	g_free (dsn);
@@ -448,6 +456,8 @@ gda_thread_provider_open_connection (GdaServerProvider *provider, GdaConnection
 			g_error_free (error);
 		g_object_unref (wr);
 		g_free (data);
+		if (wr_created)
+			g_static_mutex_unlock (&mutex);
 		return FALSE;
 	}
 	
@@ -461,11 +471,14 @@ gda_thread_provider_open_connection (GdaServerProvider *provider, GdaConnection
 	gda_connection_internal_set_provider_data (cnc, cdata, (GDestroyNotify) gda_thread_free_cnc_data);
 	setup_signals (cnc, cdata);
 
-	if (wr_created && PROV_CLASS (cdata->cnc_provider)->limiting_thread) {
-		/* keep GdaThreadWrapper for other uses */
-		g_hash_table_insert (GDA_THREAD_PROVIDER (provider)->priv->prov_wrappers,
-				     g_strdup (gda_server_provider_get_name (cdata->cnc_provider)),
-				     g_object_ref (wr));
+	if (wr_created) {
+		if (PROV_CLASS (cdata->cnc_provider)->limiting_thread) {
+			/* keep GdaThreadWrapper for other uses */
+			g_hash_table_insert (GDA_THREAD_PROVIDER (provider)->priv->prov_wrappers,
+					     g_strdup (gda_server_provider_get_name (cdata->cnc_provider)),
+					     g_object_ref (wr));
+		}
+		g_static_mutex_unlock (&mutex);
 	}
 
 	return TRUE;



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