[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / tests / refcount / objects.c
1 #include <glib.h>
2 #include <glib-object.h>
3
4 #ifdef G_OS_UNIX
5 #include <unistd.h>
6 #endif
7
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))
14
15 typedef struct _GTest GTest;
16 typedef struct _GTestClass GTestClass;
17
18 struct _GTest
19 {
20   GObject object;
21 };
22
23 struct _GTestClass
24 {
25   GObjectClass parent_class;
26 };
27
28 static GType my_test_get_type (void);
29 static volatile gboolean stopping;
30
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);
34
35 static GObjectClass *parent_class = NULL;
36
37 static GType
38 my_test_get_type (void)
39 {
40   static GType test_type = 0;
41
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),
51       0,
52       (GInstanceInitFunc) my_test_init,
53       NULL
54     };
55
56     test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
57         &test_info, 0);
58   }
59   return test_type;
60 }
61
62 static void
63 my_test_class_init (GTestClass * klass)
64 {
65   GObjectClass *gobject_class;
66
67   gobject_class = (GObjectClass *) klass;
68
69   parent_class = g_type_class_ref (G_TYPE_OBJECT);
70
71   gobject_class->dispose = my_test_dispose;
72 }
73
74 static void
75 my_test_init (GTest * test)
76 {
77   g_print ("init %p\n", test);
78 }
79
80 static void
81 my_test_dispose (GObject * object)
82 {
83   GTest *test;
84
85   test = MY_TEST (object);
86
87   g_print ("dispose %p!\n", test);
88
89   G_OBJECT_CLASS (parent_class)->dispose (object);
90 }
91
92 static void
93 my_test_do_refcount (GTest * test)
94 {
95   g_object_ref (test); 
96   g_object_unref (test); 
97 }
98
99 static gpointer
100 run_thread (GTest * test)
101 {
102   gint i = 1;
103
104   while (!stopping) {
105     my_test_do_refcount (test);
106     if ((i++ % 10000) == 0) {
107       g_print (".");
108       g_thread_yield(); /* force context switch */
109     }
110   }
111
112   return NULL;
113 }
114
115 int
116 main (int argc, char **argv)
117 {
118   gint i;
119   GTest *test1, *test2;
120   GArray *test_threads;
121   const guint n_threads = 5;
122
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));
125
126   test1 = g_object_new (G_TYPE_TEST, NULL);
127   test2 = g_object_new (G_TYPE_TEST, NULL);
128
129   test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
130
131   stopping = FALSE;
132
133   for (i = 0; i < n_threads; i++) {
134     GThread *thread;
135
136     thread = g_thread_create ((GThreadFunc) run_thread, test1, TRUE, NULL);
137     g_array_append_val (test_threads, thread);
138
139     thread = g_thread_create ((GThreadFunc) run_thread, test2, TRUE, NULL);
140     g_array_append_val (test_threads, thread);
141   }
142   g_usleep (5000000);
143
144   stopping = TRUE;
145
146   g_print ("\nstopping\n");
147
148   /* join all threads */
149   for (i = 0; i < 2 * n_threads; i++) {
150     GThread *thread;
151
152     thread = g_array_index (test_threads, GThread *, i);
153     g_thread_join (thread);
154   }
155
156   g_object_unref (test1);
157   g_object_unref (test2);
158
159   g_print ("stopped\n");
160
161   return 0;
162 }