[libsoup/gnome-3-28] soup: Use atomic integers for refcounting



commit 8e43812006a69e284f2c66a8a3f02442d2500513
Author: Edward Hervey <edward centricular com>
Date:   Thu Jun 21 07:28:03 2018 +0200

    soup: Use atomic integers for refcounting
    
    For SoupBuffer, SoupMessageBody, SoupMessageHeaders and
    SoupClientContext
    
    https://bugzilla.gnome.org/show_bug.cgi?id=785110

 libsoup/soup-message-body.c    | 24 +++++++++++++-----------
 libsoup/soup-message-headers.c | 15 ++++++++-------
 libsoup/soup-server.c          |  4 ++--
 3 files changed, 23 insertions(+), 20 deletions(-)
---
diff --git a/libsoup/soup-message-body.c b/libsoup/soup-message-body.c
index 50428eb4..0c45e18e 100644
--- a/libsoup/soup-message-body.c
+++ b/libsoup/soup-message-body.c
@@ -272,7 +272,7 @@ soup_buffer_copy (SoupBuffer *buffer)
 
        /* For non-TEMPORARY buffers, this is just a ref */
        if (priv->use != SOUP_MEMORY_TEMPORARY) {
-               priv->refcount++;
+               g_atomic_int_inc (&priv->refcount);
                return buffer;
        }
 
@@ -305,11 +305,12 @@ soup_buffer_free (SoupBuffer *buffer)
 {
        SoupBufferPrivate *priv = (SoupBufferPrivate *)buffer;
 
-       if (!--priv->refcount) {
-               if (priv->owner_dnotify)
-                       priv->owner_dnotify (priv->owner);
-               g_slice_free (SoupBufferPrivate, priv);
-       }
+       if (!g_atomic_int_dec_and_test (&priv->refcount))
+               return;
+
+       if (priv->owner_dnotify)
+               priv->owner_dnotify (priv->owner);
+       g_slice_free (SoupBufferPrivate, priv);
 }
 
 /**
@@ -725,7 +726,7 @@ soup_message_body_copy (SoupMessageBody *body)
 {
        SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
 
-       priv->ref_count++;
+       g_atomic_int_inc (&priv->ref_count);
        return body;
 }
 
@@ -741,10 +742,11 @@ soup_message_body_free (SoupMessageBody *body)
 {
        SoupMessageBodyPrivate *priv = (SoupMessageBodyPrivate *)body;
 
-       if (--priv->ref_count == 0) {
-               soup_message_body_truncate (body);
-               g_slice_free (SoupMessageBodyPrivate, priv);
-       }
+       if (!g_atomic_int_dec_and_test (&priv->ref_count))
+               return;
+
+       soup_message_body_truncate (body);
+       g_slice_free (SoupMessageBodyPrivate, priv);
 }
 
 G_DEFINE_BOXED_TYPE (SoupMessageBody, soup_message_body, soup_message_body_copy, soup_message_body_free)
diff --git a/libsoup/soup-message-headers.c b/libsoup/soup-message-headers.c
index a180c6ee..16e487af 100644
--- a/libsoup/soup-message-headers.c
+++ b/libsoup/soup-message-headers.c
@@ -90,7 +90,7 @@ soup_message_headers_new (SoupMessageHeadersType type)
 static SoupMessageHeaders *
 soup_message_headers_copy (SoupMessageHeaders *hdrs)
 {
-       hdrs->ref_count++;
+       g_atomic_int_inc (&hdrs->ref_count);
        return hdrs;
 }
 
@@ -103,12 +103,13 @@ soup_message_headers_copy (SoupMessageHeaders *hdrs)
 void
 soup_message_headers_free (SoupMessageHeaders *hdrs)
 {
-       if (--hdrs->ref_count == 0) {
-               soup_message_headers_clear (hdrs);
-               g_array_free (hdrs->array, TRUE);
-               g_clear_pointer (&hdrs->concat, g_hash_table_destroy);
-               g_slice_free (SoupMessageHeaders, hdrs);
-       }
+       if (!g_atomic_int_dec_and_test (&hdrs->ref_count))
+               return;
+
+       soup_message_headers_clear (hdrs);
+       g_array_free (hdrs->array, TRUE);
+       g_clear_pointer (&hdrs->concat, g_hash_table_destroy);
+       g_slice_free (SoupMessageHeaders, hdrs);
 }
 
 G_DEFINE_BOXED_TYPE (SoupMessageHeaders, soup_message_headers, soup_message_headers_copy, 
soup_message_headers_free)
diff --git a/libsoup/soup-server.c b/libsoup/soup-server.c
index 47ebac36..21be8543 100644
--- a/libsoup/soup-server.c
+++ b/libsoup/soup-server.c
@@ -1156,14 +1156,14 @@ soup_client_context_cleanup (SoupClientContext *client)
 static SoupClientContext *
 soup_client_context_ref (SoupClientContext *client)
 {
-       client->ref_count++;
+       g_atomic_int_inc (&client->ref_count);
        return client;
 }
 
 static void
 soup_client_context_unref (SoupClientContext *client)
 {
-       if (--client->ref_count != 0)
+       if (!g_atomic_int_dec_and_test (&client->ref_count))
                return;
 
        soup_client_context_cleanup (client);


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