dbus/test/name-test/test-threads-init.c
Havoc Pennington a2129f7ccc 2006-10-01 Havoc Pennington <hp@redhat.com>
* dbus/dbus-connection.c (_dbus_connection_close_if_only_one_ref):
	Add a hack to make DBusNewConnectionFunction work right.

	* dbus/dbus-server-socket.c (handle_new_client_fd_and_unlock): use
	the hack here. Also, fix the todo about refcount leak.

	* dbus/dbus-server-debug-pipe.c (_dbus_transport_debug_pipe_new):
	and use the hack here

        * dbus/dbus-connection.c: Kill the "shared" flag vs. the
	"shareable" flag; this was completely broken, since it meant
	dbus_connection_open() returned a connection of unknown
	shared-ness. Now, we always hold a ref on anything opened
	as shareable.

	Move the call to notify dbus-bus.c into
	connection_forget_shared_unlocked, so libdbus consistently forgets
	all its knowledge of a connection at once. This exposed numerous
	places where things were totally broken if we dropped a ref inside
	get_dispatch_status_unlocked where
	connection_forget_shared_unlocked was previously, so move
	connection_forget_shared_unlocked into
	_dbus_connection_update_dispatch_status_and_unlock. Also move the
	exit_on_disconnect here.

	(shared_connections_shutdown): this assumed weak refs to the
	shared connections; since we have strong refs now, the assertion
	was failing and stuff was left in the hash. Fix it to close
	still-open shared connections.

	* bus/dispatch.c: fixup to use dbus_connection_open_private on the
	debug pipe connections

	* dbus/dbus-connection.c (dbus_connection_dispatch): only notify
	dbus-bus.c if the closed connection is in fact shared
	(_dbus_connection_close_possibly_shared): rename from
	_dbus_connection_close_internal
	(dbus_connection_close, dbus_connection_open,
	dbus_connection_open_private): Improve docs to explain the deal
	with when you should close or unref or both

	* dbus/dbus-bus.c
	(_dbus_bus_notify_shared_connection_disconnected_unlocked): rename
	from _dbus_bus_check_connection_and_unref_unlocked and modify to
	loop over all connections

	* test/test-utils.c (test_connection_shutdown): don't try to close
	shared connections.

	* test/name-test/test-threads-init.c (main): fix warnings in here

	* dbus/dbus-sysdeps.c (_dbus_abort): support DBUS_BLOCK_ON_ABORT
	env variable to cause blocking waiting for gdb; drop
	DBUS_PRINT_BACKTRACE and just call _dbus_print_backtrace()
	unconditionally.

	* configure.in: add -export-dynamic to libtool flags if assertions enabled
	so _dbus_print_backtrace works.

	* dbus/dbus-sysdeps-unix.c (_dbus_print_backtrace): use fprintf
	instead of _dbus_verbose to print the backtrace, and diagnose lack
	of -rdynamic/-export-dynamic
2006-10-01 15:36:19 +00:00

179 lines
5.6 KiB
C

/**
* Test to make sure late thread initialization works
*/
#include <dbus/dbus.h>
#include <dbus/dbus-sysdeps.h>
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus-internals.h>
#include <dbus/dbus-connection-internal.h>
static void
_run_iteration (DBusConnection *conn)
{
DBusPendingCall *echo_pending;
DBusPendingCall *dbus_pending;
DBusMessage *method;
DBusMessage *reply;
char *echo = "echo";
/* send the first message */
method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
"/org/freedesktop/TestSuite",
"org.freedesktop.TestSuite",
"Echo");
dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL);
dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
dbus_message_unref (method);
/* send the second message */
method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
DBUS_PATH_DBUS,
"org.freedesktop.Introspectable",
"Introspect");
dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
dbus_message_unref (method);
/* block on the second message (should return immediately) */
dbus_pending_call_block (dbus_pending);
/* block on the first message */
/* if it does not return immediately chances
are we hit the block in poll bug */
dbus_pending_call_block (echo_pending);
/* check the reply only to make sure we
are not getting errors unrelated
to the block in poll bug */
reply = dbus_pending_call_steal_reply (echo_pending);
if (reply == NULL)
{
printf ("Failed: Reply is NULL ***\n");
exit (1);
}
if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
{
printf ("Failed: Reply is error: %s ***\n", dbus_message_get_error_name (reply));
exit (1);
}
dbus_message_unref (reply);
dbus_pending_call_unref (dbus_pending);
dbus_pending_call_unref (echo_pending);
}
static void
check_mutex_lock (DBusMutex *mutex1,
DBusMutex *mutex2,
dbus_bool_t is_same)
{
_dbus_assert (mutex1 != NULL);
_dbus_assert (mutex2 != NULL);
if (is_same)
{
_dbus_assert (mutex1 == mutex2);
}
else
{
_dbus_assert (mutex1 != mutex2);
}
}
static void
check_condvar_lock (DBusCondVar *condvar1,
DBusCondVar *condvar2,
dbus_bool_t is_same)
{
_dbus_assert (condvar1 != NULL);
_dbus_assert (condvar2 != NULL);
if (is_same)
{
_dbus_assert (condvar1 == condvar2);
}
else
{
_dbus_assert (condvar1 != condvar2);
}
}
int
main (int argc, char *argv[])
{
DBusMessage *method;
DBusConnection *conn;
DBusError error;
DBusMutex *mutex1, *dispatch_mutex1, *io_path_mutex1;
DBusCondVar *dispatch_cond1, *io_path_cond1;
DBusMutex *mutex2, *dispatch_mutex2, *io_path_mutex2;
DBusCondVar *dispatch_cond2, *io_path_cond2;
printf ("*** Testing late thread init\n");
dbus_error_init (&error);
conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
_dbus_connection_test_get_locks (conn, &mutex1,
&dispatch_mutex1,
&io_path_mutex1,
&dispatch_cond1,
&io_path_cond1);
_run_iteration (conn);
_dbus_connection_test_get_locks (conn, &mutex2,
&dispatch_mutex2,
&io_path_mutex2,
&dispatch_cond2,
&io_path_cond2);
check_mutex_lock (mutex1, mutex2, TRUE);
check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
_dbus_threads_init_debug ();
_dbus_connection_test_get_locks (conn, &mutex1,
&dispatch_mutex1,
&io_path_mutex1,
&dispatch_cond1,
&io_path_cond1);
check_mutex_lock (mutex1, mutex2, FALSE);
check_mutex_lock (dispatch_mutex1, dispatch_mutex2, FALSE);
check_mutex_lock (io_path_mutex1, io_path_mutex2, FALSE);
check_condvar_lock (dispatch_cond1, dispatch_cond2, FALSE);
check_condvar_lock (io_path_cond1, io_path_cond2, FALSE);
_run_iteration (conn);
_dbus_connection_test_get_locks (conn, &mutex2,
&dispatch_mutex2,
&io_path_mutex2,
&dispatch_cond2,
&io_path_cond2);
check_mutex_lock (mutex1, mutex2, TRUE);
check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
"/org/freedesktop/TestSuite",
"org.freedesktop.TestSuite",
"Exit");
dbus_connection_send (conn, method, NULL);
dbus_message_unref (method);
printf ("Success ***\n");
exit (0);
}