DBusPendingCall: exclusively use atomic operations on the refcount
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Thu, 23 Jun 2011 12:08:14 +0000 (13:08 +0100)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Thu, 14 Jul 2011 15:37:05 +0000 (16:37 +0100)
Same reasoning as for fd.o #38005, commit 50732523a7.

Reviewed-by: Will Thompson <will.thompson@collabora.co.uk>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=38005
Bug-NB: NB#263436

dbus/dbus-pending-call.c

index 70392b7..cfb2baf 100644 (file)
@@ -133,8 +133,8 @@ _dbus_pending_call_new_unlocked (DBusConnection    *connection,
     {
       pending->timeout = NULL;
     }
-      
-  pending->refcount.value = 1;
+
+  _dbus_atomic_inc (&pending->refcount);
   pending->connection = connection;
   _dbus_connection_ref_unlocked (pending->connection);
 
@@ -374,8 +374,8 @@ _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending,
 DBusPendingCall *
 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending)
 {
-  pending->refcount.value += 1;
-  
+  _dbus_atomic_inc (&pending->refcount);
+
   return pending;
 }
 
@@ -433,15 +433,14 @@ _dbus_pending_call_last_unref (DBusPendingCall *pending)
 void
 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending)
 {
-  dbus_bool_t last_unref;
-  
-  _dbus_assert (pending->refcount.value > 0);
+  dbus_int32_t old_refcount;
 
-  pending->refcount.value -= 1;
-  last_unref = pending->refcount.value == 0;
+  old_refcount = _dbus_atomic_dec (&pending->refcount);
+  _dbus_assert (old_refcount > 0);
 
   CONNECTION_UNLOCK (pending->connection);
-  if (last_unref)
+
+  if (old_refcount == 1)
     _dbus_pending_call_last_unref (pending);
 }
 
@@ -554,19 +553,8 @@ dbus_pending_call_ref (DBusPendingCall *pending)
 {
   _dbus_return_val_if_fail (pending != NULL, NULL);
 
-  /* The connection lock is better than the global
-   * lock in the atomic increment fallback
-   */
-#ifdef DBUS_HAVE_ATOMIC_INT
   _dbus_atomic_inc (&pending->refcount);
-#else
-  CONNECTION_LOCK (pending->connection);
-  _dbus_assert (pending->refcount.value > 0);
 
-  pending->refcount.value += 1;
-  CONNECTION_UNLOCK (pending->connection);
-#endif
-  
   return pending;
 }
 
@@ -583,19 +571,8 @@ dbus_pending_call_unref (DBusPendingCall *pending)
 
   _dbus_return_if_fail (pending != NULL);
 
-  /* More efficient to use the connection lock instead of atomic
-   * int fallback if we lack atomic int decrement
-   */
-#ifdef DBUS_HAVE_ATOMIC_INT
   last_unref = (_dbus_atomic_dec (&pending->refcount) == 1);
-#else
-  CONNECTION_LOCK (pending->connection);
-  _dbus_assert (pending->refcount.value > 0);
-  pending->refcount.value -= 1;
-  last_unref = pending->refcount.value == 0;
-  CONNECTION_UNLOCK (pending->connection);
-#endif
-  
+
   if (last_unref)
     _dbus_pending_call_last_unref(pending);
 }