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
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"
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 *);
81 ref_thread (gpointer data)
83 Thread *thread = data;
86 for (i = 0; i < N_REFS; i++)
88 if (thread->lock != NULL)
89 (thread->lock) (thread->mutex);
91 if (thread->ref != NULL)
93 gpointer ret = (thread->ref) (thread->thing);
95 g_assert (ret == thread->thing);
99 (thread->ref_void) (thread->thing);
102 if (thread->unlock != NULL)
103 (thread->unlock) (thread->mutex);
110 cycle_thread (gpointer data)
112 Thread *thread = data;
115 for (i = 0; i < N_REFS; i++)
117 if (thread->lock != NULL)
118 (thread->lock) (thread->mutex);
120 if (thread->ref != NULL)
122 gpointer ret = (thread->ref) (thread->thing);
124 g_assert (ret == thread->thing);
128 (thread->ref_void) (thread->thing);
131 (thread->unref) (thread->thing);
133 if (thread->unlock != NULL)
134 (thread->unlock) (thread->mutex);
141 unref_thread (gpointer data)
143 Thread *thread = data;
146 for (i = 0; i < N_REFS; i++)
148 if (thread->lock != NULL)
149 (thread->lock) (thread->mutex);
151 (thread->unref) (thread->thing);
153 if (thread->unlock != NULL)
154 (thread->unlock) (thread->mutex);
161 last_unref (void *data)
165 g_assert (!f->last_unref);
166 f->last_unref = TRUE;
170 wait_for_all_threads (Fixture *f)
174 for (i = 0; i < N_THREADS; i++)
175 g_thread_join (f->threads[i]);
179 new_conn_cb (DBusServer *server,
180 DBusConnection *server_connection,
185 g_assert (f->server_connection == NULL);
186 f->server_connection = dbus_connection_ref (server_connection);
188 test_connection_setup (f->loop, f->server_connection);
195 if (!dbus_threads_init_default ())
198 f->loop = _dbus_loop_new ();
199 g_assert (f->loop != NULL);
201 dbus_error_init (&f->e);
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);
207 if (!dbus_connection_allocate_data_slot (&connection_slot))
210 if (!dbus_server_allocate_data_slot (&server_slot))
213 if (!dbus_message_allocate_data_slot (&message_slot))
216 if (!dbus_pending_call_allocate_data_slot (&pending_call_slot))
221 setup_connection (Fixture *f,
228 dbus_server_set_new_connection_function (f->server,
229 new_conn_cb, f, NULL);
231 if (!test_server_setup (f->loop, f->server))
232 g_error ("failed to set up server");
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);
241 if (!test_connection_setup (f->loop, f->connection))
242 g_error ("failed to set up connection");
244 while (f->server_connection == NULL)
245 _dbus_loop_iterate (f->loop, TRUE);
247 test_connection_shutdown (f->loop, f->connection);
248 test_server_shutdown (f->loop, f->server);
252 test_connection (Fixture *f,
255 Thread public_api = { f->connection,
256 (RefFunc) dbus_connection_ref,
258 (VoidFunc) dbus_connection_unref,
262 Thread internal_api = { f->connection,
263 (RefFunc) _dbus_connection_ref_unlocked,
265 (VoidFunc) _dbus_connection_unref_unlocked,
267 (VoidFunc) _dbus_connection_lock,
268 (VoidFunc) _dbus_connection_unlock };
271 /* Use a slot as a pseudo-weakref */
272 if (!dbus_connection_set_data (f->connection, connection_slot, f,
276 for (i = 0; i < N_THREADS; i++)
279 f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
281 f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
284 g_assert (f->threads[i] != NULL);
287 wait_for_all_threads (f);
289 for (i = 0; i < N_THREADS; i++)
292 f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
295 f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
298 g_assert (f->threads[i] != NULL);
301 wait_for_all_threads (f);
303 for (i = 0; i < N_THREADS; i++)
306 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
309 f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
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_create (ref_thread, &public_api, TRUE, NULL);
365 f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
368 g_assert (f->threads[i] != NULL);
371 wait_for_all_threads (f);
373 for (i = 0; i < N_THREADS; i++)
376 f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
379 f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
382 g_assert (f->threads[i] != NULL);
385 wait_for_all_threads (f);
387 for (i = 0; i < N_THREADS; i++)
390 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
393 f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
396 g_assert (f->threads[i] != NULL);
399 wait_for_all_threads (f);
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);
406 g_assert (f->last_unref);
410 test_message (Fixture *f,
413 DBusMessage *message = dbus_message_new_signal ("/foo", "foo.bar.baz",
415 Thread public_api = { message,
416 (RefFunc) dbus_message_ref,
418 (VoidFunc) dbus_message_unref,
424 if (!dbus_message_set_data (message, message_slot, f, last_unref))
427 for (i = 0; i < N_THREADS; i++)
429 f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
430 g_assert (f->threads[i] != NULL);
433 wait_for_all_threads (f);
435 for (i = 0; i < N_THREADS; i++)
437 f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE, NULL);
438 g_assert (f->threads[i] != NULL);
441 wait_for_all_threads (f);
443 for (i = 0; i < N_THREADS; i++)
445 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE, NULL);
446 g_assert (f->threads[i] != NULL);
449 wait_for_all_threads (f);
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);
458 test_pending_call (Fixture *f,
461 Thread public_api = { NULL,
462 (RefFunc) dbus_pending_call_ref,
464 (VoidFunc) dbus_pending_call_unref,
468 Thread internal_api = { NULL,
469 (RefFunc) _dbus_pending_call_ref_unlocked,
471 (VoidFunc) dbus_pending_call_unref,
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,
479 (VoidFunc) _dbus_pending_call_unref_and_unlock,
481 (VoidFunc) _dbus_connection_lock,
484 DBusPendingCall *pending_call;
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);
492 public_api.thing = pending_call;
493 internal_api.thing = pending_call;
494 unref_and_unlock_api.thing = pending_call;
496 if (!dbus_pending_call_set_data (pending_call, pending_call_slot, f,
500 for (i = 0; i < N_THREADS; i++)
503 f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
505 f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
508 g_assert (f->threads[i] != NULL);
511 wait_for_all_threads (f);
513 for (i = 0; i < N_THREADS; i++)
518 f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
522 f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
526 f->threads[i] = g_thread_create (cycle_thread,
527 &unref_and_unlock_api, TRUE, NULL);
530 g_assert (f->threads[i] != NULL);
533 wait_for_all_threads (f);
535 for (i = 0; i < N_THREADS; i++)
540 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
544 f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
548 f->threads[i] = g_thread_create (unref_thread,
549 &unref_and_unlock_api, TRUE, NULL);
552 g_assert (f->threads[i] != NULL);
555 wait_for_all_threads (f);
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);
564 teardown (Fixture *f,
567 if (f->server_connection != NULL)
569 dbus_connection_close (f->server_connection);
570 dbus_connection_unref (f->server_connection);
573 if (f->connection != NULL)
575 dbus_connection_close (f->connection);
576 dbus_connection_unref (f->connection);
579 if (f->server != NULL)
581 dbus_server_disconnect (f->server);
582 dbus_server_unref (f->server);
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);
590 _dbus_loop_unref (f->loop);
591 dbus_error_free (&f->e);
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=");
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);
611 return g_test_run ();