2 * Copyright 2012 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * See the included COPYING file for more information.
14 #include <glib-object.h>
22 gint bucket[THREADS]; /* accessed from multiple threads, but should never be contested due to the sequence of thread operations */
25 thread_func (gpointer data)
27 gint idx = GPOINTER_TO_INT (data);
33 for (i = 0; i < ROUNDS; i++)
35 d = g_random_int_range (-10, 100);
38 value = GPOINTER_TO_INT (g_object_get_data (object, "test"));
39 new_value = value + d;
41 g_object_set_data (object, "test", GINT_TO_POINTER (new_value));
44 if (!g_object_replace_data (object, "test",
45 GINT_TO_POINTER (value),
46 GINT_TO_POINTER (new_value),
57 test_qdata_threaded (void)
61 GThread *threads[THREADS];
64 object = g_object_new (G_TYPE_OBJECT, NULL);
65 g_object_set_data (object, "test", GINT_TO_POINTER (0));
67 for (i = 0; i < THREADS; i++)
70 for (i = 0; i < THREADS; i++)
71 threads[i] = g_thread_new ("qdata", thread_func, GINT_TO_POINTER (i));
73 for (i = 0; i < THREADS; i++)
74 g_thread_join (threads[i]);
77 for (i = 0; i < THREADS; i++)
80 result = GPOINTER_TO_INT (g_object_get_data (object, "test"));
82 g_assert_cmpint (sum, ==, result);
84 g_object_unref (object);
94 quark = g_quark_from_static_string ("test");
95 object = g_object_new (G_TYPE_OBJECT, NULL);
97 g_object_set_qdata_full (object, quark, s, g_free);
99 s2 = g_object_dup_qdata (object, quark, (GDuplicateFunc)g_strdup, NULL);
101 g_assert_cmpstr (s, ==, s2);
106 b = g_object_replace_qdata (object, quark, s, "s2", NULL, NULL);
111 g_object_unref (object);
115 main (int argc, char **argv)
117 g_test_init (&argc, &argv, NULL);
119 fail = !!g_getenv ("FAIL");
121 g_test_add_func ("/qdata/threaded", test_qdata_threaded);
122 g_test_add_func ("/qdata/dup", test_qdata_dup);
124 return g_test_run ();