[evolution-data-server] Bug #615776 - Drop camel-private.h and offer a public alternative for locks



commit b50b385f39f183df5726f8174627a3108fc9518b
Author: Milan Crha <mcrha redhat com>
Date:   Thu Apr 15 20:47:13 2010 +0200

    Bug #615776 - Drop camel-private.h and offer a public alternative for locks

 camel/Makefile.am                                  |    2 +-
 camel/camel-block-file.c                           |    1 -
 camel/camel-certdb.c                               |  123 ++++++--
 camel/camel-certdb.h                               |   10 +
 camel/camel-data-wrapper.c                         |   61 ++++-
 camel/camel-data-wrapper.h                         |    7 +
 camel/camel-filter-driver.c                        |    1 -
 camel/camel-folder-summary.c                       |  314 +++++++++++++-------
 camel/camel-folder-summary.h                       |   11 +
 camel/camel-folder.c                               |  131 +++++++--
 camel/camel-folder.h                               |    9 +
 camel/camel-mime-parser.c                          |    1 -
 camel/camel-offline-journal.c                      |    2 +-
 camel/camel-private.h                              |  210 -------------
 camel/camel-provider.c                             |    2 +-
 camel/camel-service.c                              |   98 +++++--
 camel/camel-service.h                              |    8 +
 camel/camel-session.c                              |   90 +++++-
 camel/camel-session.h                              |    8 +
 camel/camel-store-summary.c                        |  178 ++++++++----
 camel/camel-store-summary.h                        |    9 +
 camel/camel-store.c                                |   89 +++++--
 camel/camel-store.h                                |    7 +
 camel/camel-stream-fs.c                            |    2 +-
 camel/camel-stream-vfs.c                           |    1 -
 camel/camel-tcp-stream-ssl.c                       |    1 -
 camel/camel-text-index.c                           |    1 -
 camel/camel-transport.c                            |   59 ++++-
 camel/camel-transport.h                            |    7 +
 camel/camel-uid-cache.c                            |    2 +-
 camel/camel-vee-folder.c                           |  262 +++++++++++------
 camel/camel-vee-folder.h                           |   12 +
 camel/camel-vee-store.c                            |    1 -
 camel/camel-vee-summary.c                          |   21 +-
 camel/camel-vtrash-folder.c                        |    1 -
 camel/camel-win32.c                                |    2 +-
 camel/camel-win32.h                                |   58 ++++
 camel/camel.c                                      |    2 +-
 camel/providers/groupwise/camel-groupwise-folder.c |   84 +++---
 .../groupwise/camel-groupwise-store-summary.c      |    6 +-
 camel/providers/groupwise/camel-groupwise-store.c  |   76 +++---
 camel/providers/imap/camel-imap-command.c          |   22 +-
 camel/providers/imap/camel-imap-folder.c           |   68 ++---
 camel/providers/imap/camel-imap-store-summary.c    |    6 +-
 camel/providers/imap/camel-imap-store.c            |   72 +++---
 camel/providers/imapx/camel-imapx-server.c         |    6 +-
 camel/providers/imapx/camel-imapx-store-summary.c  |    6 +-
 camel/providers/imapx/camel-imapx-store.c          |    6 +-
 camel/providers/imapx/camel-imapx-tokenise.h       |    5 +-
 camel/providers/local/camel-local-folder.c         |    2 -
 camel/providers/local/camel-mh-summary.c           |    6 +-
 camel/providers/local/camel-spool-summary.c        |    1 +
 camel/providers/nntp/camel-nntp-folder.c           |   28 +-
 camel/providers/nntp/camel-nntp-store-summary.c    |    6 +-
 camel/providers/nntp/camel-nntp-store.c            |   36 +--
 camel/providers/pop3/camel-pop3-engine.c           |    3 +-
 camel/providers/smtp/camel-smtp-transport.c        |    4 +-
 57 files changed, 1421 insertions(+), 826 deletions(-)
---
diff --git a/camel/Makefile.am b/camel/Makefile.am
index b1593c2..a885174 100644
--- a/camel/Makefile.am
+++ b/camel/Makefile.am
@@ -392,7 +392,7 @@ install-exec-hook:
 
 noinst_HEADERS =				\
 	camel-charset-map-private.h		\
-	camel-private.h				
+	camel-win32.h				
 
 BUILT_SOURCES = camel-mime-tables.c
 
diff --git a/camel/camel-block-file.c b/camel/camel-block-file.c
index 8b4cda0..0613e3f 100644
--- a/camel/camel-block-file.c
+++ b/camel/camel-block-file.c
@@ -37,7 +37,6 @@
 #include "camel-block-file.h"
 #include "camel-file-utils.h"
 #include "camel-list-utils.h"
-#include "camel-private.h"
 
 #define d(x) /*(printf("%s(%d):%s: ",  __FILE__, __LINE__, __PRETTY_FUNCTION__),(x))*/
 
diff --git a/camel/camel-certdb.c b/camel/camel-certdb.c
index 4d0cda1..6658190 100644
--- a/camel/camel-certdb.c
+++ b/camel/camel-certdb.c
@@ -38,10 +38,17 @@
 
 #include "camel-certdb.h"
 #include "camel-file-utils.h"
-#include "camel-private.h"
+#include "camel-win32.h"
 
 #define CAMEL_CERTDB_VERSION  0x100
 
+struct _CamelCertDBPrivate {
+	GMutex *db_lock;	/* for the db hashtable/array */
+	GMutex *io_lock;	/* load/save lock, for access to saved_count, etc */
+	GMutex *alloc_lock;	/* for setting up and using allocators */
+	GMutex *ref_lock;	/* for reffing/unreffing certs */
+};
+
 static gint certdb_header_load (CamelCertDB *certdb, FILE *istream);
 static gint certdb_header_save (CamelCertDB *certdb, FILE *ostream);
 static CamelCert *certdb_cert_load (CamelCertDB *certdb, FILE *istream);
@@ -187,12 +194,12 @@ camel_certdb_set_filename (CamelCertDB *certdb, const gchar *filename)
 	g_return_if_fail (CAMEL_IS_CERTDB (certdb));
 	g_return_if_fail (filename != NULL);
 
-	CAMEL_CERTDB_LOCK (certdb, db_lock);
+	camel_certdb_lock (certdb, CCD_DB_LOCK);
 
 	g_free (certdb->filename);
 	certdb->filename = g_strdup (filename);
 
-	CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+	camel_certdb_unlock (certdb, CCD_DB_LOCK);
 }
 
 static gint
@@ -252,7 +259,7 @@ camel_certdb_load (CamelCertDB *certdb)
 	g_return_val_if_fail (class->header_load != NULL, -1);
 	g_return_val_if_fail (class->cert_load != NULL, -1);
 
-	CAMEL_CERTDB_LOCK (certdb, io_lock);
+	camel_certdb_lock (certdb, CCD_IO_LOCK);
 	if (class->header_load (certdb, in) == -1)
 		goto error;
 
@@ -265,7 +272,7 @@ camel_certdb_load (CamelCertDB *certdb)
 		camel_certdb_add (certdb, cert);
 	}
 
-	CAMEL_CERTDB_UNLOCK (certdb, io_lock);
+	camel_certdb_unlock (certdb, CCD_IO_LOCK);
 
 	if (fclose (in) != 0)
 		return -1;
@@ -278,7 +285,7 @@ camel_certdb_load (CamelCertDB *certdb)
 
 	g_warning ("Cannot load certificate database: %s", g_strerror (ferror (in)));
 
-	CAMEL_CERTDB_UNLOCK (certdb, io_lock);
+	camel_certdb_unlock (certdb, CCD_IO_LOCK);
 
 	fclose (in);
 
@@ -349,7 +356,7 @@ camel_certdb_save (CamelCertDB *certdb)
 	g_return_val_if_fail (class->header_save != NULL, -1);
 	g_return_val_if_fail (class->cert_save != NULL, -1);
 
-	CAMEL_CERTDB_LOCK (certdb, io_lock);
+	camel_certdb_lock (certdb, CCD_IO_LOCK);
 
 	certdb->saved_certs = certdb->certs->len;
 	if (class->header_save (certdb, out) == -1)
@@ -362,7 +369,7 @@ camel_certdb_save (CamelCertDB *certdb)
 			goto error;
 	}
 
-	CAMEL_CERTDB_UNLOCK (certdb, io_lock);
+	camel_certdb_unlock (certdb, CCD_IO_LOCK);
 
 	if (fflush (out) != 0 || fsync (fileno (out)) == -1) {
 		i = errno;
@@ -394,7 +401,7 @@ camel_certdb_save (CamelCertDB *certdb)
 
 	g_warning ("Cannot save certificate database: %s", g_strerror (ferror (out)));
 
-	CAMEL_CERTDB_UNLOCK (certdb, io_lock);
+	camel_certdb_unlock (certdb, CCD_IO_LOCK);
 
 	i = errno;
 	fclose (out);
@@ -419,13 +426,13 @@ camel_certdb_get_cert (CamelCertDB *certdb, const gchar *fingerprint)
 
 	g_return_val_if_fail (CAMEL_IS_CERTDB (certdb), NULL);
 
-	CAMEL_CERTDB_LOCK (certdb, db_lock);
+	camel_certdb_lock (certdb, CCD_DB_LOCK);
 
 	cert = g_hash_table_lookup (certdb->cert_hash, fingerprint);
 	if (cert)
 		camel_certdb_cert_ref (certdb, cert);
 
-	CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+	camel_certdb_unlock (certdb, CCD_DB_LOCK);
 
 	return cert;
 }
@@ -435,10 +442,10 @@ camel_certdb_add (CamelCertDB *certdb, CamelCert *cert)
 {
 	g_return_if_fail (CAMEL_IS_CERTDB (certdb));
 
-	CAMEL_CERTDB_LOCK (certdb, db_lock);
+	camel_certdb_lock (certdb, CCD_DB_LOCK);
 
 	if (g_hash_table_lookup (certdb->cert_hash, cert->fingerprint)) {
-		CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+		camel_certdb_unlock (certdb, CCD_DB_LOCK);
 		return;
 	}
 
@@ -448,7 +455,7 @@ camel_certdb_add (CamelCertDB *certdb, CamelCert *cert)
 
 	certdb->flags |= CAMEL_CERTDB_DIRTY;
 
-	CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+	camel_certdb_unlock (certdb, CCD_DB_LOCK);
 }
 
 void
@@ -456,7 +463,7 @@ camel_certdb_remove (CamelCertDB *certdb, CamelCert *cert)
 {
 	g_return_if_fail (CAMEL_IS_CERTDB (certdb));
 
-	CAMEL_CERTDB_LOCK (certdb, db_lock);
+	camel_certdb_lock (certdb, CCD_DB_LOCK);
 
 	if (g_hash_table_lookup (certdb->cert_hash, cert->fingerprint)) {
 		g_hash_table_remove (certdb->cert_hash, cert->fingerprint);
@@ -466,7 +473,7 @@ camel_certdb_remove (CamelCertDB *certdb, CamelCert *cert)
 		certdb->flags |= CAMEL_CERTDB_DIRTY;
 	}
 
-	CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+	camel_certdb_unlock (certdb, CCD_DB_LOCK);
 }
 
 static CamelCert *
@@ -495,11 +502,11 @@ camel_certdb_cert_new (CamelCertDB *certdb)
 	class = CAMEL_CERTDB_GET_CLASS (certdb);
 	g_return_val_if_fail (class->cert_new != NULL, NULL);
 
-	CAMEL_CERTDB_LOCK (certdb, alloc_lock);
+	camel_certdb_lock (certdb, CCD_ALLOC_LOCK);
 
 	cert = class->cert_new (certdb);
 
-	CAMEL_CERTDB_UNLOCK (certdb, alloc_lock);
+	camel_certdb_unlock (certdb, CCD_ALLOC_LOCK);
 
 	return cert;
 }
@@ -510,9 +517,9 @@ camel_certdb_cert_ref (CamelCertDB *certdb, CamelCert *cert)
 	g_return_if_fail (CAMEL_IS_CERTDB (certdb));
 	g_return_if_fail (cert != NULL);
 
-	CAMEL_CERTDB_LOCK (certdb, ref_lock);
+	camel_certdb_lock (certdb, CCD_REF_LOCK);
 	cert->refcount++;
-	CAMEL_CERTDB_UNLOCK (certdb, ref_lock);
+	camel_certdb_unlock (certdb, CCD_REF_LOCK);
 }
 
 static void
@@ -538,7 +545,7 @@ camel_certdb_cert_unref (CamelCertDB *certdb,
 	class = CAMEL_CERTDB_GET_CLASS (certdb);
 	g_return_if_fail (class->cert_free != NULL);
 
-	CAMEL_CERTDB_LOCK (certdb, ref_lock);
+	camel_certdb_lock (certdb, CCD_REF_LOCK);
 
 	if (cert->refcount <= 1) {
 		class->cert_free (certdb, cert);
@@ -550,7 +557,7 @@ camel_certdb_cert_unref (CamelCertDB *certdb,
 		cert->refcount--;
 	}
 
-	CAMEL_CERTDB_UNLOCK (certdb, ref_lock);
+	camel_certdb_unlock (certdb, CCD_REF_LOCK);
 }
 
 static gboolean
@@ -567,7 +574,7 @@ camel_certdb_clear (CamelCertDB *certdb)
 
 	g_return_if_fail (CAMEL_IS_CERTDB (certdb));
 
-	CAMEL_CERTDB_LOCK (certdb, db_lock);
+	camel_certdb_lock (certdb, CCD_DB_LOCK);
 
 	g_hash_table_foreach_remove (certdb->cert_hash, cert_remove, NULL);
 	for (i = 0; i < certdb->certs->len; i++) {
@@ -579,7 +586,7 @@ camel_certdb_clear (CamelCertDB *certdb)
 	g_ptr_array_set_size (certdb->certs, 0);
 	certdb->flags |= CAMEL_CERTDB_DIRTY;
 
-	CAMEL_CERTDB_UNLOCK (certdb, db_lock);
+	camel_certdb_unlock (certdb, CCD_DB_LOCK);
 }
 
 static const gchar *
@@ -678,3 +685,71 @@ camel_cert_set_trust (CamelCertDB *certdb, CamelCert *cert, CamelCertTrust trust
 
 	cert->trust = trust;
 }
+
+/**
+ * camel_certdb_lock:
+ * @certdb: a #CamelCertDB
+ * @lock: lock type to lock
+ *
+ * Locks #certdb's #lock. Unlock it with camel_certdb_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_certdb_lock (CamelCertDB *certdb, CamelCertDBLock lock)
+{
+	g_return_if_fail (certdb != NULL);
+	g_return_if_fail (CAMEL_IS_CERTDB (certdb));
+	g_return_if_fail (certdb->priv != NULL);
+
+	switch (lock) {
+	case CCD_DB_LOCK:
+		g_mutex_lock (certdb->priv->db_lock);
+		break;
+	case CCD_IO_LOCK:
+		g_mutex_lock (certdb->priv->io_lock);
+		break;
+	case CCD_ALLOC_LOCK:
+		g_mutex_lock (certdb->priv->alloc_lock);
+		break;
+	case CCD_REF_LOCK:
+		g_mutex_lock (certdb->priv->ref_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_certdb_unlock:
+ * @certdb: a #CamelCertDB
+ * @lock: lock type to unlock
+ *
+ * Unlocks #certdb's #lock, previously locked with camel_certdb_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_certdb_unlock (CamelCertDB *certdb, CamelCertDBLock lock)
+{
+	g_return_if_fail (certdb != NULL);
+	g_return_if_fail (CAMEL_IS_CERTDB (certdb));
+	g_return_if_fail (certdb->priv != NULL);
+
+	switch (lock) {
+	case CCD_DB_LOCK:
+		g_mutex_unlock (certdb->priv->db_lock);
+		break;
+	case CCD_IO_LOCK:
+		g_mutex_unlock (certdb->priv->io_lock);
+		break;
+	case CCD_ALLOC_LOCK:
+		g_mutex_unlock (certdb->priv->alloc_lock);
+		break;
+	case CCD_REF_LOCK:
+		g_mutex_unlock (certdb->priv->ref_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-certdb.h b/camel/camel-certdb.h
index 4d58128..90e711f 100644
--- a/camel/camel-certdb.h
+++ b/camel/camel-certdb.h
@@ -74,6 +74,13 @@ typedef struct {
 	GByteArray *rawcert;
 } CamelCert;
 
+typedef enum _CamelCertDBLock {
+	CCD_DB_LOCK,
+	CCD_IO_LOCK,
+	CCD_ALLOC_LOCK,
+	CCD_REF_LOCK
+} CamelCertDBLock;
+
 struct _CamelCertDB {
 	CamelObject parent;
 	CamelCertDBPrivate *priv;
@@ -148,6 +155,9 @@ void camel_cert_set_string (CamelCertDB *certdb, CamelCert *cert, gint string, c
 CamelCertTrust camel_cert_get_trust (CamelCertDB *certdb, CamelCert *cert);
 void camel_cert_set_trust (CamelCertDB *certdb, CamelCert *cert, CamelCertTrust trust);
 
+void camel_certdb_lock	 (CamelCertDB *certdb, CamelCertDBLock lock);
+void camel_certdb_unlock (CamelCertDB *certdb, CamelCertDBLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_CERTDB_H */
diff --git a/camel/camel-data-wrapper.c b/camel/camel-data-wrapper.c
index f13ff9c..f3f530b 100644
--- a/camel/camel-data-wrapper.c
+++ b/camel/camel-data-wrapper.c
@@ -29,12 +29,15 @@
 #include "camel-data-wrapper.h"
 #include "camel-mime-filter-basic.h"
 #include "camel-mime-filter-crlf.h"
-#include "camel-private.h"
 #include "camel-stream-filter.h"
 #include "camel-stream.h"
 
 #define d(x)
 
+struct _CamelDataWrapperPrivate {
+	GStaticMutex stream_lock;
+};
+
 static CamelObjectClass *parent_class = NULL;
 
 static void
@@ -63,15 +66,15 @@ data_wrapper_write_to_stream (CamelDataWrapper *data_wrapper,
 		return -1;
 	}
 
-	CAMEL_DATA_WRAPPER_LOCK (data_wrapper, stream_lock);
+	camel_data_wrapper_lock (data_wrapper, CDW_STREAM_LOCK);
 	if (camel_stream_reset (data_wrapper->stream) == -1) {
-		CAMEL_DATA_WRAPPER_UNLOCK (data_wrapper, stream_lock);
+		camel_data_wrapper_unlock (data_wrapper, CDW_STREAM_LOCK);
 		return -1;
 	}
 
 	ret = camel_stream_write_to_stream (data_wrapper->stream, stream);
 
-	CAMEL_DATA_WRAPPER_UNLOCK (data_wrapper, stream_lock);
+	camel_data_wrapper_unlock (data_wrapper, CDW_STREAM_LOCK);
 
 	return ret;
 }
@@ -414,3 +417,53 @@ camel_data_wrapper_is_offline (CamelDataWrapper *data_wrapper)
 
 	return class->is_offline (data_wrapper);
 }
+
+/**
+ * camel_data_wrapper_lock:
+ * @data_wrapper: a #CamelDataWrapper
+ * @lock: lock type to lock
+ *
+ * Locks #data_wrapper's #lock. Unlock it with camel_data_wrapper_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_data_wrapper_lock (CamelDataWrapper *data_wrapper, CamelDataWrapperLock lock)
+{
+	g_return_if_fail (data_wrapper != NULL);
+	g_return_if_fail (CAMEL_IS_DATA_WRAPPER (data_wrapper));
+	g_return_if_fail (data_wrapper->priv != NULL);
+
+	switch (lock) {
+	case CDW_STREAM_LOCK:
+		g_static_mutex_lock (&data_wrapper->priv->stream_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_data_wrapper_unlock:
+ * @data_wrapper: a #CamelDataWrapper
+ * @lock: lock type to unlock
+ *
+ * Unlocks #data_wrapper's #lock, previously locked with camel_data_wrapper_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_data_wrapper_unlock (CamelDataWrapper *data_wrapper, CamelDataWrapperLock lock)
+{
+	g_return_if_fail (data_wrapper != NULL);
+	g_return_if_fail (CAMEL_IS_DATA_WRAPPER (data_wrapper));
+	g_return_if_fail (data_wrapper->priv != NULL);
+
+	switch (lock) {
+	case CDW_STREAM_LOCK:
+		g_static_mutex_unlock (&data_wrapper->priv->stream_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-data-wrapper.h b/camel/camel-data-wrapper.h
index dedd5bd..b1444e0 100644
--- a/camel/camel-data-wrapper.h
+++ b/camel/camel-data-wrapper.h
@@ -47,6 +47,10 @@ typedef struct _CamelDataWrapper CamelDataWrapper;
 typedef struct _CamelDataWrapperClass CamelDataWrapperClass;
 typedef struct _CamelDataWrapperPrivate CamelDataWrapperPrivate;
 
+typedef enum _CamelDataWrapperLock {
+	CDW_STREAM_LOCK
+} CamelDataWrapperLock;
+
 struct _CamelDataWrapper {
 	CamelObject parent;
 	CamelDataWrapperPrivate *priv;
@@ -101,6 +105,9 @@ gint		camel_data_wrapper_construct_from_stream
 						 CamelStream *stream);
 gboolean	camel_data_wrapper_is_offline	(CamelDataWrapper *data_wrapper);
 
+void		camel_data_wrapper_lock		(CamelDataWrapper *data_wrapper, CamelDataWrapperLock lock);
+void		camel_data_wrapper_unlock	(CamelDataWrapper *data_wrapper, CamelDataWrapperLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_DATA_WRAPPER_H */
diff --git a/camel/camel-filter-driver.c b/camel/camel-filter-driver.c
index 2320151..f31c230 100644
--- a/camel/camel-filter-driver.c
+++ b/camel/camel-filter-driver.c
@@ -46,7 +46,6 @@
 #include "camel-filter-search.h"
 #include "camel-list-utils.h"
 #include "camel-mime-message.h"
-#include "camel-private.h"
 #include "camel-service.h"
 #include "camel-session.h"
 #include "camel-stream-fs.h"
diff --git a/camel/camel-folder-summary.c b/camel/camel-folder-summary.c
index 5429a0f..aeb38bd 100644
--- a/camel/camel-folder-summary.c
+++ b/camel/camel-folder-summary.c
@@ -49,7 +49,6 @@
 #include "camel-mime-filter.h"
 #include "camel-mime-message.h"
 #include "camel-multipart.h"
-#include "camel-private.h"
 #include "camel-session.h"
 #include "camel-stream-filter.h"
 #include "camel-stream-mem.h"
@@ -58,11 +57,37 @@
 #include "camel-store.h"
 #include "camel-vee-folder.h"
 #include "camel-mime-part-utils.h"
+#include "camel-win32.h"
 
 /* Make 5 minutes as default cache drop */
 #define SUMMARY_CACHE_DROP 300 
 #define dd(x) if (camel_debug("sync")) x
 
+struct _CamelFolderSummaryPrivate {
+	GHashTable *filter_charset;	/* CamelMimeFilterCharset's indexed by source charset */
+
+	struct _CamelMimeFilter *filter_index;
+	struct _CamelMimeFilter *filter_64;
+	struct _CamelMimeFilter *filter_qp;
+	struct _CamelMimeFilter *filter_uu;
+	struct _CamelMimeFilter *filter_save;
+	struct _CamelMimeFilter *filter_html;
+
+	struct _CamelStream *filter_stream;
+
+	struct _CamelIndex *index;
+
+	GMutex *summary_lock;	/* for the summary hashtable/array */
+	GMutex *io_lock;	/* load/save lock, for access to saved_count, etc */
+	GMutex *filter_lock;	/* for accessing any of the filtering/indexing stuff, since we share them */
+	GMutex *alloc_lock;	/* for setting up and using allocators */
+	GMutex *ref_lock;	/* for reffing/unreffing messageinfo's ALWAYS obtain before summary_lock */
+	GHashTable *flag_cache;
+
+	gboolean need_preview;
+	GHashTable *preview_updates;
+};
+
 static GStaticMutex info_lock = G_STATIC_MUTEX_INIT;
 
 /* this lock is ONLY for the standalone messageinfo stuff */
@@ -279,7 +304,7 @@ camel_folder_summary_new (struct _CamelFolder *folder)
 void
 camel_folder_summary_set_filename(CamelFolderSummary *s, const gchar *name)
 {
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	g_free(s->summary_path);
 	s->summary_path = g_strdup(name);
@@ -287,7 +312,7 @@ camel_folder_summary_set_filename(CamelFolderSummary *s, const gchar *name)
 	g_free(s->meta_summary->path);
 	s->meta_summary->path = g_strconcat(name, "-meta", NULL);
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 }
 
 /**
@@ -359,8 +384,8 @@ camel_folder_summary_index (CamelFolderSummary *s, gint i)
 {
 	CamelMessageInfo *info = NULL;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
-	CAMEL_SUMMARY_LOCK(s, ref_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
+	camel_folder_summary_lock (s, CFS_REF_LOCK);
 
 	if (i < s->uids->len) {
 		gchar *uid;
@@ -369,14 +394,14 @@ camel_folder_summary_index (CamelFolderSummary *s, gint i)
 		/* FIXME: Get exception from caller
 		and pass it on below */
 
-		CAMEL_SUMMARY_UNLOCK(s, ref_lock);
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_REF_LOCK);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 		return camel_folder_summary_uid (s, uid);
 	}
 
-	CAMEL_SUMMARY_UNLOCK(s, ref_lock);
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_REF_LOCK);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return info;
 }
@@ -402,12 +427,12 @@ gchar *
 camel_folder_summary_uid_from_index (CamelFolderSummary *s, gint i)
 {
 	gchar *uid=NULL;
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	if (i<s->uids->len)
 		uid = g_strdup (g_ptr_array_index(s->uids, i));
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return uid;
 
@@ -431,16 +456,16 @@ camel_folder_summary_check_uid (CamelFolderSummary *s, const gchar *uid)
 	gboolean ret = FALSE;
 	gint i;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	for (i=0; i<s->uids->len; i++) {
 		if (strcmp(s->uids->pdata[i], uid) == 0) {
-			CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+			camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 			return TRUE;
 		}
 	}
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return ret;
 }
@@ -462,13 +487,13 @@ camel_folder_summary_array(CamelFolderSummary *s)
 	GPtrArray *res = g_ptr_array_new();
 	gint i;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	g_ptr_array_set_size(res, s->uids->len);
 	for (i=0;i<s->uids->len;i++)
 		res->pdata[i] = (gpointer) camel_pstring_strdup ((gchar *)g_ptr_array_index(s->uids, i));
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return res;
 }
@@ -492,12 +517,12 @@ camel_folder_summary_get_hashtable(CamelFolderSummary *s)
 	GHashTable *hash = g_hash_table_new (g_str_hash, g_str_equal);
 	gint i;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	for (i=0;i<s->uids->len;i++)
 		g_hash_table_insert (hash, (gpointer)camel_pstring_strdup ((gchar *)g_ptr_array_index(s->uids, i)), GINT_TO_POINTER(1));
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return hash;
 }
