2 * Test to make sure late thread initialization works
7 #include <dbus/dbus-sysdeps.h>
11 #include <dbus/dbus-internals.h>
12 #include <dbus/dbus-connection-internal.h>
15 _run_iteration (DBusConnection *conn)
17 DBusPendingCall *echo_pending;
18 DBusPendingCall *dbus_pending;
23 /* send the first message */
24 method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
25 "/org/freedesktop/TestSuite",
26 "org.freedesktop.TestSuite",
29 dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL);
30 dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
31 dbus_message_unref (method);
33 /* send the second message */
34 method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
36 "org.freedesktop.Introspectable",
39 dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
40 dbus_message_unref (method);
42 /* block on the second message (should return immediately) */
43 dbus_pending_call_block (dbus_pending);
45 /* block on the first message */
46 /* if it does not return immediately chances
47 are we hit the block in poll bug */
48 dbus_pending_call_block (echo_pending);
50 /* check the reply only to make sure we
51 are not getting errors unrelated
52 to the block in poll bug */
53 reply = dbus_pending_call_steal_reply (echo_pending);
57 printf ("Failed: Reply is NULL ***\n");
61 if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
63 printf ("Failed: Reply is error: %s ***\n", dbus_message_get_error_name (reply));
67 dbus_message_unref (reply);
68 dbus_pending_call_unref (dbus_pending);
69 dbus_pending_call_unref (echo_pending);
73 check_mutex_lock (DBusMutex *mutex1,
77 _dbus_assert (mutex1 != NULL);
78 _dbus_assert (mutex2 != NULL);
82 _dbus_assert (mutex1 == mutex2);
86 _dbus_assert (mutex1 != mutex2);
91 check_condvar_lock (DBusCondVar *condvar1,
92 DBusCondVar *condvar2,
95 _dbus_assert (condvar1 != NULL);
96 _dbus_assert (condvar2 != NULL);
100 _dbus_assert (condvar1 == condvar2);
104 _dbus_assert (condvar1 != condvar2);
110 main (int argc, char *argv[])
113 DBusConnection *conn;
115 DBusMutex *mutex1, *dispatch_mutex1, *io_path_mutex1;
116 DBusCondVar *dispatch_cond1, *io_path_cond1;
117 DBusMutex *mutex2, *dispatch_mutex2, *io_path_mutex2;
118 DBusCondVar *dispatch_cond2, *io_path_cond2;
120 printf ("*** Testing late thread init\n");
122 dbus_error_init (&error);
124 conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
126 _dbus_connection_test_get_locks (conn, &mutex1,
131 _run_iteration (conn);
132 _dbus_connection_test_get_locks (conn, &mutex2,
138 check_mutex_lock (mutex1, mutex2, TRUE);
139 check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
140 check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
141 check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
142 check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
144 dbus_threads_init_default ();
146 _dbus_connection_test_get_locks (conn, &mutex1,
152 /* Since 1.7 it is no longer the case that mutex1 != mutex2, because
153 * initializing global locks automatically initializes locks
154 * in general. However, it is true that the mutex is not the dummy
155 * implementation, which is what we really wanted to check here. */
156 _dbus_assert (mutex1 != (DBusMutex *) 0xABCDEF);
157 _dbus_assert (dispatch_mutex1 != (DBusMutex *) 0xABCDEF);
158 _dbus_assert (dispatch_cond1 != (DBusCondVar *) 0xABCDEF2);
159 _dbus_assert (io_path_mutex1 != (DBusMutex *) 0xABCDEF);
160 _dbus_assert (io_path_cond1 != (DBusCondVar *) 0xABCDEF2);
162 _run_iteration (conn);
163 _dbus_connection_test_get_locks (conn, &mutex2,
169 check_mutex_lock (mutex1, mutex2, TRUE);
170 check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
171 check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
172 check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
173 check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
175 method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
176 "/org/freedesktop/TestSuite",
177 "org.freedesktop.TestSuite",
179 dbus_connection_send (conn, method, NULL);
180 dbus_message_unref (method);
182 printf ("Success ***\n");