2 * Copyright 2012 Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * See the included COPYING file for more information.
12 #include <glib-object.h>
20 volatile gint bucket
[THREADS
];
23 thread_func (gpointer data
)
25 gint idx
= GPOINTER_TO_INT (data
);
31 for (i
= 0; i
< ROUNDS
; i
++)
33 d
= g_random_int_range (-10, 100);
36 value
= GPOINTER_TO_INT (g_object_get_data (object
, "test"));
37 new_value
= value
+ d
;
39 g_object_set_data (object
, "test", GINT_TO_POINTER (new_value
));
42 if (!g_object_replace_data (object
, "test",
43 GINT_TO_POINTER (value
),
44 GINT_TO_POINTER (new_value
),
55 test_qdata_threaded (void)
59 GThread
*threads
[THREADS
];
62 object
= g_object_new (G_TYPE_OBJECT
, NULL
);
63 g_object_set_data (object
, "test", GINT_TO_POINTER (0));
65 for (i
= 0; i
< THREADS
; i
++)
68 for (i
= 0; i
< THREADS
; i
++)
69 threads
[i
] = g_thread_new ("qdata", thread_func
, GINT_TO_POINTER (i
));
71 for (i
= 0; i
< THREADS
; i
++)
72 g_thread_join (threads
[i
]);
75 for (i
= 0; i
< THREADS
; i
++)
78 result
= GPOINTER_TO_INT (g_object_get_data (object
, "test"));
80 g_assert_cmpint (sum
, ==, result
);
82 g_object_unref (object
);
92 quark
= g_quark_from_static_string ("test");
93 object
= g_object_new (G_TYPE_OBJECT
, NULL
);
95 g_object_set_qdata_full (object
, quark
, s
, g_free
);
97 s2
= g_object_dup_qdata (object
, quark
, (GDuplicateFunc
)g_strdup
, NULL
);
99 g_assert_cmpstr (s
, ==, s2
);
104 b
= g_object_replace_qdata (object
, quark
, s
, "s2", NULL
, NULL
);
109 g_object_unref (object
);
113 main (int argc
, char **argv
)
115 g_test_init (&argc
, &argv
, NULL
);
117 fail
= !!g_getenv ("FAIL");
119 g_test_add_func ("/qdata/threaded", test_qdata_threaded
);
120 g_test_add_func ("/qdata/dup", test_qdata_dup
);
122 return g_test_run ();