e6743cce40eb3b8a1772e930427e67db1f92adaf
[platform/upstream/dbus.git] / test / name-test / test-threads-init.c
1 /**
2 * Test to make sure late thread initialization works
3 **/
4
5 #include <dbus/dbus.h>
6 #include <dbus/dbus-sysdeps.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <dbus/dbus-internals.h>
11
12 static void
13 _run_iteration (DBusConnection *conn)
14 {
15   DBusPendingCall *echo_pending;
16   DBusPendingCall *dbus_pending;
17   DBusMessage *method;
18   DBusMessage *reply;
19   char *echo = "echo";
20
21   /* send the first message */
22   method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
23                                          "/org/freedesktop/TestSuite",
24                                          "org.freedesktop.TestSuite",
25                                          "Echo");
26
27   dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL);
28   dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
29   dbus_message_unref (method);
30   
31   /* send the second message */
32   method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
33                                          DBUS_PATH_DBUS,
34                                          "org.freedesktop.Introspectable",
35                                          "Introspect");
36
37   dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
38   dbus_message_unref (method);
39
40   /* block on the second message (should return immediately) */
41   dbus_pending_call_block (dbus_pending);
42
43   /* block on the first message */
44   /* if it does not return immediately chances 
45      are we hit the block in poll bug */
46   dbus_pending_call_block (echo_pending);
47
48   /* check the reply only to make sure we
49      are not getting errors unrelated
50      to the block in poll bug */
51   reply = dbus_pending_call_steal_reply (echo_pending);
52
53   if (reply == NULL)
54     {
55       printf ("Failed: Reply is NULL ***\n");
56       exit (1);
57     }
58
59   if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
60     {
61       printf ("Failed: Reply is error: %s ***\n", dbus_message_get_error_name (reply));
62       exit (1);
63     } 
64
65   dbus_message_unref (reply);
66   dbus_pending_call_unref (dbus_pending);
67   dbus_pending_call_unref (echo_pending);
68   
69 }
70 static void
71 check_mutex_lock (DBusMutex *mutex1, 
72                   DBusMutex *mutex2, 
73                   dbus_bool_t is_same)
74 {
75   _dbus_assert (mutex1 != NULL);
76   _dbus_assert (mutex2 != NULL);
77   
78   if (is_same)
79     {
80       _dbus_assert (mutex1 == mutex2);
81     }
82   else
83     {
84       _dbus_assert (mutex1 != mutex2);
85     }
86 }
87
88 static void
89 check_condvar_lock (DBusCondVar *condvar1,  
90                     DBusCondVar *condvar2,   
91                     dbus_bool_t is_same)
92 {
93   _dbus_assert (condvar1 != NULL);
94   _dbus_assert (condvar2 != NULL);
95
96   if (is_same)
97     {
98       _dbus_assert (condvar1 == condvar2);
99     }
100   else
101     {
102       _dbus_assert (condvar1 != condvar2);
103     }
104 }
105
106
107 int
108 main (int argc, char *argv[])
109 {
110   long start_tv_sec, start_tv_usec;
111   long end_tv_sec, end_tv_usec;
112   int i;
113   DBusMessage *method;
114   DBusConnection *conn;
115   DBusError error;
116   DBusMutex *mutex1, *dispatch_mutex1, *io_path_mutex1;
117   DBusCondVar *dispatch_cond1, *io_path_cond1;
118   DBusMutex *mutex2, *dispatch_mutex2, *io_path_mutex2;
119   DBusCondVar *dispatch_cond2, *io_path_cond2;
120
121   printf ("*** Testing late thread init\n");
122
123   dbus_error_init (&error);
124
125   conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
126
127   _dbus_connection_test_get_locks (conn, &mutex1, 
128                                          &dispatch_mutex1, 
129                                          &io_path_mutex1,
130                                          &dispatch_cond1,
131                                          &io_path_cond1);
132   _run_iteration (conn);
133   _dbus_connection_test_get_locks (conn, &mutex2,
134                                          &dispatch_mutex2,
135                                          &io_path_mutex2,
136                                          &dispatch_cond2,
137                                          &io_path_cond2);
138
139   check_mutex_lock (mutex1, mutex2, TRUE);
140   check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
141   check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
142   check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
143   check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
144
145   _dbus_threads_init_debug ();
146
147   _dbus_connection_test_get_locks (conn, &mutex1,
148                                          &dispatch_mutex1,
149                                          &io_path_mutex1,
150                                          &dispatch_cond1,
151                                          &io_path_cond1);
152
153   check_mutex_lock (mutex1, mutex2, FALSE);
154   check_mutex_lock (dispatch_mutex1, dispatch_mutex2, FALSE);
155   check_mutex_lock (io_path_mutex1, io_path_mutex2, FALSE);
156   check_condvar_lock (dispatch_cond1, dispatch_cond2, FALSE);
157   check_condvar_lock (io_path_cond1, io_path_cond2, FALSE);
158
159   _run_iteration (conn);
160   _dbus_connection_test_get_locks (conn, &mutex2,
161                                          &dispatch_mutex2,
162                                          &io_path_mutex2,
163                                          &dispatch_cond2,
164                                          &io_path_cond2);
165
166   check_mutex_lock (mutex1, mutex2, TRUE);
167   check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
168   check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
169   check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
170   check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
171
172   method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService",
173                                          "/org/freedesktop/TestSuite",
174                                          "org.freedesktop.TestSuite",
175                                          "Exit");
176   dbus_connection_send (conn, method, NULL);
177   dbus_message_unref (method);
178
179   printf ("Success ***\n");
180   exit (0);
181 }