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,
184 dbus_bool_t have_mem;
186 g_assert (f->server_connection == NULL);
187 f->server_connection = dbus_connection_ref (server_connection);
189 test_connection_setup (f->loop, f->server_connection);
196 if (!dbus_threads_init_default ())
199 f->loop = _dbus_loop_new ();
200 g_assert (f->loop != NULL);
202 dbus_error_init (&f->e);
204 f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e);
205 assert_no_error (&f->e);
206 g_assert (f->server != NULL);
208 if (!dbus_connection_allocate_data_slot (&connection_slot))
211 if (!dbus_server_allocate_data_slot (&server_slot))
214 if (!dbus_message_allocate_data_slot (&message_slot))
217 if (!dbus_pending_call_allocate_data_slot (&pending_call_slot))
222 setup_connection (Fixture *f,
229 dbus_server_set_new_connection_function (f->server,
230 new_conn_cb, f, NULL);
232 if (!test_server_setup (f->loop, f->server))
233 g_error ("failed to set up server");
235 address = dbus_server_get_address (f->server);
236 g_assert (address != NULL);
237 f->connection = dbus_connection_open_private (address, &f->e);
238 assert_no_error (&f->e);
239 g_assert (f->connection != NULL);
242 if (!test_connection_setup (f->loop, f->connection))
243 g_error ("failed to set up connection");
245 while (f->server_connection == NULL)
246 _dbus_loop_iterate (f->loop, TRUE);
248 test_connection_shutdown (f->loop, f->connection);
249 test_server_shutdown (f->loop, f->server);
253 test_connection (Fixture *f,
256 Thread public_api = { f->connection,
257 (RefFunc) dbus_connection_ref,
259 (VoidFunc) dbus_connection_unref,
263 Thread internal_api = { f->connection,
264 (RefFunc) _dbus_connection_ref_unlocked,
266 (VoidFunc) _dbus_connection_unref_unlocked,
268 (VoidFunc) _dbus_connection_lock,
269 (VoidFunc) _dbus_connection_unlock };
272 /* Use a slot as a pseudo-weakref */
273 if (!dbus_connection_set_data (f->connection, connection_slot, f,
277 for (i = 0; i < N_THREADS; i++)
280 f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
282 f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
285 g_assert (f->threads[i] != NULL);
288 wait_for_all_threads (f);
290 for (i = 0; i < N_THREADS; i++)
293 f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
296 f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
299 g_assert (f->threads[i] != NULL);
302 wait_for_all_threads (f);
304 for (i = 0; i < N_THREADS; i++)
307 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
310 f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
313 g_assert (f->threads[i] != NULL);
316 wait_for_all_threads (f);
318 /* Destroy the connection. This should be the last-unref. */
319 g_assert (!f->last_unref);
320 dbus_connection_close (f->connection);
321 dbus_connection_unref (f->connection);
322 f->connection = NULL;
323 g_assert (f->last_unref);
327 server_lock (void *server)
329 SERVER_LOCK (((DBusServer *) server));
333 server_unlock (void *server)
335 SERVER_UNLOCK (((DBusServer *) server));
339 test_server (Fixture *f,
342 Thread public_api = { f->server,
343 (RefFunc) dbus_server_ref,
345 (VoidFunc) dbus_server_unref,
349 Thread internal_api = { f->server,
351 (VoidFunc) _dbus_server_ref_unlocked,
352 (VoidFunc) _dbus_server_unref_unlocked,
358 if (!dbus_server_set_data (f->server, server_slot, f, last_unref))
361 for (i = 0; i < N_THREADS; i++)
364 f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
366 f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
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_create (cycle_thread, &public_api, TRUE,
380 f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
383 g_assert (f->threads[i] != NULL);
386 wait_for_all_threads (f);
388 for (i = 0; i < N_THREADS; i++)
391 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
394 f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
397 g_assert (f->threads[i] != NULL);
400 wait_for_all_threads (f);
402 /* Destroy the server. This should be the last-unref. */
403 g_assert (!f->last_unref);
404 dbus_server_disconnect (f->server);
405 dbus_server_unref (f->server);
407 g_assert (f->last_unref);
411 test_message (Fixture *f,
414 DBusMessage *message = dbus_message_new_signal ("/foo", "foo.bar.baz",
416 Thread public_api = { message,
417 (RefFunc) dbus_message_ref,
419 (VoidFunc) dbus_message_unref,
425 if (!dbus_message_set_data (message, message_slot, f, last_unref))
428 for (i = 0; i < N_THREADS; i++)
430 f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
431 g_assert (f->threads[i] != NULL);
434 wait_for_all_threads (f);
436 for (i = 0; i < N_THREADS; i++)
438 f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE, NULL);
439 g_assert (f->threads[i] != NULL);
442 wait_for_all_threads (f);
444 for (i = 0; i < N_THREADS; i++)
446 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE, NULL);
447 g_assert (f->threads[i] != NULL);
450 wait_for_all_threads (f);
452 /* Destroy the server. This should be the last-unref. */
453 g_assert (!f->last_unref);
454 dbus_message_unref (message);
455 g_assert (f->last_unref);
459 test_pending_call (Fixture *f,
462 Thread public_api = { NULL,
463 (RefFunc) dbus_pending_call_ref,
465 (VoidFunc) dbus_pending_call_unref,
469 Thread internal_api = { NULL,
470 (RefFunc) _dbus_pending_call_ref_unlocked,
472 (VoidFunc) dbus_pending_call_unref,
474 (VoidFunc) _dbus_connection_lock,
475 (VoidFunc) _dbus_connection_unlock };
476 /* This one can't be used to ref, only to cycle or unref. */
477 Thread unref_and_unlock_api = { NULL,
478 (RefFunc) _dbus_pending_call_ref_unlocked,
480 (VoidFunc) _dbus_pending_call_unref_and_unlock,
482 (VoidFunc) _dbus_connection_lock,
485 DBusPendingCall *pending_call;
487 _dbus_connection_lock (f->connection);
488 pending_call = _dbus_pending_call_new_unlocked (f->connection,
489 DBUS_TIMEOUT_INFINITE, NULL);
490 g_assert (pending_call != NULL);
491 _dbus_connection_unlock (f->connection);
493 public_api.thing = pending_call;
494 internal_api.thing = pending_call;
495 unref_and_unlock_api.thing = pending_call;
497 if (!dbus_pending_call_set_data (pending_call, pending_call_slot, f,
501 for (i = 0; i < N_THREADS; i++)
504 f->threads[i] = g_thread_create (ref_thread, &public_api, TRUE, NULL);
506 f->threads[i] = g_thread_create (ref_thread, &internal_api, TRUE,
509 g_assert (f->threads[i] != NULL);
512 wait_for_all_threads (f);
514 for (i = 0; i < N_THREADS; i++)
519 f->threads[i] = g_thread_create (cycle_thread, &public_api, TRUE,
523 f->threads[i] = g_thread_create (cycle_thread, &internal_api, TRUE,
527 f->threads[i] = g_thread_create (cycle_thread,
528 &unref_and_unlock_api, TRUE, NULL);
531 g_assert (f->threads[i] != NULL);
534 wait_for_all_threads (f);
536 for (i = 0; i < N_THREADS; i++)
541 f->threads[i] = g_thread_create (unref_thread, &public_api, TRUE,
545 f->threads[i] = g_thread_create (unref_thread, &internal_api, TRUE,
549 f->threads[i] = g_thread_create (unref_thread,
550 &unref_and_unlock_api, TRUE, NULL);
553 g_assert (f->threads[i] != NULL);
556 wait_for_all_threads (f);
558 /* Destroy the pending call. This should be the last-unref. */
559 g_assert (!f->last_unref);
560 dbus_pending_call_unref (pending_call);
561 g_assert (f->last_unref);
565 teardown (Fixture *f,
568 if (f->server_connection != NULL)
570 dbus_connection_close (f->server_connection);
571 dbus_connection_unref (f->server_connection);
574 if (f->connection != NULL)
576 dbus_connection_close (f->connection);
577 dbus_connection_unref (f->connection);
580 if (f->server != NULL)
582 dbus_server_disconnect (f->server);
583 dbus_server_unref (f->server);
586 dbus_connection_free_data_slot (&connection_slot);
587 dbus_server_free_data_slot (&server_slot);
588 dbus_message_free_data_slot (&message_slot);
589 dbus_pending_call_free_data_slot (&pending_call_slot);
591 _dbus_loop_unref (f->loop);
592 dbus_error_free (&f->e);
599 g_thread_init (NULL);
600 g_test_init (&argc, &argv, NULL);
601 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
603 g_test_add ("/refs/connection", Fixture, NULL, setup_connection,
604 test_connection, teardown);
605 g_test_add ("/refs/message", Fixture, NULL, setup,
606 test_message, teardown);
607 g_test_add ("/refs/pending-call", Fixture, NULL, setup_connection,
608 test_pending_call, teardown);
609 g_test_add ("/refs/server", Fixture, NULL, setup,
610 test_server, teardown);
612 return g_test_run ();