@@ -541,7 +566,7 @@ message_info_from_uid (CamelFolderSummary *s, const gchar *uid)
 	CamelMessageInfo *info;
 	gint ret;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	info = g_hash_table_lookup (s->loaded_infos, uid);
 
@@ -558,7 +583,7 @@ message_info_from_uid (CamelFolderSummary *s, const gchar *uid)
 		folder_name = s->folder->full_name;
 		cdb = s->folder->parent_store->cdb_r;
 
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 		data.summary = s;
 		data.double_ref = TRUE;
@@ -570,7 +595,7 @@ message_info_from_uid (CamelFolderSummary *s, const gchar *uid)
 			return NULL;
 		}
 
-		CAMEL_SUMMARY_LOCK(s, summary_lock);
+		camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 		/* We would have double reffed at camel_read_mir_callback */
 		info = g_hash_table_lookup (s->loaded_infos, uid);
@@ -589,7 +614,7 @@ message_info_from_uid (CamelFolderSummary *s, const gchar *uid)
 	if (info)
 		camel_message_info_ref (info);
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return info;
 }
@@ -628,11 +653,11 @@ camel_folder_summary_next_uid(CamelFolderSummary *s)
 {
 	guint32 uid;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	uid = s->nextuid++;
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	/* FIXME: sync this to disk */
 /*	summary_header_save(s);*/
@@ -652,11 +677,11 @@ void
 camel_folder_summary_set_uid(CamelFolderSummary *s, guint32 uid)
 {
 	/* TODO: sync to disk? */
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	s->nextuid = MAX(s->nextuid, uid);
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 }
 
 /**
@@ -758,9 +783,9 @@ camel_folder_summary_get_changed (CamelFolderSummary *s)
 	 * We need to have collate or something to get the modified ones
 	 * from DB and merge */
 
-	CAMEL_SUMMARY_LOCK (s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	g_hash_table_foreach (s->loaded_infos, (GHFunc) append_changed_uids, res);
-	CAMEL_SUMMARY_UNLOCK (s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return res;
 }
@@ -777,9 +802,9 @@ cfs_count_dirty (CamelFolderSummary *s)
 {
 	gint count = 0;
 
-	CAMEL_SUMMARY_LOCK (s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	g_hash_table_foreach (s->loaded_infos, (GHFunc) count_changed_uids, &count);
-	CAMEL_SUMMARY_UNLOCK (s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return count;
 }
@@ -819,11 +844,11 @@ remove_cache (CamelSession *session, CamelSessionThreadMsg *msg)
 
 	dd(printf("removing cache for  %s %d %p\n", s->folder ? s->folder->full_name : s->summary_path, g_hash_table_size (s->loaded_infos), (gpointer) s->loaded_infos));
 	/* FIXME[disk-summary] hack. fix it */
-	CAMEL_SUMMARY_LOCK (s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
-	CAMEL_SUMMARY_LOCK(s, ref_lock);
+	camel_folder_summary_lock (s, CFS_REF_LOCK);
 	g_hash_table_foreach_remove  (s->loaded_infos, (GHRFunc) remove_item, &to_free_list);
-	CAMEL_SUMMARY_UNLOCK(s, ref_lock);
+	camel_folder_summary_unlock (s, CFS_REF_LOCK);
 
 	/* Deferred freeing as _free function will try to remove
 	   entries from the hash_table in foreach_remove otherwise */
@@ -831,7 +856,7 @@ remove_cache (CamelSession *session, CamelSessionThreadMsg *msg)
 		camel_message_info_free (l->data);
 	g_slist_free (to_free_list);
 
-	CAMEL_SUMMARY_UNLOCK (s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 	dd(printf("done .. now %d\n",g_hash_table_size (s->loaded_infos)));
 
 	s->cache_load_time = time(NULL);
@@ -952,20 +977,20 @@ preview_update_exec (CamelSession *session, CamelSessionThreadMsg *msg)
 		g_hash_table_destroy (preview_data);
 	}
 
-	CAMEL_SUMMARY_LOCK (m->folder->summary, summary_lock);
+	camel_folder_summary_lock (m->folder->summary, CFS_SUMMARY_LOCK);
 	g_hash_table_foreach (m->folder->summary->loaded_infos, (GHFunc)pick_uids, uids_uncached);
-	CAMEL_SUMMARY_UNLOCK (m->folder->summary, summary_lock);
+	camel_folder_summary_unlock (m->folder->summary, CFS_SUMMARY_LOCK);
 
 	for (i=0; i < uids_uncached->len; i++) {
 		g_hash_table_remove (hash, uids_uncached->pdata[i]);
 		camel_pstring_free (uids_uncached->pdata[i]); /* unref the hash table key */
 	}
 
-	CAMEL_FOLDER_REC_LOCK(m->folder, lock);
+	camel_folder_lock (m->folder, CF_REC_LOCK);
 	camel_db_begin_transaction (m->folder->parent_store->cdb_w, NULL);
 	g_hash_table_foreach (hash, (GHFunc)msg_update_preview, m->folder);
 	camel_db_end_transaction (m->folder->parent_store->cdb_w, NULL);
-	CAMEL_FOLDER_REC_UNLOCK(m->folder, lock);
+	camel_folder_unlock (m->folder, CF_REC_LOCK);
 	camel_folder_free_uids(m->folder, uids_uncached);
 	camel_folder_summary_free_hashtable (hash);
 }
@@ -1035,9 +1060,9 @@ camel_folder_summary_reload_from_db (CamelFolderSummary *s, CamelException *ex)
 void
 camel_folder_summary_add_preview (CamelFolderSummary *s, CamelMessageInfo *info)
 {
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	g_hash_table_insert (CAMEL_FOLDER_SUMMARY_GET_PRIVATE(s)->preview_updates, (gchar *)info->uid, ((CamelMessageInfoBase *)info)->preview);
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 }
 
 /**
@@ -1218,14 +1243,14 @@ camel_read_mir_callback (gpointer  ref, gint ncol, gchar ** cols, gchar ** name)
 	mir = g_new0 (CamelMIRecord , 1);
 	mir_from_cols (mir, s, ncol, cols, name);
 
-	CAMEL_SUMMARY_LOCK (s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	if (g_hash_table_lookup (s->loaded_infos, mir->uid)) {
 		/* Unlock and better return*/
-		CAMEL_SUMMARY_UNLOCK (s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 		camel_db_camel_mir_free (mir);
 		return ret;
 	}
-	CAMEL_SUMMARY_UNLOCK (s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	info = ((CamelFolderSummaryClass *)(CAMEL_OBJECT_GET_CLASS(s)))->message_info_from_db (s, mir);
 
@@ -1295,7 +1320,7 @@ camel_folder_summary_load(CamelFolderSummary *s)
 	if (in == NULL)
 		return -1;
 
-	CAMEL_SUMMARY_LOCK(s, io_lock);
+	camel_folder_summary_lock (s, CFS_IO_LOCK);
 	if ( ((CamelFolderSummaryClass *)(CAMEL_OBJECT_GET_CLASS(s)))->summary_header_load(s, in) == -1)
 		goto error;
 
@@ -1318,7 +1343,7 @@ camel_folder_summary_load(CamelFolderSummary *s)
 		camel_folder_summary_add (s, mi);
 	}
 
-	CAMEL_SUMMARY_UNLOCK(s, io_lock);
+	camel_folder_summary_unlock (s, CFS_IO_LOCK);
 
 	if (fclose (in) != 0)
 		return -1;
@@ -1331,7 +1356,7 @@ error:
 	if (errno != EINVAL)
 		g_warning ("Cannot load summary file: '%s': %s", s->summary_path, g_strerror (errno));
 
-	CAMEL_SUMMARY_UNLOCK(s, io_lock);
+	camel_folder_summary_unlock (s, CFS_IO_LOCK);
 	fclose (in);
 	s->flags |= ~CAMEL_SUMMARY_DIRTY;
 #endif
@@ -1567,10 +1592,10 @@ save_message_infos_to_db (CamelFolderSummary *s, gboolean fresh_mirs, CamelExcep
 	if (camel_db_prepare_message_info_table (cdb, folder_name, ex) != 0) {
 		return -1;
 	}
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	/* Push MessageInfo-es */
 	g_hash_table_foreach (s->loaded_infos, save_to_db_cb, &args);
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 /* FIXME[disk-summary] make sure we free the message infos that are loaded
  * are freed if not used anymore or should we leave that to the timer? */
 
@@ -1598,10 +1623,10 @@ camel_folder_summary_save_to_db (CamelFolderSummary *s, CamelException *ex)
 	d(printf ("\ncamel_folder_summary_save_to_db called \n"));
 	if (CAMEL_FOLDER_SUMMARY_GET_PRIVATE(s)->need_preview && g_hash_table_size(CAMEL_FOLDER_SUMMARY_GET_PRIVATE(s)->preview_updates)) {
 		camel_db_begin_transaction (s->folder->parent_store->cdb_w, NULL);
-		CAMEL_SUMMARY_LOCK(s, summary_lock);
+		camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 		g_hash_table_foreach (CAMEL_FOLDER_SUMMARY_GET_PRIVATE(s)->preview_updates, (GHFunc)msg_save_preview, s->folder);
 		g_hash_table_remove_all (CAMEL_FOLDER_SUMMARY_GET_PRIVATE(s)->preview_updates);
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 		camel_db_end_transaction (s->folder->parent_store->cdb_w, NULL);
 	}
 
@@ -1769,7 +1794,7 @@ camel_folder_summary_save(CamelFolderSummary *s)
 
 	io(printf("saving header\n"));
 
-	CAMEL_SUMMARY_LOCK(s, io_lock);
+	camel_folder_summary_lock (s, CFS_IO_LOCK);
 
 	if (((CamelFolderSummaryClass *)(CAMEL_OBJECT_GET_CLASS(s)))->summary_header_save(s, out) == -1)
 		goto exception;
@@ -1805,7 +1830,7 @@ camel_folder_summary_save(CamelFolderSummary *s)
 	fclose (out);
 	fclose (out_meta);
 
-	CAMEL_SUMMARY_UNLOCK(s, io_lock);
+	camel_folder_summary_unlock (s, CFS_IO_LOCK);
 
 #ifdef G_OS_WIN32
 	g_unlink(s->summary_path);
@@ -1834,7 +1859,7 @@ exception:
 	fclose (out);
 	fclose (out_meta);
 
-	CAMEL_SUMMARY_UNLOCK(s, io_lock);
+	camel_folder_summary_unlock (s, CFS_IO_LOCK);
 
 	g_unlink (path);
 	g_unlink (path_meta);
@@ -1909,10 +1934,10 @@ camel_folder_summary_header_load(CamelFolderSummary *s)
 		return -1;
 	}
 
-	CAMEL_SUMMARY_LOCK(s, io_lock);
+	camel_folder_summary_lock (s, CFS_IO_LOCK);
 	ret = ((CamelFolderSummaryClass *)(CAMEL_OBJECT_GET_CLASS(s)))->summary_header_load(s, in);
 	ret = summary_meta_header_load(s, in_meta);
-	CAMEL_SUMMARY_UNLOCK(s, io_lock);
+	camel_folder_summary_unlock (s, CFS_IO_LOCK);
 
 	fclose(in);
 	fclose(in_meta);
@@ -1934,10 +1959,10 @@ summary_assign_uid(CamelFolderSummary *s, CamelMessageInfo *info)
 		uid = info->uid = (gchar *)camel_pstring_add (camel_folder_summary_next_uid_string(s), TRUE);
 	}
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	while ((mi = g_hash_table_lookup(s->loaded_infos, uid))) {
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 		if (mi == info)
 			return 0;
@@ -1948,10 +1973,10 @@ summary_assign_uid(CamelFolderSummary *s, CamelMessageInfo *info)
 		uid = info->uid = camel_pstring_add (camel_folder_summary_next_uid_string(s), TRUE);
 		camel_message_info_set_flags(info, CAMEL_MESSAGE_FOLDER_FLAGGED, CAMEL_MESSAGE_FOLDER_FLAGGED);
 
-		CAMEL_SUMMARY_LOCK(s, summary_lock);
+		camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	}
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return 1;
 }
@@ -1978,7 +2003,7 @@ camel_folder_summary_add (CamelFolderSummary *s, CamelMessageInfo *info)
 	if (summary_assign_uid(s, info) == 0)
 		return;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	/* Summary always holds a ref for the loaded infos */
 	/* camel_message_info_ref(info); FIXME: Check how things are loaded. */
@@ -1990,7 +2015,7 @@ camel_folder_summary_add (CamelFolderSummary *s, CamelMessageInfo *info)
 	g_hash_table_insert (s->loaded_infos, (gpointer) camel_message_info_uid (info), info);
 	s->flags |= CAMEL_SUMMARY_DIRTY;
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 }
 
 /**
@@ -2004,7 +2029,7 @@ camel_folder_summary_insert (CamelFolderSummary *s, CamelMessageInfo *info, gboo
 	if (info == NULL)
 		return;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	/* Summary always holds a ref for the loaded infos */
 	/* camel_message_info_ref(info); FIXME: Check how things are loaded. */
@@ -2021,7 +2046,7 @@ camel_folder_summary_insert (CamelFolderSummary *s, CamelMessageInfo *info, gboo
 	if (!load)
 		s->flags |= CAMEL_SUMMARY_DIRTY;
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 }
 
 static void
@@ -2195,7 +2220,7 @@ camel_folder_summary_info_new_from_parser(CamelFolderSummary *s, CamelMimeParser
 		if (p->index)
 			summary_assign_uid(s, info);
 
-		CAMEL_SUMMARY_LOCK(s, filter_lock);
+		camel_folder_summary_lock (s, CFS_FILTER_LOCK);
 
 		if (p->index) {
 			if (p->filter_index == NULL)
@@ -2216,7 +2241,7 @@ camel_folder_summary_info_new_from_parser(CamelFolderSummary *s, CamelMimeParser
 				CAMEL_MIME_FILTER_INDEX (p->filter_index), NULL);
 		}
 
-		CAMEL_SUMMARY_UNLOCK(s, filter_lock);
+		camel_folder_summary_unlock (s, CFS_FILTER_LOCK);
 
 		((CamelMessageInfoBase *)info)->size = camel_mime_parser_tell(mp) - start;
 	}
@@ -2248,7 +2273,7 @@ camel_folder_summary_info_new_from_message(CamelFolderSummary *s, CamelMimeMessa
 	if (p->index)
 		summary_assign_uid(s, info);
 
-	CAMEL_SUMMARY_LOCK(s, filter_lock);
+	camel_folder_summary_lock (s, CFS_FILTER_LOCK);
 
 	if (p->index) {
 		if (p->filter_index == NULL)
@@ -2275,7 +2300,7 @@ camel_folder_summary_info_new_from_message(CamelFolderSummary *s, CamelMimeMessa
 			CAMEL_MIME_FILTER_INDEX (p->filter_index), NULL);
 	}
 
-	CAMEL_SUMMARY_UNLOCK(s, filter_lock);
+	camel_folder_summary_unlock (s, CFS_FILTER_LOCK);
 
 	return info;
 }
@@ -2311,9 +2336,9 @@ camel_folder_summary_content_info_free(CamelFolderSummary *s, CamelMessageConten
 void
 camel_folder_summary_touch(CamelFolderSummary *s)
 {
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	s->flags |= CAMEL_SUMMARY_DIRTY;
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 }
 
 /**
@@ -2328,9 +2353,9 @@ camel_folder_summary_clear(CamelFolderSummary *s)
 	d(printf ("\ncamel_folder_summary_clearcalled \n"));
 	s->flags &= ~CAMEL_SUMMARY_DIRTY;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	if (camel_folder_summary_count(s) == 0) {
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 		return;
 	}
 
@@ -2342,7 +2367,7 @@ camel_folder_summary_clear(CamelFolderSummary *s)
 	g_hash_table_destroy(s->loaded_infos);
 	s->loaded_infos = g_hash_table_new(g_str_hash, g_str_equal);
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 }
 
 /**
@@ -2366,9 +2391,9 @@ camel_folder_summary_clear_db (CamelFolderSummary *s)
 	folder_name = s->folder->full_name;
 	cdb = s->folder->parent_store->cdb_w;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 	if (camel_folder_summary_count(s) == 0) {
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 		return;
 	}
 
@@ -2380,7 +2405,7 @@ camel_folder_summary_clear_db (CamelFolderSummary *s)
 	g_hash_table_destroy(s->loaded_infos);
 	s->loaded_infos = g_hash_table_new(g_str_hash, g_str_equal);
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	camel_db_clear_folder_summary (cdb, folder_name, NULL);
 }
@@ -2421,7 +2446,7 @@ camel_folder_summary_remove (CamelFolderSummary *s, CamelMessageInfo *info)
 	gboolean found;
 	gint ret;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	found = g_hash_table_lookup (s->loaded_infos, camel_message_info_uid (info)) != NULL;
 	g_hash_table_remove (s->loaded_infos, camel_message_info_uid(info));
@@ -2429,7 +2454,7 @@ camel_folder_summary_remove (CamelFolderSummary *s, CamelMessageInfo *info)
 
 	s->flags |= CAMEL_SUMMARY_DIRTY;
 	s->meta_summary->msg_expunged = TRUE;
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	if (!ret && camel_db_delete_uid (s->folder->parent_store->cdb_w, s->folder->full_name, camel_message_info_uid(info), NULL) != 0)
 		return;
@@ -2451,13 +2476,13 @@ camel_folder_summary_remove_uid(CamelFolderSummary *s, const gchar *uid)
 		CamelMessageInfo *oldinfo;
 		gchar *olduid;
 
-		CAMEL_SUMMARY_LOCK(s, summary_lock);
-		CAMEL_SUMMARY_LOCK(s, ref_lock);
+		camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
+		camel_folder_summary_lock (s, CFS_REF_LOCK);
 		if (g_hash_table_lookup_extended(s->loaded_infos, uid, (gpointer)&olduid, (gpointer)&oldinfo)) {
 				/* make sure it doesn't vanish while we're removing it */
 				oldinfo->refcount++;
-				CAMEL_SUMMARY_UNLOCK(s, ref_lock);
-				CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+				camel_folder_summary_unlock (s, CFS_REF_LOCK);
+				camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 				camel_folder_summary_remove(s, oldinfo);
 				camel_message_info_free(oldinfo);
 		} else {
@@ -2465,8 +2490,8 @@ camel_folder_summary_remove_uid(CamelFolderSummary *s, const gchar *uid)
 				gint ret;
 				/* Info isn't loaded into the memory. We must just remove the UID*/
 				ret = summary_remove_uid (s, uid);
-				CAMEL_SUMMARY_UNLOCK(s, ref_lock);
-				CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+				camel_folder_summary_unlock (s, CFS_REF_LOCK);
+				camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 				if (!ret && camel_db_delete_uid (s->folder->parent_store->cdb_w, s->folder->full_name, tmpid, NULL) != 0) {
 						g_free(tmpid);
@@ -2489,25 +2514,25 @@ camel_folder_summary_remove_uid_fast (CamelFolderSummary *s, const gchar *uid)
 		CamelMessageInfo *oldinfo;
 		gchar *olduid;
 
-		CAMEL_SUMMARY_LOCK(s, summary_lock);
-		CAMEL_SUMMARY_LOCK(s, ref_lock);
+		camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
+		camel_folder_summary_lock (s, CFS_REF_LOCK);
 		if (g_hash_table_lookup_extended(s->loaded_infos, uid, (gpointer)&olduid, (gpointer)&oldinfo)) {
 				/* make sure it doesn't vanish while we're removing it */
 				oldinfo->refcount++;
-				CAMEL_SUMMARY_UNLOCK(s, ref_lock);
+				camel_folder_summary_unlock (s, CFS_REF_LOCK);
 				g_hash_table_remove (s->loaded_infos, olduid);
 				summary_remove_uid (s, olduid);
 				s->flags |= CAMEL_SUMMARY_DIRTY;
 				s->meta_summary->msg_expunged = TRUE;
 				camel_message_info_free(oldinfo);
 				camel_message_info_free(oldinfo);
-				CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+				camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 		} else {
 				gchar *tmpid = g_strdup (uid);
 				/* Info isn't loaded into the memory. We must just remove the UID*/
 				summary_remove_uid (s, uid);
-				CAMEL_SUMMARY_UNLOCK(s, ref_lock);
-				CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+				camel_folder_summary_unlock (s, CFS_REF_LOCK);
+				camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 				g_free (tmpid);
 		}
 }
@@ -2524,24 +2549,23 @@ camel_folder_summary_remove_index_fast (CamelFolderSummary *s, gint index)
         CamelMessageInfo *oldinfo;
         gchar *olduid;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
-	CAMEL_SUMMARY_LOCK(s, ref_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
+	camel_folder_summary_lock (s, CFS_REF_LOCK);
 
 	if (g_hash_table_lookup_extended(s->loaded_infos, uid, (gpointer)&olduid, (gpointer)&oldinfo)) {
 		/* make sure it doesn't vanish while we're removing it */
 		g_hash_table_remove (s->loaded_infos, uid);
 		camel_pstring_free (uid);
 		g_ptr_array_remove_index(s->uids, index);
-		CAMEL_SUMMARY_UNLOCK(s, ref_lock);
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_REF_LOCK);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 		camel_message_info_free(oldinfo);
 	} else {
 		/* Info isn't loaded into the memory. We must just remove the UID*/
 		g_ptr_array_remove_index(s->uids, index);
 		camel_pstring_free (uid);
-		CAMEL_SUMMARY_UNLOCK(s, ref_lock);
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
-
+		camel_folder_summary_unlock (s, CFS_REF_LOCK);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 	}
 }
 
@@ -2575,7 +2599,7 @@ camel_folder_summary_remove_range (CamelFolderSummary *s, gint start, gint end)
 	if (end < start)
 		return;
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	if (start < s->uids->len) {
 
@@ -2618,11 +2642,11 @@ camel_folder_summary_remove_range (CamelFolderSummary *s, gint start, gint end)
 
 		s->flags |= CAMEL_SUMMARY_DIRTY;
 
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 		camel_exception_clear (&ex);
 	} else {
-		CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+		camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 	}
 }
 
@@ -3141,9 +3165,9 @@ camel_folder_summary_content_info_new(CamelFolderSummary *s)
 
 	class = CAMEL_FOLDER_SUMMARY_GET_CLASS (s);
 
-	CAMEL_SUMMARY_LOCK(s, alloc_lock);
+	camel_folder_summary_lock (s, CFS_ALLOC_LOCK);
 	ci = g_slice_alloc0 (class->content_info_size);
-	CAMEL_SUMMARY_UNLOCK(s, alloc_lock);
+	camel_folder_summary_unlock (s, CFS_ALLOC_LOCK);
 
 	return ci;
 }
@@ -4483,10 +4507,10 @@ camel_message_info_new (CamelFolderSummary *s)
 	CamelMessageInfo *info;
 
 	if (s) {
-		CAMEL_SUMMARY_LOCK(s, alloc_lock);
+		camel_folder_summary_lock (s, CFS_ALLOC_LOCK);
 		class = CAMEL_FOLDER_SUMMARY_GET_CLASS (s);
 		info = g_slice_alloc0 (class->message_info_size);
-		CAMEL_SUMMARY_UNLOCK(s, alloc_lock);
+		camel_folder_summary_unlock (s, CFS_ALLOC_LOCK);
 	} else {
 		info = g_slice_alloc0 (sizeof(CamelMessageInfoBase));
 	}
