From fe4018941190f8bf020e4a8ed2999c212e0e113d Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Sat, 15 Feb 2003 16:25:08 +0000 Subject: 2003-02-15 Alexander Larsson * dbus/dbus-threads.c: * dbus/dbus-threads.h: Add condvars. Remove static mutext from API. Implement static mutexes by initializing them from threads_init. * glib/dbus-gthread.c: * qt/dbus-qthread.cpp: Update with the thread api changes. * dbus/dbus-list.c: * dbus/dbus-list.h: Turn StaticMutex into normal mutex + init function. Export new functions _dbus_list_alloc_link, _dbus_list_free_link, _dbus_list_append_link, _dbus_list_prepend_link * dbus/dbus-sysdeps.c: * dbus/dbus-sysdeps.h: New type dbus_atomic_t, and new functions _dbus_atomic_inc, _dbus_atomic_dec. Only slow fallback implementation at the moment. * dbus/dbus-protocol.h: Add DBUS_MESSAGE_LOCAL_DISCONNECT define * dbus/dbus-message.c: Make ref/unref atomic. Fix some docs. * dbus/dbus-connection-internal.h: * dbus/dbus-connection.c: * dbus/dbus-connection.h: Make threadsafe. Change _peek to _borrow,_return & _steal_borrowed. Change disconnect callback to event. Make dbus_connection_dispatch_messages reentrant. * dbus/dbus-transport.c: Don't ref the connection on calls to the transport implementation. * dbus/dbus-message-handler.c: Make threadsafe. * glib/dbus-gmain.c: Don't use peek_message anymore * test/Makefile.am: * test/debug-thread.c: * test/debug-thread.h: Simple thread implementation that asserts() on deadlocks in single-threaded code. * test/bus-test.c: (main) Call debug_threads_init. * test/watch.c: Use disconnect message instead of disconnect callback. * bus/connection.c: * bus/connection.h: Don't call dbus_connection_set_disconnect_function. Instead export bus_connection_disconnect. * bus/dispatch.c: Call bus_connection_disconnect when we get a disconnected message. --- dbus/dbus-list.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 6 deletions(-) (limited to 'dbus/dbus-list.c') diff --git a/dbus/dbus-list.c b/dbus/dbus-list.c index e71a7c4a..7f12e3db 100644 --- a/dbus/dbus-list.c +++ b/dbus/dbus-list.c @@ -35,7 +35,15 @@ */ static DBusMemPool *list_pool; -static DBusStaticMutex list_pool_lock = DBUS_STATIC_MUTEX_INIT; +static DBusMutex *list_pool_lock = NULL; + +DBusMutex *_dbus_list_init_lock (void); +DBusMutex * +_dbus_list_init_lock (void) +{ + list_pool_lock = dbus_mutex_new (); + return list_pool_lock; +} /** * @defgroup DBusListInternals Linked list implementation details @@ -55,7 +63,7 @@ alloc_link (void *data) { DBusList *link; - if (!dbus_static_mutex_lock (&list_pool_lock)) + if (!dbus_mutex_lock (list_pool_lock)) return NULL; if (!list_pool) @@ -64,7 +72,7 @@ alloc_link (void *data) if (list_pool == NULL) { - dbus_static_mutex_unlock (&list_pool_lock); + dbus_mutex_unlock (list_pool_lock); return NULL; } } @@ -72,7 +80,7 @@ alloc_link (void *data) link = _dbus_mem_pool_alloc (list_pool); link->data = data; - dbus_static_mutex_unlock (&list_pool_lock); + dbus_mutex_unlock (list_pool_lock); return link; } @@ -80,9 +88,9 @@ alloc_link (void *data) static void free_link (DBusList *link) { - dbus_static_mutex_lock (&list_pool_lock); + dbus_mutex_lock (list_pool_lock); _dbus_mem_pool_dealloc (list_pool, link); - dbus_static_mutex_unlock (&list_pool_lock); + dbus_mutex_unlock (list_pool_lock); } static void @@ -189,6 +197,33 @@ link_after (DBusList **list, * */ +/** + * Allocates a linked list node. Useful for preallocating + * nodes and using _dbus_list_append_link() to avoid + * allocations. + * + * @param data the value to store in the link. + * @returns a newly allocated link. + */ +DBusList* +_dbus_list_alloc_link (void *data) +{ + return alloc_link (data); +} + +/** + * Frees a linked list node allocated with _dbus_list_alloc_link. + * Does not free the data in the node. + * + * @param link the list node + */ +void +_dbus_list_free_link (DBusList *link) +{ + free_link (link); +} + + /** * Appends a value to the list. May return #FALSE * if insufficient memory exists to add a list link. @@ -235,6 +270,43 @@ _dbus_list_prepend (DBusList **list, return TRUE; } +/** + * Appends a link to the list. + * Cannot fail due to out of memory. + * This is a constant-time operation. + * + * @param list address of the list head. + * @param link the link to append. + */ +void +_dbus_list_append_link (DBusList **list, + DBusList *link) +{ + _dbus_list_prepend_link (list, link); + + /* Now cycle the list forward one so the prepended node is the tail */ + *list = (*list)->next; + + return TRUE; +} + +/** + * Prepends a link to the list. + * Cannot fail due to out of memory. + * This is a constant-time operation. + * + * @param list address of the list head. + * @param link the link to prepend. + */ +void +_dbus_list_prepend_link (DBusList **list, + DBusList *link) +{ + link_before (list, *list, link); + + return TRUE; +} + /** * Inserts data into the list before the given existing link. * -- cgit