2 #include <glib-object.h>
8 #define G_TYPE_TEST (my_test_get_type ())
9 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
10 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
11 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
12 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
13 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
15 typedef struct _GTest GTest;
16 typedef struct _GTestClass GTestClass;
18 #if G_GNUC_CHECK_VERSION (4, 0)
19 /* Increase the alignment of GTest to check whether
20 * G_TYPE_CHECK_INSTANCE_CAST() would trigger a "-Wcast-align=strict" warning.
21 * That would happen, when trying to cast a "GObject*" to "GTest*", if latter
22 * has larger alignment.
24 * Note that merely adding a int64 field to GTest does not increase the
25 * alignment above 4 bytes on i386, hence use the __attribute__((__aligned__())).
27 #define _GTest_increase_alignment __attribute__((__aligned__(__alignof(gint64))))
29 #define _GTest_increase_alignment
36 /* See _GTest_increase_alignment. */
37 long double increase_alignment2;
38 } _GTest_increase_alignment;
42 GObjectClass parent_class;
45 static GType my_test_get_type (void);
46 static gint stopping; /* (atomic) */
48 static void my_test_class_init (GTestClass * klass);
49 static void my_test_init (GTest * test);
50 static void my_test_dispose (GObject * object);
52 static GObjectClass *parent_class = NULL;
55 my_test_get_type (void)
57 static GType test_type = 0;
60 const GTypeInfo test_info = {
64 (GClassInitFunc) my_test_class_init,
69 (GInstanceInitFunc) my_test_init,
73 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
80 my_test_class_init (GTestClass * klass)
82 GObjectClass *gobject_class;
84 gobject_class = (GObjectClass *) klass;
85 parent_class = g_type_class_ref (G_TYPE_OBJECT);
87 gobject_class->dispose = my_test_dispose;
91 my_test_init (GTest * test)
93 g_test_message ("init %p\n", test);
97 my_test_dispose (GObject * object)
101 test = MY_TEST (object);
103 g_test_message ("dispose %p!\n", test);
105 G_OBJECT_CLASS (parent_class)->dispose (object);
109 my_test_do_refcount (GTest * test)
112 g_object_unref (test);
116 run_thread (GTest * test)
120 while (!g_atomic_int_get (&stopping)) {
121 my_test_do_refcount (test);
122 if ((i++ % 10000) == 0) {
123 g_thread_yield (); /* force context switch */
131 test_refcount_object_basics (void)
134 GTest *test1, *test2;
135 GArray *test_threads;
136 const guint n_threads = 5;
138 test1 = g_object_new (G_TYPE_TEST, NULL);
139 test2 = g_object_new (G_TYPE_TEST, NULL);
141 test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
143 g_atomic_int_set (&stopping, 0);
145 for (i = 0; i < n_threads; i++) {
148 thread = g_thread_new (NULL, (GThreadFunc) run_thread, test1);
149 g_array_append_val (test_threads, thread);
151 thread = g_thread_new (NULL, (GThreadFunc) run_thread, test2);
152 g_array_append_val (test_threads, thread);
156 g_atomic_int_set (&stopping, 1);
158 /* join all threads */
159 for (i = 0; i < 2 * n_threads; i++) {
162 thread = g_array_index (test_threads, GThread *, i);
163 g_thread_join (thread);
166 g_object_unref (test1);
167 g_object_unref (test2);
168 g_array_unref (test_threads);
172 main (int argc, gchar *argv[])
174 g_log_set_always_fatal (G_LOG_LEVEL_WARNING |
175 G_LOG_LEVEL_CRITICAL |
176 g_log_set_always_fatal (G_LOG_FATAL_MASK));
178 g_test_init (&argc, &argv, NULL);
180 g_test_add_func ("/gobject/refcount/object-basics", test_refcount_object_basics);
182 return g_test_run ();