Merge branch 'g-clear-pointer-no-side-effects' into 'master'
[glib.git] / tests / refcount / objects.c
blob963766d003d10f50c2a5445e465890983eb8a48d
1 #include <glib.h>
2 #include <glib-object.h>
4 #ifdef G_OS_UNIX
5 #include <unistd.h>
6 #endif
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 struct _GTest
20 GObject object;
23 struct _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;
37 static GType
38 my_test_get_type (void)
40 static GType test_type = 0;
42 if (!test_type) {
43 const GTypeInfo test_info = {
44 sizeof (GTestClass),
45 NULL,
46 NULL,
47 (GClassInitFunc) my_test_class_init,
48 NULL,
49 NULL,
50 sizeof (GTest),
52 (GInstanceInitFunc) my_test_init,
53 NULL
56 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
57 &test_info, 0);
59 return test_type;
62 static void
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;
74 static void
75 my_test_init (GTest * test)
77 g_print ("init %p\n", test);
80 static void
81 my_test_dispose (GObject * object)
83 GTest *test;
85 test = MY_TEST (object);
87 g_print ("dispose %p!\n", test);
89 G_OBJECT_CLASS (parent_class)->dispose (object);
92 static void
93 my_test_do_refcount (GTest * test)
95 g_object_ref (test);
96 g_object_unref (test);
99 static gpointer
100 run_thread (GTest * test)
102 gint i = 1;
104 while (!stopping) {
105 my_test_do_refcount (test);
106 if ((i++ % 10000) == 0) {
107 g_print (".");
108 g_thread_yield(); /* force context switch */
112 return NULL;
116 main (int argc, char **argv)
118 gint i;
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 *));
131 stopping = FALSE;
133 for (i = 0; i < n_threads; i++) {
134 GThread *thread;
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);
142 g_usleep (5000000);
144 stopping = TRUE;
146 g_print ("\nstopping\n");
148 /* join all threads */
149 for (i = 0; i < 2 * n_threads; i++) {
150 GThread *thread;
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");
162 return 0;