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
;
25 GObjectClass parent_class
;
28 static GType
my_test_get_type (void);
29 static volatile gboolean stopping
;
31 static void my_test_class_init (GTestClass
* klass
);
32 static void my_test_init (GTest
* test
);
33 static void my_test_dispose (GObject
* object
);
35 static GObjectClass
*parent_class
= NULL
;
38 my_test_get_type (void)
40 static GType test_type
= 0;
43 const GTypeInfo test_info
= {
47 (GClassInitFunc
) my_test_class_init
,
52 (GInstanceInitFunc
) my_test_init
,
56 test_type
= g_type_register_static (G_TYPE_OBJECT
, "GTest",
63 my_test_class_init (GTestClass
* klass
)
65 GObjectClass
*gobject_class
;
67 gobject_class
= (GObjectClass
*) klass
;
69 parent_class
= g_type_class_ref (G_TYPE_OBJECT
);
71 gobject_class
->dispose
= my_test_dispose
;
75 my_test_init (GTest
* test
)
77 g_print ("init %p\n", test
);
81 my_test_dispose (GObject
* object
)
85 test
= MY_TEST (object
);
87 g_print ("dispose %p!\n", test
);
89 G_OBJECT_CLASS (parent_class
)->dispose (object
);
93 my_test_do_refcount (GTest
* test
)
96 g_object_unref (test
);
100 run_thread (GTest
* test
)
105 my_test_do_refcount (test
);
106 if ((i
++ % 10000) == 0) {
108 g_thread_yield(); /* force context switch */
116 main (int argc
, char **argv
)
119 GTest
*test1
, *test2
;
120 GArray
*test_threads
;
121 const guint n_threads
= 5;
123 g_print ("START: %s\n", argv
[0]);
124 g_log_set_always_fatal (G_LOG_LEVEL_WARNING
| G_LOG_LEVEL_CRITICAL
| g_log_set_always_fatal (G_LOG_FATAL_MASK
));
126 test1
= g_object_new (G_TYPE_TEST
, NULL
);
127 test2
= g_object_new (G_TYPE_TEST
, NULL
);
129 test_threads
= g_array_new (FALSE
, FALSE
, sizeof (GThread
*));
133 for (i
= 0; i
< n_threads
; i
++) {
136 thread
= g_thread_create ((GThreadFunc
) run_thread
, test1
, TRUE
, NULL
);
137 g_array_append_val (test_threads
, thread
);
139 thread
= g_thread_create ((GThreadFunc
) run_thread
, test2
, TRUE
, NULL
);
140 g_array_append_val (test_threads
, thread
);
146 g_print ("\nstopping\n");
148 /* join all threads */
149 for (i
= 0; i
< 2 * n_threads
; i
++) {
152 thread
= g_array_index (test_threads
, GThread
*, i
);
153 g_thread_join (thread
);
156 g_object_unref (test1
);
157 g_object_unref (test2
);
158 g_array_unref (test_threads
);
160 g_print ("stopped\n");