X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dbus%2Fdbus-dataslot.c;h=e37c9dd5f7801138513a7eedc04b68c9016eb613;hb=82600c61dcb715e1651faaa4b5e577fca90bc35d;hp=8026d048e887a2f297226dd01a8a2021c25dbf70;hpb=cc73b3da32ff6d4bebe9013b812f2845ad282cf7;p=platform%2Fupstream%2Fdbus.git diff --git a/dbus/dbus-dataslot.c b/dbus/dbus-dataslot.c index 8026d04..e37c9dd 100644 --- a/dbus/dbus-dataslot.c +++ b/dbus/dbus-dataslot.c @@ -1,4 +1,4 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ /* dbus-dataslot.c storing data on objects * * Copyright (C) 2003 Red Hat, Inc. @@ -17,11 +17,13 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ + +#include #include "dbus-dataslot.h" -#include "dbus-threads.h" +#include "dbus-threads-internal.h" /** * @defgroup DBusDataSlot Data slots @@ -41,13 +43,14 @@ * @param allocator the allocator to initialize */ dbus_bool_t -_dbus_data_slot_allocator_init (DBusDataSlotAllocator *allocator) +_dbus_data_slot_allocator_init (DBusDataSlotAllocator *allocator, + DBusGlobalLock lock) { allocator->allocated_slots = NULL; allocator->n_allocated_slots = 0; allocator->n_used_slots = 0; - allocator->lock = NULL; - + allocator->lock = lock; + return TRUE; } @@ -59,28 +62,18 @@ _dbus_data_slot_allocator_init (DBusDataSlotAllocator *allocator) * is allocated and stored at *slot_id_p. * * @param allocator the allocator - * @param mutex the lock for this allocator * @param slot_id_p address to fill with the slot ID * @returns #TRUE on success */ dbus_bool_t _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator, - DBusMutex *mutex, dbus_int32_t *slot_id_p) { dbus_int32_t slot; - if (!_dbus_mutex_lock (mutex)) + if (!_dbus_lock (allocator->lock)) return FALSE; - if (allocator->n_allocated_slots == 0) - { - _dbus_assert (allocator->lock == NULL); - allocator->lock = mutex; - } - else - _dbus_assert (allocator->lock == mutex); - if (*slot_id_p >= 0) { slot = *slot_id_p; @@ -142,14 +135,14 @@ _dbus_data_slot_allocator_alloc (DBusDataSlotAllocator *allocator, slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots); out: - _dbus_mutex_unlock (allocator->lock); + _dbus_unlock (allocator->lock); return slot >= 0; } /** * Deallocates an ID previously allocated with * _dbus_data_slot_allocator_alloc(). Existing data stored on - * existing #DBusDataList objects with this ID will be freed when the + * existing #DBusDataSlotList objects with this ID will be freed when the * data list is finalized, but may not be retrieved (and may only be * replaced if someone else reallocates the slot). * The slot value is reset to -1 if this is the last unref. @@ -161,8 +154,10 @@ void _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p) { - _dbus_mutex_lock (allocator->lock); - + if (!_dbus_lock (allocator->lock)) + _dbus_assert_not_reached ("we should have initialized global locks " + "before we allocated this slot"); + _dbus_assert (*slot_id_p < allocator->n_allocated_slots); _dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p); _dbus_assert (allocator->allocated_slots[*slot_id_p].refcount > 0); @@ -171,7 +166,7 @@ _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator, if (allocator->allocated_slots[*slot_id_p].refcount > 0) { - _dbus_mutex_unlock (allocator->lock); + _dbus_unlock (allocator->lock); return; } @@ -186,19 +181,12 @@ _dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator, if (allocator->n_used_slots == 0) { - DBusMutex *mutex = allocator->lock; - dbus_free (allocator->allocated_slots); allocator->allocated_slots = NULL; allocator->n_allocated_slots = 0; - allocator->lock = NULL; - - _dbus_mutex_unlock (mutex); - } - else - { - _dbus_mutex_unlock (allocator->lock); } + + _dbus_unlock (allocator->lock); } /** @@ -243,11 +231,13 @@ _dbus_data_slot_list_set (DBusDataSlotAllocator *allocator, * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts * are disabled, since then the asserts are empty. */ - if (!_dbus_mutex_lock (allocator->lock)) - return FALSE; + if (!_dbus_lock (allocator->lock)) + _dbus_assert_not_reached ("we should have initialized global locks " + "before we allocated this slot"); + _dbus_assert (slot < allocator->n_allocated_slots); _dbus_assert (allocator->allocated_slots[slot].slot_id == slot); - _dbus_mutex_unlock (allocator->lock); + _dbus_unlock (allocator->lock); #endif if (slot >= list->n_slots) @@ -301,12 +291,14 @@ _dbus_data_slot_list_get (DBusDataSlotAllocator *allocator, * be e.g. realloc()ing allocated_slots. We avoid doing this if asserts * are disabled, since then the asserts are empty. */ - if (!_dbus_mutex_lock (allocator->lock)) - return FALSE; + if (!_dbus_lock (allocator->lock)) + _dbus_assert_not_reached ("we should have initialized global locks " + "before we allocated this slot"); + _dbus_assert (slot >= 0); _dbus_assert (slot < allocator->n_allocated_slots); _dbus_assert (allocator->allocated_slots[slot].slot_id == slot); - _dbus_mutex_unlock (allocator->lock); + _dbus_unlock (allocator->lock); #endif if (slot >= list->n_slots) @@ -356,7 +348,7 @@ _dbus_data_slot_list_free (DBusDataSlotList *list) /** @} */ -#ifdef DBUS_BUILD_TESTS +#ifdef DBUS_ENABLE_EMBEDDED_TESTS #include "dbus-test.h" #include @@ -382,17 +374,12 @@ _dbus_data_slot_test (void) int i; DBusFreeFunction old_free_func; void *old_data; - DBusMutex *mutex; - - if (!_dbus_data_slot_allocator_init (&allocator)) + + if (!_dbus_data_slot_allocator_init (&allocator, _DBUS_LOCK_server_slots)) _dbus_assert_not_reached ("no memory for allocator"); _dbus_data_slot_list_init (&list); - mutex = _dbus_mutex_new (); - if (mutex == NULL) - _dbus_assert_not_reached ("failed to alloc mutex"); - #define N_SLOTS 100 i = 0; @@ -403,8 +390,8 @@ _dbus_data_slot_test (void) * here. */ dbus_int32_t tmp = -1; - - _dbus_data_slot_allocator_alloc (&allocator, mutex, &tmp); + + _dbus_data_slot_allocator_alloc (&allocator, &tmp); if (tmp != i) _dbus_assert_not_reached ("did not allocate slots in numeric order\n"); @@ -469,9 +456,7 @@ _dbus_data_slot_test (void) ++i; } - _dbus_mutex_free (mutex); - return TRUE; } -#endif /* DBUS_BUILD_TESTS */ +#endif /* DBUS_ENABLE_EMBEDDED_TESTS */