[balsa/gtk3] Always use multithreading, and use only glib



commit 4b2150bbe1d47c07c64343b7097c157eb4b62d0c
Author: Peter Bloomfield <PeterBloomfield bellsouth net>
Date:   Sun Aug 21 14:11:23 2016 -0400

    Always use multithreading, and use only glib
    
        Always use multithreading, and use only the glib abstraction
        instead of "native" pthreads.
    
        * configure.ac: remove --enable-threads configure option and its
        consequences.
        * libbalsa/filter-file.c: remove reference to BALSA_USE_THREADS.
        * libbalsa/gmime-gpgme-signature.c
        (g_mime_gpgme_sigstat_new_from_gpgme_ctx):
        * 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),
        (connection_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):
        * libbalsa/imap/imap-commands.c (imap_mbox_handle_noop),
        (imap_mbox_select), (imap_mbox_examine), (imap_mbox_create),
        (imap_mbox_delete), (imap_mbox_rename), (imap_mbox_subscribe),
        (imap_mbox_list), (imap_mbox_lsub), (imap_mbox_status),
        (imap_mbox_append_multi), (imap_mbox_close), (imap_mbox_expunge),
        (imap_mbox_expunge_a), (imap_mbox_find_all),
        (imap_mbox_find_unseen), (imap_mbox_handle_msgno_has_flags),
        (imap_mbox_handle_fetch_range), (imap_mbox_handle_fetch_set),
        (imap_mbox_handle_fetch_rfc822),
        (imap_mbox_handle_fetch_rfc822_uid), (imap_mbox_handle_fetch_body),
        (imap_mbox_store_flag), (imap_mbox_store_flag_a),
        (imap_mbox_handle_copy), (imap_mbox_unselect), (imap_mbox_thread),
        (imap_mbox_sort_msgno), (imap_mbox_sort_filter),
        (imap_mbox_filter_msgnos), (imap_mbox_complete_msgids),
        (imap_mbox_get_quota), (imap_mbox_get_my_rights),
        (imap_mbox_get_acl):
        * libbalsa/imap/imap-handle.c (imap_mbox_handle_init),
        (async_process), (idle_start), (imap_mbox_handle_connect),
        (imap_mbox_handle_reconnect), (imap_handle_force_disconnect),
        (imap_mbox_handle_get_delim), (imap_mbox_handle_finalize):
        * libbalsa/imap/imap-tls.c (locking_function), (id_function),
        (dyn_create_function), (dyn_lock_function), (dyn_destroy_function),
        (imaptls_thread_setup), (imaptls_thread_cleanup),
        (imap_create_ssl):
        * libbalsa/imap/imap_private.h:
        * libbalsa/imap/imap_search.c (imap_search_exec):
        * libbalsa/information.h:
        * libbalsa/libbalsa-conf.c (lbc_lock), (lbc_unlock),
        (libbalsa_conf_sync), (libbalsa_conf_queue_sync):
        * libbalsa/libbalsa-gpgme-cb.c (lb_gpgme_passphrase),

 ChangeLog                        |  103 ++++++++++++++++++++++++++++
 configure.ac                     |   23 +------
 libbalsa/filter-file.c           |    4 -
 libbalsa/gmime-gpgme-signature.c |   11 ++-
 libbalsa/imap-server.c           |  100 +++++++++------------------
 libbalsa/imap/imap-commands.c    |  138 +++++++++++++++++++-------------------
 libbalsa/imap/imap-handle.c      |   40 +++++------
 libbalsa/imap/imap-tls.c         |   54 ++++-----------
 libbalsa/imap/imap_private.h     |   22 ++-----
 libbalsa/imap/imap_search.c      |    4 +-
 libbalsa/information.h           |   10 +--
 libbalsa/libbalsa-conf.c         |   16 -----
 libbalsa/libbalsa-gpgme-cb.c     |   40 ++++-------
 libbalsa/libbalsa-gpgme.c        |   30 ++++-----
 libbalsa/libbalsa.c              |  100 +++++++++++----------------
 libbalsa/libbalsa.h              |   12 +---
 libbalsa/libbalsa_private.h      |    8 --
 libbalsa/mailbox.c               |  135 ++++++++++---------------------------
 libbalsa/mailbox.h               |   22 +------
 libbalsa/mailbox_imap.c          |    4 -
 libbalsa/mailbox_local.c         |   27 +------
 libbalsa/mailbox_mbox.c          |    9 ---
 libbalsa/mime-stream-shared.c    |    4 -
 libbalsa/mime-stream-shared.h    |   10 ---
 libbalsa/missing_time.c          |   16 +---
 libbalsa/rfc3156.c               |   13 ++--
 libbalsa/send.c                  |  113 +++++--------------------------
 libbalsa/send.h                  |    6 +--
 src/ab-main.c                    |    5 +-
 src/balsa-app.c                  |   36 ++++------
 src/filter-edit-callbacks.c      |    4 -
 src/information-dialog.h         |   10 +--
 src/main-window.c                |  136 +++++++++----------------------------
 src/main.c                       |   51 ++------------
 src/save-restore.c               |    9 ---
 src/sendmsg-window.c             |    6 --
 src/threads.h                    |    5 +-
 37 files changed, 458 insertions(+), 878 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 80a3e62..6640e6e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,106 @@
+2016-08-21  Albrecht Dreß
+
+       Always use multithreading, and use only the glib abstraction
+       instead of "native" pthreads.
+
+       * configure.ac: remove --enable-threads configure option and its
+       consequences.
+       * libbalsa/filter-file.c: remove reference to BALSA_USE_THREADS.
+       * libbalsa/gmime-gpgme-signature.c
+       (g_mime_gpgme_sigstat_new_from_gpgme_ctx):
+       * 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),
+       (connection_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):
+       * libbalsa/imap/imap-commands.c (imap_mbox_handle_noop),
+       (imap_mbox_select), (imap_mbox_examine), (imap_mbox_create),
+       (imap_mbox_delete), (imap_mbox_rename), (imap_mbox_subscribe),
+       (imap_mbox_list), (imap_mbox_lsub), (imap_mbox_status),
+       (imap_mbox_append_multi), (imap_mbox_close), (imap_mbox_expunge),
+       (imap_mbox_expunge_a), (imap_mbox_find_all),
+       (imap_mbox_find_unseen), (imap_mbox_handle_msgno_has_flags),
+       (imap_mbox_handle_fetch_range), (imap_mbox_handle_fetch_set),
+       (imap_mbox_handle_fetch_rfc822),
+       (imap_mbox_handle_fetch_rfc822_uid), (imap_mbox_handle_fetch_body),
+       (imap_mbox_store_flag), (imap_mbox_store_flag_a),
+       (imap_mbox_handle_copy), (imap_mbox_unselect), (imap_mbox_thread),
+       (imap_mbox_sort_msgno), (imap_mbox_sort_filter),
+       (imap_mbox_filter_msgnos), (imap_mbox_complete_msgids),
+       (imap_mbox_get_quota), (imap_mbox_get_my_rights),
+       (imap_mbox_get_acl):
+       * libbalsa/imap/imap-handle.c (imap_mbox_handle_init),
+       (async_process), (idle_start), (imap_mbox_handle_connect),
+       (imap_mbox_handle_reconnect), (imap_handle_force_disconnect),
+       (imap_mbox_handle_get_delim), (imap_mbox_handle_finalize):
+       * libbalsa/imap/imap-tls.c (locking_function), (id_function),
+       (dyn_create_function), (dyn_lock_function), (dyn_destroy_function),
+       (imaptls_thread_setup), (imaptls_thread_cleanup),
+       (imap_create_ssl):
+       * libbalsa/imap/imap_private.h:
+       * libbalsa/imap/imap_search.c (imap_search_exec):
+       * libbalsa/information.h:
+       * libbalsa/libbalsa-conf.c (lbc_lock), (lbc_unlock),
+       (libbalsa_conf_sync), (libbalsa_conf_queue_sync):
+       * libbalsa/libbalsa-gpgme-cb.c (lb_gpgme_passphrase),
+       (get_passphrase_idle):
+       * libbalsa/libbalsa-gpgme.c (g_set_error_from_gpgme):
+       * libbalsa/libbalsa.c (libbalsa_init), (ask_idle), (libbalsa_ask),
+       (libbalsa_certs_destroy), (libbalsa_is_cert_known),
+       (libbalsa_am_i_subthread), (libbalsa_lock_mailbox),
+       (libbalsa_unlock_mailbox):
+       * libbalsa/libbalsa.h:
+       * libbalsa/libbalsa_private.h:
+       * libbalsa/mailbox.c (libbalsa_mailbox_class_init),
+       (lbm_index_entry_populate_from_msg), (lbm_index_entry_new_pending),
+       (lbm_index_entry_free), (libbalsa_mailbox_finalize),
+       (libbalsa_mailbox_check), (libbalsa_mailbox_real_lock_store),
+       (lbm_msgno_changed_expunged_cb), (lbm_msgnos_changed_idle_cb),
+       (lbm_msgno_changed), (lbm_msgno_filt_check_idle_cb),
+       (libbalsa_mailbox_msgno_filt_check), (lbm_cache_message),
+       (libbalsa_mailbox_get_message),
+       (libbalsa_mailbox_messages_change_flags),
+       (lbm_set_threading_idle_cb), (lbm_set_threading),
+       (lbm_get_index_entry_expunged_cb), (lbm_get_index_entry_real),
+       (lbm_get_index_entry), (lbm_check_idle),
+       (libbalsa_mailbox_unlock_store):
+       * libbalsa/mailbox.h:
+       * libbalsa/mailbox_imap.c:
+       * libbalsa/mailbox_local.c (lbm_local_save_tree_idle),
+       (message_match_real), (lbml_set_threading_idle_cb),
+       (libbalsa_mailbox_local_set_threading):
+       * libbalsa/mailbox_mbox.c (libbalsa_mailbox_mbox_class_init),
+       (libbalsa_mailbox_mbox_lock_store):
+       * libbalsa/mime-stream-shared.c
+       (libbalsa_mime_stream_shared_unlock):
+       * libbalsa/mime-stream-shared.h:
+       * libbalsa/missing_time.c (ctime_r):
+       * libbalsa/rfc3156.c (libbalsa_gpgme_sig_stat_to_gchar):
+       * libbalsa/send.c (lbs_process_queue), (handle_successful_send),
+       (libbalsa_smtp_event_cb), (libbalsa_process_queue),
+       (balsa_send_message_real):
+       * libbalsa/send.h:
+       * src/ab-main.c:
+       * src/balsa-app.c (ask_passwd_idle), (ask_password_mt),
+       (ask_password):
+       * src/filter-edit-callbacks.c:
+       * src/information-dialog.h:
+       * src/main-window.c (bw_delete_cb), (balsa_window_new),
+       (bw_real_open_mbnode_thread), (balsa_window_real_open_mbnode),
+       (bw_register_open_mailbox), (bw_unregister_open_mailbox),
+       (bw_is_open_mailbox), (check_new_messages_real),
+       (bw_check_messages_thread):
+       * src/main.c (threads_init), (threads_destroy), (real_main),
+       (balsa_cleanup):
+       * src/save-restore.c (pop3_progress_notify):
+       * src/sendmsg-window.c (balsa_sendmsg_destroy_handler):
+       * src/threads.h: ditto.
+
 2016-07-28  Peter Bloomfield  <pbloomfield bellsouth net>
 
        Implement a GtkComboBox for selecting a LibBalsaIdentity, and
diff --git a/configure.ac b/configure.ac
index 756a02b..02d4861 100644
--- a/configure.ac
+++ b/configure.ac
@@ -60,18 +60,6 @@ if test x$balsa_cv_fcntl = xyes; then
        AC_DEFINE(USE_FCNTL,1,[Define if fcntl locking should be used.])
 fi
 
-AC_ARG_ENABLE(threads,
-        AC_HELP_STRING([--enable-threads],
-                       [Use threading for mail retrieval (default=yes)]),[
-       use_threads=$enableval
-],[
-       use_threads=yes
-])
-
-if test x"$use_threads" = xyes; then 
-        AC_DEFINE(BALSA_USE_THREADS, 1,[Defined when balsa is to use threads.])
-fi
-
 AC_ARG_ENABLE(debug-threads,
         AC_HELP_STRING([--enable-debug-threads],
                        [Debug threads (default=no)]),[
@@ -409,13 +397,9 @@ if test x"$gpgmecfg" != xno ; then
                have_smime=no
        fi
        if test x"$gpgmecfg" != xno ; then
-               if test x"$use_threads" = xyes; then
-                       BALSA_LIBS="$BALSA_LIBS `$gpgmecfg --thread=pthread --libs`"
-                       if test x"$?" != x0 ; then
-                               AC_MSG_ERROR([*** You enabled gpgme and pthread support, but your gpgme 
installation does not support threads.])
-                       fi
-               else
-                       BALSA_LIBS="$BALSA_LIBS `$gpgmecfg --libs`"
+               BALSA_LIBS="$BALSA_LIBS `$gpgmecfg --thread=pthread --libs`"
+               if test x"$?" != x0 ; then
+                       AC_MSG_ERROR([*** You enabled gpgme support, but your gpgme installation does not 
support threads.])
                fi
                AC_DEFINE(HAVE_GPGME,1,[Defined when gpgme is available.])
                BALSA_CFLAGS="$BALSA_CFLAGS `$gpgmecfg --cflags`"
@@ -889,7 +873,6 @@ dnl ##########################################################################
 echo ""
 echo "================ Final configuration ==================="
 echo "    Installing into prefix: $prefix"
-echo "      Using multithreading: $use_threads"
 echo "   Enable compile warnings: $set_more_warnings"
 echo "                Use ESMTP : $with_esmtp"
 echo "               HTML widget: $use_html_widget"
diff --git a/libbalsa/filter-file.c b/libbalsa/filter-file.c
index cdb8849..d773779 100644
--- a/libbalsa/filter-file.c
+++ b/libbalsa/filter-file.c
@@ -34,10 +34,6 @@
 
 #include <stdlib.h>
 #include <stdio.h>
-/* yellow dog has crappy libc and needs pthread.h to be included here */
-#ifdef BALSA_USE_THREADS
-#  include <pthread.h>
-#endif
 #include <string.h>
 #include <time.h>
 #include <sys/types.h>
diff --git a/libbalsa/gmime-gpgme-signature.c b/libbalsa/gmime-gpgme-signature.c
index 822c303..5c47d7b 100644
--- a/libbalsa/gmime-gpgme-signature.c
+++ b/libbalsa/gmime-gpgme-signature.c
@@ -106,10 +106,13 @@ g_mime_gpgme_sigstat_new_from_gpgme_ctx(gpgme_ctx_t ctx)
 
     /* try to get the related key */
     err = gpgme_get_key(ctx, sig_stat->fingerprint, &sig_stat->key, 0);
-    if (err != GPG_ERR_NO_ERROR)
-          g_message("could not retrieve the key with fingerprint %s: %s: %s",
-                    sig_stat->fingerprint, gpgme_strsource(err),
-                    gpgme_strerror(err));
+    if (err != GPG_ERR_NO_ERROR) {
+       gchar errbuf[4096];             /* should be large enough... */
+
+       gpgme_strerror_r(err, errbuf, sizeof(errbuf));
+       g_message("could not retrieve the key with fingerprint %s: %s: %s",
+               sig_stat->fingerprint, gpgme_strsource(err), errbuf);
+    }
 
     return sig_stat;
 }
diff --git a/libbalsa/imap-server.c b/libbalsa/imap-server.c
index f724e2d..40a62c6 100644
--- a/libbalsa/imap-server.c
+++ b/libbalsa/imap-server.c
@@ -31,7 +31,6 @@
 
 #include <string.h>
 #include <stdlib.h>
-#include <pthread.h>
 
 #if defined(HAVE_LIBSECRET)
 #include <libsecret/secret.h>
@@ -62,9 +61,7 @@ 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;
@@ -96,20 +93,7 @@ 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 GMutex imap_servers_lock;
 static GHashTable *imap_servers = NULL;
 
 struct handle_info {
@@ -160,12 +144,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);
         
-        LOCK_SERVERS();
+        g_mutex_lock(&imap_servers_lock);
         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);
-        UNLOCK_SERVERS();
+        g_mutex_unlock(&imap_servers_lock);
     }
     (parent_class)->set_username(server, name);
 }
@@ -175,12 +159,12 @@ libbalsa_imap_server_set_host(LibBalsaServer * server,
 {
     if(server->user && host) { /* we have been initialized... */
         LibBalsaImapServer *imap_server = LIBBALSA_IMAP_SERVER(server);
-        LOCK_SERVERS();
+        g_mutex_lock(&imap_servers_lock);
         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);
-        UNLOCK_SERVERS();
+        g_mutex_unlock(&imap_servers_lock);
     }
     (parent_class)->set_host(server, host, use_ssl);
 }
@@ -209,9 +193,7 @@ libbalsa_imap_server_init(LibBalsaImapServer * imap_server)
 {
     LIBBALSA_SERVER(imap_server)->protocol = "imap";
     imap_server->key = NULL;
-#if defined(BALSA_USE_THREADS)
     g_mutex_init(&imap_server->lock);
-#endif
     imap_server->max_connections = MAX_CONNECTIONS_PER_SERVER;
     imap_server->used_connections = 0;
     imap_server->used_handles = NULL;
@@ -233,19 +215,14 @@ libbalsa_imap_server_finalize(GObject * object)
 
     imap_server = LIBBALSA_IMAP_SERVER(object);
 
-    LOCK_SERVERS();
+    g_mutex_lock(&imap_servers_lock);
     g_hash_table_remove(imap_servers, imap_server->key);
-    UNLOCK_SERVERS();
+    g_mutex_unlock(&imap_servers_lock);
 
     g_source_remove(imap_server->connection_cleanup_id);
 
-#if 0
-    LOCK_SERVER(imap_server);
-#endif
     libbalsa_imap_server_force_disconnect(imap_server);
-#if defined(BALSA_USE_THREADS)
     g_mutex_clear(&imap_server->lock);
-#endif
     g_free(imap_server->key); imap_server->key = NULL;
 
     G_OBJECT_CLASS(parent_class)->finalize(object);
@@ -390,7 +367,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(!TRYLOCK_SERVER(imap_server))
+    if(!g_mutex_trylock(&imap_server->lock))
         return; 
 
     idle_marker = time(NULL) - CONNECTION_CLEANUP_IDLE_TIME;
@@ -427,26 +404,17 @@ lb_imap_server_cleanup(LibBalsaImapServer * imap_server)
         }
     }
 
