[balsa] Build with threads disabled



commit 0abbd5c60b01e57cbd07140001f25eaeda79edc5
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Wed Jun 3 19:12:23 2009 -0400

    Build with threads disabled
---
 ChangeLog                    |   17 ++++++++
 configure.in                 |    3 +
 libbalsa/imap-server.c       |   87 +++++++++++++++++++++++++----------------
 libbalsa/imap/imap_private.h |    1 +
 4 files changed, 74 insertions(+), 34 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b358a80..69650c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2009-06-03  Peter Bloomfield
+
+	Build with threads disabled
+
+	* configure.in: must enable threads to use WebKit.
+	* libbalsa/imap-server.c (libbalsa_imap_server_set_username),
+	(libbalsa_imap_server_set_host), (libbalsa_imap_server_init),
+	(libbalsa_imap_server_finalize), (lb_imap_server_cleanup),
+	(get_or_create), (libbalsa_imap_server_get_handle),
+	(libbalsa_imap_server_get_handle_with_user),
+	(libbalsa_imap_server_release_handle),
+	(libbalsa_imap_server_force_disconnect),
+	(libbalsa_imap_server_close_all_connections),
+	(libbalsa_imap_server_has_free_handles): use macros to manage
+	locks.
+	* libbalsa/imap/imap_private.h: add TRYLOCK macro.
+
 2009-05-27  Peter Bloomfield
 
 	* src/balsa-mime-widget-callbacks.c
diff --git a/configure.in b/configure.in
index 281a554..d5a28b2 100644
--- a/configure.in
+++ b/configure.in
@@ -355,6 +355,9 @@ AC_SUBST(BALSA_DEFS)
 #
 AC_MSG_CHECKING(whether to use WebKit)
 if test x"$use_webkit" != xno ; then
+    if test x"$use_threads" = xno ; then
+        AC_MSG_ERROR([WebKit cannot be used with threads disabled.])
+    fi
     AC_MSG_RESULT([yes])
     PKG_CHECK_MODULES(WEBKIT, [webkit-1.0])
     AC_DEFINE(HAVE_WEBKIT,1,[Defined when WebKit can be used.])