@@ -4512,10 +4536,10 @@ camel_message_info_ref(gpointer o)
 	CamelMessageInfo *mi = o;
 
 	if (mi->summary) {
-		CAMEL_SUMMARY_LOCK(mi->summary, ref_lock);
+		camel_folder_summary_lock (mi->summary, CFS_REF_LOCK);
 		g_assert(mi->refcount >= 1);
 		mi->refcount++;
-		CAMEL_SUMMARY_UNLOCK(mi->summary, ref_lock);
+		camel_folder_summary_unlock (mi->summary, CFS_REF_LOCK);
 	} else {
 		GLOBAL_INFO_LOCK(info);
 		g_assert(mi->refcount >= 1);
@@ -4559,16 +4583,16 @@ camel_message_info_free(gpointer o)
 	g_return_if_fail(mi != NULL);
 
 	if (mi->summary) {
-		CAMEL_SUMMARY_LOCK(mi->summary, ref_lock);
+		camel_folder_summary_lock (mi->summary, CFS_REF_LOCK);
 
 		if (mi->refcount >= 1)
 			mi->refcount--;
 		if (mi->refcount > 0) {
-			CAMEL_SUMMARY_UNLOCK(mi->summary, ref_lock);
+			camel_folder_summary_unlock (mi->summary, CFS_REF_LOCK);
 			return;
 		}
 
-		CAMEL_SUMMARY_UNLOCK(mi->summary, ref_lock);
+		camel_folder_summary_unlock (mi->summary, CFS_REF_LOCK);
 
 		/* FIXME: this is kinda busted, should really be handled by message info free */
 		if (mi->summary->build_content
@@ -5180,3 +5204,77 @@ camel_folder_summary_guess_content_info (CamelMessageInfo *mi, CamelContentType
 
 	return NULL;
 }
+
+/**
+ * camel_folder_summary_lock:
+ * @summary: a #CamelFolderSummary
+ * @lock: lock type to lock
+ *
+ * Locks #summary's #lock. Unlock it with camel_folder_summary_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_folder_summary_lock (CamelFolderSummary *summary, CamelFolderSummaryLock lock)
+{
+	g_return_if_fail (summary != NULL);
+	g_return_if_fail (CAMEL_IS_FOLDER_SUMMARY (summary));
+	g_return_if_fail (summary->priv != NULL);
+
+	switch (lock) {
+	case CFS_SUMMARY_LOCK:
+		g_mutex_lock (summary->priv->summary_lock);
+		break;
+	case CFS_IO_LOCK:
+		g_mutex_lock (summary->priv->io_lock);
+		break;
+	case CFS_FILTER_LOCK:
+		g_mutex_lock (summary->priv->filter_lock);
+		break;
+	case CFS_ALLOC_LOCK:
+		g_mutex_lock (summary->priv->alloc_lock);
+		break;
+	case CFS_REF_LOCK:
+		g_mutex_lock (summary->priv->ref_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_folder_summary_unlock:
+ * @summary: a #CamelFolderSummary
+ * @lock: lock type to unlock
+ *
+ * Unlocks #summary's #lock, previously locked with camel_folder_summary_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_folder_summary_unlock (CamelFolderSummary *summary, CamelFolderSummaryLock lock)
+{
+	g_return_if_fail (summary != NULL);
+	g_return_if_fail (CAMEL_IS_FOLDER_SUMMARY (summary));
+	g_return_if_fail (summary->priv != NULL);
+
+	switch (lock) {
+	case CFS_SUMMARY_LOCK:
+		g_mutex_unlock (summary->priv->summary_lock);
+		break;
+	case CFS_IO_LOCK:
+		g_mutex_unlock (summary->priv->io_lock);
+		break;
+	case CFS_FILTER_LOCK:
+		g_mutex_unlock (summary->priv->filter_lock);
+		break;
+	case CFS_ALLOC_LOCK:
+		g_mutex_unlock (summary->priv->alloc_lock);
+		break;
+	case CFS_REF_LOCK:
+		g_mutex_unlock (summary->priv->ref_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-folder-summary.h b/camel/camel-folder-summary.h
index 67db608..887b345 100644
--- a/camel/camel-folder-summary.h
+++ b/camel/camel-folder-summary.h
@@ -217,6 +217,14 @@ typedef enum _CamelFolderSummaryFlags {
 	CAMEL_SUMMARY_DIRTY = 1<<0
 } CamelFolderSummaryFlags;
 
+typedef enum _CamelFolderSummaryLock {
+	CFS_SUMMARY_LOCK,
+	CFS_IO_LOCK,
+	CFS_FILTER_LOCK,
+	CFS_ALLOC_LOCK,
+	CFS_REF_LOCK
+} CamelFolderSummaryLock;
+
 struct _CamelFolderSummary {
 	CamelObject parent;
 	CamelFolderSummaryPrivate *priv;
@@ -519,6 +527,9 @@ void camel_message_info_dump (CamelMessageInfo *mi);
 /* Migration code */
 gint camel_folder_summary_migrate_infos(CamelFolderSummary *s);
 
+void camel_folder_summary_lock   (CamelFolderSummary *summary, CamelFolderSummaryLock lock);
+void camel_folder_summary_unlock (CamelFolderSummary *summary, CamelFolderSummaryLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_FOLDER_SUMMARY_H */
diff --git a/camel/camel-folder.c b/camel/camel-folder.c
index 848787c..4967605 100644
--- a/camel/camel-folder.c
+++ b/camel/camel-folder.c
@@ -38,7 +38,6 @@
 #include "camel-mempool.h"
 #include "camel-mime-message.h"
 #include "camel-operation.h"
-#include "camel-private.h"
 #include "camel-session.h"
 #include "camel-store.h"
 #include "camel-vtrash-folder.h"
@@ -47,6 +46,15 @@
 #define d(x)
 #define w(x)
 
+struct _CamelFolderPrivate {
+	GStaticRecMutex lock;
+	GStaticMutex change_lock;
+	/* must require the 'change_lock' to access this */
+	gint frozen;
+	struct _CamelFolderChangeInfo *changed_frozen; /* queues changed events */
+	gboolean skip_folder_lock;
+};
+
 static CamelObjectClass *parent_class = NULL;
 
 /* Forward Declarations */
@@ -645,12 +653,12 @@ folder_freeze (CamelFolder *folder)
 {
 	g_return_if_fail (folder->priv->frozen >= 0);
 
-	CAMEL_FOLDER_LOCK (folder, change_lock);
+	camel_folder_lock (folder, CF_CHANGE_LOCK);
 
 	folder->priv->frozen++;
 
 	d (printf ("freeze (%p '%s') = %d\n", folder, folder->full_name, folder->priv->frozen));
-	CAMEL_FOLDER_UNLOCK (folder, change_lock);
+	camel_folder_unlock (folder, CF_CHANGE_LOCK);
 }
 
 static void
@@ -660,7 +668,7 @@ folder_thaw (CamelFolder * folder)
 
 	g_return_if_fail (folder->priv->frozen > 0);
 
-	CAMEL_FOLDER_LOCK (folder, change_lock);
+	camel_folder_lock (folder, CF_CHANGE_LOCK);
 
 	folder->priv->frozen--;
 
@@ -672,7 +680,7 @@ folder_thaw (CamelFolder * folder)
 		folder->priv->changed_frozen = camel_folder_change_info_new ();
 	}
 
-	CAMEL_FOLDER_UNLOCK (folder, change_lock);
+	camel_folder_unlock (folder, CF_CHANGE_LOCK);
 
 	if (info) {
 		camel_object_trigger_event (folder, "folder_changed", info);
@@ -859,12 +867,12 @@ camel_folder_sync (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_if_fail (class->sync != NULL);
 
-	CAMEL_FOLDER_REC_LOCK (folder, lock);
+	camel_folder_lock (folder, CF_REC_LOCK);
 
 	if (!(folder->folder_flags & CAMEL_FOLDER_HAS_BEEN_DELETED))
 		class->sync (folder, expunge, ex);
 
-	CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+	camel_folder_unlock (folder, CF_REC_LOCK);
 }
 
 /**
@@ -885,9 +893,9 @@ camel_folder_refresh_info (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_if_fail (class->refresh_info != NULL);
 
-	CAMEL_FOLDER_REC_LOCK (folder, lock);
+	camel_folder_lock (folder, CF_REC_LOCK);
 	class->refresh_info (folder, ex);
-	CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+	camel_folder_unlock (folder, CF_REC_LOCK);
 }
 
 /**
@@ -970,12 +978,12 @@ camel_folder_expunge (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_if_fail (class->expunge != NULL);
 
-	CAMEL_FOLDER_REC_LOCK (folder, lock);
+	camel_folder_lock (folder, CF_REC_LOCK);
 
 	if (!(folder->folder_flags & CAMEL_FOLDER_HAS_BEEN_DELETED))
 		class->expunge (folder, ex);
 
-	CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+	camel_folder_unlock (folder, CF_REC_LOCK);
 }
 
 /**
@@ -1066,9 +1074,9 @@ camel_folder_append_message (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_if_fail (class->append_message != NULL);
 
-	CAMEL_FOLDER_REC_LOCK (folder, lock);
+	camel_folder_lock (folder, CF_REC_LOCK);
 	class->append_message (folder, message, info, appended_uid, ex);
-	CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+	camel_folder_unlock (folder, CF_REC_LOCK);
 }
 
 /**
@@ -1397,11 +1405,11 @@ camel_folder_get_message (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_val_if_fail (class->get_message != NULL, NULL);
 
-	CAMEL_FOLDER_REC_LOCK (folder, lock);
+	camel_folder_lock (folder, CF_REC_LOCK);
 
 	ret = class->get_message (folder, uid, ex);
 
-	CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+	camel_folder_unlock (folder, CF_REC_LOCK);
 
 	if (ret && camel_debug_start (":folder")) {
 		printf ("CamelFolder:get_message ('%s', '%s') =\n", folder->full_name, uid);
@@ -1436,7 +1444,7 @@ camel_folder_sync_message (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_if_fail (class->get_message != NULL);
 
-	CAMEL_FOLDER_REC_LOCK (folder, lock);
+	camel_folder_lock (folder, CF_REC_LOCK);
 
 	/* Use the sync_message method if the class implements it. */
 	if (class->sync_message != NULL)
@@ -1449,7 +1457,7 @@ camel_folder_sync_message (CamelFolder *folder,
 			  camel_object_unref (message);
 	}
 
-	CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+	camel_folder_unlock (folder, CF_REC_LOCK);
 }
 
 /**
@@ -1666,7 +1674,7 @@ camel_folder_search_by_expression (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_val_if_fail (class->search_by_expression != NULL, NULL);
 
-	/* NOTE: that it is upto the callee to lock */
+	/* NOTE: that it is upto the callee to CF_REC_LOCK */
 
 	return class->search_by_expression (folder, expression, ex);
 }
@@ -1696,7 +1704,7 @@ camel_folder_count_by_expression (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_val_if_fail (class->count_by_expression != NULL, 0);
 
-	/* NOTE: that it is upto the callee to lock */
+	/* NOTE: that it is upto the callee to CF_REC_LOCK */
 
 	return class->count_by_expression (folder, expression, ex);
 }
@@ -1727,7 +1735,7 @@ camel_folder_search_by_uids (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_val_if_fail (class->search_by_uids != NULL, NULL);
 
-	/* NOTE: that it is upto the callee to lock */
+	/* NOTE: that it is upto the callee to CF_REC_LOCK */
 
 	return class->search_by_uids (folder, expr, uids, ex);
 }
@@ -1752,7 +1760,7 @@ camel_folder_search_free (CamelFolder *folder,
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_if_fail (class->search_free != NULL);
 
-	/* NOTE: upto the callee to lock */
+	/* NOTE: upto the callee to CF_REC_LOCK */
 
 	class->search_free (folder, result);
 }
@@ -1822,9 +1830,9 @@ camel_folder_delete (CamelFolder *folder)
 	class = CAMEL_FOLDER_GET_CLASS (folder);
 	g_return_if_fail (class->delete != NULL);
 
-	CAMEL_FOLDER_REC_LOCK (folder, lock);
+	camel_folder_lock (folder, CF_REC_LOCK);
 	if (folder->folder_flags & CAMEL_FOLDER_HAS_BEEN_DELETED) {
-		CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+		camel_folder_unlock (folder, CF_REC_LOCK);
 		return;
 	}
 
@@ -1832,7 +1840,7 @@ camel_folder_delete (CamelFolder *folder)
 
 	class->delete (folder);
 
-	CAMEL_FOLDER_REC_UNLOCK (folder, lock);
+	camel_folder_unlock (folder, CF_REC_LOCK);
 
 	/* Delete the references of the folder from the DB.*/
 	camel_db_delete_folder (folder->parent_store->cdb_w, folder->full_name, NULL);
@@ -1934,6 +1942,15 @@ camel_folder_is_frozen (CamelFolder *folder)
 	return class->is_frozen (folder);
 }
 
+/* FIXME: This function shouldn't be needed, but it's used in CamelVeeFolder */
+gint
+camel_folder_get_frozen_count (CamelFolder *folder)
+{
+	g_return_val_if_fail (CAMEL_IS_FOLDER (folder), 0);
+
+	return folder->priv->frozen;
+}
+
 /**
  * camel_folder_get_quota_info:
  * @folder: a #CamelFolder
@@ -2195,14 +2212,14 @@ folder_changed (CamelObject *obj, gpointer event_data)
 		return TRUE;
 	}
 
-	CAMEL_FOLDER_LOCK (folder, change_lock);
+	camel_folder_lock (folder, CF_CHANGE_LOCK);
 	if (folder->priv->frozen) {
 		camel_folder_change_info_cat (folder->priv->changed_frozen, changed);
-		CAMEL_FOLDER_UNLOCK (folder, change_lock);
+		camel_folder_unlock (folder, CF_CHANGE_LOCK);
 
 		return FALSE;
 	}
-	CAMEL_FOLDER_UNLOCK (folder, change_lock);
+	camel_folder_unlock (folder, CF_CHANGE_LOCK);
 
 	if (session->junk_plugin && changed->uid_changed->len) {
 		guint32 flags;
@@ -2253,9 +2270,9 @@ folder_changed (CamelObject *obj, gpointer event_data)
 		camel_folder_freeze (folder);
 		/* Copy changes back to changed_frozen list to retain
 		 * them while we are filtering */
-		CAMEL_FOLDER_LOCK (folder, change_lock);
+		camel_folder_lock (folder, CF_CHANGE_LOCK);
 		camel_folder_change_info_cat (folder->priv->changed_frozen, changed);
-		CAMEL_FOLDER_UNLOCK (folder, change_lock);
+		camel_folder_unlock (folder, CF_CHANGE_LOCK);
 		msg->driver = driver;
 		camel_exception_init (&msg->ex);
 		camel_session_thread_queue (session, &msg->msg, 0);
@@ -2761,3 +2778,59 @@ camel_folder_change_info_free (CamelFolderChangeInfo *info)
 	g_ptr_array_free (info->uid_recent, TRUE);
 	g_slice_free (CamelFolderChangeInfo, info);
 }
+
+/**
+ * camel_folder_lock:
+ * @folder: a #CamelFolder
+ * @lock: lock type to lock
+ *
+ * Locks #folder's #lock. Unlock it with camel_folder_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_folder_lock (CamelFolder *folder, CamelFolderLock lock)
+{
+	g_return_if_fail (folder != NULL);
+	g_return_if_fail (CAMEL_IS_FOLDER (folder));
+	g_return_if_fail (folder->priv != NULL);
+
+	switch (lock) {
+	case CF_CHANGE_LOCK:
+		g_static_mutex_lock (&folder->priv->change_lock);
+		break;
+	case CF_REC_LOCK:
+		if (folder->priv->skip_folder_lock == FALSE) g_static_rec_mutex_lock (&folder->priv->lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_folder_unlock:
+ * @folder: a #CamelFolder
+ * @lock: lock type to unlock
+ *
+ * Unlocks #folder's #lock, previously locked with camel_folder_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_folder_unlock (CamelFolder *folder, CamelFolderLock lock)
+{
+	g_return_if_fail (folder != NULL);
+	g_return_if_fail (CAMEL_IS_FOLDER (folder));
+	g_return_if_fail (folder->priv != NULL);
+
+	switch (lock) {
+	case CF_CHANGE_LOCK:
+		g_static_mutex_unlock (&folder->priv->change_lock);
+		break;
+	case CF_REC_LOCK:
+		if (folder->priv->skip_folder_lock == FALSE) g_static_rec_mutex_unlock (&folder->priv->lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-folder.h b/camel/camel-folder.h
index a637ac5..9483c23 100644
--- a/camel/camel-folder.h
+++ b/camel/camel-folder.h
@@ -86,6 +86,11 @@ enum {
 	CAMEL_FOLDER_PROPERTIES = CAMEL_FOLDER_ARG_PROPERTIES | CAMEL_ARG_PTR
 };
 
+typedef enum _CamelFolderLock {
+	CF_CHANGE_LOCK,
+	CF_REC_LOCK
+} CamelFolderLock;
+
 struct _CamelFolderChangeInfo {
 	GPtrArray *uid_added;
 	GPtrArray *uid_removed;
@@ -388,6 +393,7 @@ void		camel_folder_rename		(CamelFolder *folder,
 void		camel_folder_freeze		(CamelFolder *folder);
 void		camel_folder_thaw		(CamelFolder *folder);
 gboolean	camel_folder_is_frozen		(CamelFolder *folder);
+gint		camel_folder_get_frozen_count	(CamelFolder *folder);
 
 /* quota support */
 CamelFolderQuotaInfo *
@@ -451,6 +457,9 @@ void		camel_folder_change_info_recent_uid
 						(CamelFolderChangeInfo *info,
 						 const gchar *uid);
 