-    UNLOCK_SERVER(imap_server);
+    g_mutex_unlock(&imap_server->lock);
 }
 
 static gboolean connection_cleanup(gpointer ptr)
 {
-#if 0 && defined(BALSA_USE_THREADS)
-    pthread_t cleanup_thread;
-#endif                          /*BALSA_USE_THREADS */
     LibBalsaImapServer *imap_server;
 
     g_return_val_if_fail(LIBBALSA_IS_IMAP_SERVER(ptr), FALSE);
 
     imap_server = LIBBALSA_IMAP_SERVER(ptr);
-#if 0 && defined(BALSA_USE_THREADS)
-    pthread_create(&cleanup_thread, NULL,
-                   (void *) lb_imap_server_cleanup, imap_server);
-    pthread_detach(cleanup_thread);
-#else                           /*BALSA_USE_THREADS */
     lb_imap_server_cleanup(imap_server);
-#endif                          /*BALSA_USE_THREADS */
 
     return TRUE;
 }
@@ -458,15 +426,15 @@ static LibBalsaImapServer* get_or_create(const gchar *username,
     gchar *key;
 
     if (!imap_servers) {
-        LOCK_SERVERS();
+       g_mutex_lock(&imap_servers_lock);
         if (!imap_servers)
             imap_servers = g_hash_table_new(g_str_hash, g_str_equal);
-        UNLOCK_SERVERS();
+        g_mutex_unlock(&imap_servers_lock);
     }
 
     /* lookup username@host */
     key = g_strdup_printf("%s@%s", username, host);
-    LOCK_SERVERS();
+    g_mutex_lock(&imap_servers_lock);
     imap_server = g_hash_table_lookup(imap_servers, key);
     if (!imap_server) {
         imap_server = g_object_new(LIBBALSA_TYPE_IMAP_SERVER, NULL);
@@ -476,7 +444,7 @@ static LibBalsaImapServer* get_or_create(const gchar *username,
         g_free(key);
         g_object_ref(imap_server);
     }
-    UNLOCK_SERVERS();
+    g_mutex_unlock(&imap_servers_lock);
     return imap_server;
 }
 
@@ -695,7 +663,7 @@ libbalsa_imap_server_get_handle(LibBalsaImapServer *imap_server, GError **err)
     if (!imap_server || imap_server->offline_mode)
         return NULL;
 
-    LOCK_SERVER(imap_server);
+    g_mutex_lock(&imap_server->lock);
     /* look for free connection */
     if (imap_server->free_handles) {
         GList *conn;
@@ -710,15 +678,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) {
-        UNLOCK_SERVER(imap_server);
+       g_mutex_unlock(&imap_server->lock);
         info = lb_imap_server_info_new(server);
-        LOCK_SERVER(imap_server);
+        g_mutex_lock(&imap_server->lock);
         /* 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);
-            UNLOCK_SERVER(imap_server);
+            g_mutex_unlock(&imap_server->lock);
             return NULL;
         }
          */
@@ -731,7 +699,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);
-                UNLOCK_SERVER(imap_server);
+                g_mutex_unlock(&imap_server->lock);
                 return NULL;
             }
         }
@@ -740,7 +708,7 @@ libbalsa_imap_server_get_handle(LibBalsaImapServer *imap_server, GError **err)
                                                    info);
         imap_server->used_connections++;
     }
-    UNLOCK_SERVER(imap_server);
+    g_mutex_unlock(&imap_server->lock);
 
     /* cppcheck-suppress nullPointer */
     return info ? info->handle : NULL;
@@ -770,7 +738,7 @@ libbalsa_imap_server_get_handle_with_user(LibBalsaImapServer *imap_server,
     if (imap_server->offline_mode)
         return NULL;
 
-    LOCK_SERVER(imap_server);
+    g_mutex_lock(&imap_server->lock);
     /* look for free reusable connection */
     if (imap_server->free_handles) {
         GList *conn=NULL;
@@ -790,17 +758,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) {
-        UNLOCK_SERVER(imap_server);
+       g_mutex_unlock(&imap_server->lock);
         info = lb_imap_server_info_new(server);
         if (!info)
             return NULL;
-        LOCK_SERVER(imap_server);
+        g_mutex_lock(&imap_server->lock);
         /* 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);
-            UNLOCK_SERVER(imap_server);
+            g_mutex_unlock(&imap_server->lock);
             return NULL;
         }
          */
@@ -818,7 +786,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);
-        UNLOCK_SERVER(imap_server);
+        g_mutex_unlock(&imap_server->lock);
         return NULL;
     }
 
@@ -829,7 +797,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);
-            UNLOCK_SERVER(imap_server);
+            g_mutex_unlock(&imap_server->lock);
             return NULL;
         }
     }
@@ -838,7 +806,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++;
-    UNLOCK_SERVER(imap_server);
+    g_mutex_unlock(&imap_server->lock);
 
     return info->handle;
 }