diff --git a/libbalsa/imap-server.c b/libbalsa/imap-server.c
index 901a469..4fc78e8 100644
--- a/libbalsa/imap-server.c
+++ b/libbalsa/imap-server.c
@@ -45,7 +45,9 @@ struct LibBalsaImapServer_ {
     guint max_connections;
     gboolean offline_mode;
     
+#if defined(BALSA_USE_THREADS)
     GMutex *lock; /* protects the following members */
+#endif
     guint used_connections;
     GList *used_handles;
     GList *free_handles;
@@ -77,7 +79,20 @@ static gboolean connection_cleanup(gpointer ptr);
 /* We try to avoid too many connections per server */
 #define MAX_CONNECTIONS_PER_SERVER 20
 
+#if defined(BALSA_USE_THREADS)
 static pthread_mutex_t imap_servers_lock = PTHREAD_MUTEX_INITIALIZER;
+#define LOCK_SERVERS()   pthread_mutex_lock(&imap_servers_lock)
+#define UNLOCK_SERVERS() pthread_mutex_unlock(&imap_servers_lock)
+#define LOCK_SERVER(server)    g_mutex_lock((server)->lock)
+#define TRYLOCK_SERVER(server) g_mutex_trylock((server)->lock)
+#define UNLOCK_SERVER(server)  g_mutex_unlock((server)->lock)
+#else
+#define LOCK_SERVERS()
+#define UNLOCK_SERVERS()
+#define LOCK_SERVER(server)
+#define TRYLOCK_SERVER(server) TRUE
+#define UNLOCK_SERVER(server)
+#endif
 static GHashTable *imap_servers = NULL;
 
 struct handle_info {
@@ -128,12 +143,12 @@ static void libbalsa_imap_server_set_username(LibBalsaServer * server,
     if(server->host && name) { /* we have been initialized... */
         LibBalsaImapServer *imap_server = LIBBALSA_IMAP_SERVER(server);
         
-        pthread_mutex_lock(&imap_servers_lock);
+        LOCK_SERVERS();
         g_hash_table_steal(imap_servers, imap_server->key);
         g_free(imap_server->key);
         imap_server->key = g_strdup_printf("%s %s", name, server->host);
         g_hash_table_insert(imap_servers, imap_server->key, imap_server);
-        pthread_mutex_unlock(&imap_servers_lock);
+        UNLOCK_SERVERS();
     }
     (parent_class)->set_username(server, name);
 }
@@ -143,12 +158,12 @@ libbalsa_imap_server_set_host(LibBalsaServer * server,
 {
     if(server->user && host) { /* we have been initialized... */
         LibBalsaImapServer *imap_server = LIBBALSA_IMAP_SERVER(server);
-        pthread_mutex_lock(&imap_servers_lock);
+        LOCK_SERVERS();
         g_hash_table_steal(imap_servers, imap_server->key);
         g_free(imap_server->key);
         imap_server->key = g_strdup_printf("%s %s", server->user, host);
         g_hash_table_insert(imap_servers, imap_server->key, imap_server);
-        pthread_mutex_unlock(&imap_servers_lock);
+        UNLOCK_SERVERS();
     }
     (parent_class)->set_host(server, host, use_ssl);
 }
@@ -177,7 +192,9 @@ libbalsa_imap_server_init(LibBalsaImapServer * imap_server)
 {
     LIBBALSA_SERVER(imap_server)->protocol = "imap";
     imap_server->key = NULL;
+#if defined(BALSA_USE_THREADS)
     imap_server->lock = g_mutex_new();
+#endif
     imap_server->max_connections = MAX_CONNECTIONS_PER_SERVER;
     imap_server->used_connections = 0;
     imap_server->used_handles = NULL;
@@ -205,17 +222,19 @@ libbalsa_imap_server_finalize(GObject * object)
     server = LIBBALSA_SERVER(object);
     imap_server = LIBBALSA_IMAP_SERVER(object);
 
-    pthread_mutex_lock(&imap_servers_lock);
+    LOCK_SERVERS();
     g_hash_table_remove(imap_servers, imap_server->key);
-    pthread_mutex_unlock(&imap_servers_lock);
+    UNLOCK_SERVERS();
     
     g_source_remove(imap_server->connection_cleanup_id);
 
 #if 0
-    g_mutex_lock(imap_server->lock);
+    LOCK_SERVER(imap_server);
 #endif
     libbalsa_imap_server_force_disconnect(imap_server);
+#if defined(BALSA_USE_THREADS)
     g_mutex_free(imap_server->lock);
+#endif
     g_free(imap_server->key); imap_server->key = NULL;
 
     G_OBJECT_CLASS(parent_class)->finalize(object);
@@ -358,7 +377,7 @@ lb_imap_server_cleanup(LibBalsaImapServer * imap_server)
     /* Quit if there is an action going on, eg. an connection is being
      * opened and the user is asked to confirm the certificate or
      * provide password, etc. */
-    if(!g_mutex_trylock(imap_server->lock))
+    if(!TRYLOCK_SERVER(imap_server))
         return; 
 
     idle_marker = time(NULL) - CONNECTION_CLEANUP_IDLE_TIME;
@@ -395,7 +414,7 @@ lb_imap_server_cleanup(LibBalsaImapServer * imap_server)
         }
     }
 
-    g_mutex_unlock(imap_server->lock);
+    UNLOCK_SERVER(imap_server);
 }
 
 static gboolean connection_cleanup(gpointer ptr)
@@ -426,15 +445,15 @@ static LibBalsaImapServer* get_or_create(const gchar *username,
     gchar *key;
 
     if (!imap_servers) {
-        pthread_mutex_lock(&imap_servers_lock);
+        LOCK_SERVERS();
         if (!imap_servers)
             imap_servers = g_hash_table_new(g_str_hash, g_str_equal);
-        pthread_mutex_unlock(&imap_servers_lock);
+        UNLOCK_SERVERS();
     }
 
     /* lookup username host */
     key = g_strdup_printf("%s %s", username, host);
-    pthread_mutex_lock(&imap_servers_lock);
+    LOCK_SERVERS();
     imap_server = g_hash_table_lookup(imap_servers, key);
     if (!imap_server) {
         imap_server = g_object_new(LIBBALSA_TYPE_IMAP_SERVER, NULL);
@@ -444,7 +463,7 @@ static LibBalsaImapServer* get_or_create(const gchar *username,
         g_free(key);
         g_object_ref(imap_server);
     }
-    pthread_mutex_unlock(&imap_servers_lock);
+    UNLOCK_SERVERS();
     return imap_server;
 }
 
@@ -624,7 +643,7 @@ libbalsa_imap_server_get_handle(LibBalsaImapServer *imap_server, GError **err)
     if (imap_server->offline_mode)
         return NULL;
 
-    g_mutex_lock(imap_server->lock);
+    LOCK_SERVER(imap_server);
     /* look for free connection */
     if (imap_server->free_handles) {
         GList *conn;
@@ -639,15 +658,15 @@ libbalsa_imap_server_get_handle(LibBalsaImapServer *imap_server, GError **err)
     /* create if used < max connections */
     if (!info
         && imap_server->used_connections < imap_server->max_connections) {
-        g_mutex_unlock(imap_server->lock);
+        UNLOCK_SERVER(imap_server);
         info = lb_imap_server_info_new(server);
-        g_mutex_lock(imap_server->lock);
+        LOCK_SERVER(imap_server);
         /* FIXME: after dropping and reacquiring the lock,
          * (imap_server->used_connections < imap_server->max_connections)
          * might no longer be true--do we care?
         if (imap_server->used_connections >= imap_server->max_connections) {
             lb_imap_server_info_free(info);
-            g_mutex_unlock(imap_server->lock);
+            UNLOCK_SERVER(imap_server);
             return NULL;
         }
          */
@@ -658,7 +677,7 @@ libbalsa_imap_server_get_handle(LibBalsaImapServer *imap_server, GError **err)
                                         REQ_SSL(server));
             if(rc != IMAP_SUCCESS) {
                 handle_connection_error(rc, info, server, err);
-                g_mutex_unlock(imap_server->lock);
+                UNLOCK_SERVER(imap_server);
                 return NULL;
             }
         }
@@ -667,7 +686,7 @@ libbalsa_imap_server_get_handle(LibBalsaImapServer *imap_server, GError **err)
                                                    info);
         imap_server->used_connections++;
     }
