From d4451ce306da5e746347985dbdf256fe05b4d9e2 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 23 Jun 2011 13:38:04 +0100 Subject: [PATCH 6/8] DBusMessage: always access refcount atomically --- dbus/dbus-message.c | 85 ++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 68 insertions(+), 17 deletions(-) diff --git a/dbus/dbus-message.c b/dbus/dbus-message.c index a59ed9b..bd9f9bf 100644 --- a/dbus/dbus-message.c +++ b/dbus/dbus-message.c @@ -526,7 +526,15 @@ dbus_message_get_cached (void) _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE); _dbus_assert (message != NULL); - _dbus_assert (message->refcount.value == 0); +#ifndef DBUS_DISABLE_ASSERT + { + dbus_int32_t old_refcount = _dbus_atomic_inc (&message->refcount); + + _dbus_atomic_dec (&message->refcount); + _dbus_assert (old_refcount == 0); + } +#endif + _dbus_assert (message->counters == NULL); _DBUS_UNLOCK (message_cache); @@ -586,8 +594,15 @@ dbus_message_cache_or_finalize (DBusMessage *message) { dbus_bool_t was_cached; int i; - - _dbus_assert (message->refcount.value == 0); + +#ifndef DBUS_DISABLE_ASSERT + { + dbus_int32_t old_refcount = _dbus_atomic_inc (&message->refcount); + + _dbus_atomic_dec (&message->refcount); + _dbus_assert (old_refcount == 0); + } +#endif /* This calls application code and has to be done first thing * without holding the lock @@ -649,8 +664,15 @@ dbus_message_cache_or_finalize (DBusMessage *message) #endif out: - _dbus_assert (message->refcount.value == 0); - +#ifndef DBUS_DISABLE_ASSERT + { + dbus_int32_t old_refcount = _dbus_atomic_inc (&message->refcount); + + _dbus_atomic_dec (&message->refcount); + _dbus_assert (old_refcount == 0); + } +#endif + _DBUS_UNLOCK (message_cache); if (!was_cached) @@ -1039,7 +1061,14 @@ dbus_message_get_reply_serial (DBusMessage *message) static void dbus_message_finalize (DBusMessage *message) { - _dbus_assert (message->refcount.value == 0); +#ifndef DBUS_DISABLE_ASSERT + { + dbus_int32_t old_refcount = _dbus_atomic_inc (&message->refcount); + + _dbus_atomic_dec (&message->refcount); + _dbus_assert (old_refcount == 0); + } +#endif /* This calls application callbacks! */ _dbus_data_slot_list_free (&message->slot_list); @@ -1056,8 +1085,15 @@ dbus_message_finalize (DBusMessage *message) dbus_free(message->unix_fds); #endif - _dbus_assert (message->refcount.value == 0); - +#ifndef DBUS_DISABLE_ASSERT + { + dbus_int32_t old_refcount = _dbus_atomic_inc (&message->refcount); + + _dbus_atomic_dec (&message->refcount); + _dbus_assert (old_refcount == 0); + } +#endif + dbus_free (message); } @@ -1076,7 +1112,7 @@ dbus_message_new_empty_header (void) else { from_cache = FALSE; - message = dbus_new (DBusMessage, 1); + message = dbus_new0 (DBusMessage, 1); if (message == NULL) return NULL; #ifndef DBUS_DISABLE_CHECKS @@ -1088,8 +1124,17 @@ dbus_message_new_empty_header (void) message->n_unix_fds_allocated = 0; #endif } - - message->refcount.value = 1; + +#ifdef DBUS_DISABLE_ASSERT + _dbus_atomic_inc (&message->refcount); +#else + { + dbus_int32_t old_refcount = _dbus_atomic_inc (&message->refcount); + + _dbus_assert (old_refcount == 0); + } +#endif + message->byte_order = DBUS_COMPILER_BYTE_ORDER; message->locked = FALSE; #ifndef DBUS_DISABLE_CHECKS @@ -1448,7 +1493,7 @@ dbus_message_copy (const DBusMessage *message) if (retval == NULL) return NULL; - retval->refcount.value = 1; + _dbus_atomic_inc (&retval->refcount); retval->byte_order = message->byte_order; retval->locked = FALSE; #ifndef DBUS_DISABLE_CHECKS @@ -1519,14 +1564,20 @@ dbus_message_copy (const DBusMessage *message) DBusMessage * dbus_message_ref (DBusMessage *message) { - dbus_int32_t old_refcount; - _dbus_return_val_if_fail (message != NULL, NULL); _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL); _dbus_return_val_if_fail (!message->in_cache, NULL); - - old_refcount = _dbus_atomic_inc (&message->refcount); - _dbus_assert (old_refcount >= 1); + +#ifdef DBUS_DISABLE_ASSERT + _dbus_atomic_inc (&message->refcount); +#else + { + dbus_int32_t old_refcount; + + old_refcount = _dbus_atomic_inc (&message->refcount); + _dbus_assert (old_refcount >= 1); + } +#endif return message; } -- 1.7.5.4