+void		camel_folder_lock		(CamelFolder *folder, CamelFolderLock lock);
+void		camel_folder_unlock		(CamelFolder *folder, CamelFolderLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_FOLDER_H */
diff --git a/camel/camel-mime-parser.c b/camel/camel-mime-parser.c
index 8ee3310..a4cfaf9 100644
--- a/camel/camel-mime-parser.c
+++ b/camel/camel-mime-parser.c
@@ -37,7 +37,6 @@
 #include "camel-mime-filter.h"
 #include "camel-mime-parser.h"
 #include "camel-mime-utils.h"
-#include "camel-private.h"
 #include "camel-seekable-stream.h"
 #include "camel-stream.h"
 
diff --git a/camel/camel-offline-journal.c b/camel/camel-offline-journal.c
index 34b6014..c7c74d2 100644
--- a/camel/camel-offline-journal.c
+++ b/camel/camel-offline-journal.c
@@ -41,7 +41,7 @@
 #include "camel-folder-summary.h"
 #include "camel-folder.h"
 #include "camel-offline-journal.h"
-#include "camel-private.h"
+#include "camel-win32.h"
 
 #define d(x)
 
diff --git a/camel/camel-provider.c b/camel/camel-provider.c
index 02fa28b..506da13 100644
--- a/camel/camel-provider.c
+++ b/camel/camel-provider.c
@@ -39,10 +39,10 @@
 #include <gmodule.h>
 
 #include "camel-exception.h"
-#include "camel-private.h"
 #include "camel-provider.h"
 #include "camel-string-utils.h"
 #include "camel-vee-store.h"
+#include "camel-win32.h"
 
 /* table of CamelProviderModule's */
 static GHashTable *module_table;
diff --git a/camel/camel-service.c b/camel/camel-service.c
index abbec77..2988d36 100644
--- a/camel/camel-service.c
+++ b/camel/camel-service.c
@@ -36,13 +36,17 @@
 
 #include "camel-exception.h"
 #include "camel-operation.h"
-#include "camel-private.h"
 #include "camel-service.h"
 #include "camel-session.h"
 
 #define d(x)
 #define w(x)
 
+struct _CamelServicePrivate {
+	GStaticRecMutex connect_lock;	/* for locking connection operations */
+	GStaticMutex connect_op_lock;	/* for locking the connection_op */
+};
+
 static CamelObjectClass *camel_service_parent_class = NULL;
 
 static void
@@ -420,16 +424,16 @@ camel_service_connect (CamelService *service,
 	class = CAMEL_SERVICE_GET_CLASS (service);
 	g_return_val_if_fail (class->connect != NULL, FALSE);
 
-	CAMEL_SERVICE_REC_LOCK (service, connect_lock);
+	camel_service_lock (service, CS_REC_CONNECT_LOCK);
 
 	if (service->status == CAMEL_SERVICE_CONNECTED) {
-		CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
+		camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 		return TRUE;
 	}
 
 	/* Register a separate operation for connecting, so that
 	 * the offline code can cancel it. */
-	CAMEL_SERVICE_LOCK (service, connect_op_lock);
+	camel_service_lock (service, CS_CONNECT_OP_LOCK);
 	service->connect_op = camel_operation_registered ();
 	if (!service->connect_op) {
 		service->connect_op = camel_operation_new (NULL, NULL);
@@ -437,13 +441,13 @@ camel_service_connect (CamelService *service,
 		unreg = TRUE;
 	}
 	connect_op = service->connect_op;
-	CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
+	camel_service_unlock (service, CS_CONNECT_OP_LOCK);
 
 	service->status = CAMEL_SERVICE_CONNECTING;
 	ret = class->connect (service, ex);
 	service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
 
-	CAMEL_SERVICE_LOCK (service, connect_op_lock);
+	camel_service_lock (service, CS_CONNECT_OP_LOCK);
 	if (connect_op) {
 		if (unreg && service->connect_op)
 			camel_operation_unregister (connect_op);
@@ -451,9 +455,9 @@ camel_service_connect (CamelService *service,
 		camel_operation_unref (connect_op);
 		service->connect_op = NULL;
 	}
-	CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
+	camel_service_unlock (service, CS_CONNECT_OP_LOCK);
 
-	CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
+	camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 
 	return ret;
 }
@@ -483,35 +487,35 @@ camel_service_disconnect (CamelService *service,
 	class = CAMEL_SERVICE_GET_CLASS (service);
 	g_return_val_if_fail (class->disconnect != NULL, FALSE);
 
-	CAMEL_SERVICE_REC_LOCK (service, connect_lock);
+	camel_service_lock (service, CS_REC_CONNECT_LOCK);
 
 	if (service->status != CAMEL_SERVICE_DISCONNECTED
 	    && service->status != CAMEL_SERVICE_DISCONNECTING) {
-		CAMEL_SERVICE_LOCK (service, connect_op_lock);
+		camel_service_lock (service, CS_CONNECT_OP_LOCK);
 		service->connect_op = camel_operation_registered ();
 		if (!service->connect_op) {
 			service->connect_op = camel_operation_new (NULL, NULL);
 			camel_operation_register (service->connect_op);
 			unreg = TRUE;
 		}
-		CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
+		camel_service_unlock (service, CS_CONNECT_OP_LOCK);
 
 		service->status = CAMEL_SERVICE_DISCONNECTING;
 		res = class->disconnect (service, clean, ex);
 		service->status = CAMEL_SERVICE_DISCONNECTED;
 
-		CAMEL_SERVICE_LOCK (service, connect_op_lock);
+		camel_service_lock (service, CS_CONNECT_OP_LOCK);
 		if (unreg)
 			camel_operation_unregister (service->connect_op);
 
 		camel_operation_unref (service->connect_op);
 		service->connect_op = NULL;
-		CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
+		camel_service_unlock (service, CS_CONNECT_OP_LOCK);
 	}
 
-	CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
+	camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 
-		service->status = CAMEL_SERVICE_DISCONNECTED;
+	service->status = CAMEL_SERVICE_DISCONNECTED;
 	return res;
 }
 
@@ -533,10 +537,10 @@ camel_service_cancel_connect (CamelService *service)
 	class = CAMEL_SERVICE_GET_CLASS (service);
 	g_return_if_fail (class->cancel_connect != NULL);
 
-	CAMEL_SERVICE_LOCK (service, connect_op_lock);
+	camel_service_lock (service, CS_CONNECT_OP_LOCK);
 	if (service->connect_op)
 		class->cancel_connect (service);
-	CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
+	camel_service_unlock (service, CS_CONNECT_OP_LOCK);
 }
 
 /**
@@ -668,9 +672,65 @@ camel_service_query_auth_types (CamelService *service,
 
 	/* Note that we get the connect lock here, which means the
 	 * callee must not call the connect functions itself. */
-	CAMEL_SERVICE_REC_LOCK (service, connect_lock);
+	camel_service_lock (service, CS_REC_CONNECT_LOCK);
 	ret = class->query_auth_types (service, ex);
-	CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
+	camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 
 	return ret;
 }
+
+/**
+ * camel_service_lock:
+ * @service: a #CamelService
+ * @lock: lock type to lock
+ *
+ * Locks #service's #lock. Unlock it with camel_service_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_service_lock (CamelService *service, CamelServiceLock lock)
+{
+	g_return_if_fail (service != NULL);
+	g_return_if_fail (CAMEL_IS_SERVICE (service));
+	g_return_if_fail (service->priv != NULL);
+
+	switch (lock) {
+	case CS_REC_CONNECT_LOCK:
+		g_static_rec_mutex_lock (&service->priv->connect_lock);
+		break;
+	case CS_CONNECT_OP_LOCK:
+		g_static_mutex_lock (&service->priv->connect_op_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_service_unlock:
+ * @service: a #CamelService
+ * @lock: lock type to unlock
+ *
+ * Unlocks #service's #lock, previously locked with camel_service_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_service_unlock (CamelService *service, CamelServiceLock lock)
+{
+	g_return_if_fail (service != NULL);
+	g_return_if_fail (CAMEL_IS_SERVICE (service));
+	g_return_if_fail (service->priv != NULL);
+
+	switch (lock) {
+	case CS_REC_CONNECT_LOCK:
+		g_static_rec_mutex_unlock (&service->priv->connect_lock);
+		break;
+	case CS_CONNECT_OP_LOCK:
+		g_static_mutex_unlock (&service->priv->connect_op_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-service.h b/camel/camel-service.h
index a498802..8dff22b 100644
--- a/camel/camel-service.h
+++ b/camel/camel-service.h
@@ -71,6 +71,11 @@ typedef enum {
 	CAMEL_SERVICE_DISCONNECTING
 } CamelServiceConnectionStatus;
 
+typedef enum _CamelServiceLock {
+	CS_REC_CONNECT_LOCK,
+	CS_CONNECT_OP_LOCK
+} CamelServiceLock;
+
 struct _CamelService {
 	CamelObject parent;
 	CamelServicePrivate *priv;
@@ -134,6 +139,9 @@ CamelProvider *	camel_service_get_provider	(CamelService *service);
 GList *		camel_service_query_auth_types	(CamelService *service,
 						 CamelException *ex);
 
+void		camel_service_lock		(CamelService *service, CamelServiceLock lock);
+void		camel_service_unlock		(CamelService *service, CamelServiceLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_SERVICE_H */
diff --git a/camel/camel-session.c b/camel/camel-session.c
index 0644466..35ac457 100644
--- a/camel/camel-session.c
+++ b/camel/camel-session.c
@@ -39,7 +39,6 @@
 
 #include "camel-exception.h"
 #include "camel-file-utils.h"
-#include "camel-private.h"
 #include "camel-session.h"
 #include "camel-store.h"
 #include "camel-string-utils.h"
@@ -50,6 +49,19 @@
 
 #define d(x)
 
+struct _CamelSessionPrivate {
+	GMutex *lock;		/* for locking everything basically */
+	GMutex *thread_lock;	/* locking threads */
+
+	gint thread_id;
+	GHashTable *thread_active;
+	GThreadPool *thread_pool;
+
+	GHashTable *thread_msg_op;
+	GHashTable *junk_headers;
+
+};
+
 static void
 cs_thread_status (CamelOperation *op,
                   const gchar *what,
@@ -192,10 +204,10 @@ session_thread_msg_new (CamelSession *session,
 	m->session = camel_object_ref (session);
 	m->op = camel_operation_new(cs_thread_status, m);
 	camel_exception_init(&m->ex);
-	CAMEL_SESSION_LOCK(session, thread_lock);
+	camel_session_lock (session, CS_THREAD_LOCK);
 	m->id = session->priv->thread_id++;
 	g_hash_table_insert(session->priv->thread_active, GINT_TO_POINTER(m->id), m);
-	CAMEL_SESSION_UNLOCK(session, thread_lock);
+	camel_session_unlock (session, CS_THREAD_LOCK);
 
 	return m;
 }
@@ -209,9 +221,9 @@ session_thread_msg_free (CamelSession *session,
 
 	d(printf("free message %p session %p\n", msg, session));
 
-	CAMEL_SESSION_LOCK(session, thread_lock);
+	camel_session_lock (session, CS_THREAD_LOCK);
 	g_hash_table_remove(session->priv->thread_active, GINT_TO_POINTER(msg->id));
-	CAMEL_SESSION_UNLOCK(session, thread_lock);
+	camel_session_unlock (session, CS_THREAD_LOCK);
 
 	d(printf("free msg, ops->free = %p\n", msg->ops->free));
 
@@ -247,7 +259,7 @@ session_thread_queue (CamelSession *session,
 	GThreadPool *thread_pool;
 	gint id;
 
-	CAMEL_SESSION_LOCK(session, thread_lock);
+	camel_session_lock (session, CS_THREAD_LOCK);
 	thread_pool = session->priv->thread_pool;
 	if (thread_pool == NULL) {
 		thread_pool = g_thread_pool_new (
@@ -255,7 +267,7 @@ session_thread_queue (CamelSession *session,
 			session, 1, FALSE, NULL);
 		session->priv->thread_pool = thread_pool;
 	}
-	CAMEL_SESSION_UNLOCK(session, thread_lock);
+	camel_session_unlock (session, CS_THREAD_LOCK);
 
 	id = msg->id;
 	g_thread_pool_push(thread_pool, msg, NULL);
@@ -271,9 +283,9 @@ session_thread_wait (CamelSession *session,
 
 	/* we just busy wait, only other alternative is to setup a reply port? */
 	do {
-		CAMEL_SESSION_LOCK(session, thread_lock);
+		camel_session_lock (session, CS_THREAD_LOCK);
 		wait = g_hash_table_lookup(session->priv->thread_active, GINT_TO_POINTER(id)) != NULL;
-		CAMEL_SESSION_UNLOCK(session, thread_lock);
+		camel_session_unlock (session, CS_THREAD_LOCK);
 		if (wait) {
 			g_usleep(20000);
 		}
@@ -388,9 +400,9 @@ camel_session_get_service (CamelSession *session,
 	class = CAMEL_SESSION_GET_CLASS (session);
 	g_return_val_if_fail (class->get_service != NULL, NULL);
 
-	CAMEL_SESSION_LOCK (session, lock);
+	camel_session_lock (session, CS_SESSION_LOCK);
 	service = class->get_service (session, url_string, type, ex);
-	CAMEL_SESSION_UNLOCK (session, lock);
+	camel_session_unlock (session, CS_SESSION_LOCK);
 
 	return service;
 }
@@ -922,3 +934,59 @@ camel_session_forward_to (CamelSession *session,
 
 	class->forward_to (session, folder, message, address, ex);
 }
+
+/**
+ * camel_session_lock:
+ * @session: a #CamelSession
+ * @lock: lock type to lock
+ *
+ * Locks #session's #lock. Unlock it with camel_session_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_session_lock (CamelSession *session, CamelSessionLock lock)
+{
+	g_return_if_fail (session != NULL);
+	g_return_if_fail (CAMEL_IS_SESSION (session));
+	g_return_if_fail (session->priv != NULL);
+
+	switch (lock) {
+	case CS_SESSION_LOCK:
+		g_mutex_lock (session->priv->lock);
+		break;
+	case CS_THREAD_LOCK:
+		g_mutex_lock (session->priv->thread_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_session_unlock:
+ * @session: a #CamelSession
+ * @lock: lock type to unlock
+ *
+ * Unlocks #session's #lock, previously locked with camel_session_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_session_unlock (CamelSession *session, CamelSessionLock lock)
+{
+	g_return_if_fail (session != NULL);
+	g_return_if_fail (CAMEL_IS_SESSION (session));
+	g_return_if_fail (session->priv != NULL);
+
+	switch (lock) {
+	case CS_SESSION_LOCK:
+		g_mutex_unlock (session->priv->lock);
+		break;
+	case CS_THREAD_LOCK:
+		g_mutex_unlock (session->priv->thread_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-session.h b/camel/camel-session.h
index dcb93a5..7f01fc3 100644
--- a/camel/camel-session.h
+++ b/camel/camel-session.h
@@ -63,6 +63,11 @@ enum {
 	CAMEL_SESSION_PASSPHRASE = 1 << 4
 };
 
+typedef enum _CamelSessionLock {
+	CS_SESSION_LOCK,
+	CS_THREAD_LOCK
+} CamelSessionLock;
+
 struct _CamelSession {
 	CamelObject parent;
 	CamelSessionPrivate *priv;
@@ -239,6 +244,9 @@ void		   camel_session_forward_to         (CamelSession *session,
 						     const gchar *address,
 						     CamelException *ex);
 
+void		camel_session_lock		(CamelSession *session, CamelSessionLock lock);
+void		camel_session_unlock		(CamelSession *session, CamelSessionLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_SESSION_H */
diff --git a/camel/camel-store-summary.c b/camel/camel-store-summary.c
index f6c27e0..295cc22 100644
--- a/camel/camel-store-summary.c
+++ b/camel/camel-store-summary.c
@@ -37,13 +37,19 @@
 #include <libedataserver/e-memory.h>
 
 #include "camel-file-utils.h"
-#include "camel-private.h"
 #include "camel-store-summary.h"
 #include "camel-url.h"
+#include "camel-win32.h"
 
 #define d(x)
 #define io(x)			/* io debug */
 
+struct _CamelStoreSummaryPrivate {
+	GMutex *summary_lock;	/* for the summary hashtable/array */
+	GMutex *io_lock;	/* load/save lock, for access to saved_count, etc */
+	GMutex *ref_lock;	/* for reffing/unreffing messageinfo's ALWAYS obtain before CSS_SUMMARY_LOCK */
+};
+
 /* possible versions, for versioning changes */
 #define CAMEL_STORE_SUMMARY_VERSION_0 (1)
 #define CAMEL_STORE_SUMMARY_VERSION_2 (2)
@@ -69,7 +75,6 @@ store_summary_finalize (CamelStoreSummary *summary)
 
 	g_mutex_free (summary->priv->summary_lock);
 	g_mutex_free (summary->priv->io_lock);
-	g_mutex_free (summary->priv->alloc_lock);
 	g_mutex_free (summary->priv->ref_lock);
 
 	g_free (summary->priv);
@@ -251,7 +256,7 @@ store_summary_store_info_set_string (CamelStoreSummary *summary,
 
 	switch (type) {
 	case CAMEL_STORE_INFO_PATH:
-		CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+		camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 		g_hash_table_remove (summary->folders_path, (gchar *)camel_store_info_path (summary, info));
 		g_free (info->path);
 		g_free (info->uri);
@@ -259,10 +264,10 @@ store_summary_store_info_set_string (CamelStoreSummary *summary,
 		info->path = g_strdup (str);
 		g_hash_table_insert (summary->folders_path, (gchar *)camel_store_info_path (summary, info), info);
 		summary->flags |= CAMEL_STORE_SUMMARY_DIRTY;
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+		camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 		break;
 	case CAMEL_STORE_INFO_NAME:
-		CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+		camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 		g_hash_table_remove (summary->folders_path, (gchar *)camel_store_info_path (summary, info));
 		p = strrchr (info->path, '/');
 		if (p) {
@@ -278,7 +283,7 @@ store_summary_store_info_set_string (CamelStoreSummary *summary,
 		g_free (info->uri);
 		info->uri = NULL;
 		g_hash_table_insert (summary->folders_path, (gchar *)camel_store_info_path (summary, info), info);
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+		camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 		break;
 	case CAMEL_STORE_INFO_URI:
 		g_warning ("Cannot set store info uri, aborting");
@@ -321,7 +326,6 @@ camel_store_summary_init (CamelStoreSummary *summary)
 
 	summary->priv->summary_lock = g_mutex_new ();
 	summary->priv->io_lock = g_mutex_new ();
-	summary->priv->alloc_lock = g_mutex_new ();
 	summary->priv->ref_lock = g_mutex_new ();
 }
 
@@ -369,12 +373,12 @@ camel_store_summary_set_filename (CamelStoreSummary *summary,
 {
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 
 	g_free (summary->summary_path);
 	summary->summary_path = g_strdup (name);
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 }
 
 /**
@@ -390,13 +394,13 @@ camel_store_summary_set_uri_base (CamelStoreSummary *summary,
 {
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 
 	if (summary->uri_base)
 		camel_url_free (summary->uri_base);
 	summary->uri_base = camel_url_new_with_base (base, "");
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 }
 
 /**
@@ -437,18 +441,18 @@ camel_store_summary_index (CamelStoreSummary *summary,
 
 	g_return_val_if_fail (CAMEL_IS_STORE_SUMMARY (summary), NULL);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, ref_lock);
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_REF_LOCK);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 
 	if (i < summary->folders->len)
 		info = g_ptr_array_index (summary->folders, i);
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 
 	if (info)
 		info->refcount++;
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+	camel_store_summary_unlock (summary, CSS_REF_LOCK);
 
 	return info;
 }
@@ -473,8 +477,8 @@ camel_store_summary_array (CamelStoreSummary *summary)
 
 	g_return_val_if_fail (CAMEL_IS_STORE_SUMMARY (summary), NULL);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, ref_lock);
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_REF_LOCK);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 
 	res = g_ptr_array_new ();
 	g_ptr_array_set_size (res, summary->folders->len);
@@ -483,8 +487,8 @@ camel_store_summary_array (CamelStoreSummary *summary)
 		info->refcount++;
 	}
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
+	camel_store_summary_unlock (summary, CSS_REF_LOCK);
 
 	return res;
 }
@@ -535,17 +539,17 @@ camel_store_summary_path (CamelStoreSummary *summary,
 	g_return_val_if_fail (CAMEL_IS_STORE_SUMMARY (summary), NULL);
 	g_return_val_if_fail (path != NULL, NULL);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, ref_lock);
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_REF_LOCK);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 
 	info = g_hash_table_lookup (summary->folders_path, path);
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 
 	if (info)
 		info->refcount++;
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+	camel_store_summary_unlock (summary, CSS_REF_LOCK);
 
 	return info;
 }
@@ -576,7 +580,7 @@ camel_store_summary_load (CamelStoreSummary *summary)
 	if (in == NULL)
 		return -1;
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, io_lock);
+	camel_store_summary_lock (summary, CSS_IO_LOCK);
 	if (class->summary_header_load (summary, in) == -1)
 		goto error;
 
@@ -590,7 +594,7 @@ camel_store_summary_load (CamelStoreSummary *summary)
 		camel_store_summary_add (summary, info);
 	}
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, io_lock);
+	camel_store_summary_unlock (summary, CSS_IO_LOCK);
 
 	if (fclose (in) != 0)
 		return -1;
@@ -602,7 +606,7 @@ camel_store_summary_load (CamelStoreSummary *summary)
 error:
 	i = ferror (in);
 	g_warning ("Cannot load summary file: %s", g_strerror (ferror (in)));
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, io_lock);
+	camel_store_summary_unlock (summary, CSS_IO_LOCK);
 	fclose (in);
 	summary->flags |= ~CAMEL_STORE_SUMMARY_DIRTY;
 	errno = i;
@@ -659,12 +663,12 @@ camel_store_summary_save (CamelStoreSummary *summary)
 
 	io (printf ("saving header\n"));
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, io_lock);
+	camel_store_summary_lock (summary, CSS_IO_LOCK);
 
 	if (class->summary_header_save (summary, out) == -1) {
 		i = errno;
 		fclose (out);
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, io_lock);
+		camel_store_summary_unlock (summary, CSS_IO_LOCK);
 		errno = i;
 		return -1;
 	}
@@ -679,7 +683,7 @@ camel_store_summary_save (CamelStoreSummary *summary)
 		class->store_info_save (summary, out, info);
 	}
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, io_lock);
+	camel_store_summary_unlock (summary, CSS_IO_LOCK);
 
 	if (fflush (out) != 0 || fsync (fileno (out)) == -1) {
 		i = errno;
@@ -722,9 +726,9 @@ camel_store_summary_header_load (CamelStoreSummary *summary)
 	if (in == NULL)
 		return -1;
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, io_lock);
+	camel_store_summary_lock (summary, CSS_IO_LOCK);
 	ret = class->summary_header_load (summary, in);
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, io_lock);
+	camel_store_summary_unlock (summary, CSS_IO_LOCK);
 
 	fclose (in);
 	summary->flags &= ~CAMEL_STORE_SUMMARY_DIRTY;
@@ -758,13 +762,13 @@ camel_store_summary_add (CamelStoreSummary *summary,
 		return;
 	}
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 
 	g_ptr_array_add (summary->folders, info);
 	g_hash_table_insert (summary->folders_path, (gchar *)camel_store_info_path (summary, info), info);
 	summary->flags |= CAMEL_STORE_SUMMARY_DIRTY;
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 }
 
 /**
@@ -785,7 +789,7 @@ camel_store_summary_add_from_path (CamelStoreSummary *summary,
 	g_return_val_if_fail (CAMEL_IS_STORE_SUMMARY (summary), NULL);
 	g_return_val_if_fail (path != NULL, NULL);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 
 	info = g_hash_table_lookup (summary->folders_path, path);
 	if (info != NULL) {
@@ -798,7 +802,7 @@ camel_store_summary_add_from_path (CamelStoreSummary *summary,
 		summary->flags |= CAMEL_STORE_SUMMARY_DIRTY;
 	}
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 
 	return info;
 }
@@ -849,15 +853,15 @@ camel_store_summary_info_free (CamelStoreSummary *summary,
 	class = CAMEL_STORE_SUMMARY_GET_CLASS (summary);
 	g_return_if_fail (class->store_info_free != NULL);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, ref_lock);
+	camel_store_summary_lock (summary, CSS_REF_LOCK);
 
 	info->refcount--;
 	if (info->refcount > 0) {
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+		camel_store_summary_unlock (summary, CSS_REF_LOCK);
 		return;
 	}
 
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+	camel_store_summary_unlock (summary, CSS_REF_LOCK);
 
 	class->store_info_free (summary, info);
 }
@@ -876,9 +880,9 @@ camel_store_summary_info_ref (CamelStoreSummary *summary,
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 	g_return_if_fail (info != NULL && info->refcount >= 1);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, ref_lock);
+	camel_store_summary_lock (summary, CSS_REF_LOCK);
 	info->refcount++;
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+	camel_store_summary_unlock (summary, CSS_REF_LOCK);
 }
 
 /**
@@ -893,9 +897,9 @@ camel_store_summary_touch (CamelStoreSummary *summary)
 {
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 	summary->flags |= CAMEL_STORE_SUMMARY_DIRTY;
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 }
 
 /**
@@ -911,9 +915,9 @@ camel_store_summary_clear (CamelStoreSummary *summary)
 
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 	if (camel_store_summary_count (summary) == 0) {
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+		camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 		return;
 	}
 
@@ -924,7 +928,7 @@ camel_store_summary_clear (CamelStoreSummary *summary)
 	g_hash_table_destroy (summary->folders_path);
 	summary->folders_path = g_hash_table_new (g_str_hash, g_str_equal);
 	summary->flags |= CAMEL_STORE_SUMMARY_DIRTY;
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 }
 
 /**
@@ -941,11 +945,11 @@ camel_store_summary_remove (CamelStoreSummary *summary,
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 	g_return_if_fail (info != NULL);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 	g_hash_table_remove (summary->folders_path, camel_store_info_path (summary, info));
 	g_ptr_array_remove (summary->folders, info);
 	summary->flags |= CAMEL_STORE_SUMMARY_DIRTY;
-	CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+	camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 
 	camel_store_summary_info_free (summary, info);
 }
@@ -967,18 +971,18 @@ camel_store_summary_remove_path (CamelStoreSummary *summary,
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 	g_return_if_fail (path != NULL);
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, ref_lock);
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_REF_LOCK);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
         if (g_hash_table_lookup_extended (summary->folders_path, path, (gpointer)&oldpath, (gpointer)&oldinfo)) {
 		/* make sure it doesn't vanish while we're removing it */
 		oldinfo->refcount++;
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+		camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
+		camel_store_summary_unlock (summary, CSS_REF_LOCK);
 		camel_store_summary_remove (summary, oldinfo);
 		camel_store_summary_info_free (summary, oldinfo);
         } else {
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, ref_lock);
+		camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
+		camel_store_summary_unlock (summary, CSS_REF_LOCK);
 	}
 }
 
@@ -995,7 +999,7 @@ camel_store_summary_remove_index (CamelStoreSummary *summary,
 {
 	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
 
-	CAMEL_STORE_SUMMARY_LOCK (summary, summary_lock);
+	camel_store_summary_lock (summary, CSS_SUMMARY_LOCK);
 	if (index < summary->folders->len) {
 		CamelStoreInfo *info = summary->folders->pdata[index];
 
@@ -1003,10 +1007,10 @@ camel_store_summary_remove_index (CamelStoreSummary *summary,
 		g_ptr_array_remove_index (summary->folders, index);
 		summary->flags |= CAMEL_STORE_SUMMARY_DIRTY;
 
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+		camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 		camel_store_summary_info_free (summary, info);
 	} else {
-		CAMEL_STORE_SUMMARY_UNLOCK (summary, summary_lock);
+		camel_store_summary_unlock (summary, CSS_SUMMARY_LOCK);
 	}
 }
 
@@ -1083,3 +1087,65 @@ camel_store_info_set_string (CamelStoreSummary *summary,
 
 	class->store_info_set_string (summary, info, type, value);
 }
+
+/**
+ * camel_store_summary_lock:
+ * @summary: a #CamelStoreSummary
+ * @lock: lock type to lock
+ *
+ * Locks #summary's #lock. Unlock it with camel_store_summary_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_store_summary_lock (CamelStoreSummary *summary, CamelStoreSummaryLock lock)
+{
+	g_return_if_fail (summary != NULL);
+	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
+	g_return_if_fail (summary->priv != NULL);
+
+	switch (lock) {
+	case CSS_SUMMARY_LOCK:
+		g_mutex_lock (summary->priv->summary_lock);
+		break;
+	case CSS_IO_LOCK:
+		g_mutex_lock (summary->priv->io_lock);
+		break;
+	case CSS_REF_LOCK:
+		g_mutex_lock (summary->priv->ref_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_store_summary_unlock:
+ * @summary: a #CamelStoreSummary
+ * @lock: lock type to unlock
+ *
+ * Unlocks #summary's #lock, previously locked with camel_store_summary_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_store_summary_unlock (CamelStoreSummary *summary, CamelStoreSummaryLock lock)
+{
+	g_return_if_fail (summary != NULL);
+	g_return_if_fail (CAMEL_IS_STORE_SUMMARY (summary));
+	g_return_if_fail (summary->priv != NULL);
+
+	switch (lock) {
+	case CSS_SUMMARY_LOCK:
+		g_mutex_unlock (summary->priv->summary_lock);
+		break;
+	case CSS_IO_LOCK:
+		g_mutex_unlock (summary->priv->io_lock);
+		break;
+	case CSS_REF_LOCK:
+		g_mutex_unlock (summary->priv->ref_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-store-summary.h b/camel/camel-store-summary.h
index fdfa05d..7e8a338 100644
--- a/camel/camel-store-summary.h
+++ b/camel/camel-store-summary.h
@@ -89,6 +89,12 @@ typedef enum _CamelStoreSummaryFlags {
 	CAMEL_STORE_SUMMARY_FRAGMENT = 1<<1 /* path name is stored in fragment rather than path */
 } CamelStoreSummaryFlags;
 
+typedef enum _CamelStoreSummaryLock {
+	CSS_SUMMARY_LOCK,
+	CSS_IO_LOCK,
+	CSS_REF_LOCK
+} CamelStoreSummaryLock;
+
 struct _CamelStoreSummary {
 	CamelObject parent;
 	CamelStoreSummaryPrivate *priv;
@@ -182,6 +188,9 @@ void camel_store_info_set_string(CamelStoreSummary *summary, CamelStoreInfo *inf
 #define camel_store_info_uri(s, i) (camel_store_info_string((CamelStoreSummary *)s, (const CamelStoreInfo *)i, CAMEL_STORE_INFO_URI))
 #define camel_store_info_name(s, i) (camel_store_info_string((CamelStoreSummary *)s, (const CamelStoreInfo *)i, CAMEL_STORE_INFO_NAME))
 
+void camel_store_summary_lock   (CamelStoreSummary *summary, CamelStoreSummaryLock lock);
+void camel_store_summary_unlock (CamelStoreSummary *summary, CamelStoreSummaryLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_STORE_SUMMARY_H */
diff --git a/camel/camel-store.c b/camel/camel-store.c
index 619ec52..c8f7928 100644
--- a/camel/camel-store.c
+++ b/camel/camel-store.c
@@ -37,7 +37,6 @@
 #include "camel-debug.h"
 #include "camel-exception.h"
 #include "camel-folder.h"
-#include "camel-private.h"
 #include "camel-session.h"
 #include "camel-store.h"
 #include "camel-vtrash-folder.h"
@@ -45,6 +44,10 @@
 #define d(x)
 #define w(x)
 
+struct _CamelStorePrivate {
+	GStaticRecMutex folder_lock;	/* for locking folder operations */
+};
+
 static gpointer camel_store_parent_class;
 
 /**
@@ -457,9 +460,9 @@ camel_store_create_folder (CamelStore *store,
 		return NULL;
 	}
 
-	CAMEL_STORE_LOCK(store, folder_lock);
+	camel_store_lock (store, CS_FOLDER_LOCK);
 	fi = class->create_folder (store, parent_name, folder_name, ex);
-	CAMEL_STORE_UNLOCK(store, folder_lock);
+	camel_store_unlock (store, CS_FOLDER_LOCK);
 
 	return fi;
 }
@@ -527,7 +530,7 @@ camel_store_delete_folder (CamelStore *store,
 
 	camel_exception_init(&local);
 
-	CAMEL_STORE_LOCK(store, folder_lock);
+	camel_store_lock (store, CS_FOLDER_LOCK);
 
 	class->delete_folder(store, folder_name, &local);
 
@@ -541,7 +544,7 @@ camel_store_delete_folder (CamelStore *store,
 	else
 		camel_exception_xfer(ex, &local);
 
-	CAMEL_STORE_UNLOCK(store, folder_lock);
+	camel_store_unlock (store, CS_FOLDER_LOCK);
 }
 
 /**
@@ -588,7 +591,7 @@ camel_store_rename_folder (CamelStore *store,
 	old_name = g_strdup(old_namein);
 	oldlen = strlen(old_name);
 
-	CAMEL_STORE_LOCK(store, folder_lock);
+	camel_store_lock (store, CS_FOLDER_LOCK);
 
 	/* If the folder is open (or any subfolders of the open folder)
 	   We need to rename them atomically with renaming the actual folder path */
@@ -603,7 +606,7 @@ camel_store_rename_folder (CamelStore *store,
 				&& strncmp(folder->full_name, old_name, oldlen) == 0
 				&& folder->full_name[oldlen] == '/')) {
 				d(printf("Found subfolder of '%s' == '%s'\n", old_name, folder->full_name));
-				CAMEL_FOLDER_REC_LOCK(folder, lock);
+				camel_folder_lock (folder, CF_REC_LOCK);
 			} else {
 				g_ptr_array_remove_index_fast(folders, i);
 				i--;
@@ -631,7 +634,7 @@ camel_store_rename_folder (CamelStore *store,
 				camel_folder_rename(folder, new);
 				g_free(new);
 
-				CAMEL_FOLDER_REC_UNLOCK(folder, lock);
+				camel_folder_unlock (folder, CF_REC_LOCK);
 				camel_object_unref (folder);
 			}
 
@@ -649,13 +652,13 @@ camel_store_rename_folder (CamelStore *store,
 			/* Failed, just unlock our folders for re-use */
 			for (i=0;i<folders->len;i++) {
 				folder = folders->pdata[i];
-				CAMEL_FOLDER_REC_UNLOCK(folder, lock);
+				camel_folder_unlock (folder, CF_REC_LOCK);
 				camel_object_unref (folder);
 			}
 		}
 	}
 
-	CAMEL_STORE_UNLOCK(store, folder_lock);
+	camel_store_unlock (store, CS_FOLDER_LOCK);
 
 	g_ptr_array_free(folders, TRUE);
 	g_free(old_name);
@@ -681,9 +684,9 @@ camel_store_get_inbox (CamelStore *store,
 	class = CAMEL_STORE_GET_CLASS (store);
 	g_return_val_if_fail (class->get_inbox != NULL, NULL);
 
-	CAMEL_STORE_LOCK (store, folder_lock);
+	camel_store_lock (store, CS_FOLDER_LOCK);
 	folder = class->get_inbox (store, ex);
-	CAMEL_STORE_UNLOCK (store, folder_lock);
+	camel_store_unlock (store, CS_FOLDER_LOCK);
 
 	return folder;
 }
@@ -1214,9 +1217,9 @@ camel_store_folder_subscribed (CamelStore *store,
 	class = CAMEL_STORE_GET_CLASS (store);
 	g_return_val_if_fail (class->folder_subscribed != NULL, FALSE);
 
-	CAMEL_STORE_LOCK (store, folder_lock);
+	camel_store_lock (store, CS_FOLDER_LOCK);
 	ret = class->folder_subscribed (store, folder_name);
-	CAMEL_STORE_UNLOCK (store, folder_lock);
+	camel_store_unlock (store, CS_FOLDER_LOCK);
 
 	return ret;
 }
@@ -1243,9 +1246,9 @@ camel_store_subscribe_folder (CamelStore *store,
 	class = CAMEL_STORE_GET_CLASS (store);
 	g_return_if_fail (class->subscribe_folder != NULL);
 
-	CAMEL_STORE_LOCK (store, folder_lock);
+	camel_store_lock (store, CS_FOLDER_LOCK);
 	class->subscribe_folder (store, folder_name, ex);
-	CAMEL_STORE_UNLOCK (store, folder_lock);
+	camel_store_unlock (store, CS_FOLDER_LOCK);
 }
 
 /**
@@ -1273,7 +1276,7 @@ camel_store_unsubscribe_folder (CamelStore *store,
 
 	camel_exception_init (&local);
 
-	CAMEL_STORE_LOCK (store, folder_lock);
+	camel_store_lock (store, CS_FOLDER_LOCK);
 
 	class->unsubscribe_folder (store, folder_name, ex);
 
@@ -1282,7 +1285,7 @@ camel_store_unsubscribe_folder (CamelStore *store,
 	else
 		camel_exception_xfer (ex, &local);
 
-	CAMEL_STORE_UNLOCK (store, folder_lock);
+	camel_store_unlock (store, CS_FOLDER_LOCK);
 }
 
 /**
@@ -1398,3 +1401,53 @@ camel_store_can_refresh_folder (CamelStore *store,
 
 	return class->can_refresh_folder (store, info, ex);
 }
+
+/**
+ * camel_store_lock:
+ * @store: a #CamelStore
+ * @lock: lock type to lock
+ *
+ * Locks #store's #lock. Unlock it with camel_store_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_store_lock (CamelStore *store, CamelStoreLock lock)
+{
+	g_return_if_fail (store != NULL);
+	g_return_if_fail (CAMEL_IS_STORE (store));
+	g_return_if_fail (store->priv != NULL);
+
+	switch (lock) {
+	case CS_FOLDER_LOCK:
+		g_static_rec_mutex_lock (&store->priv->folder_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_store_unlock:
+ * @store: a #CamelStore
+ * @lock: lock type to unlock
+ *
+ * Unlocks #store's #lock, previously locked with camel_store_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_store_unlock (CamelStore *store, CamelStoreLock lock)
+{
+	g_return_if_fail (store != NULL);
+	g_return_if_fail (CAMEL_IS_STORE (store));
+	g_return_if_fail (store->priv != NULL);
+
+	switch (lock) {
+	case CS_FOLDER_LOCK:
+		g_static_rec_mutex_unlock (&store->priv->folder_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-store.h b/camel/camel-store.h
index da4cf6b..944fc17 100644
--- a/camel/camel-store.h
+++ b/camel/camel-store.h
@@ -50,6 +50,10 @@ enum {
 	CAMEL_STORE_ARG_FIRST = CAMEL_SERVICE_ARG_FIRST + 100
 };
 
+typedef enum _CamelStoreLock {
+	CS_FOLDER_LOCK
+} CamelStoreLock;
+
 typedef struct _CamelFolderInfo {
 	struct _CamelFolderInfo *next;
 	struct _CamelFolderInfo *parent;
@@ -299,6 +303,9 @@ gboolean	camel_store_can_refresh_folder	(CamelStore *store,
 						 CamelFolderInfo *info,
 						 CamelException *ex);
 
+void		camel_store_lock		(CamelStore *store, CamelStoreLock lock);
+void		camel_store_unlock		(CamelStore *store, CamelStoreLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_STORE_H */
diff --git a/camel/camel-stream-fs.c b/camel/camel-stream-fs.c
index f1e2b00..4502212 100644
--- a/camel/camel-stream-fs.c
+++ b/camel/camel-stream-fs.c
@@ -36,8 +36,8 @@
 
 #include "camel-file-utils.h"
 #include "camel-operation.h"
-#include "camel-private.h"
 #include "camel-stream-fs.h"
+#include "camel-win32.h"
 
 struct _CamelStreamFsPrivate {
 	gint fd;	/* file descriptor on the underlying file */
diff --git a/camel/camel-stream-vfs.c b/camel/camel-stream-vfs.c
index 0f1bad0..e1de33d 100644
--- a/camel/camel-stream-vfs.c
+++ b/camel/camel-stream-vfs.c
@@ -32,7 +32,6 @@
 
 #include "camel-file-utils.h"
 #include "camel-operation.h"
-#include "camel-private.h"
 #include "camel-stream-vfs.h"
 
 static CamelStreamClass *parent_class = NULL;
diff --git a/camel/camel-tcp-stream-ssl.c b/camel/camel-tcp-stream-ssl.c
index 42d2f4e..117239f 100644
--- a/camel/camel-tcp-stream-ssl.c
+++ b/camel/camel-tcp-stream-ssl.c
@@ -55,7 +55,6 @@
 #include "camel-certdb.h"
 #include "camel-file-utils.h"
 #include "camel-operation.h"
-#include "camel-private.h"
 #include "camel-session.h"
 #include "camel-stream-fs.h"
 #include "camel-tcp-stream-ssl.h"
diff --git a/camel/camel-text-index.c b/camel/camel-text-index.c
index 0d9c571..ac471cb 100644
--- a/camel/camel-text-index.c
+++ b/camel/camel-text-index.c
@@ -40,7 +40,6 @@
 #include "camel-mempool.h"
 #include "camel-object.h"
 #include "camel-partition-table.h"
-#include "camel-private.h"
 #include "camel-text-index.h"
 
 #define w(x)
diff --git a/camel/camel-transport.c b/camel/camel-transport.c
index 6b4680c..29aba48 100644
--- a/camel/camel-transport.c
+++ b/camel/camel-transport.c
@@ -29,9 +29,12 @@
 
 #include "camel-address.h"
 #include "camel-mime-message.h"
-#include "camel-private.h"
 #include "camel-transport.h"
 
+struct _CamelTransportPrivate {
+	GMutex *send_lock;   /* for locking send operations */
+};
+
 static CamelServiceClass *parent_class = NULL;
 
 static void
@@ -109,9 +112,59 @@ camel_transport_send_to (CamelTransport *transport,
 	class = CAMEL_TRANSPORT_GET_CLASS (transport);
 	g_return_val_if_fail (class->send_to != NULL, FALSE);
 
-	CAMEL_TRANSPORT_LOCK (transport, send_lock);
+	camel_transport_lock (transport, CT_SEND_LOCK);
 	sent = class->send_to (transport, message, from, recipients, ex);
-	CAMEL_TRANSPORT_UNLOCK (transport, send_lock);
+	camel_transport_unlock (transport, CT_SEND_LOCK);
 
 	return sent;
 }
+
+/**
+ * camel_transport_lock:
+ * @transport: a #CamelTransport
+ * @lock: lock type to lock
+ *
+ * Locks #transport's #lock. Unlock it with camel_transport_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_transport_lock (CamelTransport *transport, CamelTransportLock lock)
+{
+	g_return_if_fail (transport != NULL);
+	g_return_if_fail (CAMEL_IS_TRANSPORT (transport));
+	g_return_if_fail (transport->priv != NULL);
+
+	switch (lock) {
+	case CT_SEND_LOCK:
+		g_mutex_lock (transport->priv->send_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_transport_unlock:
+ * @transport: a #CamelTransport
+ * @lock: lock type to unlock
+ *
+ * Unlocks #transport's #lock, previously locked with camel_transport_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_transport_unlock (CamelTransport *transport, CamelTransportLock lock)
+{
+	g_return_if_fail (transport != NULL);
+	g_return_if_fail (CAMEL_IS_TRANSPORT (transport));
+	g_return_if_fail (transport->priv != NULL);
+
+	switch (lock) {
+	case CT_SEND_LOCK:
+		g_mutex_unlock (transport->priv->send_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-transport.h b/camel/camel-transport.h
index 0817bdb..3508351 100644
--- a/camel/camel-transport.h
+++ b/camel/camel-transport.h
@@ -51,6 +51,10 @@ enum {
 	CAMEL_TRANSPORT_ARG_FIRST  = CAMEL_SERVICE_ARG_FIRST + 100
 };
 
+typedef enum _CamelTransportLock {
+	CT_SEND_LOCK
+} CamelTransportLock;
+
 struct _CamelTransport {
 	CamelService parent;
 	CamelTransportPrivate *priv;
@@ -74,6 +78,9 @@ gboolean camel_transport_send_to (CamelTransport *transport,
 
 CamelType camel_transport_get_type (void);
 
+void camel_transport_lock	(CamelTransport *transport, CamelTransportLock lock);
+void camel_transport_unlock	(CamelTransport *transport, CamelTransportLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_TRANSPORT_H */
diff --git a/camel/camel-uid-cache.c b/camel/camel-uid-cache.c
index 109b8f5..6baddb6 100644
--- a/camel/camel-uid-cache.c
+++ b/camel/camel-uid-cache.c
@@ -35,8 +35,8 @@
 #include <glib/gstdio.h>
 
 #include "camel-file-utils.h"
-#include "camel-private.h"
 #include "camel-uid-cache.h"
+#include "camel-win32.h"
 
 struct _uid_state {
 	gint level;
diff --git a/camel/camel-vee-folder.c b/camel/camel-vee-folder.c
index 4d10b7c..6a7a62c 100644
--- a/camel/camel-vee-folder.c
+++ b/camel/camel-vee-folder.c
@@ -33,7 +33,6 @@
 #include "camel-exception.h"
 #include "camel-folder-search.h"
 #include "camel-mime-message.h"
-#include "camel-private.h"
 #include "camel-session.h"
 #include "camel-store.h"
 #include "camel-vee-folder.h"
@@ -46,6 +45,17 @@
 #define d(x)
 #define dd(x) (camel_debug ("vfolder")?(x):0)
 
+struct _CamelVeeFolderPrivate {
+	gboolean destroyed;
+	GList *folders;			/* lock using subfolder_lock before changing/accessing */
+	GList *folders_changed;		/* for list of folders that have changed between updates */
+
+	GMutex *summary_lock;		/* for locking vfolder summary */
+	GMutex *subfolder_lock;		/* for locking the subfolder list */
+	GMutex *changed_lock;		/* for locking the folders-changed list */
+	gint unread_vfolder;
+};
+
 #define _PRIVATE(o) (((CamelVeeFolder *)(o))->priv)
 
 #if 0
@@ -181,36 +191,36 @@ camel_vee_folder_add_folder (CamelVeeFolder *vf, CamelFolder *sub)
 		return;
 	}
 
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 
 	/* for normal vfolders we want only unique ones, for unmatched we want them all recorded */
 	if (g_list_find (p->folders, sub) == NULL) {
 		p->folders = g_list_append (
 			p->folders, camel_object_ref (sub));
 
-		CAMEL_FOLDER_LOCK (vf, change_lock);
+		camel_folder_lock (CAMEL_FOLDER (vf), CF_CHANGE_LOCK);
 
 		/* update the freeze state of 'sub' to match our freeze state */
-		for (i = 0; i < ((CamelFolder *)vf)->priv->frozen; i++)
+		for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)vf); i++)
 			camel_folder_freeze (sub);
 
-		CAMEL_FOLDER_UNLOCK (vf, change_lock);
+		camel_folder_unlock (CAMEL_FOLDER (vf), CF_CHANGE_LOCK);
 	}
 	if ((vf->flags & CAMEL_STORE_FOLDER_PRIVATE) == 0 && !CAMEL_IS_VEE_FOLDER (sub) && folder_unmatched != NULL) {
 		struct _CamelVeeFolderPrivate *up = _PRIVATE (folder_unmatched);
 		up->folders = g_list_append (
 			up->folders, camel_object_ref (sub));
 
-		CAMEL_FOLDER_LOCK (folder_unmatched, change_lock);
+		camel_folder_lock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
 
 		/* update the freeze state of 'sub' to match Unmatched's freeze state */
-		for (i = 0; i < ((CamelFolder *)folder_unmatched)->priv->frozen; i++)
+		for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)folder_unmatched); i++)
 			camel_folder_freeze (sub);
 
-		CAMEL_FOLDER_UNLOCK (folder_unmatched, change_lock);
+		camel_folder_unlock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
 	}
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 
 	d (printf ("camel_vee_folder_add_folder (%s, %s)\n", ((CamelFolder *)vf)->full_name, sub->full_name));
 
@@ -237,14 +247,14 @@ camel_vee_folder_remove_folder (CamelVeeFolder *vf, CamelFolder *sub)
 	gint i;
 	CamelVeeFolder *folder_unmatched = vf->parent_vee_store ? vf->parent_vee_store->folder_unmatched : NULL;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 
-	CAMEL_VEE_FOLDER_LOCK (vf, changed_lock);
+	camel_vee_folder_lock (vf, CVF_CHANGED_LOCK);
 	p->folders_changed = g_list_remove (p->folders_changed, sub);
-	CAMEL_VEE_FOLDER_UNLOCK (vf, changed_lock);
+	camel_vee_folder_unlock (vf, CVF_CHANGED_LOCK);
 
 	if (g_list_find (p->folders, sub) == NULL) {
-		CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+		camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 		return;
 	}
 
@@ -255,17 +265,17 @@ camel_vee_folder_remove_folder (CamelVeeFolder *vf, CamelFolder *sub)
 	p->folders = g_list_remove (p->folders, sub);
 
 	/* undo the freeze state that we have imposed on this source folder */
-	CAMEL_FOLDER_LOCK (vf, change_lock);
-	for (i = 0; i < ((CamelFolder *)vf)->priv->frozen; i++)
+	camel_folder_lock (CAMEL_FOLDER (vf), CF_CHANGE_LOCK);
+	for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)vf); i++)
 		camel_folder_thaw (sub);
-	CAMEL_FOLDER_UNLOCK (vf, change_lock);
+	camel_folder_unlock (CAMEL_FOLDER (vf), CF_CHANGE_LOCK);
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 
 	if (folder_unmatched != NULL) {
 		struct _CamelVeeFolderPrivate *up = _PRIVATE (folder_unmatched);
 
-		CAMEL_VEE_FOLDER_LOCK (folder_unmatched, subfolder_lock);
+		camel_vee_folder_lock (folder_unmatched, CVF_SUBFOLDER_LOCK);
 		/* if folder deleted, then blow it away from unmatched always, and remove all refs to it */
 		if (sub->folder_flags & CAMEL_FOLDER_HAS_BEEN_DELETED) {
 			while (g_list_find (up->folders, sub)) {
@@ -273,10 +283,10 @@ camel_vee_folder_remove_folder (CamelVeeFolder *vf, CamelFolder *sub)
 				camel_object_unref (sub);
 
 				/* undo the freeze state that Unmatched has imposed on this source folder */
-				CAMEL_FOLDER_LOCK (folder_unmatched, change_lock);
-				for (i = 0; i < ((CamelFolder *)folder_unmatched)->priv->frozen; i++)
+				camel_folder_lock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
+				for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)folder_unmatched); i++)
 					camel_folder_thaw (sub);
-				CAMEL_FOLDER_UNLOCK (folder_unmatched, change_lock);
+				camel_folder_unlock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
 			}
 		} else if ((vf->flags & CAMEL_STORE_FOLDER_PRIVATE) == 0) {
 			if (g_list_find (up->folders, sub) != NULL) {
@@ -284,13 +294,13 @@ camel_vee_folder_remove_folder (CamelVeeFolder *vf, CamelFolder *sub)
 				camel_object_unref (sub);
 
 				/* undo the freeze state that Unmatched has imposed on this source folder */
-				CAMEL_FOLDER_LOCK (folder_unmatched, change_lock);
-				for (i = 0; i < ((CamelFolder *)folder_unmatched)->priv->frozen; i++)
+				camel_folder_lock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
+				for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)folder_unmatched); i++)
 					camel_folder_thaw (sub);
-				CAMEL_FOLDER_UNLOCK (folder_unmatched, change_lock);
+				camel_folder_unlock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
 			}
 		}