-    g_mutex_unlock(imap_server->lock);
+    UNLOCK_SERVER(imap_server);
 
     return info ? info->handle : NULL;
 }
@@ -697,7 +716,7 @@ libbalsa_imap_server_get_handle_with_user(LibBalsaImapServer *imap_server,
     if (imap_server->offline_mode)
         return NULL;
 
-    g_mutex_lock(imap_server->lock);
+    LOCK_SERVER(imap_server);
     /* look for free reusable connection */
     if (imap_server->free_handles) {
         GList *conn=NULL;
@@ -717,17 +736,17 @@ libbalsa_imap_server_get_handle_with_user(LibBalsaImapServer *imap_server,
      * those that do not SELECT any mailbox. */
     if (!info
         && imap_server->used_connections < imap_server->max_connections-1) {
-        g_mutex_unlock(imap_server->lock);
+        UNLOCK_SERVER(imap_server);
         info = lb_imap_server_info_new(server);
         if (!info)
             return NULL;
-        g_mutex_lock(imap_server->lock);
+        LOCK_SERVER(imap_server);
         /* FIXME: after dropping and reacquiring the lock,
          * (imap_server->used_connections < imap_server->max_connections)
          * might no longer be true--do we care?
         if (imap_server->used_connections >= imap_server->max_connections) {
             lb_imap_server_info_free(info);
-            g_mutex_unlock(imap_server->lock);
+            UNLOCK_SERVER(imap_server);
             return NULL;
         }
          */
@@ -745,7 +764,7 @@ libbalsa_imap_server_get_handle_with_user(LibBalsaImapServer *imap_server,
                     LIBBALSA_MAILBOX_TOOMANYOPEN_ERROR,
                     _("Exceeded the number of connections per server %s"),
                     server->host);
-        g_mutex_unlock(imap_server->lock);
+        UNLOCK_SERVER(imap_server);
         return NULL;
     }
 
@@ -754,7 +773,7 @@ libbalsa_imap_server_get_handle_with_user(LibBalsaImapServer *imap_server,
                                     REQ_SSL(server));
         if(rc != IMAP_SUCCESS) {
             handle_connection_error(rc, info, server, err);
-            g_mutex_unlock(imap_server->lock);
+            UNLOCK_SERVER(imap_server);
             return NULL;
         }
     }
@@ -763,7 +782,7 @@ libbalsa_imap_server_get_handle_with_user(LibBalsaImapServer *imap_server,
     imap_server->used_handles = g_list_prepend(imap_server->used_handles,
                                                info);
     imap_server->used_connections++;
-    g_mutex_unlock(imap_server->lock);
+    UNLOCK_SERVER(imap_server);
 
     return info->handle;
 }
@@ -783,7 +802,7 @@ void libbalsa_imap_server_release_handle(LibBalsaImapServer *imap_server,
     if (!handle)
         return;
 
-    g_mutex_lock(imap_server->lock);
+    LOCK_SERVER(imap_server);
     /* remove from used list */
     if (imap_server->used_handles) {
         GList *conn;
@@ -800,7 +819,7 @@ void libbalsa_imap_server_release_handle(LibBalsaImapServer *imap_server,
     /* add to free list */
         imap_server->free_handles = g_list_append(imap_server->free_handles,
                                                   info);
-    g_mutex_unlock(imap_server->lock);
+    UNLOCK_SERVER(imap_server);
 }
 
 /**
@@ -845,7 +864,7 @@ libbalsa_imap_server_has_persistent_cache(LibBalsaImapServer *srv)
  **/
 void libbalsa_imap_server_force_disconnect(LibBalsaImapServer *imap_server)
 {
-    g_mutex_lock(imap_server->lock);
+    LOCK_SERVER(imap_server);
     g_list_foreach(imap_server->used_handles,
                    (GFunc) lb_imap_server_info_free, NULL);
     g_list_free(imap_server->used_handles);
@@ -854,7 +873,7 @@ void libbalsa_imap_server_force_disconnect(LibBalsaImapServer *imap_server)
                    (GFunc) lb_imap_server_info_free, NULL);
     g_list_free(imap_server->free_handles);
     imap_server->free_handles = NULL;
-    g_mutex_unlock(imap_server->lock);
+    UNLOCK_SERVER(imap_server);
 }
 
 /**
@@ -874,10 +893,10 @@ static void close_all_connections_cb(gpointer key, gpointer value,
 void
 libbalsa_imap_server_close_all_connections(void)
 {
-    pthread_mutex_lock(&imap_servers_lock);
+    LOCK_SERVERS();
     if (imap_servers)
         g_hash_table_foreach(imap_servers, close_all_connections_cb, NULL);
-    pthread_mutex_unlock(&imap_servers_lock);
+    UNLOCK_SERVERS();
     libbalsa_imap_purge_temp_dir(0);
 }
 
@@ -893,10 +912,10 @@ libbalsa_imap_server_close_all_connections(void)
 gboolean libbalsa_imap_server_has_free_handles(LibBalsaImapServer *imap_server)
 {
     gboolean result;
-    g_mutex_lock(imap_server->lock);
+    LOCK_SERVER(imap_server);
     result = imap_server->used_connections < imap_server->max_connections
         || imap_server->free_handles;
-    g_mutex_unlock(imap_server->lock);
+    UNLOCK_SERVER(imap_server);
     return result;
 }
 
diff --git a/libbalsa/imap/imap_private.h b/libbalsa/imap/imap_private.h
index bdd0467..a0f5771 100644
--- a/libbalsa/imap/imap_private.h
+++ b/libbalsa/imap/imap_private.h
@@ -30,6 +30,7 @@
 #define HANDLE_UNLOCK(h) pthread_mutex_unlock(&h->mutex)
 #else
 #define HANDLE_LOCK(h) 
+#define HANDLE_TRYLOCK(h) 0
 #define HANDLE_UNLOCK(h)
 #endif
 



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