-/* -*- 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.
*
- * Licensed under the Academic Free License version 1.2
+ * Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
*
* 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 <config.h>
#include "dbus-dataslot.h"
-#include "dbus-threads.h"
+#include "dbus-threads-internal.h"
/**
* @defgroup DBusDataSlot Data slots
allocator->allocated_slots = NULL;
allocator->n_allocated_slots = 0;
allocator->n_used_slots = 0;
- allocator->lock = NULL;
+ allocator->lock_loc = NULL;
return TRUE;
}
* is allocated and stored at *slot_id_p.
*
* @param allocator the allocator
- * @param mutex the lock for this allocator
+ * @param mutex_loc the location 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,
+ DBusMutex **mutex_loc,
dbus_int32_t *slot_id_p)
{
dbus_int32_t slot;
- if (!dbus_mutex_lock (mutex))
- return FALSE;
+ _dbus_mutex_lock (*mutex_loc);
if (allocator->n_allocated_slots == 0)
{
- _dbus_assert (allocator->lock == NULL);
- allocator->lock = mutex;
+ _dbus_assert (allocator->lock_loc == NULL);
+ allocator->lock_loc = mutex_loc;
+ }
+ else if (allocator->lock_loc != mutex_loc)
+ {
+ _dbus_warn_check_failed ("D-Bus threads were initialized after first using the D-Bus library. If your application does not directly initialize threads or use D-Bus, keep in mind that some library or plugin may have used D-Bus or initialized threads behind your back. You can often fix this problem by calling dbus_init_threads() or dbus_g_threads_init() early in your main() method, before D-Bus is used.\n");
+ _dbus_assert_not_reached ("exiting");
}
- else
- _dbus_assert (allocator->lock == mutex);
if (*slot_id_p >= 0)
{
slot, allocator, allocator->n_allocated_slots, allocator->n_used_slots);
out:
- dbus_mutex_unlock (allocator->lock);
+ _dbus_mutex_unlock (*(allocator->lock_loc));
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.
_dbus_data_slot_allocator_free (DBusDataSlotAllocator *allocator,
dbus_int32_t *slot_id_p)
{
- dbus_mutex_lock (allocator->lock);
+ _dbus_mutex_lock (*(allocator->lock_loc));
_dbus_assert (*slot_id_p < allocator->n_allocated_slots);
_dbus_assert (allocator->allocated_slots[*slot_id_p].slot_id == *slot_id_p);
if (allocator->allocated_slots[*slot_id_p].refcount > 0)
{
- dbus_mutex_unlock (allocator->lock);
+ _dbus_mutex_unlock (*(allocator->lock_loc));
return;
}
if (allocator->n_used_slots == 0)
{
- DBusMutex *mutex = allocator->lock;
+ DBusMutex **mutex_loc = allocator->lock_loc;
dbus_free (allocator->allocated_slots);
allocator->allocated_slots = NULL;
allocator->n_allocated_slots = 0;
- allocator->lock = NULL;
+ allocator->lock_loc = NULL;
- dbus_mutex_unlock (mutex);
+ _dbus_mutex_unlock (*mutex_loc);
}
else
{
- dbus_mutex_unlock (allocator->lock);
+ _dbus_mutex_unlock (*(allocator->lock_loc));
}
}
* 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;
+ _dbus_mutex_lock (*(allocator->lock_loc));
_dbus_assert (slot < allocator->n_allocated_slots);
_dbus_assert (allocator->allocated_slots[slot].slot_id == slot);
- dbus_mutex_unlock (allocator->lock);
+ _dbus_mutex_unlock (*(allocator->lock_loc));
#endif
if (slot >= list->n_slots)
* 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;
+ _dbus_mutex_lock (*(allocator->lock_loc));
_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_mutex_unlock (*(allocator->lock_loc));
#endif
if (slot >= list->n_slots)
}
/**
- * Frees the data slot list and all data slots contained
- * in it, calling application-provided free functions
- * if they exist.
+ * Frees all data slots contained in the list, calling
+ * application-provided free functions if they exist.
*
- * @param list the list to free
+ * @param list the list to clear
*/
void
-_dbus_data_slot_list_free (DBusDataSlotList *list)
+_dbus_data_slot_list_clear (DBusDataSlotList *list)
{
int i;
list->slots[i].free_data_func = NULL;
++i;
}
+}
+/**
+ * Frees the data slot list and all data slots contained
+ * in it, calling application-provided free functions
+ * if they exist.
+ *
+ * @param list the list to free
+ */
+void
+_dbus_data_slot_list_free (DBusDataSlotList *list)
+{
+ _dbus_data_slot_list_clear (list);
+
dbus_free (list->slots);
list->slots = NULL;
list->n_slots = 0;
_dbus_data_slot_list_init (&list);
- mutex = dbus_mutex_new ();
+ _dbus_mutex_new_at_location (&mutex);
if (mutex == NULL)
_dbus_assert_not_reached ("failed to alloc mutex");
*/
dbus_int32_t tmp = -1;
- _dbus_data_slot_allocator_alloc (&allocator, mutex, &tmp);
+ _dbus_data_slot_allocator_alloc (&allocator, &mutex, &tmp);
if (tmp != i)
_dbus_assert_not_reached ("did not allocate slots in numeric order\n");
++i;
}
- dbus_mutex_free (mutex);
+ _dbus_mutex_free_at_location (&mutex);
return TRUE;
}