@@ -858,7 +826,7 @@ void libbalsa_imap_server_release_handle(LibBalsaImapServer *imap_server,
     if (!handle)
         return;
 
-    LOCK_SERVER(imap_server);
+    g_mutex_lock(&imap_server->lock);
     /* remove from used list */
     if (imap_server->used_handles) {
         GList *conn;
@@ -875,7 +843,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);
-    UNLOCK_SERVER(imap_server);
+    g_mutex_unlock(&imap_server->lock);
 }
 
 /**
@@ -921,7 +889,7 @@ libbalsa_imap_server_has_persistent_cache(LibBalsaImapServer *srv)
 void
 libbalsa_imap_server_force_disconnect(LibBalsaImapServer *imap_server)
 {
-    LOCK_SERVER(imap_server);
+       g_mutex_lock(&imap_server->lock);
     g_list_foreach(imap_server->used_handles,
                    (GFunc) lb_imap_server_info_free, NULL);
     g_list_free(imap_server->used_handles);
@@ -930,7 +898,7 @@ 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;
-    UNLOCK_SERVER(imap_server);
+    g_mutex_unlock(&imap_server->lock);
 }
 
 /**
@@ -950,10 +918,10 @@ static void close_all_connections_cb(gpointer key, gpointer value,
 void
 libbalsa_imap_server_close_all_connections(void)
 {
-    LOCK_SERVERS();
+       g_mutex_lock(&imap_servers_lock);
     if (imap_servers)
         g_hash_table_foreach(imap_servers, close_all_connections_cb, NULL);
-    UNLOCK_SERVERS();
+    g_mutex_unlock(&imap_servers_lock);
     libbalsa_imap_purge_temp_dir(0);
 }
 
@@ -969,10 +937,10 @@ libbalsa_imap_server_close_all_connections(void)
 gboolean libbalsa_imap_server_has_free_handles(LibBalsaImapServer *imap_server)
 {
     gboolean result;
-    LOCK_SERVER(imap_server);
+    g_mutex_lock(&imap_server->lock);
     result = imap_server->used_connections < imap_server->max_connections
         || imap_server->free_handles;
-    UNLOCK_SERVER(imap_server);
+    g_mutex_unlock(&imap_server->lock);
     return result;
 }
 
diff --git a/libbalsa/imap/imap-commands.c b/libbalsa/imap/imap-commands.c
index 24e7ec9..2eda0dc 100644
--- a/libbalsa/imap/imap-commands.c
+++ b/libbalsa/imap/imap-commands.c
@@ -152,11 +152,11 @@ ImapResponse
 imap_mbox_handle_noop(ImapMboxHandle *handle)
 {
   ImapResponse rc;
-  if(HANDLE_TRYLOCK(handle) != 0) return IMR_OK;
+  if(!g_mutex_trylock(&handle->mutex)) return IMR_OK;
   IMAP_REQUIRED_STATE3(handle, IMHS_CONNECTED, IMHS_AUTHENTICATED,
                        IMHS_SELECTED, IMR_BAD);
   rc = imap_cmd_exec(handle, "NOOP");
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -244,9 +244,9 @@ imap_mbox_select(ImapMboxHandle* handle, const char *mbox,
                  gboolean *readonly_mbox)
 {
   ImapResponse rc;
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   rc = imap_mbox_select_unlocked(handle, mbox, readonly_mbox);
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -254,7 +254,7 @@ imap_mbox_select(ImapMboxHandle* handle, const char *mbox,
 ImapResponse
 imap_mbox_examine(ImapMboxHandle* handle, const char* mbox)
 {
-  HANDLE_LOCK(handle);
+       g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE3(handle, IMHS_CONNECTED, IMHS_AUTHENTICATED,
                        IMHS_SELECTED, IMR_BAD);
   {
@@ -268,7 +268,7 @@ imap_mbox_examine(ImapMboxHandle* handle, const char* mbox)
     handle->state = IMHS_SELECTED;
     handle->has_rights = 0;
   } 
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
   }
 }
@@ -281,14 +281,14 @@ imap_mbox_create(ImapMboxHandle* handle, const char* mbox)
   gchar *mbx7, *cmd;
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle, IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
 
   mbx7 = imap_utf8_to_mailbox(mbox);
   cmd = g_strdup_printf("CREATE \"%s\"", mbx7);
   rc = imap_cmd_exec(handle, cmd);
   g_free(mbx7); g_free(cmd);
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
 
   return rc;
 }
@@ -301,14 +301,14 @@ imap_mbox_delete(ImapMboxHandle* handle, const char* mbox)
   gchar *mbx7, *cmd;
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle,IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
 
   mbx7 = imap_utf8_to_mailbox(mbox);
   cmd = g_strdup_printf("DELETE \"%s\"", mbx7);
   rc = imap_cmd_exec(handle, cmd);
   g_free(cmd);
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
 
   return rc;
 }
@@ -323,7 +323,7 @@ imap_mbox_rename(ImapMboxHandle* handle,
   gchar *mbx7o, *mbx7n, *cmd;  
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle,IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
 
   mbx7o = imap_utf8_to_mailbox(old_mbox);
@@ -333,7 +333,7 @@ imap_mbox_rename(ImapMboxHandle* handle,
   rc = imap_cmd_exec(handle, cmd);
   g_free(cmd);
 
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -347,7 +347,7 @@ imap_mbox_subscribe(ImapMboxHandle* handle,
   gchar *mbx7, *cmd;
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle,IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
 
   mbx7 = imap_utf8_to_mailbox(mbox);
@@ -357,7 +357,7 @@ imap_mbox_subscribe(ImapMboxHandle* handle,
   rc = imap_cmd_exec(handle, cmd);
   g_free(mbx7); g_free(cmd);
 
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -369,7 +369,7 @@ imap_mbox_list(ImapMboxHandle *handle, const char* what)
   gchar *mbx7, *cmd;
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle,IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
   mbx7 = imap_utf8_to_mailbox(what);
   cmd = g_strdup_printf("LIST \"%s\" \"%%\"", mbx7);
@@ -377,7 +377,7 @@ imap_mbox_list(ImapMboxHandle *handle, const char* what)
   g_free(cmd);
   g_free(mbx7);
 
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -389,7 +389,7 @@ imap_mbox_lsub(ImapMboxHandle *handle, const char* what)
   gchar *mbx7, *cmd;
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle,IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
 
   mbx7 = imap_utf8_to_mailbox(what);
@@ -397,7 +397,7 @@ imap_mbox_lsub(ImapMboxHandle *handle, const char* what)
   rc = imap_cmd_exec(handle, cmd);
   g_free(mbx7); g_free(cmd);
 
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -425,11 +425,11 @@ imap_mbox_status(ImapMboxHandle *r, const char*what,
     gchar *mbx7 = imap_utf8_to_mailbox(what);
     gchar *items = g_strjoinv(" ", (gchar**)&item_arr[0]);
     gchar *cmd = g_strdup_printf("STATUS \"%s\" (%s)", mbx7, items);
-    HANDLE_LOCK(r);
+    g_mutex_lock(&r->mutex);
     g_hash_table_insert(r->status_resps, (gpointer)what, res);
     rc = imap_cmd_exec(r, cmd);
     g_hash_table_remove(r->status_resps, what);
-    HANDLE_UNLOCK(r);
+    g_mutex_unlock(&r->mutex);
     g_free(mbx7); g_free(cmd);
     g_free(items);
   }
@@ -668,10 +668,10 @@ imap_mbox_append_multi(ImapMboxHandle *handle,
 {
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle,IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
   rc = imap_mbox_append_multi_real(handle, mbox, dump_cb, cb_arg, uids);
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -725,13 +725,13 @@ ImapResponse
 imap_mbox_close(ImapMboxHandle *h)
 {
   ImapResponse rc;
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
 
   rc = imap_cmd_exec(h, "CLOSE");  
   if(rc == IMR_OK)
     h->state = IMHS_AUTHENTICATED;
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return rc;
 }
 
@@ -741,10 +741,10 @@ imap_mbox_expunge(ImapMboxHandle *handle)
 {
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
   rc = imap_cmd_exec(handle, "EXPUNGE");
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -752,13 +752,13 @@ ImapResponse
 imap_mbox_expunge_a(ImapMboxHandle *handle)
 {
   ImapResponse rc;
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
   /* extra care would be required to use this once since no other
      commands that use sequence numbers can be issued before this one
      finishes... */
   rc = imap_cmd_issue(handle, "EXPUNGE");
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -817,7 +817,7 @@ imap_mbox_find_all(ImapMboxHandle * h,
   gchar *cmd;
   ImapResponse rc;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
   filter_str = mbox_view_get_str(&h->mbox_view);
   cmd = g_strdup_printf("SEARCH ALL (SUBJECT \"%s\"%s%s)", search_str,
@@ -825,7 +825,7 @@ imap_mbox_find_all(ImapMboxHandle * h,
   rc = imap_mbox_find_helper(h, cmd, msgcnt, msgs);
   g_free(cmd);
 
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return rc;
 }
 
@@ -836,10 +836,10 @@ imap_mbox_find_unseen(ImapMboxHandle * h,
 {
     ImapResponse rc;
 
-    HANDLE_LOCK(h);
+    g_mutex_lock(&h->mutex);
     IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
     rc = imap_mbox_find_helper(h, "SEARCH UNSEEN UNDELETED", msgcnt, msgs);
-    HANDLE_UNLOCK(h);
+    g_mutex_unlock(&h->mutex);
     return rc;
 }
 
@@ -971,7 +971,7 @@ imap_mbox_handle_msgno_has_flags(ImapMboxHandle *h, unsigned msgno,
   ImapMsgFlag needed_flags;
   gboolean retval;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
 
   flags = &g_array_index(h->flag_cache, ImapFlagCache, msgno-1);
@@ -982,7 +982,7 @@ imap_mbox_handle_msgno_has_flags(ImapMboxHandle *h, unsigned msgno,
     (!needed_flags||imap_assure_needed_flags(h, needed_flags) == IMR_OK) &&
     (flags->flag_values & flag_set) == flag_set &&
     (flags->flag_values & flag_unset) == 0;
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
 
   return retval;
 }
@@ -1063,7 +1063,7 @@ imap_mbox_handle_fetch_range(ImapMboxHandle* handle,
   if(lo>hi) return IMR_OK;
   if(lo<1) lo = 1;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
 
   cf = (ImapCoalesceFunc)(mbox_view_is_active(&handle->mbox_view)
@@ -1081,7 +1081,7 @@ imap_mbox_handle_fetch_range(ImapMboxHandle* handle,
     g_free(seq);
   } else rc = IMR_OK;
 
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -1118,9 +1118,9 @@ imap_mbox_handle_fetch_set(ImapMboxHandle* handle,
                            unsigned *set, unsigned cnt, ImapFetchType ift)
 {
   ImapResponse rc;
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   rc = imap_mbox_handle_fetch_set_unlocked(handle, set, cnt, ift);
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -1202,7 +1202,7 @@ imap_mbox_handle_fetch_rfc822(ImapMboxHandle* handle,
   ImapResponse rc = IMR_OK;
   gchar *seq;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
 
   seq = imap_coalesce_set(cnt, set);
@@ -1232,7 +1232,7 @@ imap_mbox_handle_fetch_rfc822(ImapMboxHandle* handle,
       imap_mbox_handle_set_msg(handle, "Unordered data received from server");
     }
   }
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
 
   return rc;
 }
@@ -1332,7 +1332,7 @@ imap_mbox_handle_fetch_rfc822_uid(ImapMboxHandle* handle, unsigned uid,
   struct FetchBodyHeaderText separate_arg;
 
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
 
   /* Consider switching between BODY.PEEK[HEADER] BODY.PEEK[TEXT] and
@@ -1359,7 +1359,7 @@ imap_mbox_handle_fetch_rfc822_uid(ImapMboxHandle* handle, unsigned uid,
 
   handle->body_cb  = cb;
   handle->body_arg = arg;
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -1404,7 +1404,7 @@ imap_mbox_handle_fetch_body(ImapMboxHandle* handle,
   const gchar *peek_string = peek_only ? ".PEEK" : "";
   struct PassHeaderTextOrdered pass_ordered_data;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
   fcb = handle->body_cb;
   farg = handle->body_arg;
@@ -1426,7 +1426,7 @@ imap_mbox_handle_fetch_body(ImapMboxHandle* handle,
     if(rc != IMR_NO) { /* unknown-cte */
       handle->body_cb  = fcb;
       handle->body_arg = farg;
-      HANDLE_UNLOCK(handle);
+      g_mutex_unlock(&handle->mutex);
       return rc;
     }
   }
@@ -1465,7 +1465,7 @@ imap_mbox_handle_fetch_body(ImapMboxHandle* handle,
   handle->body_cb  = fcb;
   handle->body_arg = farg;
 
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -1537,14 +1537,14 @@ imap_mbox_store_flag(ImapMboxHandle *h, unsigned msgcnt, unsigned*seqno,
   ImapResponse res;
   gchar* cmd;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
   cmd = imap_store_prepare(h, msgcnt, seqno, flg, state);
   if(cmd) {
     res = imap_cmd_exec(h, cmd);
     g_free(cmd);
   } res = IMR_OK;
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return res;
 }
 
@@ -1555,7 +1555,7 @@ imap_mbox_store_flag_a(ImapMboxHandle *h, unsigned msgcnt, unsigned*seqno,
   gchar* cmd;
   ImapResponse rc;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
   cmd = imap_store_prepare(h, msgcnt, seqno, flg, state);
   if(cmd) {
@@ -1565,7 +1565,7 @@ imap_mbox_store_flag_a(ImapMboxHandle *h, unsigned msgcnt, unsigned*seqno,
   } else /* no action to be done, perhaps message has the flag set already? */
     rc = IMR_OK;
   
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return rc;
 }
 
@@ -1580,7 +1580,7 @@ imap_mbox_handle_copy(ImapMboxHandle* handle, unsigned cnt, unsigned *seqno,
 {
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
   {
     gchar *mbx7 = imap_utf8_to_mailbox(dest);
@@ -1608,7 +1608,7 @@ imap_mbox_handle_copy(ImapMboxHandle* handle, unsigned cnt, unsigned *seqno,
       handle->uidplus.store_response = 0;
     }
   }
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -1642,7 +1642,7 @@ imap_mbox_unselect(ImapMboxHandle *h)
 {
   ImapResponse rc;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
   if(imap_mbox_handle_can_do(h, IMCAP_UNSELECT)) {
     rc = imap_cmd_exec(h, "UNSELECT");  
@@ -1651,7 +1651,7 @@ imap_mbox_unselect(ImapMboxHandle *h)
   } else 
     rc = IMR_OK;
 
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return rc;
 }
 
@@ -1664,7 +1664,7 @@ imap_mbox_thread(ImapMboxHandle *h, const char *how, ImapSearchKey *filter)
 {
   ImapResponse rc = IMR_NO;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
 
   if(imap_mbox_handle_can_do(h, IMCAP_THREAD_REFERENCES)) {
@@ -1691,7 +1691,7 @@ imap_mbox_thread(ImapMboxHandle *h, const char *how, ImapSearchKey *filter)
     imap_handle_idle_enable(h, 30);
   }
  exit_cleanup:
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
 
   return rc;
 }
@@ -1917,7 +1917,7 @@ imap_mbox_sort_msgno(ImapMboxHandle *handle, ImapSortKey key,
 {
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
   if(imap_mbox_handle_can_do(handle, IMCAP_SORT)) 
     rc = imap_mbox_sort_msgno_srv(handle, key, ascending, msgno, cnt);
@@ -1926,7 +1926,7 @@ imap_mbox_sort_msgno(ImapMboxHandle *handle, ImapSortKey key,
       ? imap_mbox_sort_msgno_client(handle, key, ascending, msgno, cnt)
       : IMR_NO;
   }
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -1953,7 +1953,7 @@ imap_mbox_sort_filter(ImapMboxHandle *handle, ImapSortKey key, int ascending,
   unsigned i;
 
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
   if(key == IMSO_MSGNO) {
     if(filter) { /* CASE 1a */
@@ -2037,7 +2037,7 @@ imap_mbox_sort_filter(ImapMboxHandle *handle, ImapSortKey key, int ascending,
                          GUINT_TO_POINTER(handle->mbox_view.arr[i]));
   }
  cleanup:
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
 
   return rc;
 }
@@ -2056,10 +2056,10 @@ imap_mbox_filter_msgnos(ImapMboxHandle *handle, ImapSearchKey *filter,
                        GHashTable *msgnos)
 {
   ImapResponse res;
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   res = imap_search_exec_unlocked(handle, FALSE, filter,
                                  (ImapSearchCb)make_msgno_table, msgnos);
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return res;
 }
 
@@ -2098,7 +2098,7 @@ imap_mbox_complete_msgids(ImapMboxHandle *h,
   ImapFetchBodyInternalCb cb;
   void *arg;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   IMAP_REQUIRED_STATE1(h, IMHS_SELECTED, IMR_BAD);
   seq = imap_coalesce_seq_range(first_seqno_to_fetch, msgids->len,
                                (ImapCoalesceFunc)need_msgid, msgids);
@@ -2115,7 +2115,7 @@ imap_mbox_complete_msgids(ImapMboxHandle *h,
     g_free(cmd);
   }
 
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return rc;
 }
 
@@ -2127,7 +2127,7 @@ imap_mbox_get_quota(ImapMboxHandle* handle, const char* mbox,
   if (!imap_mbox_handle_can_do(handle, IMCAP_QUOTA))
     return IMR_NO;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE2(handle, IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
   {
   gchar *mbx7 = imap_utf8_to_mailbox(mbox);
@@ -2138,7 +2138,7 @@ imap_mbox_get_quota(ImapMboxHandle* handle, const char* mbox,
   g_free(mbx7); g_free(cmd);
   *max = handle->quota_max_k;
   *used = handle->quota_used_k;
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
   }
 }
@@ -2165,7 +2165,7 @@ imap_mbox_get_my_rights(ImapMboxHandle* handle, ImapAclType* my_rights,
 {
   ImapResponse rc;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   IMAP_REQUIRED_STATE1(handle, IMHS_SELECTED, IMR_BAD);
 
   if (force_update || !handle->has_rights) {
@@ -2176,7 +2176,7 @@ imap_mbox_get_my_rights(ImapMboxHandle* handle, ImapAclType* my_rights,
 
   *my_rights = handle->rights;
   
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
 }
 
@@ -2187,7 +2187,7 @@ imap_mbox_get_acl(ImapMboxHandle* handle, const char* mbox, GList** acls)
   if (!imap_mbox_handle_can_do(handle, IMCAP_ACL))
     return IMR_NO;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
 
   IMAP_REQUIRED_STATE2(handle, IMHS_AUTHENTICATED, IMHS_SELECTED, IMR_BAD);
   {
@@ -2201,7 +2201,7 @@ imap_mbox_get_acl(ImapMboxHandle* handle, const char* mbox, GList** acls)
   g_list_free(*acls);
   *acls = handle->acls;
   handle->acls = NULL;
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return rc;
   }
 }
diff --git a/libbalsa/imap/imap-handle.c b/libbalsa/imap/imap-handle.c
index b38c86c..6209325 100644
--- a/libbalsa/imap/imap-handle.c
+++ b/libbalsa/imap/imap-handle.c
@@ -165,9 +165,7 @@ imap_mbox_handle_init(ImapMboxHandle *handle)
   handle->has_rights = 0;
   mbox_view_init(&handle->mbox_view);
 
-#if defined(BALSA_USE_THREADS)
-  pthread_mutex_init(&handle->mutex, NULL);
-#endif
+  g_mutex_init(&handle->mutex);
 }
 
 static void
@@ -414,22 +412,22 @@ async_process(GIOChannel *source, GIOCondition condition, gpointer data)
   g_return_val_if_fail(h, FALSE);
 
   if(ASYNC_DEBUG) printf("async_process() ENTER\n");
-  if(HANDLE_TRYLOCK(h) != 0) 
+  if(!g_mutex_trylock(&h->mutex))
     return FALSE;/* async data on already locked handle? Don't try again. */
   if(ASYNC_DEBUG) printf("async_process() LOCKED\n");
   if(h->state == IMHS_DISCONNECTED) {
     if(ASYNC_DEBUG) printf("async_process() on disconnected\n");
-    HANDLE_UNLOCK(h);
+    g_mutex_unlock(&h->mutex);
     return FALSE;
   }
   if( (condition & G_IO_HUP) == G_IO_HUP) {
       imap_handle_disconnect(h);
-      HANDLE_UNLOCK(h);
+      g_mutex_unlock(&h->mutex);
       return FALSE;
   }
   retval_async = async_process_real(h);
 
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return retval_async;
 }
 
@@ -442,7 +440,7 @@ idle_start(gpointer data)
 
   /* The test below can probably be weaker since it is ok for the
      channel to get disconnected before IDLE gets activated */
-  if(HANDLE_TRYLOCK(h) != 0)
+  if(!g_mutex_trylock(&h->mutex))
     return TRUE;/* Don't block, just try again later. */
   IMAP_REQUIRED_STATE3(h, IMHS_CONNECTED, IMHS_AUTHENTICATED,
                        IMHS_SELECTED, FALSE);
@@ -460,7 +458,7 @@ idle_start(gpointer data)
   h->idle_enable_id = 0;
   h->idle_state = IDLE_RESPONSE_PENDING;
 
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return FALSE;
 }
 
@@ -587,7 +585,7 @@ imap_mbox_handle_connect(ImapMboxHandle* ret, const char *host, int over_ssl)
 
   g_return_val_if_fail(imap_mbox_is_disconnected(ret), IMAP_CONNECT_FAILED);
 
-  HANDLE_LOCK(ret);
+  g_mutex_lock(&ret->mutex);
   ret->over_ssl = over_ssl;
 
   g_free(ret->host);   ret->host   = g_strdup(host);
@@ -601,7 +599,7 @@ imap_mbox_handle_connect(ImapMboxHandle* ret, const char *host, int over_ssl)
     }
   }
 
-  HANDLE_UNLOCK(ret);
+  g_mutex_unlock(&ret->mutex);
 
   return rc;
 }
@@ -631,7 +629,7 @@ imap_mbox_handle_reconnect(ImapMboxHandle* h, gboolean *readonly)
 {
   ImapResult rc;
   
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
 
   if( (rc=imap_mbox_connect(h)) == IMAP_SUCCESS) {
     if( (rc = imap_authenticate(h)) == IMAP_SUCCESS) {
@@ -654,7 +652,7 @@ imap_mbox_handle_reconnect(ImapMboxHandle* h, gboolean *readonly)
 
     }
   }
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
   return rc;
 }
 
@@ -663,9 +661,9 @@ imap_mbox_handle_reconnect(ImapMboxHandle* h, gboolean *readonly)
 void
 imap_handle_force_disconnect(ImapMboxHandle *h)
 {
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   imap_handle_disconnect(h);
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
 }
 
 ImapTlsMode
@@ -947,7 +945,7 @@ imap_mbox_handle_get_delim(ImapMboxHandle* handle,
   guint handler_id;
   gchar * cmd, *mbx7;
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   /* FIXME: block other list response signals here? */
   handler_id = g_signal_connect(G_OBJECT(handle), "list-response",
                                G_CALLBACK(get_delim),
@@ -959,7 +957,7 @@ imap_mbox_handle_get_delim(ImapMboxHandle* handle,
   imap_cmd_exec(handle, cmd); /* ignore return code.. */
   g_free(cmd);
   g_signal_handler_disconnect(G_OBJECT(handle), handler_id);
-  HANDLE_UNLOCK(handle);
+  g_mutex_unlock(&handle->mutex);
   return delim;
 
 }
@@ -984,7 +982,7 @@ imap_mbox_handle_finalize(GObject* gobject)
   ImapMboxHandle* handle = IMAP_MBOX_HANDLE(gobject);
   g_return_if_fail(handle);
 
-  HANDLE_LOCK(handle);
+  g_mutex_lock(&handle->mutex);
   if(handle->state != IMHS_DISCONNECTED) {
     handle->doing_logout = TRUE;
     imap_cmd_exec(handle, "LOGOUT");
@@ -1006,10 +1004,8 @@ imap_mbox_handle_finalize(GObject* gobject)
   g_list_free(handle->acls); handle->acls = NULL;
   g_free(handle->quota_root); handle->quota_root = NULL;
 
-  HANDLE_UNLOCK(handle);
-#if defined(BALSA_USE_THREADS)
-  pthread_mutex_destroy(&handle->mutex);
-#endif
+  g_mutex_unlock(&handle->mutex);
+  g_mutex_clear(&handle->mutex);
 
   G_OBJECT_CLASS(parent_class)->finalize(gobject);  
 }
diff --git a/libbalsa/imap/imap-tls.c b/libbalsa/imap/imap-tls.c
index ca994ae..629f6df 100644
--- a/libbalsa/imap/imap-tls.c
+++ b/libbalsa/imap/imap-tls.c
@@ -49,10 +49,6 @@
 #include <openssl/x509v3.h>
 #include <openssl/err.h>
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#endif
-
 /* Support for SSLv3 should *not* be enabled as it is unsafe (see
  * <http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-3566> and
  * <http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-8730>.
@@ -66,38 +62,29 @@
 #include "imap_private.h"
 
 static SSL_CTX *global_ssl_context = NULL;
-#ifdef BALSA_USE_THREADS
-static pthread_mutex_t global_tls_lock = PTHREAD_MUTEX_INITIALIZER;
-
-/* provide support only for _POSIX_THREADS */
-#define MUTEX_TYPE pthread_mutex_t
-#define MUTEX_SETUP(m)   pthread_mutex_init (&(m), NULL)
-#define MUTEX_CLEANUP(m) pthread_mutex_destroy(&(m))
-#define MUTEX_LOCK(m)    pthread_mutex_lock(&(m))
-#define MUTEX_UNLOCK(m)  pthread_mutex_unlock(&(m))
-#define THREAD_ID        pthread_self()
+static GMutex global_tls_lock;
 
 /* OpenSSL static locks */
-static MUTEX_TYPE *mutexes = NULL;
+static GMutex *mutexes = NULL;
 
 static void
 locking_function(int mode, int n, const char *file, int line)
 {
   if(mode & CRYPTO_LOCK)
-    MUTEX_LOCK(mutexes[n]);
+         g_mutex_lock(&mutexes[n]);
   else
-    MUTEX_UNLOCK(mutexes[n]);
+         g_mutex_unlock(&mutexes[n]);
 }
 
 static unsigned long
 id_function(void)
 {
-  return (unsigned long)THREAD_ID;
+  return (unsigned long) g_thread_self();
 }
 
 /* OpenSSL dynamic locks */
 struct CRYPTO_dynlock_value {
-  MUTEX_TYPE mutex;
+  GMutex mutex;
 };
 
 static struct CRYPTO_dynlock_value*
@@ -108,7 +95,7 @@ dyn_create_function(const char *file, int line)
 
   if(!value)
     return NULL;
-  MUTEX_SETUP(value->mutex);
+  g_mutex_init(&value->mutex);
   return value;
 }
 
@@ -117,16 +104,16 @@ dyn_lock_function(int mode, struct CRYPTO_dynlock_value *l,
                   const char *file, int line)
 {
   if(mode & CRYPTO_LOCK)
-    MUTEX_LOCK(l->mutex);
+         g_mutex_lock(&l->mutex);
   else
-    MUTEX_UNLOCK(l->mutex);
+         g_mutex_unlock(&l->mutex);
 }
 
 static void
 dyn_destroy_function(struct CRYPTO_dynlock_value *l,
                      const char *file, int line)
 {
-  MUTEX_CLEANUP(l->mutex);
+       g_mutex_clear(&l->mutex);
   free(l);
 }
 
@@ -135,11 +122,11 @@ imaptls_thread_setup(void)
 {
   int i, mutex_cnt = CRYPTO_num_locks();
 
-  mutexes = (MUTEX_TYPE*)malloc(mutex_cnt*sizeof(MUTEX_TYPE));
+  mutexes = (GMutex*)malloc(mutex_cnt*sizeof(GMutex));
   if(!mutexes)
     return 0;
   for(i=0; i<mutex_cnt; i++)
-    MUTEX_SETUP(mutexes[i]);
+         g_mutex_init(&mutexes[i]);
   CRYPTO_set_id_callback(id_function);
   CRYPTO_set_locking_callback(locking_function);
   
@@ -163,27 +150,18 @@ imaptls_thread_cleanup(void)
   CRYPTO_set_dynlock_lock_callback(NULL);
   CRYPTO_set_dynlock_destroy_callback(NULL);
   for(i=0; i<mutex_cnt; i++)
-    MUTEX_CLEANUP(mutexes[i]);
+         g_mutex_clear(&mutexes[i]);
   free(mutexes); mutexes = NULL;
   return 1;
 }
 #endif /* DO_PROPER_OPENSSL_CLEANUP */
 
-#else /* BALSA_USE_THREADS */
-static void
-imaptls_thread_setup(void)
-{
-}
-#endif /* BALSA_USE_THREADS */
-
 SSL*
 imap_create_ssl(void)
 {
   SSL *ssl;
 
-#ifdef BALSA_USE_THREADS
-  pthread_mutex_lock(&global_tls_lock);
-#endif
+  g_mutex_lock(&global_tls_lock);
   if(!global_ssl_context) {
     /* Initialize OpenSSL library, follow "Network Security with
        OpenSSL", ISBN 0-596-00270-X, guidelines. Example 4-2. */
@@ -213,9 +191,7 @@ imap_create_ssl(void)
     */
    SSL_CTX_set_default_verify_paths (global_ssl_context);
   }
-#ifdef BALSA_USE_THREADS
-  pthread_mutex_unlock(&global_tls_lock);
-#endif
+  g_mutex_unlock(&global_tls_lock);
 
   ssl = SSL_new(global_ssl_context);
 
diff --git a/libbalsa/imap/imap_private.h b/libbalsa/imap/imap_private.h
index 018845c..debadc0 100644
--- a/libbalsa/imap/imap_private.h
+++ b/libbalsa/imap/imap_private.h
@@ -19,22 +19,12 @@
  * 02111-1307, USA.
  */
 
+#include <glib.h>
 #include <glib-object.h>
 #include <openssl/ssl.h>
 
 #include "config.h"
 
-#if defined(BALSA_USE_THREADS)
-#include <pthread.h>
-#define HANDLE_LOCK(h)   pthread_mutex_lock(&h->mutex)
-#define HANDLE_TRYLOCK(h) pthread_mutex_trylock(&h->mutex)
-#define HANDLE_UNLOCK(h) pthread_mutex_unlock(&h->mutex)
-#else
-#define HANDLE_LOCK(h) 
-#define HANDLE_TRYLOCK(h) 0
-#define HANDLE_UNLOCK(h)
-#endif
-
 #include "imap-commands.h"
 #include "imap_compress.h"
 
@@ -125,9 +115,7 @@ struct _ImapMboxHandle {
   GHashTable *status_resps; /* A hash of STATUS responses that we wait for */
 
   GIOChannel *iochannel; /* IO channel used for monitoring the connection */
-#if defined(BALSA_USE_THREADS)
-  pthread_mutex_t mutex;
-#endif
+  GMutex mutex;
   guint idle_enable_id; /* callback to issue IDLE after a period of
                            inactivity */
   guint async_watch_id;  /* callback to process incoming data */
@@ -165,17 +153,17 @@ struct _ImapMboxHandle {
 
 #define IMAP_REQUIRED_STATE1(h, state1, ret)            \
     do{if(!(h) || h->state != (state1))                 \
-            { HANDLE_UNLOCK(h);return (ret);}}while(0);
+            {g_mutex_unlock(&h->mutex); return (ret);}}while(0);
 #define IMAP_REQUIRED_STATE1_U(h, state1, ret)                  \
     do{if(!(h) || h->state != (state1)) return (ret);}while(0);
 
 #define IMAP_REQUIRED_STATE2(h, state1, state2, ret)                    \
     do{if(!(h) || !(h->state == (state1) || h->state == (state2)))      \
-            {HANDLE_UNLOCK(h); return (ret);}}while(0);
+            {g_mutex_unlock(&h->mutex); return (ret);}}while(0);
 #define IMAP_REQUIRED_STATE3(h, state1, state2, state3, ret)           \
     do{if(!(h) || !(h->state == (state1) || h->state == (state2)||     \
                     h->state == (state3)))                             \
-            {HANDLE_UNLOCK(h);return (ret);}}while(0);
+            {g_mutex_unlock(&h->mutex);return (ret);}}while(0);
 #define IMAP_REQUIRED_STATE3_U(h, state1, state2, state3, ret)           \
     do{if(!(h) || !(h->state == (state1) || h->state == (state2)||     \
                     h->state == (state3)))                             \
diff --git a/libbalsa/imap/imap_search.c b/libbalsa/imap/imap_search.c
index a6bc504..0b7fab3 100644
--- a/libbalsa/imap/imap_search.c
+++ b/libbalsa/imap/imap_search.c
@@ -551,9 +551,9 @@ imap_search_exec(ImapMboxHandle *h, gboolean uid,
 {
   ImapResponse rc;
 
-  HANDLE_LOCK(h);
+  g_mutex_lock(&h->mutex);
   rc = imap_search_exec_unlocked(h, uid, s, cb, cb_arg);
-  HANDLE_UNLOCK(h);
+  g_mutex_unlock(&h->mutex);
 
   return rc;
 }
diff --git a/libbalsa/information.h b/libbalsa/information.h
index 0427376..8dd621e 100644
--- a/libbalsa/information.h
+++ b/libbalsa/information.h
@@ -45,17 +45,11 @@ extern LibBalsaInformationFunc libbalsa_real_information_func;
 
 void libbalsa_information(LibBalsaInformationType type,
                           const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((format (printf, 2, 3)))
-#endif
-;
+       G_GNUC_PRINTF(2, 3);
 void libbalsa_information_parented(GtkWindow *parent,
                                    LibBalsaInformationType type,
                                    const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((format (printf, 3, 4)))
-#endif
-;
+       G_GNUC_PRINTF(3, 4);
 void libbalsa_information_varg(GtkWindow *parent,
                                LibBalsaInformationType type,
                                const char *fmt, va_list ap);
diff --git a/libbalsa/libbalsa-conf.c b/libbalsa/libbalsa-conf.c
index f0dd35d..9e1afce 100644
--- a/libbalsa/libbalsa-conf.c
+++ b/libbalsa/libbalsa-conf.c
@@ -146,18 +146,14 @@ lbc_init(LibBalsaConf * conf, const gchar * filename,
     }
 }
 
-#ifdef BALSA_USE_THREADS
 static GRecMutex lbc_mutex;
-#endif                          /* BALSA_USE_THREADS */
 
 static void
 lbc_lock(void)
 {
     static gboolean initialized = FALSE;
 
-#ifdef BALSA_USE_THREADS
     g_rec_mutex_lock(&lbc_mutex);
-#endif                          /* BALSA_USE_THREADS */
     if (!initialized) {
         lbc_init(&lbc_conf, "config", ".gnome2");
         lbc_init(&lbc_conf_priv, "config-private", ".gnome2_private");
@@ -168,9 +164,7 @@ lbc_lock(void)
 static void
 lbc_unlock(void)
 {
-#ifdef BALSA_USE_THREADS
     g_rec_mutex_unlock(&lbc_mutex);
-#endif                          /* BALSA_USE_THREADS */
 }
 
 /* 
@@ -525,16 +519,12 @@ lbc_sync(LibBalsaConf * conf)
 }
 
 static guint lbc_sync_idle_id;
-#ifdef BALSA_USE_THREADS
 G_LOCK_DEFINE_STATIC(lbc_sync_idle_id);
-#endif                          /* BALSA_USE_THREADS */
 
 void
 libbalsa_conf_sync(void)
 {
-#ifdef BALSA_USE_THREADS
     G_LOCK(lbc_sync_idle_id);
-#endif                          /* BALSA_USE_THREADS */
 #if DEBUG
     g_print("%s id %d, will be cleared\n", __func__, lbc_sync_idle_id);
 #endif                          /* DEBUG */
@@ -542,9 +532,7 @@ libbalsa_conf_sync(void)
         g_source_remove(lbc_sync_idle_id);
         lbc_sync_idle_id = 0;
     }
-#ifdef BALSA_USE_THREADS
     G_UNLOCK(lbc_sync_idle_id);
-#endif                          /* BALSA_USE_THREADS */
     lbc_lock();
     lbc_sync(&lbc_conf);
     lbc_sync(&lbc_conf_priv);
@@ -562,9 +550,7 @@ libbalsa_conf_sync_idle_cb(gpointer data)
 void
 libbalsa_conf_queue_sync(void)
 {
-#ifdef BALSA_USE_THREADS
     G_LOCK(lbc_sync_idle_id);
-#endif                          /* BALSA_USE_THREADS */
 #if DEBUG
     g_print("%s id %d, will be set if zero\n", __func__, lbc_sync_idle_id);
 #endif                          /* DEBUG */
@@ -574,7 +560,5 @@ libbalsa_conf_queue_sync(void)
 #if DEBUG
     g_print("%s id now %d\n", __func__, lbc_sync_idle_id);
 #endif                          /* DEBUG */
-#ifdef BALSA_USE_THREADS
     G_UNLOCK(lbc_sync_idle_id);
-#endif                          /* BALSA_USE_THREADS */
 }
diff --git a/libbalsa/libbalsa-gpgme-cb.c b/libbalsa/libbalsa-gpgme-cb.c
index d6af616..fa0680d 100644
--- a/libbalsa/libbalsa-gpgme-cb.c
+++ b/libbalsa/libbalsa-gpgme-cb.c
@@ -32,11 +32,6 @@
 #include "macosx-helpers.h"
 #endif
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#include "misc.h"
-#endif
-
 #include <gpgme.h>
 #include <glib.h>
 #include <glib/gi18n.h>
@@ -56,17 +51,17 @@ enum {
     GPG_KEY_NUM_COLUMNS
 };
 
-#ifdef BALSA_USE_THREADS
+
 /* FIXME: is this really necessary? */
 typedef struct {
-    pthread_cond_t cond;
+    GCond cond;
     const gchar *uid_hint;
     const gchar *passphrase_info;
     gint was_bad;
     GtkWindow *parent;
     gchar *res;
+    gboolean done;
 } ask_passphrase_data_t;
-#endif
 
 
 static void key_selection_changed_cb(GtkTreeSelection * selection,
@@ -77,9 +72,7 @@ static gchar *get_passphrase_real(const gchar * uid_hint,
                                  const gchar * passphrase_info,
                                  int prev_was_bad, GtkWindow * parent);
 
-#ifdef BALSA_USE_THREADS
 static gboolean get_passphrase_idle(gpointer data);
-#endif
 
 
 gpgme_error_t
@@ -97,33 +90,29 @@ lb_gpgme_passphrase(void *hook, const gchar * uid_hint,
     else
        parent = NULL;
 
-#ifdef BALSA_USE_THREADS
     if (!libbalsa_am_i_subthread())
        passwd =
            get_passphrase_real(uid_hint, passphrase_info, prev_was_bad,
                                parent);
     else {
-       static pthread_mutex_t get_passphrase_lock =
-           PTHREAD_MUTEX_INITIALIZER;
+       static GMutex get_passphrase_lock;
        ask_passphrase_data_t apd;
 
-       pthread_mutex_lock(&get_passphrase_lock);
-       pthread_cond_init(&apd.cond, NULL);
+       g_mutex_lock(&get_passphrase_lock);
+       g_cond_init(&apd.cond);
        apd.uid_hint = uid_hint;
        apd.was_bad = prev_was_bad;
        apd.passphrase_info = passphrase_info;
        apd.parent = parent;
+       apd.done = FALSE;
        g_idle_add(get_passphrase_idle, &apd);
-       pthread_cond_wait(&apd.cond, &get_passphrase_lock);
-       pthread_cond_destroy(&apd.cond);
-       pthread_mutex_unlock(&get_passphrase_lock);
+       while (!apd.done) {
+               g_cond_wait(&apd.cond, &get_passphrase_lock);
+       }
+       g_cond_clear(&apd.cond);
+       g_mutex_unlock(&get_passphrase_lock);
        passwd = apd.res;
     }
-#else
-    passwd =
-        get_passphrase_real(uid_hint, passphrase_info, prev_was_bad,
-                            parent);
-#endif                         /* BALSA_USE_THREADS */
 
     if (!passwd) {
        foo = write(fd, "\n", 1);
@@ -433,7 +422,6 @@ get_passphrase_real(const gchar * uid_hint, const gchar * passphrase_info,
 }
 
 
-#ifdef BALSA_USE_THREADS
 /* get_passphrase_idle:
    called in MT mode by the main thread.
  */
@@ -446,11 +434,11 @@ get_passphrase_idle(gpointer data)
     apd->res =
        get_passphrase_real(apd->uid_hint, apd->passphrase_info,
                            apd->was_bad, apd->parent);
+    apd->done = TRUE;
     gdk_threads_leave();
-    pthread_cond_signal(&apd->cond);
+    g_cond_signal(&apd->cond);
     return FALSE;
 }
-#endif
 
 
 /* callback function if a new row is selected in the list */
diff --git a/libbalsa/libbalsa-gpgme.c b/libbalsa/libbalsa-gpgme.c
index f22cf2c..182cb58 100644
--- a/libbalsa/libbalsa-gpgme.c
+++ b/libbalsa/libbalsa-gpgme.c
@@ -28,11 +28,6 @@
 #include <locale.h>
 #endif
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#include "misc.h"
-#endif
-
 #if HAVE_MACOSX_DESKTOP
 #include "macosx-helpers.h"
 #endif
@@ -629,18 +624,19 @@ static void
 g_set_error_from_gpgme(GError ** error, gpgme_error_t gpgme_err,
                       const gchar * message)
 {
-    gchar *errstr;
-    gchar *srcstr;
-
-    if (!error)
-       return;
-
-    srcstr = utf8_valid_str(gpgme_strsource(gpgme_err));
-    errstr = utf8_valid_str(gpgme_strerror(gpgme_err));
-    g_set_error(error, GPGME_ERROR_QUARK, gpgme_err, "%s: %s: %s", srcstr,
-               message, errstr);
-    g_free(srcstr);
-    g_free(errstr);
+    if (error != NULL) {
+       gchar errbuf[4096];             /* should be large enough... */
+        gchar *errstr;
+        gchar *srcstr;
+
+        srcstr = utf8_valid_str(gpgme_strsource(gpgme_err));
+        gpgme_strerror_r(gpgme_err, errbuf, sizeof(errbuf));
+        errstr = utf8_valid_str(errbuf);
+        g_set_error(error, GPGME_ERROR_QUARK, gpgme_err, "%s: %s: %s", srcstr,
+               message, errstr);
+        g_free(srcstr);
+        g_free(errstr);
+    }
 }
 
 
diff --git a/libbalsa/libbalsa.c b/libbalsa/libbalsa.c
index 87df3b4..5e172f3 100644
--- a/libbalsa/libbalsa.c
+++ b/libbalsa/libbalsa.c
@@ -57,9 +57,7 @@
 #include "missing.h"
 #include <glib/gi18n.h>
 
-#ifdef BALSA_USE_THREADS
-static pthread_t main_thread_id;
-#endif
+static GThread *main_thread_id;
 
 
 void
@@ -82,9 +80,7 @@ libbalsa_init(LibBalsaInformationFunc information_callback)
     notify_init("Basics");
 #endif
 
-#ifdef BALSA_USE_THREADS
-    main_thread_id = pthread_self();
-#endif
+    main_thread_id = g_thread_self();
 
     uname(&utsname);
 
@@ -289,13 +285,12 @@ libbalsa_get_icon_from_flags(LibBalsaMessageFlag flags)
 }
 
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
 typedef struct {
-    pthread_mutex_t lock;
-    pthread_cond_t condvar;
+    GMutex lock;
+    GCond condvar;
     int (*cb)(void *arg);
     void *arg;
+    gboolean done;
     int res;
 } AskData;
 
@@ -309,8 +304,9 @@ ask_idle(gpointer data)
     printf("ask_idle: ENTER %p\n", data);
     gdk_threads_enter();
     ad->res = (ad->cb)(ad->arg);
+    ad->done = TRUE;
     gdk_threads_leave();
-    pthread_cond_signal(&ad->condvar);
+    g_cond_signal(&ad->condvar);
     printf("ask_idle: LEAVE %p\n", data);
     return FALSE;
 }
@@ -324,7 +320,7 @@ libbalsa_ask(gboolean (*cb)(void *arg), void *arg)
 {
     AskData ad;
 
-    if (pthread_self() == main_thread_id) {
+    if (!libbalsa_am_i_subthread()) {
         int ret;
         printf("Main thread asks the following question.\n");
         gdk_threads_enter();
@@ -333,27 +329,22 @@ libbalsa_ask(gboolean (*cb)(void *arg), void *arg)
         return ret;
     }
     printf("Side thread asks the following question.\n");
-    pthread_mutex_init(&ad.lock, NULL);
-    pthread_cond_init(&ad.condvar, NULL);
+    g_mutex_init(&ad.lock);
+    g_cond_init(&ad.condvar);
     ad.cb  = cb;
     ad.arg = arg;
+    ad.done = FALSE;
 
-    pthread_mutex_lock(&ad.lock);
-    pthread_cond_init(&ad.condvar, NULL);
+    g_mutex_lock(&ad.lock);
     g_idle_add(ask_idle, &ad);
-    pthread_cond_wait(&ad.condvar, &ad.lock);
+    while (!ad.done) {
+       g_cond_wait(&ad.condvar, &ad.lock);
+    }
 
-    pthread_cond_destroy(&ad.condvar);
-    pthread_mutex_unlock(&ad.lock);
+    g_cond_clear(&ad.condvar);
+    g_mutex_unlock(&ad.lock);
     return ad.res;
 }
-#else /* BALSA_USE_THREADS */
-static gboolean
-libbalsa_ask(gboolean (*cb)(void *arg), void *arg)
-{
-    return cb(arg);
-}
-#endif /* BALSA_USE_THREADS */
 
 
 static int libbalsa_ask_for_cert_acceptance(X509 *cert,
@@ -415,24 +406,16 @@ x509_fingerprint (char *s, unsigned len, X509 * cert)
 }
 
 static GList *accepted_certs = NULL; /* certs accepted for this session */
-
-#ifdef BALSA_USE_THREADS
-static pthread_mutex_t certificate_lock = PTHREAD_MUTEX_INITIALIZER;
-#define LOCK_CERTIFICATES   pthread_mutex_lock(&certificate_lock)
-#define UNLOCK_CERTIFICATES pthread_mutex_unlock(&certificate_lock)
-#else
-#define LOCK_CERTIFICATES
-#define UNLOCK_CERTIFICATES
-#endif
+static GMutex certificate_lock;
 
 void
 libbalsa_certs_destroy(void)
 {
-    LOCK_CERTIFICATES;
+       g_mutex_lock(&certificate_lock);
     g_list_foreach(accepted_certs, (GFunc)X509_free, NULL);
     g_list_free(accepted_certs);
     accepted_certs = NULL;
-    UNLOCK_CERTIFICATES;
+    g_mutex_unlock(&certificate_lock);
 }
 
 /* compare Example 10-7 in the OpenSSL book */
@@ -445,11 +428,11 @@ libbalsa_is_cert_known(X509* cert, long vfy_result)
     gboolean res = FALSE;
     GList *lst;
 
-    LOCK_CERTIFICATES;
+    g_mutex_lock(&certificate_lock);
     for(lst = accepted_certs; lst; lst = lst->next) {
         int res = X509_cmp(cert, lst->data);
         if(res == 0) {
-           UNLOCK_CERTIFICATES;
+               g_mutex_unlock(&certificate_lock);
             return TRUE;
        }
     }
@@ -473,12 +456,12 @@ libbalsa_is_cert_known(X509* cert, long vfy_result)
         ERR_clear_error();
         fclose(fp);
     }
-    UNLOCK_CERTIFICATES;
+    g_mutex_unlock(&certificate_lock);
     
     if(!res) {
        const char *reason = X509_verify_cert_error_string(vfy_result);
        res = libbalsa_ask_for_cert_acceptance(cert, reason);
-       LOCK_CERTIFICATES;
+       g_mutex_lock(&certificate_lock);
        if(res == 2) {
            cert_name = g_strconcat(g_get_home_dir(),
                                    "/.balsa/certificates", NULL);
@@ -494,7 +477,7 @@ libbalsa_is_cert_known(X509* cert, long vfy_result)
        if(res == 1)
            accepted_certs = 
                g_list_prepend(accepted_certs, X509_dup(cert));
-       UNLOCK_CERTIFICATES;
+       g_mutex_unlock(&certificate_lock);
     }
 
     return res;
@@ -654,8 +637,7 @@ libbalsa_abort_on_timeout(const char *host)
 }
 
 
-#ifdef BALSA_USE_THREADS
-pthread_t
+GThread *
 libbalsa_get_main_thread(void)
 {
     return main_thread_id;
@@ -664,14 +646,13 @@ libbalsa_get_main_thread(void)
 gboolean
 libbalsa_am_i_subthread(void)
 {
-    return pthread_self() != main_thread_id;
+    return g_thread_self() != main_thread_id;
 }
-#endif /* BALSA_USE_THREADS */
 
-#ifdef BALSA_USE_THREADS
+
 #include "libbalsa_private.h"  /* for prototypes */
-static pthread_mutex_t mailbox_mutex = PTHREAD_MUTEX_INITIALIZER;
-static pthread_cond_t  mailbox_cond  = PTHREAD_COND_INITIALIZER;
+static GMutex mailbox_mutex;
+static GCond  mailbox_cond;
 
 /* Lock/unlock a mailbox; no argument checking--we'll assume the caller
  * took care of that. 
@@ -680,13 +661,13 @@ static pthread_cond_t  mailbox_cond  = PTHREAD_COND_INITIALIZER;
 void
 libbalsa_lock_mailbox(LibBalsaMailbox * mailbox)
 {
-    pthread_t thread_id = pthread_self();
+       GThread *thread_id = g_thread_self();
 
-    pthread_mutex_lock(&mailbox_mutex);
+    g_mutex_lock(&mailbox_mutex);
 
     if (mailbox->thread_id && mailbox->thread_id != thread_id)
         while (mailbox->lock)
-            pthread_cond_wait(&mailbox_cond, &mailbox_mutex);
+            g_cond_wait(&mailbox_cond, &mailbox_mutex);
 
     /* We'll assume that no-one would destroy a mailbox while we've been
      * trying to lock it. If they have, we have larger problems than
@@ -694,33 +675,32 @@ libbalsa_lock_mailbox(LibBalsaMailbox * mailbox)
     mailbox->lock++;
     mailbox->thread_id = thread_id;
 
-    pthread_mutex_unlock(&mailbox_mutex);
+    g_mutex_unlock(&mailbox_mutex);
 }
 
 void
 libbalsa_unlock_mailbox(LibBalsaMailbox * mailbox)
 {
-    pthread_t self;
+       GThread *self;
 
-    self = pthread_self();
+    self = g_thread_self();
 
-    pthread_mutex_lock(&mailbox_mutex);
+    g_mutex_lock(&mailbox_mutex);
 
     if (mailbox->lock == 0 || self != mailbox->thread_id) {
        g_warning("Not holding mailbox lock!!!");
-        pthread_mutex_unlock(&mailbox_mutex);
+        g_mutex_unlock(&mailbox_mutex);
        return;
     }
 
     if(--mailbox->lock == 0) {
         mailbox->thread_id = 0;
-        pthread_cond_broadcast(&mailbox_cond);
+        g_cond_broadcast(&mailbox_cond);
     }
 
-    pthread_mutex_unlock(&mailbox_mutex);
+    g_mutex_unlock(&mailbox_mutex);
 }
 
-#endif                         /* BALSA_USE_THREADS */
 
 /* Initialized by the front end. */
 void (*libbalsa_progress_set_text) (LibBalsaProgress * progress,
diff --git a/libbalsa/libbalsa.h b/libbalsa/libbalsa.h
index b2a18e0..f34f3cd 100644
--- a/libbalsa/libbalsa.h
+++ b/libbalsa/libbalsa.h
@@ -138,13 +138,8 @@ void libbalsa_certs_destroy(void);
 
 gboolean libbalsa_abort_on_timeout(const char *host);
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-pthread_t libbalsa_get_main_thread(void);
+GThread *libbalsa_get_main_thread(void);
 gboolean libbalsa_am_i_subthread(void);
-#else
-#define libbalsa_am_i_subthread() FALSE
-#endif /* BALSA_USE_THREADS */
 #if defined(BALSA_DEBUG_THREADS)
 #define gdk_threads_enter()                       \
     do if (libbalsa_am_i_subthread())             \
@@ -155,10 +150,7 @@ gboolean libbalsa_am_i_subthread(void);
 #endif
 #define gdk_threads_leave()
 void libbalsa_message(const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((format (printf, 1, 2)))
-#endif
-;
+       G_GNUC_PRINTF(1, 2);
 gchar * libbalsa_rot(const gchar * pass);
 
 typedef enum {
diff --git a/libbalsa/libbalsa_private.h b/libbalsa/libbalsa_private.h
index ddb9f7e..aaa1722 100644
--- a/libbalsa/libbalsa_private.h
+++ b/libbalsa/libbalsa_private.h
@@ -45,18 +45,10 @@ struct LibBalsaMailboxIndexEntry_ {
     unsigned foreground_set:1;
     unsigned background_set:1;
     unsigned unseen:1;
-#ifdef BALSA_USE_THREADS
     unsigned idle_pending:1;
-#endif                          /* BALSA_USE_THREADS */
 } ;
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
 void libbalsa_lock_mailbox(LibBalsaMailbox * mailbox);
 void libbalsa_unlock_mailbox(LibBalsaMailbox * mailbox);
-#else
-# define libbalsa_lock_mailbox(m)
-# define libbalsa_unlock_mailbox(m)
-#endif
 
 #endif                         /* __LIBBALSA_PRIVATE_H__ */
diff --git a/libbalsa/mailbox.c b/libbalsa/mailbox.c
index 18884ed..c5421b1 100644
--- a/libbalsa/mailbox.c
+++ b/libbalsa/mailbox.c
@@ -64,10 +64,8 @@ static void libbalsa_mailbox_real_load_config(LibBalsaMailbox * mailbox,
                                               const gchar * group);
 static gboolean libbalsa_mailbox_real_close_backend (LibBalsaMailbox *
                                                      mailbox);
-#ifdef BALSA_USE_THREADS
 static void libbalsa_mailbox_real_lock_store(LibBalsaMailbox * mailbox,
                                              gboolean lock);
-#endif                          /* BALSA_USE_THREADS */
 
 /* SIGNALS MEANINGS :
    - CHANGED: notification signal sent by the mailbox to allow the
@@ -233,9 +231,7 @@ libbalsa_mailbox_class_init(LibBalsaMailboxClass * klass)
     klass->close_backend  = libbalsa_mailbox_real_close_backend;
     klass->total_messages = NULL;
     klass->duplicate_msgnos = NULL;
-#ifdef BALSA_USE_THREADS
     klass->lock_store  = libbalsa_mailbox_real_lock_store;
-#endif                          /* BALSA_USE_THREADS */
 }
 
 static void
@@ -361,13 +357,10 @@ lbm_index_entry_populate_from_msg(LibBalsaMailboxIndexEntry * entry,
     entry->foreground_set = 0;
     entry->background_set = 0;
     entry->unseen        = LIBBALSA_MESSAGE_IS_UNREAD(msg);
-#ifdef BALSA_USE_THREADS
     entry->idle_pending  = 0;
-#endif                          /*BALSA_USE_THREADS */
     libbalsa_mailbox_msgno_changed(msg->mailbox, msg->msgno);
 }
 
-#ifdef BALSA_USE_THREADS
 static LibBalsaMailboxIndexEntry*
 lbm_index_entry_new_pending(void)
 {
@@ -375,15 +368,12 @@ lbm_index_entry_new_pending(void)
     entry->idle_pending = 1;
     return entry;
 }
-#endif                          /*BALSA_USE_THREADS */
 
 static void
 lbm_index_entry_free(LibBalsaMailboxIndexEntry *entry)
 {
     if(entry) {
-#ifdef BALSA_USE_THREADS
         if (!entry->idle_pending)
-#endif                          /*BALSA_USE_THREADS */
         {
             g_free(entry->from);
             g_free(entry->subject);
@@ -408,12 +398,8 @@ libbalsa_mailbox_index_entry_clear(LibBalsaMailbox * mailbox, guint msgno)
     }
 }
 
-#ifdef BALSA_USE_THREADS
-#  define VALID_ENTRY(entry) \
+#define VALID_ENTRY(entry) \
     ((entry) && !((LibBalsaMailboxIndexEntry *) (entry))->idle_pending)
-#else                           /*BALSA_USE_THREADS */
-#  define VALID_ENTRY(entry) ((entry) != NULL)
-#endif                          /*BALSA_USE_THREADS */
 
 void
 libbalsa_mailbox_index_set_flags(LibBalsaMailbox *mailbox,
@@ -437,12 +423,10 @@ libbalsa_mailbox_index_set_flags(LibBalsaMailbox *mailbox,
    destroys mailbox. Must leave it in sane state.
 */
 
-#ifdef BALSA_USE_THREADS
 static void lbm_msgno_changed_expunged_cb(LibBalsaMailbox * mailbox,
                                           guint seqno);
 static void lbm_get_index_entry_expunged_cb(LibBalsaMailbox * mailbox,
                                             guint seqno);
-#endif
 
 static void
 libbalsa_mailbox_finalize(GObject * object)
@@ -473,7 +457,6 @@ libbalsa_mailbox_finalize(GObject * object)
     mailbox->filters = NULL;
     mailbox->filters_loaded = FALSE;
 
-#ifdef BALSA_USE_THREADS
     if (mailbox->msgnos_pending) {
         g_signal_handlers_disconnect_by_func(mailbox,
                                              lbm_get_index_entry_expunged_cb,
@@ -489,7 +472,6 @@ libbalsa_mailbox_finalize(GObject * object)
         g_array_free(mailbox->msgnos_changed, TRUE);
         mailbox->msgnos_changed = NULL;
     }
-#endif                          /*BALSA_USE_THREADS */
 
     libbalsa_mailbox_view_free(mailbox->view);
     mailbox->view = NULL;
@@ -726,10 +708,6 @@ libbalsa_mailbox_check(LibBalsaMailbox * mailbox)
     }
 
     libbalsa_unlock_mailbox(mailbox);
-
-#ifdef BALSA_USE_THREADS
-    pthread_testcancel();
-#endif
 }
 
 static gboolean
@@ -1118,13 +1096,11 @@ libbalsa_mailbox_real_close_backend(LibBalsaMailbox * mailbox)
     return TRUE;                /* Default is noop. */
 }
 
-#if BALSA_USE_THREADS
 static void
 libbalsa_mailbox_real_lock_store(LibBalsaMailbox * mailbox, gboolean lock)
 {
     /* Default is noop. */
 }
-#endif                          /* BALSA_USE_THREADS */
 
 GType
 libbalsa_mailbox_type_from_path(const gchar * path)
@@ -1215,12 +1191,11 @@ lbm_node_has_unseen_child(LibBalsaMailbox * lmm, GNode * node)
     return FALSE;
 }
 
-#ifdef BALSA_USE_THREADS
 /* Protects access to mailbox->msgnos_changed; may be locked
  * with or without the gdk lock, so WE MUST NOT GRAB THE GDK LOCK WHILE
  * HOLDING IT. */
 
-static pthread_mutex_t msgnos_changed_lock = PTHREAD_MUTEX_INITIALIZER;
+static GMutex msgnos_changed_lock;
 
 static void lbm_update_msgnos(LibBalsaMailbox * mailbox, guint seqno,
                               GArray * msgnos);
@@ -1228,11 +1203,11 @@ static void lbm_update_msgnos(LibBalsaMailbox * mailbox, guint seqno,
 static void
 lbm_msgno_changed_expunged_cb(LibBalsaMailbox * mailbox, guint seqno)
 {
-    pthread_mutex_lock(&msgnos_changed_lock);
+    g_mutex_lock(&msgnos_changed_lock);
     lbm_update_msgnos(mailbox, seqno, mailbox->msgnos_changed);
-    pthread_mutex_unlock(&msgnos_changed_lock);
+    g_mutex_unlock(&msgnos_changed_lock);
 }
-#endif /* BALSA_USE_THREADS */
+
 
 static void
 lbm_msgno_row_changed(LibBalsaMailbox * mailbox, guint msgno,
@@ -1254,7 +1229,7 @@ lbm_msgno_row_changed(LibBalsaMailbox * mailbox, guint msgno,
     }
 }
 
-#ifdef BALSA_USE_THREADS
+
 static gboolean
 lbm_msgnos_changed_idle_cb(LibBalsaMailbox * mailbox)
 {
@@ -1269,7 +1244,7 @@ lbm_msgnos_changed_idle_cb(LibBalsaMailbox * mailbox)
             mailbox->msgnos_changed->len);
 #endif
 
-    pthread_mutex_lock(&msgnos_changed_lock);
+    g_mutex_lock(&msgnos_changed_lock);
     for (i = 0; i < mailbox->msgnos_changed->len; i++) {
         guint msgno = g_array_index(mailbox->msgnos_changed, guint, i);
         GtkTreeIter iter;
@@ -1280,10 +1255,10 @@ lbm_msgnos_changed_idle_cb(LibBalsaMailbox * mailbox)
 #if DEBUG
         g_print("%s %s msgno %d\n", __func__, mailbox->name, msgno);
 #endif
-        pthread_mutex_unlock(&msgnos_changed_lock);
+        g_mutex_unlock(&msgnos_changed_lock);
         iter.user_data = NULL;
         lbm_msgno_row_changed(mailbox, msgno, &iter);
-        pthread_mutex_lock(&msgnos_changed_lock);
+        g_mutex_lock(&msgnos_changed_lock);
     }
 
 #if DEBUG
@@ -1291,20 +1266,19 @@ lbm_msgnos_changed_idle_cb(LibBalsaMailbox * mailbox)
             mailbox->msgnos_changed->len);
 #endif
     mailbox->msgnos_changed->len = 0;
-    pthread_mutex_unlock(&msgnos_changed_lock);
+    g_mutex_unlock(&msgnos_changed_lock);
 
     g_object_unref(mailbox);
     return FALSE;
 }
-#endif /* BALSA_USE_THREADS */
+
 
 static void
 lbm_msgno_changed(LibBalsaMailbox * mailbox, guint seqno,
                   GtkTreeIter * iter)
 {
-#ifdef BALSA_USE_THREADS
     if (libbalsa_am_i_subthread()) {
-        pthread_mutex_lock(&msgnos_changed_lock);
+        g_mutex_lock(&msgnos_changed_lock);
         if (!mailbox->msgnos_changed) {
             mailbox->msgnos_changed =
                 g_array_new(FALSE, FALSE, sizeof(guint));
@@ -1317,7 +1291,7 @@ lbm_msgno_changed(LibBalsaMailbox * mailbox, guint seqno,
                        g_object_ref(mailbox));
 
         g_array_append_val(mailbox->msgnos_changed, seqno);
-        pthread_mutex_unlock(&msgnos_changed_lock);
+        g_mutex_unlock(&msgnos_changed_lock);
 
         /* Not calling lbm_msgno_row_changed, so we must make sure
          * iter->user_data is set: */
@@ -1327,7 +1301,6 @@ lbm_msgno_changed(LibBalsaMailbox * mailbox, guint seqno,
                             GUINT_TO_POINTER(seqno));
         return;
     }
-#endif
 
     lbm_msgno_row_changed(mailbox, seqno, iter);
 }
@@ -1638,7 +1611,6 @@ lbm_msgno_filt_check(LibBalsaMailbox * mailbox, guint seqno,
     }
 }
 
-#ifdef BALSA_USE_THREADS
 typedef struct {
     LibBalsaMailbox           *mailbox;
     guint                      seqno;
@@ -1659,7 +1631,6 @@ lbm_msgno_filt_check_idle_cb(LibBalsaMailboxMsgnoFiltCheckInfo * info)
 
     return FALSE;
 }
-#endif                          /* BALSA_USE_THREADS */
 
 void
 libbalsa_mailbox_msgno_filt_check(LibBalsaMailbox * mailbox, guint seqno,
@@ -1672,7 +1643,6 @@ libbalsa_mailbox_msgno_filt_check(LibBalsaMailbox * mailbox, guint seqno,
         return;
     }
 
-#ifdef BALSA_USE_THREADS
     if (!libbalsa_am_i_subthread()) {
         lbm_msgno_filt_check(mailbox, seqno, search_iter, hold_selected);
     } else {
@@ -1685,9 +1655,6 @@ libbalsa_mailbox_msgno_filt_check(LibBalsaMailbox * mailbox, guint seqno,
         info->hold_selected = hold_selected;
         g_idle_add((GSourceFunc) lbm_msgno_filt_check_idle_cb, info);
     }
-#else                           /* BALSA_USE_THREADS */
-    lbm_msgno_filt_check(mailbox, seqno, search_iter, hold_selected);
-#endif                          /* BALSA_USE_THREADS */
 }
 
 /* Search iters */
@@ -1974,11 +1941,8 @@ lbm_cache_message(LibBalsaMailbox * mailbox, guint msgno,
         g_ptr_array_index(mailbox->mindex, msgno - 1) =
             entry = g_new(LibBalsaMailboxIndexEntry, 1);
         lbm_index_entry_populate_from_msg(entry, message);
-    }
-#if BALSA_USE_THREADS
-    else if (entry->idle_pending)
+    } else if (entry->idle_pending)
         lbm_index_entry_populate_from_msg(entry, message);
-#endif                          /* BALSA_USE_THREADS */
 }
 
 LibBalsaMessage *
@@ -1998,16 +1962,11 @@ libbalsa_mailbox_get_message(LibBalsaMailbox * mailbox, guint msgno)
         return NULL;
     }
 
-#ifdef BALSA_USE_THREADS
     if( !(msgno > 0 && msgno <= libbalsa_mailbox_total_messages(mailbox)) ) {
        libbalsa_unlock_mailbox(mailbox);
        g_warning("get_message: msgno %d out of range", msgno);
        return NULL;
     }
-#else                           /* BALSA_USE_THREADS */
-    g_return_val_if_fail(msgno > 0 && msgno <=
-                         libbalsa_mailbox_total_messages(mailbox), NULL);
-#endif                          /* BALSA_USE_THREADS */
 
     message = LIBBALSA_MAILBOX_GET_CLASS(mailbox)->get_message(mailbox,
                                                                msgno);
@@ -2139,7 +2098,7 @@ libbalsa_mailbox_messages_change_flags(LibBalsaMailbox * mailbox,
     if (real_flag)
        libbalsa_unlock_mailbox(mailbox);
 
-    if (set & LIBBALSA_MESSAGE_FLAG_DELETED && retval)
+    if ((set & LIBBALSA_MESSAGE_FLAG_DELETED) && retval)
         libbalsa_mailbox_changed(mailbox);
 
     return retval;
@@ -2298,7 +2257,6 @@ lbm_check_and_sort(LibBalsaMailbox * mailbox)
     libbalsa_mailbox_changed(mailbox);
 }
 
-#ifdef BALSA_USE_THREADS
 static gboolean
 lbm_set_threading_idle_cb(LibBalsaMailbox * mailbox)
 {
@@ -2306,7 +2264,6 @@ lbm_set_threading_idle_cb(LibBalsaMailbox * mailbox)
     g_object_unref(mailbox);
     return FALSE;
 }
-#endif                          /* BALSA_USE_THREADS */
 
 static gboolean
 lbm_set_threading(LibBalsaMailbox * mailbox,
@@ -2317,7 +2274,6 @@ lbm_set_threading(LibBalsaMailbox * mailbox,
 
     LIBBALSA_MAILBOX_GET_CLASS(mailbox)->set_threading(mailbox,
                                                        thread_type);
-#ifdef BALSA_USE_THREADS
     if (libbalsa_am_i_subthread()) {
         gdk_threads_add_idle((GSourceFunc) lbm_set_threading_idle_cb,
                              g_object_ref(mailbox));
@@ -2326,9 +2282,6 @@ lbm_set_threading(LibBalsaMailbox * mailbox,
         lbm_check_and_sort(mailbox);
         gdk_threads_leave();
     }
-#else                           /* BALSA_USE_THREADS */
-    lbm_check_and_sort(mailbox);
-#endif                          /* BALSA_USE_THREADS */
 
     return TRUE;
 }
@@ -2901,16 +2854,16 @@ mbox_model_get_path(GtkTreeModel * tree_model, GtkTreeIter * iter)
 static GdkPixbuf *status_icons[LIBBALSA_MESSAGE_STATUS_ICONS_NUM];
 static GdkPixbuf *attach_icons[LIBBALSA_MESSAGE_ATTACH_ICONS_NUM];
 
-#ifdef BALSA_USE_THREADS
+
 /* Protects access to mailbox->msgnos_pending; */
-static pthread_mutex_t get_index_entry_lock = PTHREAD_MUTEX_INITIALIZER;
+static GMutex get_index_entry_lock;
 
 static void
 lbm_get_index_entry_expunged_cb(LibBalsaMailbox * mailbox, guint seqno)
 {
-    pthread_mutex_lock(&get_index_entry_lock);
+    g_mutex_lock(&get_index_entry_lock);
     lbm_update_msgnos(mailbox, seqno, mailbox->msgnos_pending);
-    pthread_mutex_unlock(&get_index_entry_lock);
+    g_mutex_unlock(&get_index_entry_lock);
 }
 
 static void
@@ -2922,7 +2875,7 @@ lbm_get_index_entry_real(LibBalsaMailbox * mailbox)
     g_print("%s %s %d requested\n", __func__, mailbox->name,
             mailbox->msgnos_pending->len);
 #endif
-    pthread_mutex_lock(&get_index_entry_lock);
+    g_mutex_lock(&get_index_entry_lock);
     for (i = 0; i < mailbox->msgnos_pending->len; i++) {
         guint msgno = g_array_index(mailbox->msgnos_pending, guint, i);
         LibBalsaMessage *message;
@@ -2933,12 +2886,12 @@ lbm_get_index_entry_real(LibBalsaMailbox * mailbox)
 #if DEBUG
         g_print("%s %s msgno %d\n", __func__, mailbox->name, msgno);
 #endif
-        pthread_mutex_unlock(&get_index_entry_lock);
+        g_mutex_unlock(&get_index_entry_lock);
         if ((message = libbalsa_mailbox_get_message(mailbox, msgno)))
             /* get-message has cached the message info, so we just unref
              * message. */
             g_object_unref(message);
-        pthread_mutex_lock(&get_index_entry_lock);
+        g_mutex_lock(&get_index_entry_lock);
     }
 
 #if DEBUG
@@ -2946,11 +2899,11 @@ lbm_get_index_entry_real(LibBalsaMailbox * mailbox)
             mailbox->msgnos_pending->len);
 #endif
     mailbox->msgnos_pending->len = 0;
-    pthread_mutex_unlock(&get_index_entry_lock);
+    g_mutex_unlock(&get_index_entry_lock);
 
     g_object_unref(mailbox);
 }
-#endif                          /*BALSA_USE_THREADS */
+
 
 static LibBalsaMailboxIndexEntry *
 lbm_get_index_entry(LibBalsaMailbox * lmm, guint msgno)
@@ -2964,11 +2917,10 @@ lbm_get_index_entry(LibBalsaMailbox * lmm, guint msgno)
         g_ptr_array_set_size(lmm->mindex, msgno);
 
     entry = g_ptr_array_index(lmm->mindex, msgno - 1);
-#ifdef BALSA_USE_THREADS
     if (entry)
         return entry->idle_pending ? NULL : entry;
 
-    pthread_mutex_lock(&get_index_entry_lock);
+    g_mutex_lock(&get_index_entry_lock);
     if (!lmm->msgnos_pending) {
         lmm->msgnos_pending = g_array_new(FALSE, FALSE, sizeof(guint));
         g_signal_connect(lmm, "message-expunged",
@@ -2976,12 +2928,14 @@ lbm_get_index_entry(LibBalsaMailbox * lmm, guint msgno)
     }
 
     if (!lmm->msgnos_pending->len) {
-        pthread_t get_index_entry_thread;
+        GThread *get_index_entry_thread;
 
         g_object_ref(lmm);
-        pthread_create(&get_index_entry_thread, NULL,
-                       (void *) lbm_get_index_entry_real, lmm);
-        pthread_detach(get_index_entry_thread);
+        get_index_entry_thread =
+               g_thread_new("lbm_get_index_entry_real",
+                                        (GThreadFunc) lbm_get_index_entry_real,
+                                                lmm);
+        g_thread_unref(get_index_entry_thread);
     }
 
     g_array_append_val(lmm->msgnos_pending, msgno);
@@ -2989,19 +2943,7 @@ lbm_get_index_entry(LibBalsaMailbox * lmm, guint msgno)
      * lock. */
     g_ptr_array_index(lmm->mindex, msgno - 1) =
         lbm_index_entry_new_pending();
-    pthread_mutex_unlock(&get_index_entry_lock);
-#else                           /*BALSA_USE_THREADS */
-    if (!entry) {
-        LibBalsaMessage *message =
-            libbalsa_mailbox_get_message(lmm, msgno);
-        if (message) {
-            /* get-message has cached the message info, so we just unref
-             * message. */
-            g_object_unref(message);
-            entry = g_ptr_array_index(lmm->mindex, msgno - 1);
-        }
-    }
-#endif                          /*BALSA_USE_THREADS */
+    g_mutex_unlock(&get_index_entry_lock);
 
     return entry;
 }
@@ -4268,14 +4210,11 @@ lbm_check_real(LibBalsaMailbox * mailbox)
 static gboolean
 lbm_check_idle(LibBalsaMailbox * mailbox)
 {
-#if defined(BALSA_USE_THREADS)
-    pthread_t check_thread;
+    GThread *check_thread;
 
-    pthread_create(&check_thread, NULL, (void *) lbm_check_real, mailbox);
-    pthread_detach(check_thread);
-#else                           /*BALSA_USE_THREADS */
-    lbm_check_real(mailbox);
-#endif                          /*BALSA_USE_THREADS */
+    check_thread =
+       g_thread_new("lbm_check_real", (GThreadFunc) lbm_check_real, mailbox);
+    g_thread_unref(check_thread);
 
     libbalsa_lock_mailbox(mailbox);
     mailbox->queue_check_idle_id = 0;
@@ -4390,7 +4329,6 @@ libbalsa_mailbox_move_duplicates(LibBalsaMailbox * mailbox,
     return retval;
 }
 
-#if BALSA_USE_THREADS
 /* Lock and unlock the mail store. NULL mailbox is not an error. */
 void 
 libbalsa_mailbox_lock_store(LibBalsaMailbox * mailbox)
@@ -4405,7 +4343,6 @@ libbalsa_mailbox_unlock_store(LibBalsaMailbox * mailbox)
     if (mailbox)
         LIBBALSA_MAILBOX_GET_CLASS(mailbox)->lock_store(mailbox, FALSE);
 }
-#endif                          /* BALSA_USE_THREADS */
 
 void
 libbalsa_mailbox_cache_message(LibBalsaMailbox * mailbox, guint msgno,
diff --git a/libbalsa/mailbox.h b/libbalsa/mailbox.h
index 78e479c..ea7e891 100644
--- a/libbalsa/mailbox.h
+++ b/libbalsa/mailbox.h
@@ -30,10 +30,6 @@
 #include <gdk/gdk.h>
 #include <gmime/gmime.h>
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#endif
-
 #include "libbalsa.h"
 
 #define LIBBALSA_TYPE_MAILBOX \
@@ -211,9 +207,7 @@ struct _LibBalsaMailbox {
     
     int lock; /* 0 if mailbox is unlocked; */
               /* >0 if mailbox is (recursively locked). */
-#ifdef BALSA_USE_THREADS
-    pthread_t thread_id; /* id of thread that locked the mailbox */
-#endif
+    GThread *thread_id; /* id of thread that locked the mailbox */
     gboolean is_directory;
     gboolean readonly;
     gboolean disconnected;
@@ -252,12 +246,11 @@ struct _LibBalsaMailbox {
     /* Whether the tree has been changed since some event. */
     gboolean msg_tree_changed;
 
-#ifdef BALSA_USE_THREADS
     /* Array of msgnos that need to be displayed. */
     GArray *msgnos_pending;
     /* Array of msgnos that have been changed. */
     GArray *msgnos_changed;
-#endif                          /* BALSA_USE_THREADS */
+
     guint changed_idle_id;
     guint queue_check_idle_id;
 };
@@ -340,9 +333,7 @@ struct _LibBalsaMailboxClass {
     gboolean (*close_backend)(LibBalsaMailbox * mailbox);
     guint (*total_messages)(LibBalsaMailbox * mailbox);
     GArray *(*duplicate_msgnos) (LibBalsaMailbox * mailbox);
-#if BALSA_USE_THREADS
     void (*lock_store) (LibBalsaMailbox * mailbox, gboolean lock);
-#endif                          /* BALSA_USE_THREADS */
 };
 
 GType libbalsa_mailbox_get_type(void);
@@ -654,20 +645,11 @@ void libbalsa_mailbox_set_foreground(LibBalsaMailbox * mailbox,
 void libbalsa_mailbox_set_background(LibBalsaMailbox * mailbox,
                                      GArray * msgnos, const gchar * color);
 
-#if BALSA_USE_THREADS
-
 /* Lock and unlock the mail store--currently, a no-op except for mbox.
  */
 void libbalsa_mailbox_lock_store  (LibBalsaMailbox * mailbox);
 void libbalsa_mailbox_unlock_store(LibBalsaMailbox * mailbox);
 
-#else                           /* BALSA_USE_THREADS */
-
-#define libbalsa_mailbox_lock_store(mailbox)
-#define libbalsa_mailbox_unlock_store(mailbox)
-
-#endif                          /* BALSA_USE_THREADS */
-
 /* columns ids */
 typedef enum {
     LB_MBOX_MSGNO_COL,
diff --git a/libbalsa/mailbox_imap.c b/libbalsa/mailbox_imap.c
index 6176e6d..1998311 100644
--- a/libbalsa/mailbox_imap.c
+++ b/libbalsa/mailbox_imap.c
@@ -44,10 +44,6 @@
 /* for uint32_t */
 #include <stdint.h>
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#endif
-
 #include "filter-funcs.h"
 #include "filter.h"
 #include "imap-commands.h"
diff --git a/libbalsa/mailbox_local.c b/libbalsa/mailbox_local.c
index 9675f77..186aa37 100644
--- a/libbalsa/mailbox_local.c
+++ b/libbalsa/mailbox_local.c
@@ -720,15 +720,7 @@ lbm_local_save_tree_real(LibBalsaMailboxLocal * local)
 static gboolean
 lbm_local_save_tree_idle(LibBalsaMailboxLocal * local)
 {
-#if 0 && defined(BALSA_USE_THREADS)
-    pthread_t save_tree_thread;
-
-    pthread_create(&save_tree_thread, NULL,
-                   (void *) lbm_local_save_tree_real, local);
-    pthread_detach(save_tree_thread);
-#else                           /* BALSA_USE_THREADS */
     lbm_local_save_tree_real(local);
-#endif                          /* BALSA_USE_THREADS */
 
     return FALSE;
 }
@@ -856,10 +848,8 @@ message_match_real(LibBalsaMailbox *mailbox, guint msgno,
         info  = g_ptr_array_index(local->threading_info, msgno - 1);
     }
 
-#if defined(BALSA_USE_THREADS)
     if (entry->idle_pending)
         return FALSE;   /* Can't match. */
-#endif                  /* defined(BALSA_USE_THREADS) */
 
     switch (cond->type) {
     case CONDITION_STRING:
@@ -1143,7 +1133,6 @@ lbml_set_threading(LibBalsaMailbox * mailbox,
     }
 }
 
-#ifdef BALSA_USE_THREADS
 typedef struct {
     LibBalsaMailbox *mailbox;
     LibBalsaMailboxThreadingType thread_type;
@@ -1157,7 +1146,6 @@ lbml_set_threading_idle_cb(LbmlSetThreadingInfo * info)
     g_slice_free(LbmlSetThreadingInfo, info);
     return FALSE;
 }
-#endif                          /* BALSA_USE_THREADS */
 
 static void
 libbalsa_mailbox_local_set_threading(LibBalsaMailbox * mailbox,
@@ -1201,7 +1189,6 @@ libbalsa_mailbox_local_set_threading(LibBalsaMailbox * mailbox,
             return;
     }
 
-#ifdef BALSA_USE_THREADS
     if (libbalsa_am_i_subthread()) {
         LbmlSetThreadingInfo *info;
 
@@ -1214,9 +1201,6 @@ libbalsa_mailbox_local_set_threading(LibBalsaMailbox * mailbox,
         lbml_set_threading(mailbox, thread_type);
         gdk_threads_leave();
     }
-#else                           /* BALSA_USE_THREADS */
-    lbml_set_threading(mailbox, thread_type);
-#endif                          /* BALSA_USE_THREADS */
 #if defined(DEBUG_LOADING_AND_THREADING)
     printf("after threading time=%lu\n", (unsigned long) time(NULL));
 #endif
@@ -2176,14 +2160,11 @@ lbm_local_sync_real(LibBalsaMailboxLocal * local)
 static gboolean
 lbm_local_sync_idle(LibBalsaMailboxLocal * local)
 {
-#if defined(BALSA_USE_THREADS)
-    pthread_t sync_thread;
+    GThread *sync_thread;
 
-    pthread_create(&sync_thread, NULL, (void *) lbm_local_sync_real, local);
-    pthread_detach(sync_thread);
-#else                           /*BALSA_USE_THREADS */
-    lbm_local_sync_real(local);
-#endif                          /*BALSA_USE_THREADS */
+    sync_thread =
+       g_thread_new("lbm_local_sync_real", (GThreadFunc) lbm_local_sync_real, local);
+    g_thread_unref(sync_thread);
 
     return FALSE;
 }
diff --git a/libbalsa/mailbox_mbox.c b/libbalsa/mailbox_mbox.c
index 78fa429..86a428d 100644
--- a/libbalsa/mailbox_mbox.c
+++ b/libbalsa/mailbox_mbox.c
@@ -36,9 +36,6 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-/* we include time because pthread.h may require it when compiled with c89 */
-#include <time.h>
-
 #include "libbalsa.h"
 #include "libbalsa_private.h"
 #include "misc.h"
@@ -98,10 +95,8 @@ libbalsa_mailbox_mbox_fetch_message_structure(LibBalsaMailbox * mailbox,
                                               LibBalsaFetchFlag flags);
 static guint
 libbalsa_mailbox_mbox_total_messages(LibBalsaMailbox * mailbox);
-#if BALSA_USE_THREADS
 static void libbalsa_mailbox_mbox_lock_store(LibBalsaMailbox * mailbox,
                                              gboolean lock);
-#endif                          /* BALSA_USE_THREADS */
 
 struct _LibBalsaMailboxMboxClass {
     LibBalsaMailboxLocalClass klass;
@@ -168,9 +163,7 @@ libbalsa_mailbox_mbox_class_init(LibBalsaMailboxMboxClass * klass)
        libbalsa_mailbox_mbox_fetch_message_structure;
     libbalsa_mailbox_class->total_messages =
        libbalsa_mailbox_mbox_total_messages;
-#if BALSA_USE_THREADS
     libbalsa_mailbox_class->lock_store = libbalsa_mailbox_mbox_lock_store;
-#endif                          /* BALSA_USE_THREADS */
 
     libbalsa_mailbox_local_class->check_files  = lbm_mbox_check_files;
     libbalsa_mailbox_local_class->remove_files = 
@@ -1998,7 +1991,6 @@ libbalsa_mailbox_mbox_total_messages(LibBalsaMailbox * mailbox)
     return mbox->msgno_2_msg_info ? mbox->msgno_2_msg_info->len : 0;
 }
 
-#if BALSA_USE_THREADS
 static void
 libbalsa_mailbox_mbox_lock_store(LibBalsaMailbox * mailbox, gboolean lock)
 {
@@ -2010,4 +2002,3 @@ libbalsa_mailbox_mbox_lock_store(LibBalsaMailbox * mailbox, gboolean lock)
     else
         libbalsa_mime_stream_shared_unlock(stream);
 }
-#endif                          /* BALSA_USE_THREADS */
diff --git a/libbalsa/mime-stream-shared.c b/libbalsa/mime-stream-shared.c
index 081aec4..0be2309 100644
--- a/libbalsa/mime-stream-shared.c
+++ b/libbalsa/mime-stream-shared.c
@@ -35,8 +35,6 @@
 # include "config.h"
 #endif                          /* HAVE_CONFIG_H */
 
-#if BALSA_USE_THREADS
-
 #include "mime-stream-shared.h"
 
 #include <unistd.h>
@@ -317,5 +315,3 @@ libbalsa_mime_stream_shared_unlock(GMimeStream * stream)
         g_cond_signal(&lbmss_cond);
     g_mutex_unlock(&lbmss_mutex);
 }
-
-#endif                          /* BALSA_USE_THREADS */
diff --git a/libbalsa/mime-stream-shared.h b/libbalsa/mime-stream-shared.h
index 172c556..fd10c4b 100644
--- a/libbalsa/mime-stream-shared.h
+++ b/libbalsa/mime-stream-shared.h
@@ -27,8 +27,6 @@
 # error "Include config.h before this file."
 #endif
 
-#if BALSA_USE_THREADS
-
 #include <gmime/gmime-stream-fs.h>
 
 #define LIBBALSA_TYPE_MIME_STREAM_SHARED                           \
@@ -62,12 +60,4 @@ GMimeStream *libbalsa_mime_stream_shared_new(int fd);
 void libbalsa_mime_stream_shared_lock  (GMimeStream * stream);
 void libbalsa_mime_stream_shared_unlock(GMimeStream * stream);
 
-#else                           /* BALSA_USE_THREADS */
-
-#define libbalsa_mime_stream_shared_new(fd) g_mime_stream_fs_new(fd)
-#define libbalsa_mime_stream_shared_lock(stream)
-#define libbalsa_mime_stream_shared_unlock(stream)
-
-#endif                          /* BALSA_USE_THREADS */
-
 #endif                          /* __LIBBALSA_MIME_STREAM_SHARED_H__ */
diff --git a/libbalsa/missing_time.c b/libbalsa/missing_time.c
index 8eeae41..d038824 100644
--- a/libbalsa/missing_time.c
+++ b/libbalsa/missing_time.c
@@ -30,23 +30,15 @@
 
 #include <string.h>
 
-#if BALSA_USE_THREADS
-#include <pthread.h>
-static pthread_mutex_t time_lock = PTHREAD_MUTEX_INITIALIZER;
-#define LOCK(mutex)   pthread_mutex_lock(&mutex)
-#define UNLOCK(mutex) pthread_mutex_unlock(&mutex)
-#else
-#define LOCK(mutex)
-#define UNLOCK(mutex)
-#endif /* BALSA_USE_THREADS */
-
 #ifndef HAVE_CTIME_R
 char *
 ctime_r(const time_t *clock, char *buf)
 {
-    LOCK(time_lock);
+       static GMutex time_lock;
+
+       g_mutex_lock(&mutex);
     strcpy(buf, ctime(clock));
-    UNLOCK(time_lock);
+       g_mutex_unlock(&mutex);
     return buf;
 }
 #endif
diff --git a/libbalsa/rfc3156.c b/libbalsa/rfc3156.c
index fae4284..903d05b 100644
--- a/libbalsa/rfc3156.c
+++ b/libbalsa/rfc3156.c
@@ -43,11 +43,6 @@
 #  include "gmime-application-pkcs7.h"
 #endif
 
-#ifdef BALSA_USE_THREADS
-#  include <pthread.h>
-#  include "misc.h"
-#endif
-
 #if HAVE_MACOSX_DESKTOP
 #  include "macosx-helpers.h"
 #endif
@@ -684,10 +679,14 @@ libbalsa_gpgme_sig_stat_to_gchar(gpgme_error_t stat)
     case GPG_ERR_TRY_AGAIN:
        return _
            ("GnuPG is rebuilding the trust database and is currently unavailable.");
-    default:
-       g_message("stat %d: %s %s", stat, gpgme_strsource(stat), gpgme_strerror(stat));
+    default: {
+       gchar errbuf[4096];             /* should be large enough... */
+
+       gpgme_strerror_r(stat, errbuf, sizeof(errbuf));
+       g_message("stat %d: %s %s", stat, gpgme_strsource(stat), errbuf);
        return _("An error prevented the signature verification.");
     }
+    }
 }
 
 
diff --git a/libbalsa/send.c b/libbalsa/send.c
index 85a35cb..d21fd12 100644
--- a/libbalsa/send.c
+++ b/libbalsa/send.c
@@ -33,10 +33,6 @@
 #include <stdlib.h>
 #include <time.h>
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#endif
-
 #include <string.h>
 
 #include "libbalsa.h"
@@ -220,8 +216,6 @@ static LibBalsaMsgCreateResult
 libbalsa_fill_msg_queue_item_from_queu(LibBalsaMessage * message,
                                        MessageQueueItem *mqi);
 
-#ifdef BALSA_USE_THREADS
-
 GtkWidget *send_progress_message = NULL;
 GtkWidget *send_dialog = NULL;
 GtkWidget *send_dialog_bar = NULL;
@@ -277,17 +271,6 @@ ensure_send_progress_dialog(GtkWindow * parent)
     /* Progress bar done */
 }
 
-/* define commands for locking and unlocking: it makes deadlock debugging
- * easier. */
-#define send_lock()   pthread_mutex_lock(&send_messages_lock); 
-#define send_unlock() pthread_mutex_unlock(&send_messages_lock);
-
-#else
-#define ensure_send_progress_dialog(parent)
-#define send_lock()   
-#define send_unlock() 
-#endif
-
 static void
 lbs_set_content(GMimePart * mime_part, gchar * content)
 {
@@ -663,15 +646,17 @@ lbs_process_queue(LibBalsaMailbox * outbox, LibBalsaFccboxFinder finder,
     long estimate;
     guint msgno;
     gchar *host_with_port;
-    send_lock();
+    GThread *send_mail;
+
+    g_mutex_lock(&send_messages_lock);
 
     if (!libbalsa_mailbox_open(outbox, NULL)) {
-       send_unlock();
+       g_mutex_unlock(&send_messages_lock);
        return FALSE;
     }
     if (!libbalsa_mailbox_total_messages(outbox)) {
        libbalsa_mailbox_close(outbox, TRUE);
-       send_unlock();
+       g_mutex_unlock(&send_messages_lock);
        return TRUE;
     }
     /* We create here the progress bar */
@@ -933,19 +918,17 @@ lbs_process_queue(LibBalsaMailbox * outbox, LibBalsaFccboxFinder finder,
 
     send_message_info=send_message_info_new(outbox, session, debug);
 
-#if defined(BALSA_USE_THREADS)
     sending_threads++;
-    pthread_create(&send_mail, NULL,
-                  (void *) &balsa_send_message_real, send_message_info);
-    /* Detach so we don't need to pthread_join
+    send_mail =
+       g_thread_new("balsa_send_message_real",
+                                (GThreadFunc) balsa_send_message_real,
+                                        send_message_info);
+    /* Detach so we don't need to g_thread_join
      * This means that all resources will be
      * reclaimed as soon as the thread exits
      */
-    pthread_detach(send_mail);
-    send_unlock();
-#else                          /*non-threaded code */
-    balsa_send_message_real(send_message_info);
-#endif
+    g_thread_unref(send_mail);
+    g_mutex_unlock(&send_messages_lock);
     return TRUE;
 }
 
@@ -989,7 +972,7 @@ handle_successful_send(smtp_message_t message, void *be_verbose)
     MessageQueueItem *mqi;
     const smtp_status_t *status;
 
-    send_lock();
+    g_mutex_lock(&send_messages_lock);
     /* Get the app data and decrement the reference count.  Only delete
        structures if refcount reaches zero */
     mqi = smtp_message_get_application_data (message);
@@ -1078,10 +1061,9 @@ handle_successful_send(smtp_message_t message, void *be_verbose)
     }
     if (mqi != NULL && mqi->refcount <= 0)
         msg_queue_item_destroy(mqi);
-    send_unlock();
+    g_mutex_unlock(&send_messages_lock);
 }
 
-#ifdef BALSA_USE_THREADS
 static void
 libbalsa_smtp_event_cb (smtp_session_t session, int event_no, void *arg, ...)
 {
@@ -1202,49 +1184,6 @@ libbalsa_smtp_event_cb (smtp_session_t session, int event_no, void *arg, ...)
     }
     va_end (ap);
 }
-#else /* BALSA: USE_THREADS */
-static void
-libbalsa_smtp_event_cb_serial(smtp_session_t session, int event_no,
-                              void *arg, ...)
-{
-    va_list ap;
-
-    va_start (ap, arg);
-    switch (event_no) {
-#ifdef USE_TLS
-        /* SMTP_TLS related things. Observe that we need to have SSL
-        * enabled in balsa to properly interpret libesmtp
-        * messages. */
-    case SMTP_EV_INVALID_PEER_CERTIFICATE: {
-        long vfy_result;
-       SSL  *ssl;
-       X509 *cert;
-        int *ok;
-        vfy_result = va_arg(ap, long); ok = va_arg(ap, int*);
-       ssl = va_arg(ap, SSL*);
-       cert = SSL_get_peer_certificate(ssl);
-       if(cert) {
-           *ok = libbalsa_is_cert_known(cert, vfy_result);
-           X509_free(cert);
-       }
-        break;
-    }
-    case SMTP_EV_NO_PEER_CERTIFICATE:
-    case SMTP_EV_WRONG_PEER_CERTIFICATE:
-#if LIBESMTP_1_0_3_AVAILABLE
-    case SMTP_EV_NO_CLIENT_CERTIFICATE:
-#endif
-    {
-       int *ok;
-       ok = va_arg(ap, int*);
-       *ok = 1;
-       break;
-    }
-#endif /* USE_TLS */
-    }
-    va_end (ap);
-}
-#endif /* BALSA_USE_THREADS */
 
 #else /* ESMTP */
 
@@ -1319,20 +1258,8 @@ libbalsa_process_queue(LibBalsaMailbox* outbox, LibBalsaFccboxFinder finder,
 
     send_message_info=send_message_info_new(outbox, finder, debug);
     
-#if 0 && defined(BALSA_USE_THREADS)
-    
-    pthread_create(&send_mail, NULL,
-                  (void *) &balsa_send_message_real, send_message_info);
-    /* Detach so we don't need to pthread_join
-     * This means that all resources will be
-     * reclaimed as soon as the thread exits
-     */
-    pthread_detach(send_mail);
-    
-#else                          /*non-threaded code */
-    
     balsa_send_message_real(send_message_info);
-#endif
+
     send_unlock();
     return TRUE;
 }
@@ -1430,7 +1357,6 @@ static guint
 balsa_send_message_real(SendMessageInfo* info)
 {
     gboolean session_started;
-#ifdef BALSA_USE_THREADS
     SendThreadMessage *threadmsg;
 
     /* The event callback is used to write messages to the the progress
@@ -1439,9 +1365,6 @@ balsa_send_message_real(SendMessageInfo* info)
        feedback in non-MT version.
     */
     smtp_set_eventcb (info->session, libbalsa_smtp_event_cb, NULL);
-#else
-    smtp_set_eventcb (info->session, libbalsa_smtp_event_cb_serial, NULL);
-#endif
 
     /* Add a protocol monitor when debugging is enabled. */
     if(info->debug)
@@ -1498,12 +1421,10 @@ balsa_send_message_real(SendMessageInfo* info)
      * gdk_threads_leave();
      */
 
-#ifdef BALSA_USE_THREADS
-    send_lock();
+    g_mutex_lock(&send_messages_lock);
     MSGSENDTHREAD(threadmsg, MSGSENDTHREADFINISHED, "", NULL, NULL, 0);
     sending_threads--;
-    send_unlock();
-#endif
+    g_mutex_unlock(&send_messages_lock);
         
     smtp_destroy_session (info->session);
     send_message_info_destroy(info);   
diff --git a/libbalsa/send.h b/libbalsa/send.h
index 00cc5b2..fd937c0 100644
--- a/libbalsa/send.h
+++ b/libbalsa/send.h
@@ -96,9 +96,7 @@ gboolean libbalsa_process_queue(LibBalsaMailbox* outbox,
 
 #endif
 
-#ifdef BALSA_USE_THREADS
-extern pthread_t send_mail;
-extern pthread_mutex_t send_messages_lock;
+extern GMutex send_messages_lock;
 extern int send_thread_pipes[2];
 
 typedef struct {
@@ -128,6 +126,4 @@ enum {
     MSGSENDTHREADFINISHED
 };
 
-#endif /* BALSA_USE_THREADS */
-
 #endif /* __SEND_H__ */
diff --git a/src/ab-main.c b/src/ab-main.c
index 718d0b2..b412b39 100644
--- a/src/ab-main.c
+++ b/src/ab-main.c
@@ -104,10 +104,7 @@ bab_config_init(const gchar * group, const gchar * value, gpointer data)
 }
 
 static void ab_warning(const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((format (printf, 1, 2)))
-#endif
-;
+       G_GNUC_PRINTF(1, 2);
 
 enum {
     LIST_COLUMN_NAME,
diff --git a/src/balsa-app.c b/src/balsa-app.c
index 7603af0..aa5268f 100644
--- a/src/balsa-app.c
+++ b/src/balsa-app.c
@@ -27,9 +27,6 @@
 
 #include <string.h>
 #include <stdlib.h>
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#endif
 
 /* for creat(2) */
 #include <sys/types.h>
@@ -128,12 +125,12 @@ ask_password_real(LibBalsaServer * server, LibBalsaMailbox * mbox)
     return passwd;
 }
 
-#ifdef BALSA_USE_THREADS
 typedef struct {
-    pthread_cond_t cond;
+    GCond cond;
     LibBalsaServer* server;
     LibBalsaMailbox* mbox;
     gchar* res;
+    gboolean done;
 } AskPasswdData;
 
 /* ask_passwd_idle:
@@ -145,8 +142,9 @@ ask_passwd_idle(gpointer data)
     AskPasswdData* apd = (AskPasswdData*)data;
     gdk_threads_enter();
     apd->res = ask_password_real(apd->server, apd->mbox);
+    apd->done = TRUE;
     gdk_threads_leave();
-    pthread_cond_signal(&apd->cond);
+    g_cond_signal(&apd->cond);
     return FALSE;
 }
 
@@ -156,21 +154,23 @@ ask_passwd_idle(gpointer data)
 static gchar *
 ask_password_mt(LibBalsaServer * server, LibBalsaMailbox * mbox)
 {
-    static pthread_mutex_t ask_passwd_lock = PTHREAD_MUTEX_INITIALIZER;
+    static GMutex ask_passwd_lock;
     AskPasswdData apd;
 
-    pthread_mutex_lock(&ask_passwd_lock);
-    pthread_cond_init(&apd.cond, NULL);
+    g_mutex_lock(&ask_passwd_lock);
+    g_cond_init(&apd.cond);
     apd.server = server;
     apd.mbox   = mbox;
+    apd.done   = FALSE;
     g_idle_add(ask_passwd_idle, &apd);
-    pthread_cond_wait(&apd.cond, &ask_passwd_lock);
+    while (!apd.done) {
+       g_cond_wait(&apd.cond, &ask_passwd_lock);
+    }
     
-    pthread_cond_destroy(&apd.cond);
-    pthread_mutex_unlock(&ask_passwd_lock);
+    g_cond_clear(&apd.cond);
+    g_mutex_unlock(&ask_passwd_lock);
     return apd.res;
 }
-#endif
 
 static gboolean
 set_passwd_from_matching_server(GtkTreeModel *model,
@@ -241,21 +241,15 @@ ask_password(LibBalsaServer *server, LibBalsaMailbox *mbox)
        }
     }
 
-    if (!password)
-#ifdef BALSA_USE_THREADS
-    {
+    if (!password) {
         G_LOCK_DEFINE_STATIC(ask_password);
 
         G_LOCK(ask_password);
-       password = (pthread_self() == libbalsa_get_main_thread()) ?
+       password = !libbalsa_am_i_subthread() ?
             ask_password_real(server, mbox) : ask_password_mt(server, mbox);
         G_UNLOCK(ask_password);
        return password;
     }
-#else
-       return ask_password_real(server, mbox);
-#endif
-    else
        return password;
 }
 
diff --git a/src/filter-edit-callbacks.c b/src/filter-edit-callbacks.c
index 1d05ca4..e3a0373 100644
--- a/src/filter-edit-callbacks.c
+++ b/src/filter-edit-callbacks.c
@@ -27,10 +27,6 @@
 # include "config.h"
 #endif                          /* HAVE_CONFIG_H */
 
-/* yellow dog has crappy libc and needs pthread.h to be included here */
-#ifdef BALSA_USE_THREADS
-#  include <pthread.h>
-#endif
 #include <time.h>
 
 #include <string.h>
diff --git a/src/information-dialog.h b/src/information-dialog.h
index ad160b5..d6a4de1 100644
--- a/src/information-dialog.h
+++ b/src/information-dialog.h
@@ -34,18 +34,12 @@ enum _BalsaInformationShow {
 };
 
 void balsa_information(LibBalsaInformationType type, const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((format (printf, 2, 3)))
-#endif
-;
+       G_GNUC_PRINTF(2, 3);
 
 void balsa_information_parented(GtkWindow *widget,
                                 LibBalsaInformationType type, 
                                 const char *fmt, ...)
-#ifdef __GNUC__
-    __attribute__ ((format (printf, 3, 4)));
-#endif
-;
+       G_GNUC_PRINTF(3, 4);
 
 void balsa_information_real(GtkWindow *parent, LibBalsaInformationType type,
                             const char *msg);
diff --git a/src/main-window.c b/src/main-window.c
index c42d280..70c72b9 100644
--- a/src/main-window.c
+++ b/src/main-window.c
@@ -65,10 +65,7 @@
 #include "save-restore.h"
 #include "toolbar-prefs.h"
 #include "toolbar-factory.h"
-
-#ifdef BALSA_USE_THREADS
 #include "threads.h"
-#endif
 
 #include "filter.h"
 #include "filter-funcs.h"
@@ -93,7 +90,6 @@ static GtkTargetEntry notebook_drop_types[NUM_DROP_TYPES] = {
     {"x-application/x-message-list", GTK_TARGET_SAME_APP, TARGET_MESSAGES}
 };
 
-#ifdef BALSA_USE_THREADS
 /* Define thread-related globals, including dialogs */
 GtkWidget *progress_dialog = NULL;
 GtkWidget *progress_dialog_source = NULL;
@@ -109,7 +105,6 @@ struct check_messages_thread_info {
 static void bw_check_messages_thread(struct check_messages_thread_info
                                      *info);
 
-#endif
 static void bw_display_new_mail_notification(int num_new, int has_new);
 
 static void balsa_window_class_init(BalsaWindowClass * klass);
@@ -268,7 +263,6 @@ balsa_window_init(BalsaWindow * window)
 static gboolean
 bw_delete_cb(GtkWidget* main_window)
 {
-#ifdef BALSA_USE_THREADS
     /* we cannot leave main window disabled because compose windows
      * (for example) could refuse to get deleted and we would be left
      * with disabled main window. */
@@ -286,7 +280,6 @@ bw_delete_cb(GtkWidget* main_window)
         gtk_widget_destroy(d);
         return retval != GTK_RESPONSE_YES; /* keep running unless OK */
     }
-#endif
     return FALSE; /* allow delete */
 }
 
@@ -2356,10 +2349,8 @@ balsa_window_new()
     g_signal_connect(window, "notify::is-active",
                      G_CALLBACK(bw_is_active_notify), NULL);
 
-#ifdef BALSA_USE_THREADS
     /* set initial state of Get-New-Mail button */
     bw_action_set_enabled(window, "get-new-mail", !checking_mail);
-#endif
 
     g_timeout_add_seconds(30, (GSourceFunc) bw_close_mailbox_on_timer, window);
 
@@ -2890,18 +2881,14 @@ bw_real_open_mbnode_idle_cb(BalsaWindowRealOpenMbnodeInfo * info)
 static void
 bw_real_open_mbnode_thread(BalsaWindowRealOpenMbnodeInfo * info)
 {
-#ifdef BALSA_USE_THREADS
-    static pthread_mutex_t open_lock = PTHREAD_MUTEX_INITIALIZER;
-#endif                          /* BALSA_USE_THREADS */
+    static GMutex open_lock;
     gint try_cnt;
     LibBalsaMailbox *mailbox = info->mbnode->mailbox;
     GError *err = NULL;
     gboolean successp;
 
-#ifdef BALSA_USE_THREADS
     /* Use a mutex to ensure we open only one mailbox at a time */
-    pthread_mutex_lock(&open_lock);
-#endif                          /* BALSA_USE_THREADS */
+    g_mutex_lock(&open_lock);
 
     try_cnt = 0;
     do {
@@ -2933,9 +2920,7 @@ bw_real_open_mbnode_thread(BalsaWindowRealOpenMbnodeInfo * info)
         g_object_unref(info->mbnode);
         g_free(info);
     }
-#ifdef BALSA_USE_THREADS
-    pthread_mutex_unlock(&open_lock);
-#endif                          /* BALSA_USE_THREADS */
+    g_mutex_unlock(&open_lock);
 }
 
 static void
@@ -2946,9 +2931,7 @@ balsa_window_real_open_mbnode(BalsaWindow * window,
     BalsaIndex * index;
     gchar *message;
     LibBalsaMailbox *mailbox;
-#ifdef BALSA_USE_THREADS
-    pthread_t open_thread;
-#endif                          /* BALSA_USE_THREADS */
+    GThread *open_thread;
     BalsaWindowRealOpenMbnodeInfo *info;
 
     if (bw_is_open_mailbox(mailbox = mbnode->mailbox))
@@ -2970,14 +2953,11 @@ balsa_window_real_open_mbnode(BalsaWindow * window,
     info->set_current = set_current;
     info->index = index;
     info->message = message;
-#ifdef BALSA_USE_THREADS
-    pthread_create(&open_thread, NULL,
-                   (void*(*)(void*))bw_real_open_mbnode_thread,
-                   info);
-    pthread_detach(open_thread);
-#else                           /* BALSA_USE_THREADS */
-    bw_real_open_mbnode_thread(info);
-#endif                          /* BALSA_USE_THREADS */
+    open_thread =
+       g_thread_new("bw_real_open_mbnode_thread",
+                                (GThreadFunc) bw_real_open_mbnode_thread,
+                                        info);
+    g_thread_unref(open_thread);
 }
 
 /* balsa_window_real_close_mbnode:
@@ -3151,39 +3131,33 @@ balsa_window_refresh(BalsaWindow * window)
 /* monitored functions for MT-safe manipulation of the open mailbox list
    QUESTION: could they migrate to balsa-app.c?
 */
-#ifdef BALSA_USE_THREADS
-static pthread_mutex_t open_list_lock = PTHREAD_MUTEX_INITIALIZER;
-#define LOCK_OPEN_LIST pthread_mutex_lock(&open_list_lock)
-#define UNLOCK_OPEN_LIST pthread_mutex_unlock(&open_list_lock)
-#else
-#define LOCK_OPEN_LIST
-#define UNLOCK_OPEN_LIST
-#endif
+static GMutex open_list_lock;
+
 static void
 bw_register_open_mailbox(LibBalsaMailbox *m)
 {
-    LOCK_OPEN_LIST;
+       g_mutex_lock(&open_list_lock);
     balsa_app.open_mailbox_list =
         g_list_prepend(balsa_app.open_mailbox_list, m);
-    UNLOCK_OPEN_LIST;
+    g_mutex_unlock(&open_list_lock);
     libbalsa_mailbox_set_open(m, TRUE);
 }
 static void
 bw_unregister_open_mailbox(LibBalsaMailbox *m)
 {
-    LOCK_OPEN_LIST;
+       g_mutex_lock(&open_list_lock);
     balsa_app.open_mailbox_list =
         g_list_remove(balsa_app.open_mailbox_list, m);
-    UNLOCK_OPEN_LIST;
+    g_mutex_unlock(&open_list_lock);
     libbalsa_mailbox_set_open(m, FALSE);
 }
 static gboolean
 bw_is_open_mailbox(LibBalsaMailbox *m)
 {
     GList *res;
-    LOCK_OPEN_LIST;
+    g_mutex_lock(&open_list_lock);
     res= g_list_find(balsa_app.open_mailbox_list, m);
-    UNLOCK_OPEN_LIST;
+    g_mutex_unlock(&open_list_lock);
     return (res != NULL);
 }
 
@@ -3241,7 +3215,6 @@ bw_imap_check_test(const gchar * path)
         strcmp(path, "INBOX") == 0 : balsa_app.check_imap;
 }
 
-#if BALSA_USE_THREADS
 static void
 bw_mailbox_check(LibBalsaMailbox * mailbox, BalsaWindow * window);
 
@@ -3307,23 +3280,6 @@ ensure_check_mail_dialog(BalsaWindow * window)
     gtk_widget_show_all(progress_dialog);
 }
 
-#else /* BALSA_USE_THREADS */
-static void
-bw_mailbox_check(LibBalsaMailbox * mailbox, BalsaWindow * window)
-{
-    if (libbalsa_mailbox_get_subscribe(mailbox) == LB_MAILBOX_SUBSCRIBE_NO)
-        return;
-
-    if (LIBBALSA_IS_MAILBOX_IMAP(mailbox)) {
-        if (window && !window->network_available) {
-                return;
-        }
-    }
-
-    libbalsa_mailbox_check(mailbox);
-}
-#endif /* BALSA_USE_THREADS */
-
 /*
  * Callbacks
  */
@@ -3336,32 +3292,30 @@ void
 check_new_messages_real(BalsaWindow * window, int type)
 {
     GSList *list;
-#if defined(BALSA_USE_THREADS)
     struct check_messages_thread_info *info;
-#endif
+    GThread *get_mail_thread;
 
     if (window && !BALSA_IS_WINDOW(window))
         return;
 
     list = NULL;
-#if defined(BALSA_USE_THREADS)
     /*  Only Run once -- If already checking mail, return.  */
-    pthread_mutex_lock(&checking_mail_lock);
+    g_mutex_lock(&checking_mail_lock);
     if (checking_mail) {
-        pthread_mutex_unlock(&checking_mail_lock);
+        g_mutex_unlock(&checking_mail_lock);
         fprintf(stderr, "Already Checking Mail!\n");
        if (progress_dialog)
            gtk_window_present(GTK_WINDOW(progress_dialog));
         return;
     }
-    checking_mail = 1;
+    checking_mail = TRUE;
     if (window)
         bw_action_set_enabled(window, "get-new-mail", FALSE);
 
     quiet_check = (type == TYPE_CALLBACK)
         ? 0 : balsa_app.quiet_background_check;
 
-    pthread_mutex_unlock(&checking_mail_lock);
+    g_mutex_unlock(&checking_mail_lock);
 
     if (type == TYPE_CALLBACK &&
         (balsa_app.pwindow_option == WHILERETR ||
@@ -3376,36 +3330,16 @@ check_new_messages_real(BalsaWindow * window, int type)
     info = g_new(struct check_messages_thread_info, 1);
     info->list = list;
     info->window = window ? g_object_ref(window) : window;
-    pthread_create(&get_mail_thread,
-                   NULL, (void *) &bw_check_messages_thread, info);
+    get_mail_thread =
+       g_thread_new("bw_check_messages_thread",
+                                (GThreadFunc) bw_check_messages_thread,
+                                        info);
 
-    /* Detach so we don't need to pthread_join
+    /* Detach so we don't need to g_thread_join
      * This means that all resources will be
      * reclaimed as soon as the thread exits
      */
-    pthread_detach(get_mail_thread);
-#else
-
-    if (window)
-        bw_action_set_enabled(window, "get-new-mail", FALSE);
-
-    bw_check_mailbox_list(window, balsa_app.inbox_input);
-
-    if (!balsa_app.mblist_tree_store)
-        return;
-    gtk_tree_model_foreach(GTK_TREE_MODEL(balsa_app.mblist_tree_store),
-                          (GtkTreeModelForeachFunc) bw_add_mbox_to_checklist,
-                          &list);
-    g_slist_foreach(list, (GFunc) bw_mailbox_check, window);
-    g_slist_foreach(list, (GFunc) g_object_unref, NULL);
-    g_slist_free(list);
-
-    if (window)
-        bw_action_set_enabled(window, "get-new-mail", TRUE);
-
-    if (window->network_available)
-        time(&window->last_check_time);
-#endif
+    g_thread_unref(get_mail_thread);
 }
 
 static void
@@ -3457,7 +3391,6 @@ check_new_messages_count(LibBalsaMailbox * mailbox, gboolean notify)
 }
 
 /* this one is called only in the threaded code */
-#if defined(BALSA_USE_THREADS)
 static void
 bw_mailbox_check(LibBalsaMailbox * mailbox, BalsaWindow * window)
 {
@@ -3496,15 +3429,13 @@ static void
 bw_check_messages_thread(struct check_messages_thread_info *info)
 {
     /*
-     *  It is assumed that this will always be called as a pthread,
+     *  It is assumed that this will always be called as a GThread,
      *  and that the calling procedure will check for an existing lock
      *  and set checking_mail to true before calling.
      */
     MailThreadMessage *threadmessage;
     GSList *list = info->list;
 
-    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
-
     MSGMAILTHREAD(threadmessage, LIBBALSA_NTFY_SOURCE, NULL, "POP3", 0, 0);
     bw_check_mailbox_list(info->window, balsa_app.inbox_input);
 
@@ -3515,8 +3446,8 @@ bw_check_messages_thread(struct check_messages_thread_info *info)
     MSGMAILTHREAD(threadmessage, LIBBALSA_NTFY_FINISHED, NULL, "Finished",
                   0, 0);
 
-    pthread_mutex_lock(&checking_mail_lock);
-    checking_mail = 0;
+    g_mutex_lock(&checking_mail_lock);
+    checking_mail = FALSE;
 
     if (info->window) {
         g_idle_add((GSourceFunc) bw_check_messages_thread_idle_cb,
@@ -3525,10 +3456,10 @@ bw_check_messages_thread(struct check_messages_thread_info *info)
             time(&info->window->last_check_time);
         g_object_unref(info->window);
     }
-    pthread_mutex_unlock(&checking_mail_lock);
+    g_mutex_unlock(&checking_mail_lock);
 
     g_free(info);
-    pthread_exit(0);
+    g_thread_exit(0);
 }
 
 /* mail_progress_notify_cb:
@@ -3759,7 +3690,6 @@ send_progress_notify_cb(GIOChannel * source, GIOCondition condition,
     return TRUE;
 }
 
-#endif /* BALSA_USE_THREADS */
 
 /** Returns properly formatted string informing the user about the
     amount of the new mail.
diff --git a/src/main.c b/src/main.c
index 9737c81..a2354f1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -27,10 +27,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
-#endif
-
 #ifdef HAVE_LOCALE_H
 #include <locale.h>
 #endif
@@ -53,6 +49,7 @@
 #include "information.h"
 #include "imap-server.h"
 #include "libbalsa-conf.h"
+#include "threads.h"
 
 #include "libinit_balsa/assistant_init.h"
 
@@ -61,14 +58,9 @@
 #include "libbalsa-gpgme-cb.h"
 #endif
 
-#ifdef BALSA_USE_THREADS
-#include "threads.h"
-
 /* Globals for Thread creation, messaging, pipe I/O */
-pthread_t get_mail_thread;
-pthread_t send_mail;
-pthread_mutex_t send_messages_lock;
-int checking_mail;
+GMutex send_messages_lock;
+gboolean checking_mail;
 int mail_thread_pipes[2];
 int send_thread_pipes[2];
 GIOChannel *mail_thread_msg_send;
@@ -78,7 +70,6 @@ GIOChannel *send_thread_msg_receive;
 
 static void threads_init(void);
 static void threads_destroy(void);
-#endif                         /* BALSA_USE_THREADS */
 
 static void config_init(gboolean check_only);
 static void mailboxes_init(gboolean check_only);
@@ -183,14 +174,12 @@ mailboxes_init(gboolean check_only)
     }
 }
 
-#ifdef BALSA_USE_THREADS
-
-pthread_mutex_t checking_mail_lock = PTHREAD_MUTEX_INITIALIZER;
+GMutex checking_mail_lock;
 
 static void
 threads_init(void)
 {
-    pthread_mutex_init(&send_messages_lock, NULL);
+    g_mutex_init(&send_messages_lock);
     if (pipe(mail_thread_pipes) < 0) {
        g_log("BALSA Init", G_LOG_LEVEL_DEBUG,
              "Error opening pipes.\n");
@@ -217,12 +206,10 @@ threads_init(void)
 static void
 threads_destroy(void)
 {
-    pthread_mutex_destroy(&checking_mail_lock);
-    pthread_mutex_destroy(&send_messages_lock);
+    g_mutex_clear(&checking_mail_lock);
+    g_mutex_clear(&send_messages_lock);
 }
 
-#endif                         /* BALSA_USE_THREADS */
-
 /* initial_open_mailboxes:
    open mailboxes on startup if requested so.
    This is an idle handler. Be sure to use gdk_threads_{enter/leave}
@@ -561,10 +548,8 @@ real_main(int argc, char *argv[])
     setlocale(LC_ALL, "");
 #endif
 
-#ifdef BALSA_USE_THREADS
     /* initiate thread mutexs, variables */
     threads_init();
-#endif
 
 #ifdef HAVE_RUBRICA
     /* initialise libxml */
@@ -641,9 +626,8 @@ real_main(int argc, char *argv[])
     balsa_cleanup();
     accel_map_save();
 
-#ifdef BALSA_USE_THREADS
     threads_destroy();
-#endif
+
     libbalsa_imap_server_close_all_connections();
     return 0;
 }
@@ -651,25 +635,6 @@ real_main(int argc, char *argv[])
 static void
 balsa_cleanup(void)
 {
-#ifdef BALSA_USE_THREADS
-    /* move threads shutdown to separate routine?
-       There are actually many things to do, e.g. threads should not
-       be started after this point.
-    */
-    pthread_mutex_lock(&checking_mail_lock);
-    if(checking_mail) {
-        /* We want to quit but there is a checking thread active.
-           The alternatives are to:
-           a. wait for the checking thread to finish - but it could be
-           time consuming.
-           b. send cancel signal to it.
-        */
-        pthread_cancel(get_mail_thread);
-        printf("Mail check thread cancelled. I know it is rough.\n");
-        sleep(1);
-    }
-    pthread_mutex_unlock(&checking_mail_lock);
-#endif
     balsa_app_destroy();
 
     libbalsa_conf_drop_all();
diff --git a/src/save-restore.c b/src/save-restore.c
index 51c374d..2ea3cc4 100644
--- a/src/save-restore.c
+++ b/src/save-restore.c
@@ -39,10 +39,7 @@
 #include "filter-funcs.h"
 #include "mailbox-filter.h"
 #include "libbalsa-conf.h"
-
-#ifdef BALSA_USE_THREADS
 #include "threads.h"
-#endif
 
 #if ENABLE_ESMTP
 #include "smtp-server.h"
@@ -379,7 +376,6 @@ static void
 pop3_progress_notify(LibBalsaMailbox* mailbox, int msg_type, int prog, int tot,
                      const char* msg)
 {
-#ifdef BALSA_USE_THREADS
     MailThreadMessage *message;
 
     message = g_new(MailThreadMessage, 1);
@@ -397,11 +393,6 @@ pop3_progress_notify(LibBalsaMailbox* mailbox, int msg_type, int prog, int tot,
     if (write(mail_thread_pipes[1], (void *) &message, sizeof(void *))
         != sizeof(void *))
         g_warning("pipe error");
-#else
-    while(gtk_events_pending())
-        gtk_main_iteration_do(FALSE);
-#endif
-    /* cppcheck-suppress memleak */
 }
 
 /* Initialize the specified mailbox, creating the internal data
diff --git a/src/sendmsg-window.c b/src/sendmsg-window.c
index a58df68..3106680 100644
--- a/src/sendmsg-window.c
+++ b/src/sendmsg-window.c
@@ -59,11 +59,7 @@
 #include "balsa-message.h"
 #include "balsa-index.h"
 #include "balsa-icons.h"
-
-#ifdef BALSA_USE_THREADS
-#include <pthread.h>
 #include "threads.h"
-#endif
 
 #include "missing.h"
 #include "ab-window.h"
@@ -665,9 +661,7 @@ balsa_sendmsg_destroy_handler(BalsaSendmsg * bsmsg)
     g_free(bsmsg);
 
     if (quit_on_close) {
-#ifdef BALSA_USE_THREADS
         libbalsa_wait_for_sending_thread(-1);
-#endif
        gtk_main_quit();
     }
     if(balsa_app.debug) g_message("balsa_sendmsg_destroy(): Stop.");
diff --git a/src/threads.h b/src/threads.h
index 7e3e424..bebfc6b 100644
--- a/src/threads.h
+++ b/src/threads.h
@@ -25,11 +25,10 @@
 #include <unistd.h>
 
 /*  allocated in main.c */
-extern pthread_mutex_t checking_mail_lock;
+extern GMutex checking_mail_lock;
 
 /*  define thread globals */
-extern pthread_t get_mail_thread;
-extern int checking_mail;
+extern gboolean checking_mail;
 extern int mail_thread_pipes[2];
 extern GIOChannel *mail_thread_msg_send;
 extern GIOChannel *mail_thread_msg_receive;


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