-		CAMEL_VEE_FOLDER_UNLOCK (folder_unmatched, subfolder_lock);
+		camel_vee_folder_unlock (folder_unmatched, CVF_SUBFOLDER_LOCK);
 	}
 
 	((CamelVeeFolderClass *)((CamelObject *)vf)->klass)->remove_folder (vf, sub);
@@ -338,14 +348,14 @@ camel_vee_folder_set_folders (CamelVeeFolder *vf, GList *folders)
 	CamelFolder *folder;
 
 	/* setup a table of all folders we have currently */
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 	l = p->folders;
 	while (l) {
 		g_hash_table_insert (remove, l->data, l->data);
 		camel_object_ref (l->data);
 		l = l->next;
 	}
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 
 	/* if we already have the folder, ignore it, otherwise add it */
 	l = folders;
@@ -447,10 +457,10 @@ static void vee_refresh_info (CamelFolder *folder, CamelException *ex)
 	struct _CamelVeeFolderPrivate *p = _PRIVATE (vf);
 	GList *node, *list;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, changed_lock);
+	camel_vee_folder_lock (vf, CVF_CHANGED_LOCK);
 	list = p->folders_changed;
 	p->folders_changed = NULL;
-	CAMEL_VEE_FOLDER_UNLOCK (vf, changed_lock);
+	camel_vee_folder_unlock (vf, CVF_CHANGED_LOCK);
 
 	node = list;
 	while (node) {
@@ -562,7 +572,7 @@ vee_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
 		folder->summary->visible_count = ((CamelVeeSummary *)folder->summary)->fake_visible_count;
 	((CamelVeeSummary *)folder->summary)->fake_visible_count = 0;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 
 	node = p->folders;
 	while (node) {
@@ -598,10 +608,10 @@ vee_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
 	 * actually rebuilding. */
 #if 0
 	if (node == NULL) {
-		CAMEL_VEE_FOLDER_LOCK (vf, changed_lock);
+		camel_vee_folder_lock (vf, CVF_CHANGED_LOCK);
 		g_list_free (p->folders_changed);
 		p->folders_changed = NULL;
-		CAMEL_VEE_FOLDER_UNLOCK (vf, changed_lock);
+		camel_vee_folder_unlock (vf, CVF_CHANGED_LOCK);
 	}
 #endif
 	if (vf->priv->unread_vfolder == 1) {
@@ -625,7 +635,7 @@ vee_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
 		g_slist_foreach (del, (GFunc) camel_pstring_free, NULL);
 		g_slist_free (del);
 	}
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 
 	camel_object_state_write (vf);
 }
@@ -754,7 +764,7 @@ vee_search_by_uids (CamelFolder *folder, const gchar *expression, GPtrArray *uid
 	struct _CamelVeeFolderPrivate *p = _PRIVATE (vf);
 	GHashTable *searched = g_hash_table_new (NULL, NULL);
 
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 
 	expr = g_strdup_printf ("(and %s %s)", vf->expression ? vf->expression : "", expression);
 	node = p->folders;
@@ -798,7 +808,7 @@ vee_search_by_uids (CamelFolder *folder, const gchar *expression, GPtrArray *uid
 	}
 
 	g_free (expr);
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 
 	g_hash_table_destroy (searched);
 	g_ptr_array_free (folder_uids, TRUE);
@@ -831,18 +841,18 @@ static void vee_delete (CamelFolder *folder)
 
 	/* NB: this is never called on UNMTACHED */
 
-	CAMEL_VEE_FOLDER_LOCK (folder, subfolder_lock);
+	camel_vee_folder_lock (CAMEL_VEE_FOLDER (folder), CVF_SUBFOLDER_LOCK);
 	while (p->folders) {
 		CamelFolder *f = p->folders->data;
 
 		camel_object_ref (f);
-		CAMEL_VEE_FOLDER_UNLOCK (folder, subfolder_lock);
+		camel_vee_folder_unlock (CAMEL_VEE_FOLDER (folder), CVF_SUBFOLDER_LOCK);
 
 		camel_vee_folder_remove_folder ((CamelVeeFolder *)folder, f);
 		camel_object_unref (f);
-		CAMEL_VEE_FOLDER_LOCK (folder, subfolder_lock);
+		camel_vee_folder_lock (CAMEL_VEE_FOLDER (folder), CVF_SUBFOLDER_LOCK);
 	}
-	CAMEL_VEE_FOLDER_UNLOCK (folder, subfolder_lock);
+	camel_vee_folder_unlock (CAMEL_VEE_FOLDER (folder), CVF_SUBFOLDER_LOCK);
 
 	((CamelFolderClass *)camel_vee_folder_parent)->delete (folder);
 	((CamelVeeFolder *)folder)->deleted = TRUE;
@@ -911,18 +921,18 @@ vee_folder_remove_folder (CamelVeeFolder *vf, CamelFolder *source)
 	if ((source->folder_flags & CAMEL_FOLDER_HAS_BEEN_DELETED))
 		killun = TRUE;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, summary_lock);
+	camel_vee_folder_lock (vf, CVF_SUMMARY_LOCK);
 
 	if (folder_unmatched != NULL) {
 		/* check if this folder is still to be part of unmatched */
 		if ((vf->flags & CAMEL_STORE_FOLDER_PRIVATE) == 0 && !killun) {
-			CAMEL_VEE_FOLDER_LOCK (folder_unmatched, subfolder_lock);
+			camel_vee_folder_lock (folder_unmatched, CVF_SUBFOLDER_LOCK);
 			still = g_list_find (_PRIVATE (folder_unmatched)->folders, source) != NULL;
-			CAMEL_VEE_FOLDER_UNLOCK (folder_unmatched, subfolder_lock);
+			camel_vee_folder_unlock (folder_unmatched, CVF_SUBFOLDER_LOCK);
 			camel_vee_folder_hash_folder (source, hash);
 		}
 
-		CAMEL_VEE_FOLDER_LOCK (folder_unmatched, summary_lock);
+		camel_vee_folder_lock (folder_unmatched, CVF_SUMMARY_LOCK);
 
 		/* See if we just blow all uid's from this folder away from unmatched, regardless */
 		if (killun) {
@@ -1009,7 +1019,7 @@ vee_folder_remove_folder (CamelVeeFolder *vf, CamelFolder *source)
 			folder_unmatched->changes = camel_folder_change_info_new ();
 		}
 
-		CAMEL_VEE_FOLDER_UNLOCK (folder_unmatched, summary_lock);
+		camel_vee_folder_unlock (folder_unmatched, CVF_SUMMARY_LOCK);
 	}
 
 	if (camel_folder_change_info_changed (vf->changes)) {
@@ -1017,7 +1027,7 @@ vee_folder_remove_folder (CamelVeeFolder *vf, CamelFolder *source)
 		vf->changes = camel_folder_change_info_new ();
 	}
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, summary_lock);
+	camel_vee_folder_unlock (vf, CVF_SUMMARY_LOCK);
 
 	if (unmatched_changes) {
 		camel_object_trigger_event ((CamelObject *)folder_unmatched, "folder_changed", unmatched_changes);
@@ -1159,7 +1169,7 @@ vee_rebuild_folder (CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 	u.rebuilt = rebuilded;
 	u.correlating = correlating;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, summary_lock);
+	camel_vee_folder_lock (vf, CVF_SUMMARY_LOCK);
 
 	/* we build 2 hash tables, one for all uid's not matched, the
 	   other for all matched uid's, we just ref the real memory */
@@ -1190,7 +1200,7 @@ vee_rebuild_folder (CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 	}
 
 	if (folder_unmatched != NULL)
-		CAMEL_VEE_FOLDER_LOCK (folder_unmatched, summary_lock);
+		camel_vee_folder_lock (folder_unmatched, CVF_SUMMARY_LOCK);
 
 	/* scan, looking for "old" uid's to be removed. "old" uid's
 	   are those that are from previous added sources (not in
@@ -1279,7 +1289,7 @@ vee_rebuild_folder (CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 			folder_unmatched->changes = camel_folder_change_info_new ();
 		}
 
-		CAMEL_VEE_FOLDER_UNLOCK (folder_unmatched, summary_lock);
+		camel_vee_folder_unlock (folder_unmatched, CVF_SUMMARY_LOCK);
 	}
 
 	if (camel_folder_change_info_changed (vf->changes)) {
@@ -1287,7 +1297,7 @@ vee_rebuild_folder (CamelVeeFolder *vf, CamelFolder *source, CamelException *ex)
 		vf->changes = camel_folder_change_info_new ();
 	}
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, summary_lock);
+	camel_vee_folder_unlock (vf, CVF_SUMMARY_LOCK);
 
 	/* Del the unwanted things from the summary, we don't hold any locks now. */
 	if (del_list) {
@@ -1507,9 +1517,9 @@ folder_changed_change (CamelSession *session, CamelSessionThreadMsg *msg)
 	gboolean correlating = expression_is_correlating (vf->expression);
 
 	/* Check the folder hasn't beem removed while we weren't watching */
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 	if (g_list_find (_PRIVATE (vf)->folders, sub) == NULL) {
-		CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+		camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 		return;
 	}
 
@@ -1565,10 +1575,10 @@ folder_changed_change (CamelSession *session, CamelSessionThreadMsg *msg)
 		camel_exception_clear (&ex);
 	}
 
-	CAMEL_VEE_FOLDER_LOCK (vf, summary_lock);
+	camel_vee_folder_lock (vf, CVF_SUMMARY_LOCK);
 
 	if (folder_unmatched != NULL)
-		CAMEL_VEE_FOLDER_LOCK (folder_unmatched, summary_lock);
+		camel_vee_folder_lock (folder_unmatched, CVF_SUMMARY_LOCK);
 
 	if (matches_changed || matches_added || changes->uid_removed->len||present)
 		camel_db_begin_transaction (folder->parent_store->cdb_w, NULL);
@@ -1703,7 +1713,7 @@ folder_changed_change (CamelSession *session, CamelSessionThreadMsg *msg)
 			folder_unmatched->changes = camel_folder_change_info_new ();
 		}
 
-		CAMEL_VEE_FOLDER_UNLOCK (folder_unmatched, summary_lock);
+		camel_vee_folder_unlock (folder_unmatched, CVF_SUMMARY_LOCK);
 	}
 
 	if (camel_folder_change_info_changed (vf->changes)) {
@@ -1713,7 +1723,7 @@ folder_changed_change (CamelSession *session, CamelSessionThreadMsg *msg)
 
 	if (matches_changed || matches_added || changes->uid_removed->len || present)
 		camel_db_end_transaction (folder->parent_store->cdb_w, NULL);
-	CAMEL_VEE_FOLDER_UNLOCK (vf, summary_lock);
+	camel_vee_folder_unlock (vf, CVF_SUMMARY_LOCK);
 
 	/* Cleanup stuff on our folder */
 	if (matches_added)
@@ -1724,7 +1734,7 @@ folder_changed_change (CamelSession *session, CamelSessionThreadMsg *msg)
 	if (matches_changed)
 		camel_folder_search_free (sub, matches_changed);
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 
 	/* cleanup the rest */
 	if (newchanged)
@@ -1749,10 +1759,10 @@ folder_changed_change (CamelSession *session, CamelSessionThreadMsg *msg)
 	 *   rather be safe than sorry.)
 	 */
 	if ((vf_changes && (vf->flags & CAMEL_STORE_VEE_FOLDER_AUTO) == 0) || correlating) {
-		CAMEL_VEE_FOLDER_LOCK (vf, changed_lock);
+		camel_vee_folder_lock (vf, CVF_CHANGED_LOCK);
 		if (g_list_find (vf->priv->folders_changed, sub) == NULL)
 			vf->priv->folders_changed = g_list_prepend (vf->priv->folders_changed, sub);
-		CAMEL_VEE_FOLDER_UNLOCK (vf, changed_lock);
+		camel_vee_folder_unlock (vf, CVF_CHANGED_LOCK);
 	}
 
 	if (vf_changes) {
@@ -1816,7 +1826,7 @@ subfolder_renamed_update (CamelVeeFolder *vf, CamelFolder *sub, gchar hash[8])
 	GHashTable *unmatched_uids = vf->parent_vee_store ? vf->parent_vee_store->unmatched_uids : NULL;
 	CamelFolderSummary *ssummary = sub->summary;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, summary_lock);
+	camel_vee_folder_lock (vf, CVF_SUMMARY_LOCK);
 
 	count = camel_folder_summary_count (((CamelFolder *)vf)->summary);
 	for (i=0;i<count;i++) {
@@ -1862,7 +1872,7 @@ subfolder_renamed_update (CamelVeeFolder *vf, CamelFolder *sub, gchar hash[8])
 		vf->changes = camel_folder_change_info_new ();
 	}
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, summary_lock);
+	camel_vee_folder_unlock (vf, CVF_SUMMARY_LOCK);
 
 	if (changes) {
 		camel_object_trigger_event ((CamelObject *)vf, "folder_changed", changes);
@@ -1901,7 +1911,7 @@ vee_freeze (CamelFolder *folder)
 	struct _CamelVeeFolderPrivate *p = _PRIVATE (vfolder);
 	GList *node;
 
-	CAMEL_VEE_FOLDER_LOCK (vfolder, subfolder_lock);
+	camel_vee_folder_lock (vfolder, CVF_SUBFOLDER_LOCK);
 
 	node = p->folders;
 	while (node) {
@@ -1911,7 +1921,7 @@ vee_freeze (CamelFolder *folder)
 		node = node->next;
 	}
 
-	CAMEL_VEE_FOLDER_UNLOCK (vfolder, subfolder_lock);
+	camel_vee_folder_unlock (vfolder, CVF_SUBFOLDER_LOCK);
 
 	/* call parent implementation */
 	CAMEL_FOLDER_CLASS (camel_vee_folder_parent)->freeze (folder);
@@ -1924,7 +1934,7 @@ vee_thaw (CamelFolder *folder)
 	struct _CamelVeeFolderPrivate *p = _PRIVATE (vfolder);
 	GList *node;
 
-	CAMEL_VEE_FOLDER_LOCK (vfolder, subfolder_lock);
+	camel_vee_folder_lock (vfolder, CVF_SUBFOLDER_LOCK);
 
 	node = p->folders;
 	while (node) {
@@ -1934,7 +1944,7 @@ vee_thaw (CamelFolder *folder)
 		node = node->next;
 	}
 
-	CAMEL_VEE_FOLDER_UNLOCK (vfolder, subfolder_lock);
+	camel_vee_folder_unlock (vfolder, CVF_SUBFOLDER_LOCK);
 
 	/* call parent implementation */
 	CAMEL_FOLDER_CLASS (camel_vee_folder_parent)->thaw (folder);
@@ -1979,12 +1989,12 @@ vee_set_expression (CamelVeeFolder *vf, const gchar *query)
 	GList *node;
 	CamelException ex = CAMEL_EXCEPTION_INITIALISER;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 
 	/* no change, do nothing */
 	if ((vf->expression && query && strcmp (vf->expression, query) == 0)
 	    || (vf->expression == NULL && query == NULL)) {
-		CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+		camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 		return;
 	}
 
@@ -2019,12 +2029,12 @@ vee_set_expression (CamelVeeFolder *vf, const gchar *query)
 
 	camel_exception_clear (&ex);
 
-	CAMEL_VEE_FOLDER_LOCK (vf, changed_lock);
+	camel_vee_folder_lock (vf, CVF_CHANGED_LOCK);
 	g_list_free (p->folders_changed);
 	p->folders_changed = NULL;
-	CAMEL_VEE_FOLDER_UNLOCK (vf, changed_lock);
+	camel_vee_folder_unlock (vf, CVF_CHANGED_LOCK);
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 }
 
 /* This entire code will be useless, since we sync the counts always. */
