1 /* Regression test for thread-safe reference-counting
3 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4 * Copyright © 2011 Nokia Corporation
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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
30 #include <glib-object.h>
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"
42 assert_no_error (const DBusError *e)
44 if (G_UNLIKELY (dbus_error_is_set (e)))
45 g_error ("expected success but got error: %s: %s", e->name, e->message);
50 G_STATIC_ASSERT (((unsigned) N_THREADS * (unsigned) N_REFS) < G_MAXINT32);
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;
61 DBusConnection *connection;
62 DBusConnection *server_connection;
64 GThread *threads[N_THREADS];
68 typedef void *(*RefFunc) (void *);
69 typedef void (*VoidFunc) (void *);
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)
87 ref_thread (gpointer data)
89 Thread *thread = data;
92 for (i = 0; i < N_REFS; i++)
94 if (thread->lock != NULL)
95 (thread->lock) (thread->mutex);
97 if (thread->ref != NULL)
99 gpointer ret = (thread->ref) (thread->thing);
101 g_assert (ret == thread->thing);
105 (thread->ref_void) (thread->thing);
108 if (thread->unlock != NULL)
109 (thread->unlock) (thread->mutex);
116 cycle_thread (gpointer data)
118 Thread *thread = data;
121 for (i = 0; i < N_REFS; i++)
123 if (thread->lock != NULL)
124 (thread->lock) (thread->mutex);
126 if (thread->ref != NULL)
128 gpointer ret = (thread->ref) (thread->thing);
130 g_assert (ret == thread->thing);
134 (thread->ref_void) (thread->thing);
137 (thread->unref) (thread->thing);
139 if (thread->unlock != NULL)
140 (thread->unlock) (thread->mutex);
147 unref_thread (gpointer data)
149 Thread *thread = data;
152 for (i = 0; i < N_REFS; i++)
154 if (thread->lock != NULL)
155 (thread->lock) (thread->mutex);
157 (thread->unref) (thread->thing);
159 if (thread->unlock != NULL)
160 (thread->unlock) (thread->mutex);
167 last_unref (void *data)
171 g_assert (!f->last_unref);
172 f->last_unref = TRUE;
176 wait_for_all_threads (Fixture *f)
180 for (i = 0; i < N_THREADS; i++)
181 g_thread_join (f->threads[i]);
185 new_conn_cb (DBusServer *server,
186 DBusConnection *server_connection,
190 dbus_bool_t have_mem;
192 g_assert (f->server_connection == NULL);
193 f->server_connection = dbus_connection_ref (server_connection);
195 test_connection_setup (f->loop, f->server_connection);
202 if (!dbus_threads_init_default ())
205 f->loop = _dbus_loop_new ();
206 g_assert (f->loop != NULL);
208 dbus_error_init (&f->e);
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);
214 if (!dbus_connection_allocate_data_slot (&connection_slot))
217 if (!dbus_server_allocate_data_slot (&server_slot))
220 if (!dbus_message_allocate_data_slot (&message_slot))
223 if (!dbus_pending_call_allocate_data_slot (&pending_call_slot))
228 setup_connection (Fixture *f,
235 dbus_server_set_new_connection_function (f->server,
236 new_conn_cb, f, NULL);
238 if (!test_server_setup (f->loop, f->server))
239 g_error ("failed to set up server");
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);
248 if (!test_connection_setup (f->loop, f->connection))
249 g_error ("failed to set up connection");
251 while (f->server_connection == NULL)
252 _dbus_loop_iterate (f->loop, TRUE);
254 test_connection_shutdown (f->loop, f->connection);
255 test_server_shutdown (f->loop, f->server);
259 test_connection (Fixture *f,
262 Thread public_api = { f->connection,
263 (RefFunc) dbus_connection_ref,
265 (VoidFunc) dbus_connection_unref,
269 Thread internal_api = { f->connection,
270 (RefFunc) _dbus_connection_ref_unlocked,
272 (VoidFunc) _dbus_connection_unref_unlocked,
274 (VoidFunc) _dbus_connection_lock,
275 (VoidFunc) _dbus_connection_unlock };
278 /* Use a slot as a pseudo-weakref */
279 if (!dbus_connection_set_data (f->connection, connection_slot, f,
283 for (i = 0; i < N_THREADS; i++)
286 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
288 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
290 g_assert (f->threads[i] != NULL);
293 wait_for_all_threads (f);
295 for (i = 0; i < N_THREADS; i++)
298 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
300 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
302 g_assert (f->threads[i] != NULL);
305 wait_for_all_threads (f);
307 for (i = 0; i < N_THREADS; i++)
310 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
312 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
314 g_assert (f->threads[i] != NULL);
317 wait_for_all_threads (f);
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);
328 server_lock (void *server)
330 SERVER_LOCK (((DBusServer *) server));
334 server_unlock (void *server)
336 SERVER_UNLOCK (((DBusServer *) server));
340 test_server (Fixture *f,
343 Thread public_api = { f->server,
344 (RefFunc) dbus_server_ref,
346 (VoidFunc) dbus_server_unref,
350 Thread internal_api = { f->server,
352 (VoidFunc) _dbus_server_ref_unlocked,
353 (VoidFunc) _dbus_server_unref_unlocked,
359 if (!dbus_server_set_data (f->server, server_slot, f, last_unref))
362 for (i = 0; i < N_THREADS; i++)
365 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
367 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
369 g_assert (f->threads[i] != NULL);
372 wait_for_all_threads (f);
374 for (i = 0; i < N_THREADS; i++)
377 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
379 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
381 g_assert (f->threads[i] != NULL);
384 wait_for_all_threads (f);
386 for (i = 0; i < N_THREADS; i++)
389 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
391 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
393 g_assert (f->threads[i] != NULL);
396 wait_for_all_threads (f);
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);
403 g_assert (f->last_unref);
407 test_message (Fixture *f,
410 DBusMessage *message = dbus_message_new_signal ("/foo", "foo.bar.baz",
412 Thread public_api = { message,
413 (RefFunc) dbus_message_ref,
415 (VoidFunc) dbus_message_unref,
421 if (!dbus_message_set_data (message, message_slot, f, last_unref))
424 for (i = 0; i < N_THREADS; i++)
426 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
427 g_assert (f->threads[i] != NULL);
430 wait_for_all_threads (f);
432 for (i = 0; i < N_THREADS; i++)
434 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
435 g_assert (f->threads[i] != NULL);
438 wait_for_all_threads (f);
440 for (i = 0; i < N_THREADS; i++)
442 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
443 g_assert (f->threads[i] != NULL);
446 wait_for_all_threads (f);
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);
455 test_pending_call (Fixture *f,
458 Thread public_api = { NULL,
459 (RefFunc) dbus_pending_call_ref,
461 (VoidFunc) dbus_pending_call_unref,
465 Thread internal_api = { NULL,
466 (RefFunc) _dbus_pending_call_ref_unlocked,
468 (VoidFunc) dbus_pending_call_unref,
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,
476 (VoidFunc) _dbus_pending_call_unref_and_unlock,
478 (VoidFunc) _dbus_connection_lock,
481 DBusPendingCall *pending_call;
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);
489 public_api.thing = pending_call;
490 internal_api.thing = pending_call;
491 unref_and_unlock_api.thing = pending_call;
493 if (!dbus_pending_call_set_data (pending_call, pending_call_slot, f,
497 for (i = 0; i < N_THREADS; i++)
500 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
502 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
504 g_assert (f->threads[i] != NULL);
507 wait_for_all_threads (f);
509 for (i = 0; i < N_THREADS; i++)
514 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
517 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
520 f->threads[i] = g_thread_new (NULL, cycle_thread,
521 &unref_and_unlock_api);
524 g_assert (f->threads[i] != NULL);
527 wait_for_all_threads (f);
529 for (i = 0; i < N_THREADS; i++)
534 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
537 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
540 f->threads[i] = g_thread_new (NULL, unref_thread,
541 &unref_and_unlock_api);
544 g_assert (f->threads[i] != NULL);
547 wait_for_all_threads (f);
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);
556 teardown (Fixture *f,
559 if (f->server_connection != NULL)
561 dbus_connection_close (f->server_connection);
562 dbus_connection_unref (f->server_connection);
565 if (f->connection != NULL)
567 dbus_connection_close (f->connection);
568 dbus_connection_unref (f->connection);
571 if (f->server != NULL)
573 dbus_server_disconnect (f->server);
574 dbus_server_unref (f->server);
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);
582 _dbus_loop_unref (f->loop);
583 dbus_error_free (&f->e);
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
596 g_test_init (&argc, &argv, NULL);
597 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
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);
608 return g_test_run ();