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. --- test/Makefile.am | 2 + test/bus-test.c | 4 ++ test/debug-thread.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/debug-thread.h | 29 ++++++++++ test/watch.c | 71 +++++++++++++---------- 5 files changed, 236 insertions(+), 31 deletions(-) create mode 100644 test/debug-thread.c create mode 100644 test/debug-thread.h (limited to 'test') diff --git a/test/Makefile.am b/test/Makefile.am index 4ff6c49d..ad657826 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -24,6 +24,8 @@ unbase64_SOURCES= \ bus_test_SOURCES = \ + debug-thread.c \ + debug-thread.h \ bus-test.c break_loader_SOURCES= \ diff --git a/test/bus-test.c b/test/bus-test.c index 624c11e4..38742805 100644 --- a/test/bus-test.c +++ b/test/bus-test.c @@ -8,6 +8,8 @@ #undef DBUS_COMPILATION +#include "debug-thread.h" + typedef struct { long time; @@ -140,6 +142,8 @@ main (int argc, DBusMessage *message; DBusMessageHandler *handler; + debug_threads_init (); + server = dbus_server_listen ("debug:name=test-server", &result); dbus_server_set_new_connection_function (server, new_connection_callback, diff --git a/test/debug-thread.c b/test/debug-thread.c new file mode 100644 index 00000000..5ef3ba44 --- /dev/null +++ b/test/debug-thread.c @@ -0,0 +1,161 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* dbus-test.c Program to run all tests + * + * Copyright (C) 2002 Red Hat Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 + * + */ + +#include +#include +#include "debug-thread.h" + +#define DBUS_COMPILATION +#include +#undef DBUS_COMPILATION + + +static DBusMutex * tmutex_new (void); +static void tmutex_free (DBusMutex *mutex); +static dbus_bool_t tmutex_lock (DBusMutex *mutex); +static dbus_bool_t tmutex_unlock (DBusMutex *mutex); + +static DBusCondVar*tcondvar_new (void); +static void tcondvar_free (DBusCondVar *cond); +static void tcondvar_wait (DBusCondVar *cond, + DBusMutex *mutex); +static dbus_bool_t tcondvar_wait_timeout (DBusCondVar *cond, + DBusMutex *mutex, + int timeout_msec); +static void tcondvar_wake_one (DBusCondVar *cond); +static void tcondvar_wake_all (DBusCondVar *cond); + +static const DBusThreadFunctions functions = +{ + DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK | + DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK | + DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK | + DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK | + DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK | + DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK | + DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK | + DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK | + DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK| + DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK, + tmutex_new, + tmutex_free, + tmutex_lock, + tmutex_unlock, + tcondvar_new, + tcondvar_free, + tcondvar_wait, + tcondvar_wait_timeout, + tcondvar_wake_one, + tcondvar_wake_all +}; + +static DBusMutex * +tmutex_new (void) +{ + int *tmutex; + + tmutex = malloc (sizeof (int*)); + *tmutex = 0; + + return (DBusMutex *)tmutex; +} + +static void +tmutex_free (DBusMutex *mutex) +{ + free (mutex); +} + +static dbus_bool_t +tmutex_lock (DBusMutex *mutex) +{ + int *tmutex = (int *)mutex; + + _dbus_assert (*tmutex == 0); + + *tmutex = 1; + + return TRUE; +} + +static dbus_bool_t +tmutex_unlock (DBusMutex *mutex) +{ + int *tmutex = (int *)mutex; + + _dbus_assert (*tmutex == 1); + + *tmutex = 0; + + return TRUE; +} + +static DBusCondVar* +tcondvar_new (void) +{ + return (DBusCondVar*)0xcafebabe; +} + +static void +tcondvar_free (DBusCondVar *cond) +{ +} + +static void +tcondvar_wait (DBusCondVar *cond, + DBusMutex *mutex) +{ + int *tmutex = (int *)mutex; + + _dbus_assert (*tmutex == 1); +} + +static dbus_bool_t +tcondvar_wait_timeout (DBusCondVar *cond, + DBusMutex *mutex, + int timeout_msec) +{ + int *tmutex = (int *)mutex; + + _dbus_assert (*tmutex == 1); + + return TRUE; +} + + +static void +tcondvar_wake_one (DBusCondVar *cond) +{ +} + +static void +tcondvar_wake_all (DBusCondVar *cond) +{ +} + +void +debug_threads_init (void) +{ + dbus_threads_init (&functions); +} + diff --git a/test/debug-thread.h b/test/debug-thread.h new file mode 100644 index 00000000..57adff88 --- /dev/null +++ b/test/debug-thread.h @@ -0,0 +1,29 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* dbus-test.c Program to run all tests + * + * Copyright (C) 2002 Red Hat Inc. + * + * Licensed under the Academic Free License version 1.2 + * + * 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 + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 + * + */ + +#ifndef DEBUG_THREAD_H +#define DEBUG_THREAD_H + +void debug_threads_init (void); + +#endif diff --git a/test/watch.c b/test/watch.c index a885a75c..1a31e64b 100644 --- a/test/watch.c +++ b/test/watch.c @@ -130,6 +130,17 @@ remove_server_watch (DBusWatch *watch, static int count = 0; +static void +disconnect (DBusConnection *connection) +{ + fprintf (stderr, "Disconnected\n"); + + _dbus_list_remove (&connections, connection); + dbus_connection_unref (connection); + quit_mainloop (); +} + + static void check_messages (void) { @@ -141,28 +152,37 @@ check_messages (void) DBusList *next = _dbus_list_get_next_link (&connections, link); DBusConnection *connection = link->data; DBusMessage *message; + const char *name; while ((message = dbus_connection_pop_message (connection))) { DBusMessage *reply; - fprintf (stderr, "Received message %d, sending reply\n", count); - - reply = dbus_message_new ("org.freedesktop.DBus.Test", "org.freedesktop.DBus.Test"); - dbus_connection_send_message (connection, - reply, - NULL, - NULL); - dbus_message_unref (reply); - - dbus_message_unref (message); - - count += 1; - if (count > 100) - { - printf ("Saw %d messages, exiting\n", count); - quit_mainloop (); - } + name = dbus_message_get_name (message); + if (name && strcmp (name, DBUS_MESSAGE_LOCAL_DISCONNECT) == 0) + { + disconnect (connection); + } + else + { + fprintf (stderr, "Received message %d, sending reply\n", count); + + reply = dbus_message_new ("org.freedesktop.DBus.Test", "org.freedesktop.DBus.Test"); + dbus_connection_send_message (connection, + reply, + NULL, + NULL); + dbus_message_unref (reply); + + dbus_message_unref (message); + + count += 1; + if (count > 100) + { + printf ("Saw %d messages, exiting\n", count); + quit_mainloop (); + } + } } link = next; @@ -185,6 +205,9 @@ do_mainloop (void) int initial_watch_serial; check_messages (); + + if (exited) + break; FD_ZERO (&read_set); FD_ZERO (&write_set); @@ -297,16 +320,6 @@ quit_mainloop (void) exited = TRUE; } -static void -disconnect_handler (DBusConnection *connection, - void *data) -{ - fprintf (stderr, "Disconnected\n"); - - _dbus_list_remove (&connections, connection); - dbus_connection_unref (connection); - quit_mainloop (); -} void setup_connection (DBusConnection *connection) @@ -317,10 +330,6 @@ setup_connection (DBusConnection *connection) connection, NULL); - dbus_connection_set_disconnect_function (connection, - disconnect_handler, - NULL, NULL); - dbus_connection_ref (connection); _dbus_list_append (&connections, connection); } -- cgit