3 #include <glib-object.h>
5 #define G_TYPE_TEST (my_test_get_type ())
6 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
7 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
8 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
9 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
10 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
12 typedef struct _GTest GTest;
13 typedef struct _GTestClass GTestClass;
22 GObjectClass parent_class;
25 static GType my_test_get_type (void);
26 static volatile gboolean stopping;
28 static void my_test_class_init (GTestClass * klass);
29 static void my_test_init (GTest * test);
30 static void my_test_dispose (GObject * object);
32 static GObjectClass *parent_class = NULL;
35 my_test_get_type (void)
37 static GType test_type = 0;
40 static const GTypeInfo test_info = {
44 (GClassInitFunc) my_test_class_init,
49 (GInstanceInitFunc) my_test_init,
53 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
60 my_test_class_init (GTestClass * klass)
62 GObjectClass *gobject_class;
64 gobject_class = (GObjectClass *) klass;
66 parent_class = g_type_class_ref (G_TYPE_OBJECT);
68 gobject_class->dispose = my_test_dispose;
72 my_test_init (GTest * test)
74 g_print ("init %p\n", test);
78 my_test_dispose (GObject * object)
82 test = MY_TEST (object);
84 g_print ("dispose %p!\n", object);
86 G_OBJECT_CLASS (parent_class)->dispose (object);
90 my_test_do_refcount (GTest * test)
93 g_object_unref (test);
97 run_thread (GTest * test)
102 my_test_do_refcount (test);
103 if ((i++ % 10000) == 0) {
105 g_thread_yield(); /* force context switch */
113 main (int argc, char **argv)
116 GTest *test1, *test2;
117 GArray *test_threads;
118 const guint n_threads = 5;
120 g_thread_init (NULL);
121 g_print ("START: %s\n", argv[0]);
122 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
125 test1 = g_object_new (G_TYPE_TEST, NULL);
126 test2 = g_object_new (G_TYPE_TEST, NULL);
128 test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
132 for (i = 0; i < n_threads; i++) {
135 thread = g_thread_create ((GThreadFunc) run_thread, test1, TRUE, NULL);
136 g_array_append_val (test_threads, thread);
138 thread = g_thread_create ((GThreadFunc) run_thread, test2, TRUE, NULL);
139 g_array_append_val (test_threads, thread);
145 g_print ("\nstopping\n");
147 /* join all threads */
148 for (i = 0; i < 2 * n_threads; i++) {
151 thread = g_array_index (test_threads, GThread *, i);
152 g_thread_join (thread);
155 g_print ("stopped\n");