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