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 #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"
41 assert_no_error (const DBusError *e)
43 if (G_UNLIKELY (dbus_error_is_set (e)))
44 g_error ("expected success but got error: %s: %s", e->name, e->message);
49 G_STATIC_ASSERT (((unsigned) N_THREADS * (unsigned) N_REFS) < G_MAXINT32);
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;
60 DBusConnection *connection;
61 DBusConnection *server_connection;
63 GThread *threads[N_THREADS];
67 typedef void *(*RefFunc) (void *);
68 typedef void (*VoidFunc) (void *);
80 /* provide backwards compatibility shim when building with a glib <= 2.30.x */
81 #if !GLIB_CHECK_VERSION(2,31,0)
82 #define g_thread_new(name,func,data) g_thread_create(func,data,TRUE,NULL)
86 ref_thread (gpointer data)
88 Thread *thread = data;
91 for (i = 0; i < N_REFS; i++)
93 if (thread->lock != NULL)
94 (thread->lock) (thread->mutex);
96 if (thread->ref != NULL)
98 gpointer ret = (thread->ref) (thread->thing);
100 g_assert (ret == thread->thing);
104 (thread->ref_void) (thread->thing);
107 if (thread->unlock != NULL)
108 (thread->unlock) (thread->mutex);
115 cycle_thread (gpointer data)
117 Thread *thread = data;
120 for (i = 0; i < N_REFS; i++)
122 if (thread->lock != NULL)
123 (thread->lock) (thread->mutex);
125 if (thread->ref != NULL)
127 gpointer ret = (thread->ref) (thread->thing);
129 g_assert (ret == thread->thing);
133 (thread->ref_void) (thread->thing);
136 (thread->unref) (thread->thing);
138 if (thread->unlock != NULL)
139 (thread->unlock) (thread->mutex);
146 unref_thread (gpointer data)
148 Thread *thread = data;
151 for (i = 0; i < N_REFS; i++)
153 if (thread->lock != NULL)
154 (thread->lock) (thread->mutex);
156 (thread->unref) (thread->thing);
158 if (thread->unlock != NULL)
159 (thread->unlock) (thread->mutex);
166 last_unref (void *data)
170 g_assert (!f->last_unref);
171 f->last_unref = TRUE;
175 wait_for_all_threads (Fixture *f)
179 for (i = 0; i < N_THREADS; i++)
180 g_thread_join (f->threads[i]);
184 new_conn_cb (DBusServer *server,
185 DBusConnection *server_connection,
190 g_assert (f->server_connection == NULL);
191 f->server_connection = dbus_connection_ref (server_connection);
193 test_connection_setup (f->loop, f->server_connection);
200 if (!dbus_threads_init_default ())
203 f->loop = _dbus_loop_new ();
204 g_assert (f->loop != NULL);
206 dbus_error_init (&f->e);
208 f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e);
209 assert_no_error (&f->e);
210 g_assert (f->server != NULL);
212 if (!dbus_connection_allocate_data_slot (&connection_slot))
215 if (!dbus_server_allocate_data_slot (&server_slot))
218 if (!dbus_message_allocate_data_slot (&message_slot))
221 if (!dbus_pending_call_allocate_data_slot (&pending_call_slot))
226 setup_connection (Fixture *f,
233 dbus_server_set_new_connection_function (f->server,
234 new_conn_cb, f, NULL);
236 if (!test_server_setup (f->loop, f->server))
237 g_error ("failed to set up server");
239 address = dbus_server_get_address (f->server);
240 g_assert (address != NULL);
241 f->connection = dbus_connection_open_private (address, &f->e);
242 assert_no_error (&f->e);
243 g_assert (f->connection != NULL);
246 if (!test_connection_setup (f->loop, f->connection))
247 g_error ("failed to set up connection");
249 while (f->server_connection == NULL)
250 _dbus_loop_iterate (f->loop, TRUE);
252 test_connection_shutdown (f->loop, f->connection);
253 test_server_shutdown (f->loop, f->server);
257 test_connection (Fixture *f,
260 Thread public_api = { f->connection,
261 (RefFunc) dbus_connection_ref,
263 (VoidFunc) dbus_connection_unref,
267 Thread internal_api = { f->connection,
268 (RefFunc) _dbus_connection_ref_unlocked,
270 (VoidFunc) _dbus_connection_unref_unlocked,
272 (VoidFunc) _dbus_connection_lock,
273 (VoidFunc) _dbus_connection_unlock };
276 /* Use a slot as a pseudo-weakref */
277 if (!dbus_connection_set_data (f->connection, connection_slot, f,
281 for (i = 0; i < N_THREADS; i++)
284 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
286 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
288 g_assert (f->threads[i] != NULL);
291 wait_for_all_threads (f);
293 for (i = 0; i < N_THREADS; i++)
296 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
298 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
300 g_assert (f->threads[i] != NULL);
303 wait_for_all_threads (f);
305 for (i = 0; i < N_THREADS; i++)
308 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
310 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
312 g_assert (f->threads[i] != NULL);
315 wait_for_all_threads (f);
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);
326 server_lock (void *server)
328 SERVER_LOCK (((DBusServer *) server));
332 server_unlock (void *server)
334 SERVER_UNLOCK (((DBusServer *) server));
338 test_server (Fixture *f,
341 Thread public_api = { f->server,
342 (RefFunc) dbus_server_ref,
344 (VoidFunc) dbus_server_unref,
348 Thread internal_api = { f->server,
350 (VoidFunc) _dbus_server_ref_unlocked,
351 (VoidFunc) _dbus_server_unref_unlocked,
357 if (!dbus_server_set_data (f->server, server_slot, f, last_unref))
360 for (i = 0; i < N_THREADS; i++)
363 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
365 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
367 g_assert (f->threads[i] != NULL);
370 wait_for_all_threads (f);
372 for (i = 0; i < N_THREADS; i++)
375 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
377 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
379 g_assert (f->threads[i] != NULL);
382 wait_for_all_threads (f);
384 for (i = 0; i < N_THREADS; i++)
387 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
389 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
391 g_assert (f->threads[i] != NULL);
394 wait_for_all_threads (f);
396 /* Destroy the server. This should be the last-unref. */
397 g_assert (!f->last_unref);
398 dbus_server_disconnect (f->server);
399 dbus_server_unref (f->server);
401 g_assert (f->last_unref);
405 test_message (Fixture *f,
408 DBusMessage *message = dbus_message_new_signal ("/foo", "foo.bar.baz",
410 Thread public_api = { message,
411 (RefFunc) dbus_message_ref,
413 (VoidFunc) dbus_message_unref,
419 if (!dbus_message_set_data (message, message_slot, f, last_unref))
422 for (i = 0; i < N_THREADS; i++)
424 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
425 g_assert (f->threads[i] != NULL);
428 wait_for_all_threads (f);
430 for (i = 0; i < N_THREADS; i++)
432 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
433 g_assert (f->threads[i] != NULL);
436 wait_for_all_threads (f);
438 for (i = 0; i < N_THREADS; i++)
440 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
441 g_assert (f->threads[i] != NULL);
444 wait_for_all_threads (f);
446 /* Destroy the server. This should be the last-unref. */
447 g_assert (!f->last_unref);
448 dbus_message_unref (message);
449 g_assert (f->last_unref);
453 test_pending_call (Fixture *f,
456 Thread public_api = { NULL,
457 (RefFunc) dbus_pending_call_ref,
459 (VoidFunc) dbus_pending_call_unref,
463 Thread internal_api = { NULL,
464 (RefFunc) _dbus_pending_call_ref_unlocked,
466 (VoidFunc) dbus_pending_call_unref,
468 (VoidFunc) _dbus_connection_lock,
469 (VoidFunc) _dbus_connection_unlock };
470 /* This one can't be used to ref, only to cycle or unref. */
471 Thread unref_and_unlock_api = { NULL,
472 (RefFunc) _dbus_pending_call_ref_unlocked,
474 (VoidFunc) _dbus_pending_call_unref_and_unlock,
476 (VoidFunc) _dbus_connection_lock,
479 DBusPendingCall *pending_call;
481 _dbus_connection_lock (f->connection);
482 pending_call = _dbus_pending_call_new_unlocked (f->connection,
483 DBUS_TIMEOUT_INFINITE, NULL);
484 g_assert (pending_call != NULL);
485 _dbus_connection_unlock (f->connection);
487 public_api.thing = pending_call;
488 internal_api.thing = pending_call;
489 unref_and_unlock_api.thing = pending_call;
491 if (!dbus_pending_call_set_data (pending_call, pending_call_slot, f,
495 for (i = 0; i < N_THREADS; i++)
498 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
500 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
502 g_assert (f->threads[i] != NULL);
505 wait_for_all_threads (f);
507 for (i = 0; i < N_THREADS; i++)
512 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
515 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
518 f->threads[i] = g_thread_new (NULL, cycle_thread,
519 &unref_and_unlock_api);
522 g_assert (f->threads[i] != NULL);
525 wait_for_all_threads (f);
527 for (i = 0; i < N_THREADS; i++)
532 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
535 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
538 f->threads[i] = g_thread_new (NULL, unref_thread,
539 &unref_and_unlock_api);
542 g_assert (f->threads[i] != NULL);
545 wait_for_all_threads (f);
547 /* Destroy the pending call. This should be the last-unref. */
548 g_assert (!f->last_unref);
549 dbus_pending_call_unref (pending_call);
550 g_assert (f->last_unref);
554 teardown (Fixture *f,
557 if (f->server_connection != NULL)
559 dbus_connection_close (f->server_connection);
560 dbus_connection_unref (f->server_connection);
563 if (f->connection != NULL)
565 dbus_connection_close (f->connection);
566 dbus_connection_unref (f->connection);
569 if (f->server != NULL)
571 dbus_server_disconnect (f->server);
572 dbus_server_unref (f->server);
575 dbus_connection_free_data_slot (&connection_slot);
576 dbus_server_free_data_slot (&server_slot);
577 dbus_message_free_data_slot (&message_slot);
578 dbus_pending_call_free_data_slot (&pending_call_slot);
580 _dbus_loop_unref (f->loop);
581 dbus_error_free (&f->e);
588 /* In GLib >= 2.24, < 2.31 this acts like g_thread_init() but avoids
589 * the deprecation of that function. In GLib >= 2.32 this is not
594 g_test_init (&argc, &argv, NULL);
595 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
597 g_test_add ("/refs/connection", Fixture, NULL, setup_connection,
598 test_connection, teardown);
599 g_test_add ("/refs/message", Fixture, NULL, setup,
600 test_message, teardown);
601 g_test_add ("/refs/pending-call", Fixture, NULL, setup_connection,
602 test_pending_call, teardown);
603 g_test_add ("/refs/server", Fixture, NULL, setup,
604 test_server, teardown);
606 return g_test_run ();