summaryrefslogtreecommitdiff
path: root/test/name-test/test-threads-init.c
blob: 5e22852acbd2e8e0589772064cdc8273c2f288d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * Test to make sure late thread initialization works
 */

#include <config.h>
#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_default ();

  _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);
}