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,
191 g_assert (f->server_connection == NULL);
192 f->server_connection = dbus_connection_ref (server_connection);
194 test_connection_setup (f->loop, f->server_connection);
201 if (!dbus_threads_init_default ())
204 f->loop = _dbus_loop_new ();
205 g_assert (f->loop != NULL);
207 dbus_error_init (&f->e);
209 f->server = dbus_server_listen ("tcp:host=127.0.0.1", &f->e);
210 assert_no_error (&f->e);
211 g_assert (f->server != NULL);
213 if (!dbus_connection_allocate_data_slot (&connection_slot))
216 if (!dbus_server_allocate_data_slot (&server_slot))
219 if (!dbus_message_allocate_data_slot (&message_slot))
222 if (!dbus_pending_call_allocate_data_slot (&pending_call_slot))
227 setup_connection (Fixture *f,
234 dbus_server_set_new_connection_function (f->server,
235 new_conn_cb, f, NULL);
237 if (!test_server_setup (f->loop, f->server))
238 g_error ("failed to set up server");
240 address = dbus_server_get_address (f->server);
241 g_assert (address != NULL);
242 f->connection = dbus_connection_open_private (address, &f->e);
243 assert_no_error (&f->e);
244 g_assert (f->connection != NULL);
247 if (!test_connection_setup (f->loop, f->connection))
248 g_error ("failed to set up connection");
250 while (f->server_connection == NULL)
251 _dbus_loop_iterate (f->loop, TRUE);
253 test_connection_shutdown (f->loop, f->connection);
254 test_server_shutdown (f->loop, f->server);
258 test_connection (Fixture *f,
261 Thread public_api = { f->connection,
262 (RefFunc) dbus_connection_ref,
264 (VoidFunc) dbus_connection_unref,
268 Thread internal_api = { f->connection,
269 (RefFunc) _dbus_connection_ref_unlocked,
271 (VoidFunc) _dbus_connection_unref_unlocked,
273 (VoidFunc) _dbus_connection_lock,
274 (VoidFunc) _dbus_connection_unlock };
277 /* Use a slot as a pseudo-weakref */
278 if (!dbus_connection_set_data (f->connection, connection_slot, f,
282 for (i = 0; i < N_THREADS; i++)
285 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
287 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
289 g_assert (f->threads[i] != NULL);
292 wait_for_all_threads (f);
294 for (i = 0; i < N_THREADS; i++)
297 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
299 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
301 g_assert (f->threads[i] != NULL);
304 wait_for_all_threads (f);
306 for (i = 0; i < N_THREADS; i++)
309 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
311 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
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_new (NULL, ref_thread, &public_api);
366 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
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_new (NULL, cycle_thread, &public_api);
378 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
380 g_assert (f->threads[i] != NULL);
383 wait_for_all_threads (f);
385 for (i = 0; i < N_THREADS; i++)
388 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
390 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
392 g_assert (f->threads[i] != NULL);
395 wait_for_all_threads (f);
397 /* Destroy the server. This should be the last-unref. */
398 g_assert (!f->last_unref);
399 dbus_server_disconnect (f->server);
400 dbus_server_unref (f->server);
402 g_assert (f->last_unref);
406 test_message (Fixture *f,
409 DBusMessage *message = dbus_message_new_signal ("/foo", "foo.bar.baz",
411 Thread public_api = { message,
412 (RefFunc) dbus_message_ref,
414 (VoidFunc) dbus_message_unref,
420 if (!dbus_message_set_data (message, message_slot, f, last_unref))
423 for (i = 0; i < N_THREADS; i++)
425 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
426 g_assert (f->threads[i] != NULL);
429 wait_for_all_threads (f);
431 for (i = 0; i < N_THREADS; i++)
433 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
434 g_assert (f->threads[i] != NULL);
437 wait_for_all_threads (f);
439 for (i = 0; i < N_THREADS; i++)
441 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
442 g_assert (f->threads[i] != NULL);
445 wait_for_all_threads (f);
447 /* Destroy the server. This should be the last-unref. */
448 g_assert (!f->last_unref);
449 dbus_message_unref (message);
450 g_assert (f->last_unref);
454 test_pending_call (Fixture *f,
457 Thread public_api = { NULL,
458 (RefFunc) dbus_pending_call_ref,
460 (VoidFunc) dbus_pending_call_unref,
464 Thread internal_api = { NULL,
465 (RefFunc) _dbus_pending_call_ref_unlocked,
467 (VoidFunc) dbus_pending_call_unref,
469 (VoidFunc) _dbus_connection_lock,
470 (VoidFunc) _dbus_connection_unlock };
471 /* This one can't be used to ref, only to cycle or unref. */
472 Thread unref_and_unlock_api = { NULL,
473 (RefFunc) _dbus_pending_call_ref_unlocked,
475 (VoidFunc) _dbus_pending_call_unref_and_unlock,
477 (VoidFunc) _dbus_connection_lock,
480 DBusPendingCall *pending_call;
482 _dbus_connection_lock (f->connection);
483 pending_call = _dbus_pending_call_new_unlocked (f->connection,
484 DBUS_TIMEOUT_INFINITE, NULL);
485 g_assert (pending_call != NULL);
486 _dbus_connection_unlock (f->connection);
488 public_api.thing = pending_call;
489 internal_api.thing = pending_call;
490 unref_and_unlock_api.thing = pending_call;
492 if (!dbus_pending_call_set_data (pending_call, pending_call_slot, f,
496 for (i = 0; i < N_THREADS; i++)
499 f->threads[i] = g_thread_new (NULL, ref_thread, &public_api);
501 f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api);
503 g_assert (f->threads[i] != NULL);
506 wait_for_all_threads (f);
508 for (i = 0; i < N_THREADS; i++)
513 f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api);
516 f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api);
519 f->threads[i] = g_thread_new (NULL, cycle_thread,
520 &unref_and_unlock_api);
523 g_assert (f->threads[i] != NULL);
526 wait_for_all_threads (f);
528 for (i = 0; i < N_THREADS; i++)
533 f->threads[i] = g_thread_new (NULL, unref_thread, &public_api);
536 f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api);
539 f->threads[i] = g_thread_new (NULL, unref_thread,
540 &unref_and_unlock_api);
543 g_assert (f->threads[i] != NULL);
546 wait_for_all_threads (f);
548 /* Destroy the pending call. This should be the last-unref. */
549 g_assert (!f->last_unref);
550 dbus_pending_call_unref (pending_call);
551 g_assert (f->last_unref);
555 teardown (Fixture *f,
558 if (f->server_connection != NULL)
560 dbus_connection_close (f->server_connection);
561 dbus_connection_unref (f->server_connection);
564 if (f->connection != NULL)
566 dbus_connection_close (f->connection);
567 dbus_connection_unref (f->connection);
570 if (f->server != NULL)
572 dbus_server_disconnect (f->server);
573 dbus_server_unref (f->server);
576 dbus_connection_free_data_slot (&connection_slot);
577 dbus_server_free_data_slot (&server_slot);
578 dbus_message_free_data_slot (&message_slot);
579 dbus_pending_call_free_data_slot (&pending_call_slot);
581 _dbus_loop_unref (f->loop);
582 dbus_error_free (&f->e);
589 /* In GLib >= 2.24, < 2.31 this acts like g_thread_init() but avoids
590 * the deprecation of that function. In GLib >= 2.32 this is not
595 g_test_init (&argc, &argv, NULL);
596 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
598 g_test_add ("/refs/connection", Fixture, NULL, setup_connection,
599 test_connection, teardown);
600 g_test_add ("/refs/message", Fixture, NULL, setup,
601 test_message, teardown);
602 g_test_add ("/refs/pending-call", Fixture, NULL, setup_connection,
603 test_pending_call, teardown);
604 g_test_add ("/refs/server", Fixture, NULL, setup,
605 test_server, teardown);
607 return g_test_run ();