Merge branch 'dbus-1.4'
[platform/upstream/dbus.git] / test / internals / refs.c
1 /* Regression test for thread-safe reference-counting
2  *
3  * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4  * Copyright © 2011 Nokia Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction,
9  * including without limitation the rights to use, copy, modify, merge,
10  * publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26
27 #include <config.h>
28
29 #include <glib.h>
30
31 #define DBUS_COMPILATION    /* this test uses libdbus-internal */
32 #include <dbus/dbus.h>
33 #include <dbus/dbus-connection-internal.h>
34 #include <dbus/dbus-mainloop.h>
35 #include <dbus/dbus-message-internal.h>
36 #include <dbus/dbus-pending-call-internal.h>
37 #include <dbus/dbus-server-protected.h>
38 #include "test-utils.h"
39
40 static void
41 assert_no_error (const DBusError *e)
42 {
43   if (G_UNLIKELY (dbus_error_is_set (e)))
44     g_error ("expected success but got error: %s: %s", e->name, e->message);
45 }
46
47 #define N_THREADS 200
48 #define N_REFS 10000
49 G_STATIC_ASSERT (((unsigned) N_THREADS * (unsigned) N_REFS) < G_MAXINT32);
50
51 static dbus_int32_t connection_slot = -1;
52 static dbus_int32_t server_slot = -1;
53 static dbus_int32_t message_slot = -1;
54 static dbus_int32_t pending_call_slot = -1;
55
56 typedef struct {
57   DBusError e;
58   DBusLoop *loop;
59   DBusServer *server;
60   DBusConnection *connection;
61   DBusConnection *server_connection;
62   DBusMessage *message;
63   GThread *threads[N_THREADS];
64   gboolean last_unref;
65 } Fixture;
66
67 typedef void *(*RefFunc) (void *);
68 typedef void (*VoidFunc) (void *);
69
70 typedef struct {
71   void *thing;
72   RefFunc ref;
73   VoidFunc ref_void;
74   VoidFunc unref;
75   void *mutex;
76   VoidFunc lock;
77   VoidFunc unlock;
78 } Thread;
79
80 static gpointer
81 ref_thread (gpointer data)
82 {
83   Thread *thread = data;
84   int i;
85
86   for (i = 0; i < N_REFS; i++)
87     {
88       if (thread->lock != NULL)
89         (thread->lock) (thread->mutex);
90
91       if (thread->ref != NULL)
92         {
93           gpointer ret = (thread->ref) (thread->thing);
94
95           g_assert (ret == thread->thing);
96         }
97       else
98         {
99           (thread->ref_void) (thread->thing);
100         }
101
102       if (thread->unlock != NULL)
103         (thread->unlock) (thread->mutex);
104     }
105
106   return NULL;
107 }
108
109 static gpointer
110 cycle_thread (gpointer data)
111 {
112   Thread *thread = data;
113   int i;
114
115   for (i = 0; i < N_REFS; i++)
116     {
117       if (thread->lock != NULL)
118         (thread->lock) (thread->mutex);
119
120       if (thread->ref != NULL)
121         {
122           gpointer ret = (thread->ref) (thread->thing);
123
124           g_assert (ret == thread->thing);
125         }
126       else
127         {
128           (thread->ref_void) (thread->thing);
129         }
130
131       (thread->unref) (thread->thing);
132
133       if (thread->unlock != NULL)
134         (thread->unlock) (thread->mutex);
135     }
136
137   return NULL;
138 }
139
140 static gpointer
141 unref_thread (gpointer data)
142 {
143   Thread *thread = data;
144   int i;
145
146   for (i = 0; i < N_REFS; i++)
147     {
148       if (thread->lock != NULL)
149         (thread->lock) (thread->mutex);
150
151       (thread->unref) (thread->thing);
152
153       if (thread->unlock != NULL)
154         (thread->unlock) (thread->mutex);
155     }
156
157   return NULL;
158 }
159
160 static void
161 last_unref (void *data)
162 {
163   Fixture *f = data;
164
165   g_assert (!f->last_unref);
166   f->last_unref = TRUE;
167 }
168
169 static void
170 wait_for_all_threads (Fixture *f)
171 {
172   int i;
173
174   for (i = 0; i < N_THREADS; i++)
175     g_thread_join (f->threads[i]);
176 }
177
178 static void
179 new_conn_cb (DBusServer *server,
180     DBusConnection *server_connection,
181     void *data)
182 {
183   Fixture *f = data;
184
185   g_assert (f->server_connection == NULL);
186   f->server_connection = dbus_connection_ref (server_connection);
187
188   test_connection_setup (f->loop, f->server_connection);
189 }
190
191 static void
192 setup (Fixture *f,
193     gconstpointer data)
194 {
195   if (!dbus_threads_init_default ())
196     g_error ("OOM");
197
198   f->loop = _dbus_loop_new ();
199   g_assert (f->loop != NULL);
200
201   dbus_error_init (&f->e);
202
203   f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e);
204   assert_no_error (&f->e);
205   g_assert (f->server != NULL);
206
207   if (!dbus_connection_allocate_data_slot (&connection_slot))
208     g_error ("OOM");
209
210   if (!dbus_server_allocate_data_slot (&server_slot))
211     g_error ("OOM");
212
213   if (!dbus_message_allocate_data_slot (&message_slot))
214     g_error ("OOM");
215
216   if (!dbus_pending_call_allocate_data_slot (&pending_call_slot))
217     g_error ("OOM");
218 }
219
220 static void
221 setup_connection (Fixture *f,
222     gconstpointer data)
223 {
224   char *address;
225
226   setup (f, data);
227
228   dbus_server_set_new_connection_function (f->server,
229       new_conn_cb, f, NULL);
230
231   if (!test_server_setup (f->loop, f->server))
232     g_error ("failed to set up server");
233
234   address = dbus_server_get_address (f->server);
235   g_assert (address != NULL);
236   f->connection = dbus_connection_open_private (address, &f->e);
237   assert_no_error (&f->e);
238   g_assert (f->connection != NULL);
239   dbus_free (address);
240
241   if (!test_connection_setup (f->loop, f->connection))
242     g_error ("failed to set up connection");
243
244   while (f->server_connection == NULL)
245     _dbus_loop_iterate (f->loop, TRUE);
246
247   test_connection_shutdown (f->loop, f->connection);
248   test_server_shutdown (f->loop, f->server);
249 }
250
251 static void
252 test_connection (Fixture *f,
253     gconstpointer data)
254 {
255   Thread public_api = { f->connection,
256     (RefFunc) dbus_connection_ref,
257     NULL,
258     (VoidFunc) dbus_connection_unref,
259     NULL,
260     NULL,
261     NULL };
262   Thread internal_api = { f->connection,
263     (RefFunc) _dbus_connection_ref_unlocked,
264     NULL,
265     (VoidFunc) _dbus_connection_unref_unlocked,
266     f->connection,
267     (VoidFunc) _dbus_connection_lock,
268     (VoidFunc) _dbus_connection_unlock };
269   int i;
270
271   /* Use a slot as a pseudo-weakref */
272   if (!dbus_connection_set_data (f->connection, connection_slot, f,
273         last_unref))
274     g_error ("OOM");
275
276   for (i = 0; i < N_THREADS; i++)
277     {
278       if ((i % 2) == 0)
279         f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
280       else
281         f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
282             NULL);
283
284       g_assert (f->threads[i] != NULL);
285     }
286
287   wait_for_all_threads (f);
288
289   for (i = 0; i < N_THREADS; i++)
290     {
291       if ((i % 2) == 0)
292         f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
293             NULL);
294       else
295         f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
296             NULL);
297
298       g_assert (f->threads[i] != NULL);
299     }
300
301   wait_for_all_threads (f);
302
303   for (i = 0; i < N_THREADS; i++)
304     {
305       if ((i % 2) == 0)
306         f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
307             NULL);
308       else
309         f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
310             NULL);
311
312       g_assert (f->threads[i] != NULL);
313     }
314
315   wait_for_all_threads (f);
316
317   /* Destroy the connection. This should be the last-unref. */
318   g_assert (!f->last_unref);
319   dbus_connection_close (f->connection);
320   dbus_connection_unref (f->connection);
321   f->connection = NULL;
322   g_assert (f->last_unref);
323 }
324
325 static void
326 server_lock (void *server)
327 {
328   SERVER_LOCK (((DBusServer *) server));
329 }
330
331 static void
332 server_unlock (void *server)
333 {
334   SERVER_UNLOCK (((DBusServer *) server));
335 }
336
337 static void
338 test_server (Fixture *f,
339     gconstpointer data)
340 {
341   Thread public_api = { f->server,
342     (RefFunc) dbus_server_ref,
343     NULL,
344     (VoidFunc) dbus_server_unref,
345     NULL,
346     NULL,
347     NULL };
348   Thread internal_api = { f->server,
349     NULL,
350     (VoidFunc) _dbus_server_ref_unlocked,
351     (VoidFunc) _dbus_server_unref_unlocked,
352     f->server,
353     server_lock,
354     server_unlock };
355   int i;
356
357   if (!dbus_server_set_data (f->server, server_slot, f, last_unref))
358     g_error ("OOM");
359
360   for (i = 0; i < N_THREADS; i++)
361     {
362       if ((i % 2) == 0)
363         f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
364       else
365         f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
366             NULL);
367
368       g_assert (f->threads[i] != NULL);
369     }
370
371   wait_for_all_threads (f);
372
373   for (i = 0; i < N_THREADS; i++)
374     {
375       if ((i % 2) == 0)
376         f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
377             NULL);
378       else
379         f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
380             NULL);
381
382       g_assert (f->threads[i] != NULL);
383     }
384
385   wait_for_all_threads (f);
386
387   for (i = 0; i < N_THREADS; i++)
388     {
389       if ((i % 2) == 0)
390         f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
391             NULL);
392       else
393         f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
394             NULL);
395
396       g_assert (f->threads[i] != NULL);
397     }
398
399   wait_for_all_threads (f);
400
401   /* Destroy the server. This should be the last-unref. */
402   g_assert (!f->last_unref);
403   dbus_server_disconnect (f->server);
404   dbus_server_unref (f->server);
405   f->server = NULL;
406   g_assert (f->last_unref);
407 }
408
409 static void
410 test_message (Fixture *f,
411     gconstpointer data)
412 {
413   DBusMessage *message = dbus_message_new_signal ("/foo", "foo.bar.baz",
414       "Foo");
415   Thread public_api = { message,
416     (RefFunc) dbus_message_ref,
417     NULL,
418     (VoidFunc) dbus_message_unref,
419     NULL,
420     NULL,
421     NULL };
422   int i;
423
424   if (!dbus_message_set_data (message, message_slot, f, last_unref))
425     g_error ("OOM");
426
427   for (i = 0; i < N_THREADS; i++)
428     {
429       f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
430       g_assert (f->threads[i] != NULL);
431     }
432
433   wait_for_all_threads (f);
434
435   for (i = 0; i < N_THREADS; i++)
436     {
437       f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE, NULL);
438       g_assert (f->threads[i] != NULL);
439     }
440
441   wait_for_all_threads (f);
442
443   for (i = 0; i < N_THREADS; i++)
444     {
445       f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE, NULL);
446       g_assert (f->threads[i] != NULL);
447     }
448
449   wait_for_all_threads (f);
450
451   /* Destroy the server. This should be the last-unref. */
452   g_assert (!f->last_unref);
453   dbus_message_unref (message);
454   g_assert (f->last_unref);
455 }
456
457 static void
458 test_pending_call (Fixture *f,
459     gconstpointer data)
460 {
461   Thread public_api = { NULL,
462     (RefFunc) dbus_pending_call_ref,
463     NULL,
464     (VoidFunc) dbus_pending_call_unref,
465     NULL,
466     NULL,
467     NULL };
468   Thread internal_api = { NULL,
469     (RefFunc) _dbus_pending_call_ref_unlocked,
470     NULL,
471     (VoidFunc) dbus_pending_call_unref,
472     f->connection,
473     (VoidFunc) _dbus_connection_lock,
474     (VoidFunc) _dbus_connection_unlock };
475   /* This one can't be used to ref, only to cycle or unref. */
476   Thread unref_and_unlock_api = { NULL,
477     (RefFunc) _dbus_pending_call_ref_unlocked,
478     NULL,
479     (VoidFunc) _dbus_pending_call_unref_and_unlock,
480     f->connection,
481     (VoidFunc) _dbus_connection_lock,
482     NULL };
483   int i;
484   DBusPendingCall *pending_call;
485
486   _dbus_connection_lock (f->connection);
487   pending_call = _dbus_pending_call_new_unlocked (f->connection,
488       DBUS_TIMEOUT_INFINITE, NULL);
489   g_assert (pending_call != NULL);
490   _dbus_connection_unlock (f->connection);
491
492   public_api.thing = pending_call;
493   internal_api.thing = pending_call;
494   unref_and_unlock_api.thing = pending_call;
495
496   if (!dbus_pending_call_set_data (pending_call, pending_call_slot, f,
497         last_unref))
498     g_error ("OOM");
499
500   for (i = 0; i < N_THREADS; i++)
501     {
502       if ((i % 2) == 0)
503         f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
504       else
505         f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
506             NULL);
507
508       g_assert (f->threads[i] != NULL);
509     }
510
511   wait_for_all_threads (f);
512
513   for (i = 0; i < N_THREADS; i++)
514     {
515       switch (i % 3)
516         {
517           case 0:
518             f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
519                 NULL);
520             break;
521           case 1:
522             f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
523                 NULL);
524             break;
525           default:
526             f->threads[i] = g_thread_create (cycle_thread,
527                 &unref_and_unlock_api, TRUE, NULL);
528         }
529
530       g_assert (f->threads[i] != NULL);
531     }
532
533   wait_for_all_threads (f);
534
535   for (i = 0; i < N_THREADS; i++)
536     {
537       switch (i % 3)
538         {
539           case 0:
540             f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
541                 NULL);
542             break;
543           case 1:
544             f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
545                 NULL);
546             break;
547           default:
548             f->threads[i] = g_thread_create (unref_thread,
549                 &unref_and_unlock_api, TRUE, NULL);
550         }
551
552       g_assert (f->threads[i] != NULL);
553     }
554
555   wait_for_all_threads (f);
556
557   /* Destroy the pending call. This should be the last-unref. */
558   g_assert (!f->last_unref);
559   dbus_pending_call_unref (pending_call);
560   g_assert (f->last_unref);
561 }
562
563 static void
564 teardown (Fixture *f,
565     gconstpointer data)
566 {
567   if (f->server_connection != NULL)
568     {
569       dbus_connection_close (f->server_connection);
570       dbus_connection_unref (f->server_connection);
571     }
572
573   if (f->connection != NULL)
574     {
575       dbus_connection_close (f->connection);
576       dbus_connection_unref (f->connection);
577     }
578
579   if (f->server != NULL)
580     {
581       dbus_server_disconnect (f->server);
582       dbus_server_unref (f->server);
583     }
584
585   dbus_connection_free_data_slot (&connection_slot);
586   dbus_server_free_data_slot (&server_slot);
587   dbus_message_free_data_slot (&message_slot);
588   dbus_pending_call_free_data_slot (&pending_call_slot);
589
590   _dbus_loop_unref (f->loop);
591   dbus_error_free (&f->e);
592 }
593
594 int
595 main (int argc,
596     char **argv)
597 {
598   g_thread_init (NULL);
599   g_test_init (&argc, &argv, NULL);
600   g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
601
602   g_test_add ("/refs/connection", Fixture, NULL, setup_connection,
603       test_connection, teardown);
604   g_test_add ("/refs/message", Fixture, NULL, setup,
605       test_message, teardown);
606   g_test_add ("/refs/pending-call", Fixture, NULL, setup_connection,
607       test_pending_call, teardown);
608   g_test_add ("/refs/server", Fixture, NULL, setup,
609       test_server, teardown);
610
611   return g_test_run ();
612 }