@@ -2221,14 +2231,14 @@ vee_folder_stop_folder (CamelVeeFolder *vf, CamelFolder *sub)
 	gint i;
 	CamelVeeFolder *folder_unmatched = vf->parent_vee_store ? vf->parent_vee_store->folder_unmatched : NULL;
 
-	CAMEL_VEE_FOLDER_LOCK (vf, subfolder_lock);
+	camel_vee_folder_lock (vf, CVF_SUBFOLDER_LOCK);
 
-	CAMEL_VEE_FOLDER_LOCK (vf, changed_lock);
+	camel_vee_folder_lock (vf, CVF_CHANGED_LOCK);
 	p->folders_changed = g_list_remove (p->folders_changed, sub);
-	CAMEL_VEE_FOLDER_UNLOCK (vf, changed_lock);
+	camel_vee_folder_unlock (vf, CVF_CHANGED_LOCK);
 
 	if (g_list_find (p->folders, sub) == NULL) {
-		CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+		camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 		return;
 	}
 
@@ -2239,17 +2249,17 @@ vee_folder_stop_folder (CamelVeeFolder *vf, CamelFolder *sub)
 	p->folders = g_list_remove (p->folders, sub);
 
 	/* undo the freeze state that we have imposed on this source folder */
-	CAMEL_FOLDER_LOCK (vf, change_lock);
-	for (i = 0; i < ((CamelFolder *)vf)->priv->frozen; i++)
+	camel_folder_lock (CAMEL_FOLDER (vf), CF_CHANGE_LOCK);
+	for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)vf); i++)
 		camel_folder_thaw (sub);
-	CAMEL_FOLDER_UNLOCK (vf, change_lock);
+	camel_folder_unlock (CAMEL_FOLDER (vf), CF_CHANGE_LOCK);
 
-	CAMEL_VEE_FOLDER_UNLOCK (vf, subfolder_lock);
+	camel_vee_folder_unlock (vf, CVF_SUBFOLDER_LOCK);
 
 	if (folder_unmatched != NULL) {
 		struct _CamelVeeFolderPrivate *up = _PRIVATE (folder_unmatched);
 
-		CAMEL_VEE_FOLDER_LOCK (folder_unmatched, subfolder_lock);
+		camel_vee_folder_lock (folder_unmatched, CVF_SUBFOLDER_LOCK);
 		/* if folder deleted, then blow it away from unmatched always, and remove all refs to it */
 		if (sub->folder_flags & CAMEL_FOLDER_HAS_BEEN_DELETED) {
 			while (g_list_find (up->folders, sub)) {
@@ -2257,10 +2267,10 @@ vee_folder_stop_folder (CamelVeeFolder *vf, CamelFolder *sub)
 				camel_object_unref (sub);
 
 				/* undo the freeze state that Unmatched has imposed on this source folder */
-				CAMEL_FOLDER_LOCK (folder_unmatched, change_lock);
-				for (i = 0; i < ((CamelFolder *)folder_unmatched)->priv->frozen; i++)
+				camel_folder_lock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
+				for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)folder_unmatched); i++)
 					camel_folder_thaw (sub);
-				CAMEL_FOLDER_UNLOCK (folder_unmatched, change_lock);
+				camel_folder_unlock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
 			}
 		} else if ((vf->flags & CAMEL_STORE_FOLDER_PRIVATE) == 0) {
 			if (g_list_find (up->folders, sub) != NULL) {
@@ -2268,13 +2278,13 @@ vee_folder_stop_folder (CamelVeeFolder *vf, CamelFolder *sub)
 				camel_object_unref (sub);
 
 				/* undo the freeze state that Unmatched has imposed on this source folder */
-				CAMEL_FOLDER_LOCK (folder_unmatched, change_lock);
-				for (i = 0; i < ((CamelFolder *)folder_unmatched)->priv->frozen; i++)
+				camel_folder_lock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
+				for (i = 0; i < camel_folder_get_frozen_count ((CamelFolder *)folder_unmatched); i++)
 					camel_folder_thaw (sub);
-				CAMEL_FOLDER_UNLOCK (folder_unmatched, change_lock);
+				camel_folder_unlock (CAMEL_FOLDER (folder_unmatched), CF_CHANGE_LOCK);
 			}
 		}
-		CAMEL_VEE_FOLDER_UNLOCK (folder_unmatched, subfolder_lock);
+		camel_vee_folder_unlock (folder_unmatched, CVF_SUBFOLDER_LOCK);
 	}
 
 	if (CAMEL_IS_VEE_FOLDER (sub))
@@ -2352,3 +2362,85 @@ camel_vee_folder_finalize (CamelObject *obj)
 	g_hash_table_destroy (vf->hashes);
 	g_free (p);
 }
+
+/* FIXME: This shouldn't be needed */
+gint
+camel_vee_folder_get_unread_vfolder (CamelVeeFolder *folder)
+{
+	g_return_val_if_fail (folder != NULL, 0);
+	g_return_val_if_fail (CAMEL_IS_VEE_FOLDER (folder), 0);
+
+	return folder->priv->unread_vfolder;
+}
+
+/* FIXME: This shouldn't be needed */
+void
+camel_vee_folder_set_unread_vfolder (CamelVeeFolder *folder, gint unread_vfolder)
+{
+	g_return_if_fail (folder != NULL);
+	g_return_if_fail (CAMEL_IS_VEE_FOLDER (folder));
+
+	folder->priv->unread_vfolder = unread_vfolder;
+}
+
+/**
+ * camel_vee_folder_lock:
+ * @folder: a #CamelVeeFolder
+ * @lock: lock type to lock
+ *
+ * Locks #folder's #lock. Unlock it with camel_vee_folder_unlock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_vee_folder_lock (CamelVeeFolder *folder, CamelVeeFolderLock lock)
+{
+	g_return_if_fail (folder != NULL);
+	g_return_if_fail (CAMEL_IS_VEE_FOLDER (folder));
+	g_return_if_fail (folder->priv != NULL);
+
+	switch (lock) {
+	case CVF_SUMMARY_LOCK:
+		g_mutex_lock (folder->priv->summary_lock);
+		break;
+	case CVF_SUBFOLDER_LOCK:
+		g_mutex_lock (folder->priv->subfolder_lock);
+		break;
+	case CVF_CHANGED_LOCK:
+		g_mutex_lock (folder->priv->changed_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
+
+/**
+ * camel_vee_folder_unlock:
+ * @folder: a #CamelVeeFolder
+ * @lock: lock type to unlock
+ *
+ * Unlocks #folder's #lock, previously locked with camel_vee_folder_lock().
+ *
+ * Since: 2.31.1
+ **/
+void
+camel_vee_folder_unlock (CamelVeeFolder *folder, CamelVeeFolderLock lock)
+{
+	g_return_if_fail (folder != NULL);
+	g_return_if_fail (CAMEL_IS_VEE_FOLDER (folder));
+	g_return_if_fail (folder->priv != NULL);
+
+	switch (lock) {
+	case CVF_SUMMARY_LOCK:
+		g_mutex_unlock (folder->priv->summary_lock);
+		break;
+	case CVF_SUBFOLDER_LOCK:
+		g_mutex_unlock (folder->priv->subfolder_lock);
+		break;
+	case CVF_CHANGED_LOCK:
+		g_mutex_unlock (folder->priv->changed_lock);
+		break;
+	default:
+		g_return_if_reached ();
+	}
+}
diff --git a/camel/camel-vee-folder.h b/camel/camel-vee-folder.h
index f1879af..db3c90b 100644
--- a/camel/camel-vee-folder.h
+++ b/camel/camel-vee-folder.h
@@ -41,6 +41,12 @@ typedef struct _CamelVeeFolder CamelVeeFolder;
 typedef struct _CamelVeeFolderClass CamelVeeFolderClass;
 typedef struct _CamelVeeFolderPrivate CamelVeeFolderPrivate;
 
