Re: [evolution-patches] Use a static POA in ebook



On Mon, 2005-04-25 at 22:48 -0500, Hans Petter Jansson wrote: 
> Could you wrap these in static mutexes so there's no way we'll leak in a
> threaded environment?

I'm not very experienced at threaded considerations, but how does the
attached patch look?

Note that this patch also removes a call to
e_book_backend_get_static_capabilities() in e-data-book.c, for which the
result was not used.

Ross
-- 
Ross Burton                                 mail: ross burtonini com
                                          jabber: ross burtonini com
                                     www: http://www.burtonini.com./
 PGP Fingerprint: 1A21 F5B0 D8D0 CFE3 81D4 E25A 2D09 E447 D0B4 33DF

Index: libebook/e-book-listener.c
===================================================================
--- libebook/e-book-listener.c	(revision 7)
+++ libebook/e-book-listener.c	(working copy)
@@ -402,16 +402,17 @@
 EBookListener *
 e_book_listener_new ()
 {
+	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+	static PortableServer_POA poa = NULL;
 	EBookListener *listener;
-	static PortableServer_POA poa = NULL;
-
+	
+	g_static_mutex_lock (&mutex);
 	if (poa == NULL)
 		poa = bonobo_poa_get_threaded (ORBIT_THREAD_HINT_ON_CONTEXT, _ebook_context, NULL);
+	g_static_mutex_unlock (&mutex);
+	
+	listener = g_object_new (E_TYPE_BOOK_LISTENER, "poa", poa, NULL);
 
-	listener = g_object_new (E_TYPE_BOOK_LISTENER,
-				 "poa", poa,
-				 NULL);
-
 	return listener;
 }
 
Index: libebook/e-book-view-listener.c
===================================================================
--- libebook/e-book-view-listener.c	(revision 7)
+++ libebook/e-book-view-listener.c	(working copy)
@@ -284,16 +284,17 @@
 EBookViewListener *
 e_book_view_listener_new ()
 {
+	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+	static PortableServer_POA poa = NULL;
 	EBookViewListener *listener;
-	static PortableServer_POA poa = NULL;
 
+	g_static_mutex_lock (&mutex);
 	if (poa == NULL)
 		poa = bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_OBJECT, NULL);
-	
-	listener = g_object_new (E_TYPE_BOOK_VIEW_LISTENER,
-				 "poa", poa,
-				 NULL);
+	g_static_mutex_unlock (&mutex);
 
+	listener = g_object_new (E_TYPE_BOOK_VIEW_LISTENER, "poa", poa, NULL);
+
 	listener->priv->queue = g_async_queue_new();
 	listener->priv->idle_mutex = g_mutex_new();
 	listener->priv->idle_id = -1;
Index: libedata-book/e-data-book.c
===================================================================
--- libedata-book/e-data-book.c	(revision 4)
+++ libedata-book/e-data-book.c	(working copy)
@@ -788,17 +788,19 @@
 		 ESource *source,
 		 GNOME_Evolution_Addressbook_BookListener  listener)
 {
+	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+	static PortableServer_POA poa = NULL;
 	EDataBook *book;
-	char *caps = e_book_backend_get_static_capabilities (backend);
 
-	book = g_object_new (E_TYPE_DATA_BOOK,
-			     "poa", bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_REQUEST, NULL),
-			     NULL);
+	g_static_mutex_lock (&mutex);
+	if (poa == NULL)
+		poa = bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_REQUEST, NULL);
+	g_static_mutex_unlock (&mutex);
 
+	book = g_object_new (E_TYPE_DATA_BOOK, "poa", poa, NULL);
+
 	e_data_book_construct (book, backend, source, listener);
 
-	g_free (caps);
-
 	return book;
 }
 
Index: libedata-book/e-data-book-factory.c
===================================================================
--- libedata-book/e-data-book-factory.c	(revision 6)
+++ libedata-book/e-data-book-factory.c	(working copy)
@@ -384,11 +384,16 @@
 EDataBookFactory *
 e_data_book_factory_new (void)
 {
+	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+	static PortableServer_POA poa = NULL;
 	EDataBookFactory *factory;
 
-	factory = g_object_new (E_TYPE_DATA_BOOK_FACTORY, 
-				"poa", bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_REQUEST, NULL),
-				NULL);
+	g_static_mutex_lock (&mutex);
+	if (poa == NULL)
+		poa = bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_REQUEST, NULL);
+	g_static_mutex_unlock (&mutex);
+
+	factory = g_object_new (E_TYPE_DATA_BOOK_FACTORY, "poa", poa, NULL);
 	
 	e_data_book_factory_construct (factory);
 
Index: libedata-book/e-data-book-view.c
===================================================================
--- libedata-book/e-data-book-view.c	(revision 4)
+++ libedata-book/e-data-book-view.c	(working copy)
@@ -461,11 +461,16 @@
 		      EBookBackendSExp *card_sexp,
 		      int max_results)
 {
+	static GStaticMutex mutex = G_STATIC_MUTEX_INIT;
+	static PortableServer_POA poa = NULL;
 	EDataBookView *book_view;
 
-	book_view = g_object_new (E_TYPE_DATA_BOOK_VIEW,
-				  "poa", bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_OBJECT, NULL),
-				  NULL);
+	g_static_mutex_lock (&mutex);
+	if (poa == NULL)
+		poa = bonobo_poa_get_threaded (ORBIT_THREAD_HINT_PER_OBJECT, NULL);
+	g_static_mutex_unlock (&mutex);
+
+	book_view = g_object_new (E_TYPE_DATA_BOOK_VIEW, "poa", poa, NULL);
 	
 	e_data_book_view_construct (book_view, backend, listener, card_query, card_sexp, max_results);
 

Attachment: signature.asc
Description: This is a digitally signed message part



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