[evolution-data-server] CamelMessageInfo: Use atomic operations to track reference count.



commit 40e9c591db87a4d15087ac2b51b56fbff7b6cb64
Author: Matthew Barnes <mbarnes redhat com>
Date:   Mon Sep 16 10:24:44 2013 -0400

    CamelMessageInfo: Use atomic operations to track reference count.

 camel/camel-folder-summary.c |   69 ++++++++++++++---------------------------
 camel/camel-folder-summary.h |    4 +-
 2 files changed, 26 insertions(+), 47 deletions(-)
---
diff --git a/camel/camel-folder-summary.c b/camel/camel-folder-summary.c
index ffdc074..72033ac 100644
--- a/camel/camel-folder-summary.c
+++ b/camel/camel-folder-summary.c
@@ -107,12 +107,6 @@ struct _CamelFolderSummaryPrivate {
        guint timeout_handle;
 };
 
-static GMutex info_lock;
-
-/* this lock is ONLY for the standalone messageinfo stuff */
-#define GLOBAL_INFO_LOCK(i) g_mutex_lock(&info_lock)
-#define GLOBAL_INFO_UNLOCK(i) g_mutex_unlock(&info_lock)
-
 /* this should probably be conditional on it existing */
 #define USE_BSEARCH
 
@@ -4429,17 +4423,10 @@ camel_message_info_ref (gpointer o)
 {
        CamelMessageInfo *mi = o;
 
-       if (mi->summary) {
-               camel_folder_summary_lock (mi->summary, CAMEL_FOLDER_SUMMARY_REF_LOCK);
-               g_assert (mi->refcount >= 1);
-               mi->refcount++;
-               camel_folder_summary_unlock (mi->summary, CAMEL_FOLDER_SUMMARY_REF_LOCK);
-       } else {
-               GLOBAL_INFO_LOCK (info);
-               g_assert (mi->refcount >= 1);
-               mi->refcount++;
-               GLOBAL_INFO_UNLOCK (info);
-       }
+       g_return_val_if_fail (mi != NULL, NULL);
+       g_return_val_if_fail (mi->refcount > 0, NULL);
+
+       g_atomic_int_inc (&mi->refcount);
 
        return o;
 }
@@ -4477,37 +4464,29 @@ camel_message_info_free (gpointer o)
        CamelMessageInfo *mi = o;
 
        g_return_if_fail (mi != NULL);
+       g_return_if_fail (mi->refcount > 0);
+
+       if (g_atomic_int_dec_and_test (&mi->refcount)) {
+               if (mi->summary != NULL) {
+                       CamelFolderSummaryClass *class;
+
+                       /* FIXME This is kinda busted, should really
+                        *       be handled by message_info_free(). */
+                       if (mi->summary->priv->build_content
+                           && ((CamelMessageInfoBase *) mi)->content) {
+                               camel_folder_summary_content_info_free (
+                                       mi->summary,
+                                       ((CamelMessageInfoBase *) mi)->content);
+                               ((CamelMessageInfoBase *) mi)->content = NULL;
+                       }
 
-       if (mi->summary) {
-               camel_folder_summary_lock (mi->summary, CAMEL_FOLDER_SUMMARY_REF_LOCK);
-
-               if (mi->refcount >= 1)
-                       mi->refcount--;
-               if (mi->refcount > 0) {
-                       camel_folder_summary_unlock (mi->summary, CAMEL_FOLDER_SUMMARY_REF_LOCK);
-                       return;
-               }
-
-               camel_folder_summary_unlock (mi->summary, CAMEL_FOLDER_SUMMARY_REF_LOCK);
-
-               /* FIXME: this is kinda busted, should really be handled by message info free */
-               if (mi->summary->priv->build_content
-                   && ((CamelMessageInfoBase *) mi)->content) {
-                       camel_folder_summary_content_info_free (mi->summary, ((CamelMessageInfoBase *) 
mi)->content);
-                       ((CamelMessageInfoBase *) mi)->content = NULL;
-               }
+                       class = CAMEL_FOLDER_SUMMARY_GET_CLASS (mi->summary);
+                       g_return_if_fail (class->message_info_free != NULL);
 
-               CAMEL_FOLDER_SUMMARY_GET_CLASS (mi->summary)->message_info_free (mi->summary, mi);
-       } else {
-               GLOBAL_INFO_LOCK (info);
-               mi->refcount--;
-               if (mi->refcount > 0) {
-                       GLOBAL_INFO_UNLOCK (info);
-                       return;
+                       class->message_info_free (mi->summary, mi);
+               } else {
+                       message_info_free (NULL, mi);
                }
-               GLOBAL_INFO_UNLOCK (info);
-
-               message_info_free (NULL, mi);
        }
 }
 
diff --git a/camel/camel-folder-summary.h b/camel/camel-folder-summary.h
index 546383b..09aa078 100644
--- a/camel/camel-folder-summary.h
+++ b/camel/camel-folder-summary.h
@@ -170,7 +170,7 @@ enum {
 struct _CamelMessageInfo {
        CamelFolderSummary *summary;
 
-       guint32 refcount;       /* ??? */
+       volatile gint refcount;
        const gchar *uid;
        /*FIXME: Make it work with the CAMEL_MESSADE_DB_DIRTY flag instead of another 4 bytes*/
        guint dirty : 1;
@@ -182,7 +182,7 @@ struct _CamelMessageInfo {
 struct _CamelMessageInfoBase {
        CamelFolderSummary *summary;
 
-       guint32 refcount;       /* ??? */
+       volatile gint refcount;
        const gchar *uid;
        /*FIXME: Make it work with the CAMEL_MESSADE_DB_DIRTY flag instead of another 4 bytes*/
        guint dirty : 1;


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