+typedef enum _CamelVeeFolderLock {
+	CVF_SUMMARY_LOCK,
+	CVF_SUBFOLDER_LOCK,
+	CVF_CHANGED_LOCK
+} CamelVeeFolderLock;
+
 struct _CamelVeeFolder {
 	CamelFolder parent;
 	CamelVeeFolderPrivate *priv;
@@ -97,6 +103,12 @@ void	     camel_vee_folder_unmask_event_folder_changed (CamelVeeFolder *vf, Came
 void	     camel_vee_folder_hash_folder	(CamelFolder *folder, gchar buffer[8]);
 void	     camel_vee_folder_sync_headers (CamelFolder *vf, CamelException *ex);
 
+gint camel_vee_folder_get_unread_vfolder (CamelVeeFolder *folder);
+void camel_vee_folder_set_unread_vfolder (CamelVeeFolder *folder, gint unread_vfolder);
+
+void         camel_vee_folder_lock		(CamelVeeFolder *folder, CamelVeeFolderLock lock);
+void         camel_vee_folder_unlock		(CamelVeeFolder *folder, CamelVeeFolderLock lock);
+
 G_END_DECLS
 
 #endif /* CAMEL_VEE_FOLDER_H */
diff --git a/camel/camel-vee-store.c b/camel/camel-vee-store.c
index 93fe301..a5b2884 100644
--- a/camel/camel-vee-store.c
+++ b/camel/camel-vee-store.c
@@ -29,7 +29,6 @@
 
 #include "camel-db.h"
 #include "camel-exception.h"
-#include "camel-private.h"
 #include "camel-session.h"
 #include "camel-vee-folder.h"
 #include "camel-vee-store.h"
diff --git a/camel/camel-vee-summary.c b/camel/camel-vee-summary.c
index a447162..cf73c9c 100644
--- a/camel/camel-vee-summary.c
+++ b/camel/camel-vee-summary.c
@@ -36,7 +36,6 @@
 #include "camel-vee-summary.h"
 #include "camel-vee-folder.h"
 #include "camel-vee-store.h"
-#include "camel-private.h"
 #include "camel-string-utils.h"
 
 #define d(x)
@@ -150,10 +149,10 @@ vee_info_set_user_flag(CamelMessageInfo *mi, const gchar *name, gboolean value)
 	if (camel_debug("vfolderexp"))
 		printf("Expression for vfolder '%s' is '%s'\n", mi->summary->folder->full_name, g_strescape(vf->expression, ""));
 
-	if (vf->priv->unread_vfolder == -1)
+	if (camel_vee_folder_get_unread_vfolder (vf) == -1)
 		camel_vee_summary_load_check_unread_vfolder (CAMEL_VEE_SUMMARY (mi->summary));
 
-	if (vf->priv->unread_vfolder == 1)
+	if (camel_vee_folder_get_unread_vfolder (vf) == 1)
 		hacked_unread_folder = TRUE;
 
 	if (mi->uid) {
@@ -221,9 +220,9 @@ camel_vee_summary_load_check_unread_vfolder (CamelVeeSummary *vs)
 		hacked_unread_folder = TRUE;
 
 	if (hacked_unread_folder)
-		vf->priv->unread_vfolder = 1;
+		camel_vee_folder_set_unread_vfolder (vf, 1);
 	else
-		vf->priv->unread_vfolder = 0;
+		camel_vee_folder_set_unread_vfolder (vf, 0);
 }
 
 static gboolean
@@ -236,10 +235,10 @@ vee_info_set_flags(CamelMessageInfo *mi, guint32 flags, guint32 set)
 	if (camel_debug("vfolderexp"))
 		printf("Expression for vfolder '%s' is '%s'\n", mi->summary->folder->full_name, g_strescape(vf->expression, ""));
 
-	if (vf->priv->unread_vfolder == -1)
+	if (camel_vee_folder_get_unread_vfolder (vf) == -1)
 		camel_vee_summary_load_check_unread_vfolder (CAMEL_VEE_SUMMARY (mi->summary));
 
-	if (vf->priv->unread_vfolder == 1)
+	if (camel_vee_folder_get_unread_vfolder (vf) == 1)
 		hacked_unread_folder = TRUE;
 
 	if (mi->uid) {
@@ -327,14 +326,14 @@ message_info_from_uid (CamelFolderSummary *s, const gchar *uid)
 
 	/* FIXME[disk-summary] too bad design. Need to peek it from cfs
 	 * instead of hacking ugly like this */
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
 
 	info = g_hash_table_lookup (s->loaded_infos, uid);
 
 	if (info)
 		camel_message_info_ref (info);
 
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	if (!info) {
 		CamelVeeMessageInfo *vinfo;
@@ -471,9 +470,9 @@ camel_vee_summary_add(CamelVeeSummary *s, CamelFolderSummary *summary, const gch
 	memcpy(vuid, hash, 8);
 	strcpy(vuid+8, uid);
 
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
+	camel_folder_summary_lock (CAMEL_FOLDER_SUMMARY (s), CFS_SUMMARY_LOCK);
 	mi = (CamelVeeMessageInfo *) g_hash_table_lookup(((CamelFolderSummary *) s)->loaded_infos, vuid);
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_unlock (CAMEL_FOLDER_SUMMARY (s), CFS_SUMMARY_LOCK);
 
 	if (mi) {
 		/* Possible that the entry is loaded, see if it has the summary */
diff --git a/camel/camel-vtrash-folder.c b/camel/camel-vtrash-folder.c
index a368547..7dbdece 100644
--- a/camel/camel-vtrash-folder.c
+++ b/camel/camel-vtrash-folder.c
@@ -32,7 +32,6 @@
 #include "camel-db.h"
 #include "camel-exception.h"
 #include "camel-mime-message.h"
-#include "camel-private.h"
 #include "camel-store.h"
 #include "camel-vee-store.h"
 #include "camel-vtrash-folder.h"
diff --git a/camel/camel-win32.c b/camel/camel-win32.c
index 64874c9..d284096 100644
--- a/camel/camel-win32.c
+++ b/camel/camel-win32.c
@@ -62,7 +62,7 @@ setup (void)
 	G_UNLOCK (mutex);
 }
 
-#include "camel-private.h"	/* For prototypes */
+#include "camel-win32.h"	/* For prototypes */
 
 #define GETTER(varbl)				\
 const gchar *					\
diff --git a/camel/camel-win32.h b/camel/camel-win32.h
new file mode 100644
index 0000000..fac1fdd
--- /dev/null
+++ b/camel/camel-win32.h
@@ -0,0 +1,58 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *  camel-win32.h: Private info for win32.
+ *
+ * Authors: Michael Zucchi <notzed ximian com>
+ *
+ * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU Lesser General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
+ * USA
+ */
+
+#ifndef CAMEL_WIN32_H
+#define CAMEL_WIN32_H
+
+/* need a way to configure and save this data, if this header is to
+   be installed.  For now, dont install it */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <glib.h>
+
+#ifdef G_OS_WIN32
+
+G_BEGIN_DECLS
+
+#define fsync(fd) _commit(fd)
+
+const gchar *_camel_get_localedir (void) G_GNUC_CONST;
+const gchar *_camel_get_libexecdir (void) G_GNUC_CONST;
+const gchar *_camel_get_providerdir (void) G_GNUC_CONST;
+
+#undef EVOLUTION_LOCALEDIR
+#define EVOLUTION_LOCALEDIR _camel_get_localedir ()
+
+#undef CAMEL_LIBEXECDIR
+#define CAMEL_LIBEXECDIR _camel_get_libexecdir ()
+
+#undef CAMEL_PROVIDERDIR
+#define CAMEL_PROVIDERDIR _camel_get_providerdir ()
+
+G_END_DECLS
+
+#endif /* G_OS_WIN32 */
+
+#endif /* CAMEL_WIN32_H */
diff --git a/camel/camel.c b/camel/camel.c
index a2a8fb1..9bf0796 100644
--- a/camel/camel.c
+++ b/camel/camel.c
@@ -40,7 +40,7 @@
 #include "camel-certdb.h"
 #include "camel-debug.h"
 #include "camel-provider.h"
-#include "camel-private.h"
+#include "camel-win32.h"
 
 #ifdef HAVE_NSS
 /* To protect NSS initialization and shutdown. This prevents
diff --git a/camel/providers/groupwise/camel-groupwise-folder.c b/camel/providers/groupwise/camel-groupwise-folder.c
index b3144ef..0bae726 100644
--- a/camel/providers/groupwise/camel-groupwise-folder.c
+++ b/camel/providers/groupwise/camel-groupwise-folder.c
@@ -48,8 +48,6 @@ which needs to be better organized via functions */
 #include <e-gw-connection.h>
 #include <e-gw-item.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-groupwise-folder.h"
 #include "camel-groupwise-journal.h"
 #include "camel-groupwise-private.h"
@@ -754,13 +752,13 @@ groupwise_sync (CamelFolder *folder, gboolean expunge, CamelMessageInfo *update_
 		return;
 	}
 
-	CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	if (!camel_groupwise_store_connected (gw_store, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		camel_exception_clear (ex);
 		return;
 	}
-	CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 
 	cnc = cnc_lookup (priv);
 	container_id =  camel_groupwise_store_container_id_lookup (gw_store, folder->full_name);
@@ -838,7 +836,7 @@ groupwise_sync (CamelFolder *folder, gboolean expunge, CamelMessageInfo *update_
 					}
 
 					if (g_list_length (deleted_items) == GROUPWISE_BULK_DELETE_LIMIT ) {
-						CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+						camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 
 						/*
 							Sync up the READ changes before deleting the message.
@@ -863,7 +861,7 @@ groupwise_sync (CamelFolder *folder, gboolean expunge, CamelMessageInfo *update_
 						status = e_gw_connection_remove_items (cnc, container_id, deleted_items);
 						if (status == E_GW_CONNECTION_STATUS_INVALID_CONNECTION)
 							status = e_gw_connection_remove_items (cnc, container_id, deleted_items);
-						CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+						camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 						if (status == E_GW_CONNECTION_STATUS_OK) {
 							gchar *uid;
 							while (deleted_items) {
@@ -897,7 +895,7 @@ groupwise_sync (CamelFolder *folder, gboolean expunge, CamelMessageInfo *update_
 		read_items = g_list_concat (read_items, deleted_read_items);
 
 	if (read_items) {
-		CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		status = e_gw_connection_mark_read (cnc, read_items);
 		if (status == E_GW_CONNECTION_STATUS_INVALID_CONNECTION)
 			status = e_gw_connection_mark_read (cnc, read_items);
@@ -906,11 +904,11 @@ groupwise_sync (CamelFolder *folder, gboolean expunge, CamelMessageInfo *update_
 			sync_flags (folder, read_items);
 
 		g_list_free (read_items);
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	}
 
 	if (deleted_items) {
-		CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		if (!strcmp (folder->full_name, "Trash")) {
 			status = e_gw_connection_purge_selected_items (cnc, deleted_items);
 			if (status == E_GW_CONNECTION_STATUS_INVALID_CONNECTION)
@@ -933,11 +931,11 @@ groupwise_sync (CamelFolder *folder, gboolean expunge, CamelMessageInfo *update_
 			}
 		}
 		g_list_free (deleted_items);
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	}
 
 	if (unread_items) {
-		CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		status = e_gw_connection_mark_unread (cnc, unread_items);
 		if (status == E_GW_CONNECTION_STATUS_INVALID_CONNECTION)
 			status = e_gw_connection_mark_unread (cnc, unread_items);
@@ -946,21 +944,21 @@ groupwise_sync (CamelFolder *folder, gboolean expunge, CamelMessageInfo *update_
 			sync_flags (folder, unread_items);
 
 		g_list_free (unread_items);
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	}
 
 	if (expunge) {
-		CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		status = e_gw_connection_purge_deleted_items (cnc);
 		if (status == E_GW_CONNECTION_STATUS_OK) {
 			g_message ("Purged deleted items in %s", folder->name);
 		}
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	}
 
-	CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	groupwise_sync_summary (folder, ex);
-	CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	
 	camel_object_trigger_event (CAMEL_OBJECT (folder), "folder_changed", changes);
 	camel_folder_change_info_free (changes);
@@ -1057,7 +1055,7 @@ update_update (CamelSession *session, CamelSessionThreadMsg *msg)
 	   In case if user went offline, don't do anything.
 	   m->cnc would have become invalid, as the store disconnect unrefs it.
 	 */
-	CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	if (((CamelOfflineStore *) gw_store)->state == CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL ||
 			((CamelService *)gw_store)->status == CAMEL_SERVICE_DISCONNECTED) {
 		goto end1;
@@ -1079,7 +1077,7 @@ update_update (CamelSession *session, CamelSessionThreadMsg *msg)
 	while (!done) {
 
 		if (camel_application_is_exiting) {
-				CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+				camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 				return;
 		}
 
@@ -1111,7 +1109,7 @@ update_update (CamelSession *session, CamelSessionThreadMsg *msg)
 	}
 	e_gw_connection_destroy_cursor (m->cnc, m->container_id, cursor);
 
-	CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	/* Take out only the first part in the list until the @ since it is guaranteed
 	   to be unique only until that symbol */
 
@@ -1137,7 +1135,7 @@ update_update (CamelSession *session, CamelSessionThreadMsg *msg)
 
 	return;
  end1:
-	CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	camel_operation_end (NULL);
 	if (items_full_list) {
 		g_list_foreach (items_full_list, (GFunc)g_free, NULL);
@@ -1326,7 +1324,7 @@ groupwise_refresh_folder(CamelFolder *folder, CamelException *ex)
 		gw_folder->need_refresh = TRUE;
 	}
 
-	CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_groupwise_store_connected (gw_store, ex))
 		goto end1;
@@ -1435,7 +1433,7 @@ groupwise_refresh_folder(CamelFolder *folder, CamelException *ex)
 		update_summary_string (folder, new_sync_time, ex);
 	}
 
-	CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	is_locked = FALSE;
 
 	/*
@@ -1466,7 +1464,7 @@ end2:
 	g_free (container_id);
 end1:
 	if (is_locked)
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	return;
 }
 
@@ -2343,9 +2341,9 @@ gw_update_all_items (CamelFolder *folder, GList *item_list, CamelException *ex)
 	if (item_list) {
 		CamelGroupwiseStore *gw_store = CAMEL_GROUPWISE_STORE (folder->parent_store);
 
-		CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		gw_update_cache (folder, item_list, ex, TRUE);
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 
 		g_list_foreach (item_list, (GFunc)g_free, NULL);
 		g_list_free (item_list);
@@ -2386,7 +2384,7 @@ groupwise_append_message (CamelFolder *folder, CamelMimeMessage *message,
 	}
 	cnc = cnc_lookup (priv);
 
-	CAMEL_SERVICE_REC_LOCK (folder->parent_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 	/*Get the container id*/
 	container_id = camel_groupwise_store_container_id_lookup (gw_store, folder->full_name);
 
@@ -2411,7 +2409,7 @@ groupwise_append_message (CamelFolder *folder, CamelMimeMessage *message,
 
 		if (appended_uid)
 			*appended_uid = NULL;
-		CAMEL_SERVICE_REC_UNLOCK (folder->parent_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -2424,14 +2422,14 @@ groupwise_append_message (CamelFolder *folder, CamelMimeMessage *message,
 		if (appended_uid)
 			*appended_uid = NULL;
 
-		CAMEL_SERVICE_REC_UNLOCK (folder->parent_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
 	if (appended_uid)
 		*appended_uid = g_strdup (id);
 	g_free (id);
-	CAMEL_SERVICE_REC_UNLOCK (folder->parent_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 }
 
 /* A function to compare uids, inspired by strcmp .
@@ -2492,7 +2490,7 @@ groupwise_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
 		source_container_id = NULL;
 	dest_container_id = camel_groupwise_store_container_id_lookup (gw_store, destination->full_name);
 
-	CAMEL_SERVICE_REC_LOCK (source->parent_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (source->parent_store), CS_REC_CONNECT_LOCK);
 	/* check for offline operation */
 	if (offline->state == CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL) {
 		CamelGroupwiseJournal *journal = (CamelGroupwiseJournal *) ((CamelGroupwiseFolder *) destination)->journal;
@@ -2530,7 +2528,7 @@ groupwise_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
 			}
 		}
 
-		CAMEL_SERVICE_REC_UNLOCK (source->parent_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (source->parent_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -2572,9 +2570,9 @@ groupwise_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
 					*/
 
 					wrapper = g_list_prepend (wrapper, (gchar *)uid);
-					CAMEL_SERVICE_REC_LOCK (source->parent_store, connect_lock);
+					camel_service_lock (CAMEL_SERVICE (source->parent_store), CS_REC_CONNECT_LOCK);
 					e_gw_connection_mark_read (cnc, wrapper);
-					CAMEL_SERVICE_REC_UNLOCK (source->parent_store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (source->parent_store), CS_REC_CONNECT_LOCK);
 					g_list_free (wrapper);
 					wrapper = NULL;
 				}
@@ -2588,9 +2586,9 @@ groupwise_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
 
 				if (unset_flags.bits & CAMEL_MESSAGE_SEEN) {
 					wrapper = g_list_prepend (wrapper, (gchar *)uid);
-					CAMEL_SERVICE_REC_LOCK (source->parent_store, connect_lock);
+					camel_service_lock (CAMEL_SERVICE (source->parent_store), CS_REC_CONNECT_LOCK);
 					e_gw_connection_mark_unread (cnc, wrapper);
-					CAMEL_SERVICE_REC_UNLOCK (source->parent_store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (source->parent_store), CS_REC_CONNECT_LOCK);
 					g_list_free (wrapper);
 					wrapper = NULL;
 				}
@@ -2651,7 +2649,7 @@ groupwise_transfer_messages_to (CamelFolder *source, GPtrArray *uids,
 
 	groupwise_store_set_current_folder (gw_store, source);
 
-	CAMEL_SERVICE_REC_UNLOCK (source->parent_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (source->parent_store), CS_REC_CONNECT_LOCK);
 }
 
 static void
@@ -2676,7 +2674,7 @@ groupwise_expunge (CamelFolder *folder, CamelException *ex)
 		return;
 
 	if (!strcmp (folder->full_name, "Trash")) {
-		CAMEL_SERVICE_REC_LOCK (groupwise_store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 		status = e_gw_connection_purge_deleted_items (cnc);
 		if (status == E_GW_CONNECTION_STATUS_OK) {
 			camel_folder_freeze (folder);
@@ -2684,7 +2682,7 @@ groupwise_expunge (CamelFolder *folder, CamelException *ex)
 			camel_folder_thaw (folder);
 		} else
 			g_warning ("Could not Empty Trash\n");
-		CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -2707,9 +2705,9 @@ groupwise_expunge (CamelFolder *folder, CamelException *ex)
 			}
 			if (g_list_length (deleted_items) == GROUPWISE_BULK_DELETE_LIMIT ) {
 				/* Read the FIXME below */
-				CAMEL_SERVICE_REC_LOCK (groupwise_store, connect_lock);
+				camel_service_lock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 				status = e_gw_connection_remove_items (cnc, container_id, deleted_items);
-				CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+				camel_service_unlock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 				if (status == E_GW_CONNECTION_STATUS_OK) {
 					gchar *uid;
 					while (deleted_items) {
@@ -2732,9 +2730,9 @@ groupwise_expunge (CamelFolder *folder, CamelException *ex)
 
 	if (deleted_items) {
 		/* FIXME: Put these in a function and reuse it inside the above loop, here and in groupwise_sync*/
-		CAMEL_SERVICE_REC_LOCK (groupwise_store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 		status = e_gw_connection_remove_items (cnc, container_id, deleted_items);
-		CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 		if (status == E_GW_CONNECTION_STATUS_OK) {
 			gchar *uid;
 			while (deleted_items) {
diff --git a/camel/providers/groupwise/camel-groupwise-store-summary.c b/camel/providers/groupwise/camel-groupwise-store-summary.c
index 1a2f5b6..acdabd7 100644
--- a/camel/providers/groupwise/camel-groupwise-store-summary.c
+++ b/camel/providers/groupwise/camel-groupwise-store-summary.c
@@ -31,8 +31,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-groupwise-store-summary.h"
 
 #define CAMEL_GW_STORE_SUMMARY_VERSION (0)
@@ -558,10 +556,10 @@ store_info_set_string(CamelStoreSummary *s, CamelStoreInfo *mi, gint type, const
 	switch (type) {
 		case CAMEL_STORE_INFO_LAST:
 			d(printf("Set full name %s -> %s\n", isi->full_name, str));
-			CAMEL_STORE_SUMMARY_LOCK(s, summary_lock);
+			camel_store_summary_lock (s, CSS_SUMMARY_LOCK);
 			g_free(isi->full_name);
 			isi->full_name = g_strdup(str);
-			CAMEL_STORE_SUMMARY_UNLOCK(s, summary_lock);
+			camel_store_summary_unlock (s, CSS_SUMMARY_LOCK);
 			break;
 		default:
 			camel_groupwise_store_summary_parent->store_info_set_string(s, mi, type, str);
diff --git a/camel/providers/groupwise/camel-groupwise-store.c b/camel/providers/groupwise/camel-groupwise-store.c
index c779856..2df4015 100644
--- a/camel/providers/groupwise/camel-groupwise-store.c
+++ b/camel/providers/groupwise/camel-groupwise-store.c
@@ -38,8 +38,6 @@
 #include <glib/gi18n-lib.h>
 #include <glib/gstdio.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-groupwise-folder.h"
 #include "camel-groupwise-store-summary.h"
 #include "camel-groupwise-store.h"
@@ -253,7 +251,7 @@ void
 groupwise_store_set_current_folder (CamelGroupwiseStore *groupwise_store, CamelFolder *folder)
 {
 
-	CAMEL_SERVICE_REC_LOCK (groupwise_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 
 	if (groupwise_store->current_folder) {
 		camel_object_unref (groupwise_store->current_folder);
@@ -263,7 +261,7 @@ groupwise_store_set_current_folder (CamelGroupwiseStore *groupwise_store, CamelF
 	if (folder)
 		groupwise_store->current_folder = camel_object_ref (folder);
 
-	CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 }
 
 static gboolean
@@ -288,15 +286,15 @@ groupwise_connect (CamelService *service, CamelException *ex)
 		camel_service_construct (service, service->session, service->provider, service->url, ex);
 	}
 
-	CAMEL_SERVICE_REC_LOCK (service, connect_lock);
+	camel_service_lock (service, CS_REC_CONNECT_LOCK);
 
 	if (priv->cnc) {
-		CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
+		camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 		return TRUE;
 	}
 
 	if (!check_for_connection (service, ex) || !groupwise_auth_loop (service, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
+		camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 		camel_service_disconnect (service, TRUE, NULL);
 		return FALSE;
 	}
@@ -322,7 +320,7 @@ groupwise_connect (CamelService *service, CamelException *ex)
 
 	camel_store_summary_save ((CamelStoreSummary *) store->summary);
 
-	CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
+	camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 	if (E_IS_GW_CONNECTION (priv->cnc)) {
 		return TRUE;
 	}
@@ -392,7 +390,7 @@ groupwise_disconnect (CamelService *service, gboolean clean, CamelException *ex)
 	CamelGroupwiseStore *groupwise_store = CAMEL_GROUPWISE_STORE (service);
 
 	if (clean) {
-		CAMEL_SERVICE_REC_LOCK (groupwise_store, connect_lock);
+		camel_service_lock (service, CS_REC_CONNECT_LOCK);
 		if (groupwise_store->priv && groupwise_store->priv->cnc) {
 			g_object_unref (groupwise_store->priv->cnc);
 			groupwise_store->priv->cnc = NULL;
@@ -400,7 +398,7 @@ groupwise_disconnect (CamelService *service, gboolean clean, CamelException *ex)
 
 		groupwise_store_set_current_folder (groupwise_store, NULL);
 
-		CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+		camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 	}
 
 	/* groupwise_disconnect_cleanup (service, clean, ex); */
@@ -562,18 +560,18 @@ groupwise_get_folder (CamelStore *store, const gchar *folder_name, guint32 flags
 
 	camel_exception_clear (ex);
 
-	CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 
 	groupwise_store_set_current_folder (gw_store, NULL);
 
 	if (!camel_groupwise_store_connected (gw_store, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		return NULL;
 	}
 
 	if (!E_IS_GW_CONNECTION( priv->cnc)) {
 		if (!groupwise_connect (CAMEL_SERVICE(store), ex)) {
-			CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 			return NULL;
 		}
 	}
@@ -585,7 +583,7 @@ groupwise_get_folder (CamelStore *store, const gchar *folder_name, guint32 flags
 	g_free(storage_path);
 	folder = camel_gw_folder_new (store, folder_name, folder_dir, ex);
 	if (!folder) {
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_INVALID, _("Authentication failed"));
 		g_free (folder_dir);
 		g_free (container_id);
@@ -616,7 +614,7 @@ groupwise_get_folder (CamelStore *store, const gchar *folder_name, guint32 flags
 				&cursor);
 
 		if (status != E_GW_CONNECTION_STATUS_OK) {
-			CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 			g_free (container_id);
 			return NULL;
 		}
@@ -632,7 +630,7 @@ groupwise_get_folder (CamelStore *store, const gchar *folder_name, guint32 flags
 						all_ok = FALSE;
 						break;
 						/*
-						   CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+						   camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 						   e_gw_connection_destroy_cursor (priv->cnc, container_id, cursor);
 						//camel_folder_summary_clear (folder->summary);
 						camel_folder_summary_save_to_db (folder->summary, ex);
@@ -685,7 +683,7 @@ groupwise_get_folder (CamelStore *store, const gchar *folder_name, guint32 flags
 	groupwise_store_set_current_folder (gw_store, folder);
 
 	g_free (container_id);
-	CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 
 	return folder;
 }
@@ -706,16 +704,16 @@ gw_store_reload_folder (CamelGroupwiseStore *gw_store, CamelFolder *folder, guin
 
 	camel_exception_clear (ex);
 
-	CAMEL_SERVICE_REC_LOCK (gw_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_groupwise_store_connected (gw_store, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
 	if (!E_IS_GW_CONNECTION( priv->cnc)) {
 		if (!groupwise_connect (CAMEL_SERVICE((CamelStore*)gw_store), ex)) {
-			CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 			return;
 		}
 	}
@@ -746,7 +744,7 @@ gw_store_reload_folder (CamelGroupwiseStore *gw_store, CamelFolder *folder, guin
 									NULL,
 									&cursor);
 			if (status != E_GW_CONNECTION_STATUS_OK) {
-					CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 					g_free (container_id);
 					return;
 			}
@@ -758,7 +756,7 @@ gw_store_reload_folder (CamelGroupwiseStore *gw_store, CamelFolder *folder, guin
 									cursor, FALSE,
 									CURSOR_ITEM_LIMIT, position, &list);
 					if (status != E_GW_CONNECTION_STATUS_OK) {
-							CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+							camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 							e_gw_connection_destroy_cursor (priv->cnc, container_id, cursor);
 							camel_folder_summary_save_to_db (folder->summary, ex);
 							camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_INVALID, _("Authentication failed"));
@@ -809,7 +807,7 @@ gw_store_reload_folder (CamelGroupwiseStore *gw_store, CamelFolder *folder, guin
 	groupwise_store_set_current_folder (gw_store, NULL);
 
 	g_free (container_id);
-	CAMEL_SERVICE_REC_UNLOCK (gw_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (gw_store), CS_REC_CONNECT_LOCK);
 	return;
 }
 
@@ -1086,7 +1084,7 @@ store_refresh_refresh (CamelSession *session, CamelSessionThreadMsg *msg)
 	struct _store_refresh_msg *m = (struct _store_refresh_msg *)msg;
 	CamelGroupwiseStore *groupwise_store = CAMEL_GROUPWISE_STORE(m->store);
 
-	CAMEL_SERVICE_REC_LOCK (m->store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (m->store), CS_REC_CONNECT_LOCK);
 	if (!camel_groupwise_store_connected (groupwise_store, &m->ex))
 		goto done;
 	/*Get the folder list and save it here*/
@@ -1095,7 +1093,7 @@ store_refresh_refresh (CamelSession *session, CamelSessionThreadMsg *msg)
 		goto done;
 	camel_store_summary_save ((CamelStoreSummary *)groupwise_store->summary);
 done:
-	CAMEL_SERVICE_REC_UNLOCK (m->store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (m->store), CS_REC_CONNECT_LOCK);
 }
 
 static void
@@ -1127,18 +1125,18 @@ groupwise_get_folder_info (CamelStore *store, const gchar *top, guint32 flags, C
 	    && camel_service_connect ((CamelService *)store, ex)))
 		goto offline;
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	groupwise_folders_sync (groupwise_store, ex);
 	if (camel_exception_is_set (ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return NULL;
 	}
 
 	camel_store_summary_touch ((CamelStoreSummary *)groupwise_store->summary);
 	camel_store_summary_save ((CamelStoreSummary *)groupwise_store->summary);
 
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 offline:
 	info = groupwise_get_folder_info_offline (store, top, flags, ex);
@@ -1160,7 +1158,7 @@ create_junk_folder (CamelStore *store)
 	parent_id = "";
 	/* TODO: check for offlining*/
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	status = e_gw_connection_modify_junk_settings (priv->cnc, JUNK_ENABLE, 0, 0,  JUNK_PERSISTENCE);
 	if (status == E_GW_CONNECTION_STATUS_INVALID_CONNECTION)
 		status = e_gw_connection_modify_junk_settings (priv->cnc, JUNK_ENABLE, 0, 0,  JUNK_PERSISTENCE);
@@ -1177,7 +1175,7 @@ create_junk_folder (CamelStore *store)
 		g_hash_table_insert (priv->parent_hash, g_strdup(child_container_id), g_strdup(parent_id));
 		camel_object_trigger_event (CAMEL_OBJECT (store), "folder_created", root);
 	}
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	return root;
 }
@@ -1222,7 +1220,7 @@ groupwise_create_folder(CamelStore *store,
 			return NULL;
 		}
 	}
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	status = e_gw_connection_create_folder(priv->cnc,parent_id,folder_name, &child_container_id);
 	if (status == E_GW_CONNECTION_STATUS_INVALID_CONNECTION)
 		status = e_gw_connection_create_folder(priv->cnc,parent_id,folder_name, &child_container_id);
@@ -1236,7 +1234,7 @@ groupwise_create_folder(CamelStore *store,
 
 		camel_object_trigger_event (CAMEL_OBJECT (store), "folder_created", root);
 	}
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	return root;
 }
 
@@ -1250,10 +1248,10 @@ groupwise_delete_folder(CamelStore *store,
 	EGwConnectionStatus status;
 	const gchar * container;
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_groupwise_store_connected (groupwise_store, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -1273,7 +1271,7 @@ groupwise_delete_folder(CamelStore *store,
 
 		g_hash_table_remove (priv->parent_hash, container);
 	}
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 static void
@@ -1294,10 +1292,10 @@ groupwise_rename_folder(CamelStore *store,
 		return;
 	}
 
-	CAMEL_SERVICE_REC_LOCK (groupwise_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_groupwise_store_connected (groupwise_store, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -1314,7 +1312,7 @@ groupwise_rename_folder(CamelStore *store,
 	{
 		camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM, _("Cannot rename GroupWise folder '%s' to '%s'"),
 				      old_name, new_name);
-		CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -1337,7 +1335,7 @@ groupwise_rename_folder(CamelStore *store,
 
 	g_free (oldpath);
 	g_free (newpath);
-	CAMEL_SERVICE_REC_UNLOCK (groupwise_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (groupwise_store), CS_REC_CONNECT_LOCK);
 }
 
 gchar *
diff --git a/camel/providers/imap/camel-imap-command.c b/camel/providers/imap/camel-imap-command.c
index bacf17f..a1e1fbf 100644
--- a/camel/providers/imap/camel-imap-command.c
+++ b/camel/providers/imap/camel-imap-command.c
@@ -35,14 +35,14 @@
 
 #include <glib/gi18n-lib.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-imap-command.h"
 #include "camel-imap-folder.h"
 #include "camel-imap-store-summary.h"
 #include "camel-imap-store.h"
 #include "camel-imap-utils.h"
 
+extern gint camel_verbose_debug;
+
 static gboolean imap_command_start (CamelImapStore *store, CamelFolder *folder,
 				    const gchar *cmd, CamelException *ex);
 static CamelImapResponse *imap_read_response (CamelImapStore *store,
@@ -88,7 +88,7 @@ camel_imap_command (CamelImapStore *store, CamelFolder *folder,
 	va_list ap;
 	gchar *cmd;
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (fmt) {
 		va_start (ap, fmt);
@@ -104,7 +104,7 @@ camel_imap_command (CamelImapStore *store, CamelFolder *folder,
 
 	if (!imap_command_start (store, folder, cmd, ex)) {
 		g_free (cmd);
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return NULL;
 	}
 	g_free (cmd);
@@ -158,12 +158,12 @@ camel_imap_command_start (CamelImapStore *store, CamelFolder *folder,
 	cmd = imap_command_strdup_vprintf (store, fmt, ap);
 	va_end (ap);
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	ok = imap_command_start (store, folder, cmd, ex);
 	g_free (cmd);
 
 	if (!ok)
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	return ok;
 }
 
@@ -276,7 +276,7 @@ camel_imap_command_continuation (CamelImapStore *store, const gchar *cmd,
 			camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
 					     g_strerror (errno));
 		camel_service_disconnect (CAMEL_SERVICE (store), FALSE, NULL);
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return NULL;
 	}
 
@@ -306,7 +306,7 @@ camel_imap_command_response (CamelImapStore *store, gchar **response,
 	gchar *respbuf;
 
 	if (camel_imap_store_readline (store, &respbuf, ex) < 0) {
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return CAMEL_IMAP_RESPONSE_ERROR;
 	}
 
@@ -363,7 +363,7 @@ camel_imap_command_response (CamelImapStore *store, gchar **response,
 
 	if (type == CAMEL_IMAP_RESPONSE_ERROR ||
 	    type == CAMEL_IMAP_RESPONSE_TAGGED)
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	return type;
 }
@@ -380,7 +380,7 @@ imap_read_response (CamelImapStore *store, CamelException *ex)
 	 * we're still locked. This lock is owned by response
 	 * and gets unlocked when response is freed.
 	 */
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	response = g_new0 (CamelImapResponse, 1);
 /*FIXME	if (store->current_folder && camel_disco_store_status (CAMEL_DISCO_STORE (store)) != CAMEL_DISCO_STORE_RESYNCING) {
@@ -637,7 +637,7 @@ camel_imap_response_free (CamelImapStore *store, CamelImapResponse *response)
 	}
 
 	g_free (response);
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 /**
diff --git a/camel/providers/imap/camel-imap-folder.c b/camel/providers/imap/camel-imap-folder.c
index e7a9209..29ab68d 100644
--- a/camel/providers/imap/camel-imap-folder.c
+++ b/camel/providers/imap/camel-imap-folder.c
@@ -38,8 +38,6 @@
 
 #include <libedataserver/e-time-utils.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-imap-command.h"
 #include "camel-imap-folder.h"
 #include "camel-imap-journal.h"
@@ -707,7 +705,7 @@ imap_refresh_info (CamelFolder *folder, CamelException *ex)
 	 * Also, if this is the INBOX, some servers (cryus) wont tell
 	 * us with a NOOP of new messages, so force a reselect which
 	 * should do it.  */
-	CAMEL_SERVICE_REC_LOCK (imap_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	if (camel_application_is_exiting  || !camel_imap_store_connected(imap_store, ex))
 		goto done;
@@ -777,7 +775,7 @@ imap_refresh_info (CamelFolder *folder, CamelException *ex)
 			imap_rescan (folder, camel_folder_summary_count (folder->summary), ex);
 	}
 done:
-	CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	camel_folder_summary_save_to_db (folder->summary, ex);
 	camel_store_summary_save ((CamelStoreSummary *)((CamelImapStore *)folder->parent_store)->summary);
@@ -941,7 +939,7 @@ imap_rescan (CamelFolder *folder, gint exists, CamelException *ex)
 
 	if (summary_got == 0 && summary_len == 0) {
 		camel_operation_end (NULL);
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		g_free(new);
 		return;
 	}
@@ -954,7 +952,7 @@ imap_rescan (CamelFolder *folder, gint exists, CamelException *ex)
 		g_free (new);
 
 		if (type != CAMEL_IMAP_RESPONSE_ERROR && type != CAMEL_IMAP_RESPONSE_TAGGED)
-			CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 		return;
 	}
@@ -1382,7 +1380,7 @@ imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
 	}
 
 	camel_exception_init (&local_ex);
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	/* write local changes first */
 	replay_offline_journal (store, imap_folder, ex);
@@ -1494,7 +1492,7 @@ imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
 		g_ptr_array_free (matches, TRUE);
 
 		/* We unlock here so that other threads can have a chance to grab the connect_lock */
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 		/* check for an exception */
 		if (camel_exception_is_set (&local_ex)) {
@@ -1503,7 +1501,7 @@ imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
 		}
 
 		/* Re-lock the connect_lock */
-		CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	}
 
 	if (expunge)
@@ -1515,7 +1513,7 @@ imap_sync (CamelFolder *folder, gboolean expunge, CamelException *ex)
 	/* Save the summary */
 	imap_sync_offline (folder, ex);
 
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 static gint
@@ -1576,12 +1574,12 @@ imap_expunge_uids_online (CamelFolder *folder, GPtrArray *uids, CamelException *
 	gint i;
 	GSList *list=NULL;
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if ((store->capabilities & IMAP_CAPABILITY_UIDPLUS) == 0) {
 		((CamelFolderClass *)CAMEL_OBJECT_GET_CLASS(folder))->sync(folder, 0, ex);
 		if (camel_exception_is_set(ex)) {
-			CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 			return;
 		}
 	}
@@ -1596,7 +1594,7 @@ imap_expunge_uids_online (CamelFolder *folder, GPtrArray *uids, CamelException *
 		if (response)
 			camel_imap_response_free (store, response);
 		if (camel_exception_is_set (ex)) {
-			CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 			g_free (set);
 			return;
 		}
@@ -1627,7 +1625,7 @@ imap_expunge_uids_online (CamelFolder *folder, GPtrArray *uids, CamelException *
 		g_free (set);
 	}
 
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	changes = camel_folder_change_info_new ();
 	for (i = 0; i < uids->len; i++) {
@@ -1688,22 +1686,22 @@ imap_expunge_uids_resyncing (CamelFolder *folder, GPtrArray *uids, CamelExceptio
 	 * marked un-deleted.
 	 */
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	((CamelFolderClass *)CAMEL_OBJECT_GET_CLASS(folder))->sync(folder, 0, ex);
 	if (camel_exception_is_set(ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
 	response = camel_imap_command (store, folder, ex, "UID SEARCH DELETED");
 	if (!response) {
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 	result = camel_imap_response_extract (store, response, "SEARCH", ex);
 	if (!result) {
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -1764,7 +1762,7 @@ imap_expunge_uids_resyncing (CamelFolder *folder, GPtrArray *uids, CamelExceptio
 			if (!response) {
 				g_ptr_array_free (keep_uids, TRUE);
 				g_ptr_array_free (mark_uids, TRUE);
-				CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+				camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 				return;
 			}
 			camel_imap_response_free (store, response);
@@ -1788,7 +1786,7 @@ imap_expunge_uids_resyncing (CamelFolder *folder, GPtrArray *uids, CamelExceptio
 			if (!response) {
 				g_ptr_array_free (keep_uids, TRUE);
 				g_ptr_array_free (mark_uids, TRUE);
-				CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+				camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 				return;
 			}
 			camel_imap_response_free (store, response);
@@ -1828,7 +1826,7 @@ imap_expunge_uids_resyncing (CamelFolder *folder, GPtrArray *uids, CamelExceptio
 	/* now we can free this, now that we're done with keep_uids */
 	g_free (result);
 
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 static gchar *
@@ -2046,11 +2044,11 @@ imap_append_online (CamelFolder *folder, CamelMimeMessage *message,
 	camel_imap_response_free (store, response);
 
 	/* Make sure a "folder_changed" is emitted. */
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	if (store->current_folder != folder ||
 	    camel_folder_summary_count (folder->summary) == count)
 		imap_refresh_info (folder, ex);
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 void
@@ -2104,10 +2102,10 @@ imap_transfer_offline (CamelFolder *source, GPtrArray *uids,
 	 * deadlock in the case where we're simultaneously also trying
 	 * to copy messages in the other direction from another thread.
 	 */
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	CAMEL_IMAP_FOLDER_REC_LOCK (source, cache_lock);
 	CAMEL_IMAP_FOLDER_REC_LOCK (dest, cache_lock);
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (transferred_uids) {
 		*transferred_uids = g_ptr_array_new ();
@@ -2947,16 +2945,16 @@ imap_get_message (CamelFolder *folder, const gchar *uid, CamelException *ex)
 				gchar *body, *found_uid;
 				gint i;
 
-				CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+				camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 				if (!camel_imap_store_connected(store, ex)) {
-					CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 					camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
 							     _("This message is not currently available"));
 					goto fail;
 				}
 
 				response = camel_imap_command (store, folder, ex, "UID FETCH %s BODY", uid);
-				CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+				camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 				if (response) {
 					for (i = 0, body = NULL; i < response->untagged->len; i++) {
@@ -3451,7 +3449,7 @@ imap_update_summary (CamelFolder *folder, gint exists,
 
 	if (type == CAMEL_IMAP_RESPONSE_ERROR || camel_application_is_exiting) {
 		if (type != CAMEL_IMAP_RESPONSE_ERROR && type != CAMEL_IMAP_RESPONSE_TAGGED)
-			CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 		goto lose;
 	}
@@ -3518,7 +3516,7 @@ imap_update_summary (CamelFolder *folder, gint exists,
 				camel_operation_end (NULL);
 
 				if (type != CAMEL_IMAP_RESPONSE_ERROR && type != CAMEL_IMAP_RESPONSE_TAGGED)
-					CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 				goto lose;
 			}
@@ -3791,14 +3789,14 @@ camel_imap_folder_fetch_data (CamelImapFolder *imap_folder, const gchar *uid,
 
 	camel_exception_clear(ex);
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	CAMEL_IMAP_FOLDER_REC_LOCK (imap_folder, cache_lock);
 
 	if (!camel_imap_store_connected(store, ex)) {
 		camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
 				     _("This message is not currently available"));
 		CAMEL_IMAP_FOLDER_REC_UNLOCK (imap_folder, cache_lock);
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return NULL;
 	}
 
@@ -3813,7 +3811,7 @@ camel_imap_folder_fetch_data (CamelImapFolder *imap_folder, const gchar *uid,
 					       uid, section_text);
 	}
 	/* We won't need the connect_lock again after this. */
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (!response) {
 		CAMEL_IMAP_FOLDER_REC_UNLOCK (imap_folder, cache_lock);
@@ -4004,7 +4002,7 @@ imap_get_quota_info (CamelFolder *folder)
 	if (CAMEL_OFFLINE_STORE (imap_store)->state == CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL)
 		return NULL;
 
-	CAMEL_SERVICE_REC_LOCK (imap_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected (imap_store, NULL))
 		goto done;
@@ -4081,7 +4079,7 @@ imap_get_quota_info (CamelFolder *folder)
 		g_free (folder_name);
 	}
 done:
-	CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 	return res;
 }
 
diff --git a/camel/providers/imap/camel-imap-store-summary.c b/camel/providers/imap/camel-imap-store-summary.c
index 9428dfc..a57184d 100644
--- a/camel/providers/imap/camel-imap-store-summary.c
+++ b/camel/providers/imap/camel-imap-store-summary.c
@@ -29,8 +29,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-imap-store-summary.h"
 
 #define d(x)
@@ -706,10 +704,10 @@ store_info_set_string(CamelStoreSummary *s, CamelStoreInfo *mi, gint type, const
 	switch (type) {
 	case CAMEL_IMAP_STORE_INFO_FULL_NAME:
 		d(printf("Set full name %s -> %s\n", isi->full_name, str));
-		CAMEL_STORE_SUMMARY_LOCK(s, summary_lock);
+		camel_store_summary_lock (s, CSS_SUMMARY_LOCK);
 		g_free(isi->full_name);
 		isi->full_name = g_strdup(str);
-		CAMEL_STORE_SUMMARY_UNLOCK(s, summary_lock);
+		camel_store_summary_unlock (s, CSS_SUMMARY_LOCK);
 		break;
 	default:
 		camel_imap_store_summary_parent->store_info_set_string(s, mi, type, str);
diff --git a/camel/providers/imap/camel-imap-store.c b/camel/providers/imap/camel-imap-store.c
index 81298b7..bd3f3b6 100644
--- a/camel/providers/imap/camel-imap-store.c
+++ b/camel/providers/imap/camel-imap-store.c
@@ -36,8 +36,6 @@
 #include <glib/gi18n-lib.h>
 #include <glib/gstdio.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-imap-command.h"
 #include "camel-imap-folder.h"
 #include "camel-imap-message-cache.h"
@@ -65,6 +63,8 @@
 #define strtok_r(s,sep,lasts) (*(lasts)=strtok((s),(sep)))
 #endif
 
+extern gint camel_verbose_debug;
+
 static CamelOfflineStoreClass *parent_class = NULL;
 
 static gchar imap_tag_prefix = 'A';
@@ -990,11 +990,11 @@ query_auth_types (CamelService *service, CamelException *ex)
 		return NULL;
 	}
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	connected = store->istream != NULL && store->connected;
 	if (!connected)
 		connected = connect_to_server_wrapper (service, ex);
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	if (!connected)
 		return NULL;
 
@@ -1392,10 +1392,10 @@ imap_connect (CamelService *service, CamelException *ex)
 	if (((CamelOfflineStore *) store)->state == CAMEL_OFFLINE_STORE_NETWORK_UNAVAIL)
 		return TRUE;
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	if (!connect_to_server_wrapper (service, ex) ||
 	    !imap_auth_loop (service, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		camel_service_disconnect (service, TRUE, NULL);
 		return FALSE;
 	}
@@ -1551,7 +1551,7 @@ done:
 	/* save any changes we had */
 	camel_store_summary_save((CamelStoreSummary *)store->summary);
 
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (camel_exception_is_set (ex))
 		camel_service_disconnect (service, TRUE, NULL);
@@ -1630,7 +1630,7 @@ imap_noop (CamelStore *store, CamelException *ex)
 	CamelImapResponse *response;
 	CamelFolder *current_folder;
 
-	CAMEL_SERVICE_REC_LOCK (imap_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected(imap_store, ex))
 		goto done;
@@ -1645,7 +1645,7 @@ imap_noop (CamelStore *store, CamelException *ex)
 			camel_imap_response_free (imap_store, response);
 	}
 done:
-	CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 }
 
 static CamelFolder *
@@ -1834,10 +1834,10 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 
 	camel_exception_clear(ex);
 
-	CAMEL_SERVICE_REC_LOCK(imap_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected(imap_store, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 		return NULL;
 	}
 
@@ -1855,14 +1855,14 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 		const gchar *c;
 
 		if (camel_exception_get_id(ex) == CAMEL_EXCEPTION_USER_CANCEL) {
-			CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 			return NULL;
 		}
 
 		camel_exception_clear (ex);
 
 		if (!(flags & CAMEL_STORE_FOLDER_CREATE)) {
-			CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 			camel_exception_setv (ex, CAMEL_EXCEPTION_STORE_NO_FOLDER,
 					      _("No such folder %s"), folder_name);
 			return NULL;
@@ -1874,7 +1874,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 			c++;
 
 		if (*c != '\0') {
-			CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 			camel_exception_setv (ex, CAMEL_EXCEPTION_FOLDER_INVALID_PATH,
 					      _("The folder name \"%s\" is invalid because it contains the character \"%c\""),
 					      folder_name, *c);
@@ -1895,7 +1895,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 			gint i;
 
 			if (!(response = camel_imap_command (imap_store, NULL, ex, "LIST \"\" %G", parent_real))) {
-				CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+				camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 				g_free (parent_name);
 				g_free (parent_real);
 				return NULL;
@@ -1940,7 +1940,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 				if (messages > 0) {
 					camel_exception_set (ex, CAMEL_EXCEPTION_FOLDER_INVALID_STATE,
 							     _("The parent folder is not allowed to contain subfolders"));
-					CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 					g_free (parent_name);
 					g_free (parent_real);
 					return NULL;
@@ -1950,7 +1950,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 				camel_exception_init (&lex);
 				delete_folder (store, parent_name, &lex);
 				if (camel_exception_is_set (&lex)) {
-					CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 					camel_exception_xfer (ex, &lex);
 					g_free (parent_name);
 					g_free (parent_real);
@@ -1964,7 +1964,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 				g_free (name);
 
 				if (!response) {
-					CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 					g_free (parent_name);
 					g_free (parent_real);
 					return NULL;
@@ -1988,7 +1988,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 		}
 		g_free(folder_real);
 		if (!response) {
-			CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 			return NULL;
 		}
 	} else if (flags & CAMEL_STORE_FOLDER_EXCL) {
@@ -1998,7 +1998,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 
 		camel_imap_response_free_without_processing (imap_store, response);
 
-		CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 		return NULL;
 	}
@@ -2025,7 +2025,7 @@ get_folder (CamelStore *store, const gchar *folder_name, guint32 flags, CamelExc
 	}
 	camel_imap_response_free_without_processing (imap_store, response);
 
-	CAMEL_SERVICE_REC_UNLOCK (imap_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	return new_folder;
 }
@@ -2073,7 +2073,7 @@ delete_folder (CamelStore *store, const gchar *folder_name, CamelException *ex)
 	CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
 	CamelImapResponse *response;
 
-	CAMEL_SERVICE_REC_LOCK (imap_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected(imap_store, ex))
 		goto fail;
@@ -2095,7 +2095,7 @@ delete_folder (CamelStore *store, const gchar *folder_name, CamelException *ex)
 		imap_forget_folder (imap_store, folder_name, ex);
 	}
 fail:
-	CAMEL_SERVICE_REC_UNLOCK(imap_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 }
 
 static void
@@ -2173,7 +2173,7 @@ rename_folder (CamelStore *store, const gchar *old_name, const gchar *new_name_i
 	CamelImapResponse *response;
 	gchar *oldpath, *newpath, *storage_path;
 
-	CAMEL_SERVICE_REC_LOCK (imap_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected(imap_store, ex))
 		goto fail;
@@ -2246,7 +2246,7 @@ rename_folder (CamelStore *store, const gchar *old_name, const gchar *new_name_i
 	g_free (newpath);
 fail:
 	imap_store->renaming = FALSE;
-	CAMEL_SERVICE_REC_UNLOCK(imap_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (imap_store), CS_REC_CONNECT_LOCK);
 }
 
 static CamelFolderInfo *
@@ -2715,7 +2715,7 @@ refresh_refresh(CamelSession *session, CamelSessionThreadMsg *msg)
 	struct _refresh_msg *m = (struct _refresh_msg *)msg;
 	CamelImapStore *store = (CamelImapStore *)m->store;
 
-	CAMEL_SERVICE_REC_LOCK (store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected (store, &m->ex))
 		goto done;
@@ -2732,7 +2732,7 @@ refresh_refresh(CamelSession *session, CamelSessionThreadMsg *msg)
 	get_folders_sync (store, NULL, &m->ex);
 	camel_store_summary_save ((CamelStoreSummary *)store->summary);
 done:
-	CAMEL_SERVICE_REC_UNLOCK (store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 static void
@@ -2778,7 +2778,7 @@ get_folder_info (CamelStore *store, const gchar *top, guint32 flags, CamelExcept
 		now = time(NULL);
 		ref = now > imap_store->refresh_stamp+60*60*1;
 		if (ref) {
-			CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+			camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 			ref = now > imap_store->refresh_stamp+60*60*1;
 			if (ref) {
 				struct _refresh_msg *m;
@@ -2790,14 +2790,14 @@ get_folder_info (CamelStore *store, const gchar *top, guint32 flags, CamelExcept
 				camel_exception_init(&m->ex);
 				camel_session_thread_queue(((CamelService *)store)->session, &m->msg, 0);
 			}
-			CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		}
 	} else {
 		gchar *pattern;
 		gint i;
 		CamelImapStoreNamespace *ns;
 
-		CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+		camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 		if (!camel_imap_store_connected((CamelImapStore *)store, ex))
 			goto fail;
@@ -2831,14 +2831,14 @@ get_folder_info (CamelStore *store, const gchar *top, guint32 flags, CamelExcept
 			get_folders_sync(imap_store, pattern, ex);
 		}
 		camel_store_summary_save((CamelStoreSummary *)imap_store->summary);
-		CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	}
 
 	tree = get_folder_info_offline(store, top, flags, ex);
 	return tree;
 
 fail:
-	CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	return NULL;
 }
 
@@ -2972,7 +2972,7 @@ subscribe_folder (CamelStore *store, const gchar *folder_name,
 	CamelFolderInfo *fi;
 	CamelStoreInfo *si;
 
-	CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected (imap_store, ex))
 		goto done;
@@ -3006,7 +3006,7 @@ subscribe_folder (CamelStore *store, const gchar *folder_name,
 	camel_object_trigger_event (CAMEL_OBJECT (store), "folder_subscribed", fi);
 	camel_folder_info_free (fi);
 done:
-	CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 static void
@@ -3016,7 +3016,7 @@ unsubscribe_folder (CamelStore *store, const gchar *folder_name,
 	CamelImapStore *imap_store = CAMEL_IMAP_STORE (store);
 	CamelImapResponse *response;
 
-	CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (!camel_imap_store_connected (imap_store, ex))
 		goto done;
@@ -3029,7 +3029,7 @@ unsubscribe_folder (CamelStore *store, const gchar *folder_name,
 
 	imap_folder_effectively_unsubscribed (imap_store, folder_name, ex);
 done:
-	CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 }
 
 #if 0
diff --git a/camel/providers/imapx/camel-imapx-server.c b/camel/providers/imapx/camel-imapx-server.c
index 3832bbd..7270b28 100644
--- a/camel/providers/imapx/camel-imapx-server.c
+++ b/camel/providers/imapx/camel-imapx-server.c
@@ -19,8 +19,6 @@
 #include <prerr.h>
 #endif
 
-#include <camel/camel-private.h>
-
 #include "camel-imapx-utils.h"
 #include "camel-imapx-exception.h"
 #include "camel-imapx-stream.h"
@@ -3907,7 +3905,7 @@ camel_imapx_server_connect (CamelIMAPXServer *is, gboolean connect, CamelExcepti
 {
 	gboolean ret = FALSE;
 
-	CAMEL_SERVICE_REC_LOCK (is->store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (is->store), CS_REC_CONNECT_LOCK);
 	if (connect) {
 		if (is->state == IMAPX_AUTHENTICATED || is->state == IMAPX_SELECTED) {
 			ret = TRUE;
@@ -3934,7 +3932,7 @@ camel_imapx_server_connect (CamelIMAPXServer *is, gboolean connect, CamelExcepti
 	}
 
 exit:
-	CAMEL_SERVICE_REC_UNLOCK (is->store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (is->store), CS_REC_CONNECT_LOCK);
 	return ret;
 }
 
diff --git a/camel/providers/imapx/camel-imapx-store-summary.c b/camel/providers/imapx/camel-imapx-store-summary.c
index bd64684..8eb7ec2 100644
--- a/camel/providers/imapx/camel-imapx-store-summary.c
+++ b/camel/providers/imapx/camel-imapx-store-summary.c
@@ -29,8 +29,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-imapx-utils.h"
 #include "camel-imapx-store-summary.h"
 
@@ -664,10 +662,10 @@ store_info_set_string(CamelStoreSummary *s, CamelStoreInfo *mi, gint type, const
 	switch (type) {
 	case CAMEL_IMAPX_STORE_INFO_FULL_NAME:
 		d(printf("Set full name %s -> %s\n", isi->full_name, str));
-		CAMEL_STORE_SUMMARY_LOCK(s, summary_lock);
+		camel_store_summary_lock (s, CSS_SUMMARY_LOCK);
 		g_free(isi->full_name);
 		isi->full_name = g_strdup(str);
-		CAMEL_STORE_SUMMARY_UNLOCK(s, summary_lock);
+		camel_store_summary_unlock (s, CSS_SUMMARY_LOCK);
 		break;
 	default:
 		camel_imapx_store_summary_parent->store_info_set_string(s, mi, type, str);
diff --git a/camel/providers/imapx/camel-imapx-store.c b/camel/providers/imapx/camel-imapx-store.c
index 084649b..5f8c772 100644
--- a/camel/providers/imapx/camel-imapx-store.c
+++ b/camel/providers/imapx/camel-imapx-store.c
@@ -41,8 +41,6 @@
 #include <glib/gstdio.h>
 #include <glib/gi18n-lib.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-imapx-store.h"
 #include "camel-imapx-folder.h"
 #include "camel-imapx-exception.h"
@@ -153,7 +151,7 @@ imapx_query_auth_types (CamelService *service, CamelException *ex)
 		return NULL;
 	}
 
-	CAMEL_SERVICE_REC_LOCK (istore, connect_lock);
+	camel_service_lock (service, CS_REC_CONNECT_LOCK);
 	
 	if (istore->server == NULL)
 		istore->server = camel_imapx_server_new((CamelStore *)istore, service->url);
@@ -161,7 +159,7 @@ imapx_query_auth_types (CamelService *service, CamelException *ex)
 	connected = istore->server->stream != NULL;
 	if (!connected)
 		connected = imapx_connect_to_server (istore->server, ex);
-	CAMEL_SERVICE_REC_UNLOCK (istore, connect_lock);
+	camel_service_unlock (service, CS_REC_CONNECT_LOCK);
 	if (!connected)
 		return NULL;
 
diff --git a/camel/providers/imapx/camel-imapx-tokenise.h b/camel/providers/imapx/camel-imapx-tokenise.h
index 60dea68..536b021 100644
--- a/camel/providers/imapx/camel-imapx-tokenise.h
+++ b/camel/providers/imapx/camel-imapx-tokenise.h
@@ -1,4 +1,4 @@
-/* ANSI-C code produced by gperf version 3.0.2 */
+/* ANSI-C code produced by gperf version 3.0.3 */
 /* Command-line: /usr/bin/gperf -H imapx_hash -N imapx_tokenise_struct -L ANSI-C -o -t -k'1,$' --output-file=camel-imapx-tokenise.h camel-imapx-tokens.txt  */
 
 #if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
@@ -83,6 +83,9 @@ imapx_hash (register const char *str, register unsigned int len)
 
 #ifdef __GNUC__
 __inline
+#ifdef __GNUC_STDC_INLINE__
+__attribute__ ((__gnu_inline__))
+#endif
 #endif
 struct _imapx_keyword *
 imapx_tokenise_struct (register const char *str, register unsigned int len)
diff --git a/camel/providers/local/camel-local-folder.c b/camel/providers/local/camel-local-folder.c
index 2f0a5d1..bc5b84f 100644
--- a/camel/providers/local/camel-local-folder.c
+++ b/camel/providers/local/camel-local-folder.c
@@ -41,8 +41,6 @@
 #include <posix1_lim.h>
 #endif
 
-#include <camel/camel-private.h>
-
 #include "camel-local-folder.h"
 #include "camel-local-private.h"
 #include "camel-local-store.h"
diff --git a/camel/providers/local/camel-mh-summary.c b/camel/providers/local/camel-mh-summary.c
index 7daac74..27c87de 100644
--- a/camel/providers/local/camel-mh-summary.c
+++ b/camel/providers/local/camel-mh-summary.c
@@ -34,8 +34,6 @@
 
 #include <glib/gi18n-lib.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-mh-summary.h"
 #include "camel-local-private.h"
 
@@ -284,8 +282,8 @@ mh_summary_check(CamelLocalSummary *cls, CamelFolderChangeInfo *changeinfo, Came
 	g_hash_table_destroy(left);
 
 	/* sort the summary based on message number (uid), since the directory order is not useful */
-	CAMEL_SUMMARY_LOCK(s, summary_lock);
-	CAMEL_SUMMARY_UNLOCK(s, summary_lock);
+	camel_folder_summary_lock (s, CFS_SUMMARY_LOCK);
+	camel_folder_summary_unlock (s, CFS_SUMMARY_LOCK);
 
 	return 0;
 }
diff --git a/camel/providers/local/camel-spool-summary.c b/camel/providers/local/camel-spool-summary.c
index 44ad941..a0325fc 100644
--- a/camel/providers/local/camel-spool-summary.c
+++ b/camel/providers/local/camel-spool-summary.c
@@ -36,6 +36,7 @@
 
 #include "camel-spool-summary.h"
 #include "camel-local-private.h"
+#include "camel-win32.h"
 
 #define io(x)
 #define d(x) /*(printf("%s(%d): ", __FILE__, __LINE__),(x))*/
diff --git a/camel/providers/nntp/camel-nntp-folder.c b/camel/providers/nntp/camel-nntp-folder.c
index 88469c6..91e961c 100644
--- a/camel/providers/nntp/camel-nntp-folder.c
+++ b/camel/providers/nntp/camel-nntp-folder.c
@@ -33,8 +33,6 @@
 
 #include <glib/gi18n-lib.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-nntp-folder.h"
 #include "camel-nntp-private.h"
 #include "camel-nntp-store.h"
@@ -68,7 +66,7 @@ nntp_folder_refresh_info_online (CamelFolder *folder, CamelException *ex)
 	nntp_store = (CamelNNTPStore *) folder->parent_store;
 	nntp_folder = (CamelNNTPFolder *) folder;
 
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	camel_nntp_command(nntp_store, ex, nntp_folder, &line, NULL);
 
@@ -77,7 +75,7 @@ nntp_folder_refresh_info_online (CamelFolder *folder, CamelException *ex)
 		nntp_folder->changes = camel_folder_change_info_new();
 	}
 
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	if (changes) {
 		camel_object_trigger_event ((CamelObject *) folder, "folder_changed", changes);
@@ -88,17 +86,17 @@ nntp_folder_refresh_info_online (CamelFolder *folder, CamelException *ex)
 static void
 nntp_folder_sync_online (CamelFolder *folder, CamelException *ex)
 {
-	CAMEL_SERVICE_REC_LOCK(folder->parent_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 	camel_folder_summary_save_to_db (folder->summary, ex);
-	CAMEL_SERVICE_REC_UNLOCK(folder->parent_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 }
 
 static void
 nntp_folder_sync_offline (CamelFolder *folder, CamelException *ex)
 {
-	CAMEL_SERVICE_REC_LOCK(folder->parent_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 	camel_folder_summary_save_to_db (folder->summary, ex);
-	CAMEL_SERVICE_REC_UNLOCK(folder->parent_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (folder->parent_store), CS_REC_CONNECT_LOCK);
 }
 
 static gboolean
@@ -179,13 +177,13 @@ nntp_folder_cache_message (CamelDiscoFolder *disco_folder, const gchar *uid, Cam
 	}
 	*msgid++ = 0;
 
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	stream = nntp_folder_download_message ((CamelNNTPFolder *) disco_folder, article, msgid, ex);
 	if (stream)
 		camel_object_unref (stream);
 
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 }
 
 static CamelMimeMessage *
@@ -211,7 +209,7 @@ nntp_folder_get_message (CamelFolder *folder, const gchar *uid, CamelException *
 	}
 	*msgid++ = 0;
 
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	/* Lookup in cache, NEWS is global messageid's so use a global cache path */
 	stream = camel_data_cache_get (nntp_store->cache, "cache", msgid, NULL);
@@ -246,7 +244,7 @@ fail:
 		changes = NULL;
 	}
 
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	if (changes) {
 		camel_object_trigger_event ((CamelObject *) folder, "folder_changed", changes);
@@ -340,7 +338,7 @@ nntp_folder_append_message_online (CamelFolder *folder, CamelMimeMessage *mime_m
 	struct _camel_header_raw *header, *savedhdrs, *n, *tail;
 	gchar *group, *line;
 
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	/* send 'POST' command */
 	ret = camel_nntp_command (nntp_store, ex, NULL, &line, "post");
@@ -351,7 +349,7 @@ nntp_folder_append_message_online (CamelFolder *folder, CamelMimeMessage *mime_m
 		else if (ret != -1)
 			camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
 					      _("Posting failed: %s"), line);
-		CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 		return;
 	}
 
@@ -402,7 +400,7 @@ nntp_folder_append_message_online (CamelFolder *folder, CamelMimeMessage *mime_m
 	g_free(group);
 	header->next = savedhdrs;
 
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	return;
 }
diff --git a/camel/providers/nntp/camel-nntp-store-summary.c b/camel/providers/nntp/camel-nntp-store-summary.c
index 1ff5020..6e8164f 100644
--- a/camel/providers/nntp/camel-nntp-store-summary.c
+++ b/camel/providers/nntp/camel-nntp-store-summary.c
@@ -29,8 +29,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-nntp-store-summary.h"
 
 #define d(x)
@@ -421,10 +419,10 @@ store_info_set_string(CamelStoreSummary *s, CamelStoreInfo *mi, gint type, const
 	switch (type) {
 	case CAMEL_NNTP_STORE_INFO_FULL_NAME:
 		d(printf("Set full name %s -> %s\n", nsi->full_name, str));
-		CAMEL_STORE_SUMMARY_LOCK(s, summary_lock);
+		camel_store_summary_lock (s, CSS_SUMMARY_LOCK);
 		g_free (nsi->full_name);
 		nsi->full_name = g_strdup (str);
-		CAMEL_STORE_SUMMARY_UNLOCK(s, summary_lock);
+		camel_store_summary_unlock (s, CSS_SUMMARY_LOCK);
 		break;
 	default:
 		camel_nntp_store_summary_parent->store_info_set_string (s, mi, type, str);
diff --git a/camel/providers/nntp/camel-nntp-store.c b/camel/providers/nntp/camel-nntp-store.c
index 84e38cd..6b4745b 100644
--- a/camel/providers/nntp/camel-nntp-store.c
+++ b/camel/providers/nntp/camel-nntp-store.c
@@ -34,8 +34,6 @@
 
 #include <glib/gi18n-lib.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-nntp-summary.h"
 #include "camel-nntp-store.h"
 #include "camel-nntp-store-summary.h"
@@ -162,7 +160,7 @@ connect_to_server (CamelService *service, struct addrinfo *ai, gint ssl_mode, Ca
 	guint len;
 	gchar *path;
 
-	CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (ssl_mode != MODE_CLEAR) {
 #ifdef HAVE_SSL
@@ -254,7 +252,7 @@ connect_to_server (CamelService *service, struct addrinfo *ai, gint ssl_mode, Ca
 	store->current_folder = NULL;
 
  fail:
-	CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 	return retval;
 }
 
@@ -357,7 +355,7 @@ nntp_disconnect_online (CamelService *service, gboolean clean, CamelException *e
 	CamelNNTPStore *store = CAMEL_NNTP_STORE (service);
 	gchar *line;
 
-	CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	if (clean) {
 		camel_nntp_raw_command (store, ex, &line, "quit");
@@ -365,7 +363,7 @@ nntp_disconnect_online (CamelService *service, gboolean clean, CamelException *e
 	}
 
 	if (!service_class->disconnect (service, clean, ex)) {
-		CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+		camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 		return FALSE;
 	}
 
@@ -374,7 +372,7 @@ nntp_disconnect_online (CamelService *service, gboolean clean, CamelException *e
 	g_free(store->current_folder);
 	store->current_folder = NULL;
 
-	CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 
 	return TRUE;
 }
@@ -419,11 +417,11 @@ nntp_get_folder(CamelStore *store, const gchar *folder_name, guint32 flags, Came
 	CamelNNTPStore *nntp_store = CAMEL_NNTP_STORE (store);
 	CamelFolder *folder;
 
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	folder = camel_nntp_folder_new(store, folder_name, ex);
 
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	return folder;
 }
@@ -608,13 +606,13 @@ nntp_store_get_subscribed_folder_info (CamelNNTPStore *store, const gchar *top,
 				if (folder) {
 					CamelFolderChangeInfo *changes = NULL;
 
-					CAMEL_SERVICE_REC_LOCK(store, connect_lock);
+					camel_service_lock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 					camel_nntp_command(store, ex, folder, &line, NULL);
 					if (camel_folder_change_info_changed(folder->changes)) {
 						changes = folder->changes;
 						folder->changes = camel_folder_change_info_new();
 					}
-					CAMEL_SERVICE_REC_UNLOCK(store, connect_lock);
+					camel_service_unlock (CAMEL_SERVICE (store), CS_REC_CONNECT_LOCK);
 					if (changes) {
 						camel_object_trigger_event((CamelObject *) folder, "folder_changed", changes);
 						camel_folder_change_info_free(changes);
@@ -767,7 +765,7 @@ nntp_store_get_folder_info_all(CamelNNTPStore *nntp_store, const gchar *top, gui
 	gint ret = -1;
 	CamelFolderInfo *fi = NULL;
 
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	if (top == NULL)
 		top = "";
@@ -837,7 +835,7 @@ nntp_store_get_folder_info_all(CamelNNTPStore *nntp_store, const gchar *top, gui
 
 	fi = nntp_store_get_cached_folder_info (nntp_store, top, flags, ex);
  error:
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	return fi;
 }
@@ -899,7 +897,7 @@ nntp_store_subscribe_folder (CamelStore *store, const gchar *folder_name,
 	CamelStoreInfo *si;
 	CamelFolderInfo *fi;
 
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	si = camel_store_summary_path(CAMEL_STORE_SUMMARY(nntp_store->summary), folder_name);
 	if (!si) {
@@ -913,14 +911,14 @@ nntp_store_subscribe_folder (CamelStore *store, const gchar *folder_name,
 			fi->flags |= CAMEL_FOLDER_NOINFERIORS | CAMEL_FOLDER_NOCHILDREN;
 			camel_store_summary_touch ((CamelStoreSummary *) nntp_store->summary);
 			camel_store_summary_save ((CamelStoreSummary *) nntp_store->summary);
-			CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 			camel_object_trigger_event ((CamelObject *) nntp_store, "folder_subscribed", fi);
 			camel_folder_info_free (fi);
 			return;
 		}
 	}
 
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 }
 
 static void
@@ -930,7 +928,7 @@ nntp_store_unsubscribe_folder (CamelStore *store, const gchar *folder_name,
 	CamelNNTPStore *nntp_store = CAMEL_NNTP_STORE(store);
 	CamelFolderInfo *fi;
 	CamelStoreInfo *fitem;
-	CAMEL_SERVICE_REC_LOCK(nntp_store, connect_lock);
+	camel_service_lock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 
 	fitem = camel_store_summary_path(CAMEL_STORE_SUMMARY(nntp_store->summary), folder_name);
 
@@ -944,14 +942,14 @@ nntp_store_unsubscribe_folder (CamelStore *store, const gchar *folder_name,
 			fi = nntp_folder_info_from_store_info (nntp_store, nntp_store->do_short_folder_notation, fitem);
 			camel_store_summary_touch ((CamelStoreSummary *) nntp_store->summary);
 			camel_store_summary_save ((CamelStoreSummary *) nntp_store->summary);
-			CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+			camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 			camel_object_trigger_event ((CamelObject *) nntp_store, "folder_unsubscribed", fi);
 			camel_folder_info_free (fi);
 			return;
 		}
 	}
 
-	CAMEL_SERVICE_REC_UNLOCK(nntp_store, connect_lock);
+	camel_service_unlock (CAMEL_SERVICE (nntp_store), CS_REC_CONNECT_LOCK);
 }
 
 /* stubs for various folder operations we're not implementing */
diff --git a/camel/providers/pop3/camel-pop3-engine.c b/camel/providers/pop3/camel-pop3-engine.c
index 1853e8f..9cf9875 100644
--- a/camel/providers/pop3/camel-pop3-engine.c
+++ b/camel/providers/pop3/camel-pop3-engine.c
@@ -31,8 +31,6 @@
 #include <glib.h>
 #include <glib/gi18n-lib.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-pop3-engine.h"
 #include "camel-pop3-stream.h"
 
@@ -43,6 +41,7 @@
 extern CamelServiceAuthType camel_pop3_password_authtype;
 extern CamelServiceAuthType camel_pop3_apop_authtype;
 
+extern gint camel_verbose_debug;
 #define dd(x) (camel_verbose_debug?(x):0)
 
 static void get_capabilities(CamelPOP3Engine *pe);
diff --git a/camel/providers/smtp/camel-smtp-transport.c b/camel/providers/smtp/camel-smtp-transport.c
index ba4e13c..8c9364e 100644
--- a/camel/providers/smtp/camel-smtp-transport.c
+++ b/camel/providers/smtp/camel-smtp-transport.c
@@ -36,8 +36,6 @@
 
 #include <glib/gi18n-lib.h>
 
-#include <camel/camel-private.h>
-
 #include "camel-smtp-transport.h"
 
 #ifdef G_OS_WIN32
@@ -48,6 +46,8 @@
 #undef MIN
 #undef MAX
 
+extern gint camel_verbose_debug;
+
 #define d(x) (camel_verbose_debug ? (x) : 0)
 
 /* Specified in RFC 821 */



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