Branch data Line data Source code
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 : : #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.
23 : : *
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__())).
26 : : */
27 : : #define _GTest_increase_alignment __attribute__((__aligned__(__alignof(gint64))))
28 : : #else
29 : : #define _GTest_increase_alignment
30 : : #endif
31 : :
32 : : struct _GTest
33 : : {
34 : : GObject object;
35 : :
36 : : /* See _GTest_increase_alignment. */
37 : : long double increase_alignment2;
38 : : } _GTest_increase_alignment;
39 : :
40 : : struct _GTestClass
41 : : {
42 : : GObjectClass parent_class;
43 : : };
44 : :
45 : : static GType my_test_get_type (void);
46 : : static gint stopping; /* (atomic) */
47 : :
48 : : static void my_test_class_init (GTestClass * klass,
49 : : void * class_data);
50 : : static void my_test_init (GTest * test,
51 : : void * class_data);
52 : : static void my_test_dispose (GObject * object);
53 : :
54 : : static GObjectClass *parent_class = NULL;
55 : :
56 : : static GType
57 : 4 : my_test_get_type (void)
58 : : {
59 : : static GType test_type = 0;
60 : :
61 : 4 : if (!test_type) {
62 : 2 : const GTypeInfo test_info = {
63 : : sizeof (GTestClass),
64 : : NULL,
65 : : NULL,
66 : : (GClassInitFunc) my_test_class_init,
67 : : NULL,
68 : : NULL,
69 : : sizeof (GTest),
70 : : 0,
71 : : (GInstanceInitFunc) my_test_init,
72 : : NULL
73 : : };
74 : :
75 : 2 : test_type = g_type_register_static (G_TYPE_OBJECT, "GTest",
76 : : &test_info, 0);
77 : 1 : }
78 : 4 : return test_type;
79 : : }
80 : :
81 : : static void
82 : 2 : my_test_class_init (GTestClass * klass,
83 : : G_GNUC_UNUSED void * class_data)
84 : : {
85 : : GObjectClass *gobject_class;
86 : :
87 : 2 : gobject_class = (GObjectClass *) klass;
88 : 2 : parent_class = g_type_class_ref (G_TYPE_OBJECT);
89 : :
90 : 2 : gobject_class->dispose = my_test_dispose;
91 : 2 : }
92 : :
93 : : static void
94 : 4 : my_test_init (GTest * test,
95 : : G_GNUC_UNUSED void * class_data)
96 : : {
97 : 4 : g_test_message ("init %p\n", test);
98 : 4 : }
99 : :
100 : : static void
101 : 4 : my_test_dispose (GObject * object)
102 : : {
103 : : GTest *test;
104 : :
105 : 4 : test = MY_TEST (object);
106 : :
107 : 4 : g_test_message ("dispose %p!\n", test);
108 : :
109 : 4 : G_OBJECT_CLASS (parent_class)->dispose (object);
110 : 4 : }
111 : :
112 : : static void
113 : 40648191 : my_test_do_refcount (GTest * test)
114 : : {
115 : 40648191 : g_object_ref (test);
116 : 40648191 : g_object_unref (test);
117 : 40648191 : }
118 : :
119 : : static gpointer
120 : 20 : run_thread (GTest * test)
121 : : {
122 : 20 : gint i = 1;
123 : :
124 : 40635866 : while (!g_atomic_int_get (&stopping)) {
125 : 40635846 : my_test_do_refcount (test);
126 : 40635846 : if ((i++ % 10000) == 0) {
127 : 4104 : g_thread_yield (); /* force context switch */
128 : 3504 : }
129 : : }
130 : :
131 : 20 : return NULL;
132 : : }
133 : :
134 : : static void
135 : 2 : test_refcount_object_basics (void)
136 : : {
137 : : guint i;
138 : : GTest *test1, *test2;
139 : : GArray *test_threads;
140 : 2 : const guint n_threads = 5;
141 : :
142 : 2 : test1 = g_object_new (G_TYPE_TEST, NULL);
143 : 2 : test2 = g_object_new (G_TYPE_TEST, NULL);
144 : :
145 : 2 : test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
146 : :
147 : 2 : g_atomic_int_set (&stopping, 0);
148 : :
149 : 12 : for (i = 0; i < n_threads; i++) {
150 : : GThread *thread;
151 : :
152 : 10 : thread = g_thread_new (NULL, (GThreadFunc) run_thread, test1);
153 : 10 : g_array_append_val (test_threads, thread);
154 : :
155 : 10 : thread = g_thread_new (NULL, (GThreadFunc) run_thread, test2);
156 : 10 : g_array_append_val (test_threads, thread);
157 : 5 : }
158 : :
159 : 2 : g_usleep (5000000);
160 : 2 : g_atomic_int_set (&stopping, 1);
161 : :
162 : : /* join all threads */
163 : 22 : for (i = 0; i < 2 * n_threads; i++) {
164 : : GThread *thread;
165 : :
166 : 20 : thread = g_array_index (test_threads, GThread *, i);
167 : 20 : g_thread_join (thread);
168 : 10 : }
169 : :
170 : 2 : g_object_unref (test1);
171 : 2 : g_object_unref (test2);
172 : 2 : g_array_unref (test_threads);
173 : 2 : }
174 : :
175 : : int
176 : 2 : main (int argc, gchar *argv[])
177 : : {
178 : 2 : g_log_set_always_fatal (G_LOG_LEVEL_WARNING |
179 : 2 : G_LOG_LEVEL_CRITICAL |
180 : 2 : g_log_set_always_fatal (G_LOG_FATAL_MASK));
181 : :
182 : 2 : g_test_init (&argc, &argv, NULL);
183 : :
184 : 2 : g_test_add_func ("/gobject/refcount/object-basics", test_refcount_object_basics);
185 : :
186 : 2 : return g_test_run ();
187 : : }
|