1 #include <glib.h>
2 #include <glib-object.h>
3
4 #define G_TYPE_TEST (my_test_get_type ())
5 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
6 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
7 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
8 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
9 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
10
11 enum {
12 PROP_0,
13 PROP_DUMMY
14 };
15
16 typedef struct _GTest GTest;
17 typedef struct _GTestClass GTestClass;
18
19 struct _GTest
20 {
21 GObject object;
22 gint id;
23 gint dummy;
24
25 gint count;
26 gint setcount;
27 };
28
29 struct _GTestClass
30 {
31 GObjectClass parent_class;
32 };
33
34 static GType my_test_get_type (void);
G_DEFINE_TYPE(GTest,my_test,G_TYPE_OBJECT)35 G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
36
37 static gint stopping; /* (atomic) */
38
39 static void my_test_get_property (GObject *object,
40 guint prop_id,
41 GValue *value,
42 GParamSpec *pspec);
43 static void my_test_set_property (GObject *object,
44 guint prop_id,
45 const GValue *value,
46 GParamSpec *pspec);
47
48 static void
49 my_test_class_init (GTestClass * klass)
50 {
51 GObjectClass *gobject_class;
52
53 gobject_class = (GObjectClass *) klass;
54
55 gobject_class->get_property = my_test_get_property;
56 gobject_class->set_property = my_test_set_property;
57
58 g_object_class_install_property (gobject_class,
59 PROP_DUMMY,
60 g_param_spec_int ("dummy",
61 NULL,
62 NULL,
63 0, G_MAXINT, 0,
64 G_PARAM_READWRITE));
65 }
66
67 static void
my_test_init(GTest * test)68 my_test_init (GTest * test)
69 {
70 static guint static_id = 1;
71 test->id = static_id++;
72 }
73
74 static void
my_test_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)75 my_test_get_property (GObject *object,
76 guint prop_id,
77 GValue *value,
78 GParamSpec *pspec)
79 {
80 GTest *test;
81
82 test = MY_TEST (object);
83
84 switch (prop_id)
85 {
86 case PROP_DUMMY:
87 g_value_set_int (value, g_atomic_int_get (&test->dummy));
88 break;
89 default:
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
91 break;
92 }
93 }
94
95 static void
my_test_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)96 my_test_set_property (GObject *object,
97 guint prop_id,
98 const GValue *value,
99 GParamSpec *pspec)
100 {
101 GTest *test;
102
103 test = MY_TEST (object);
104
105 switch (prop_id)
106 {
107 case PROP_DUMMY:
108 g_atomic_int_set (&test->dummy, g_value_get_int (value));
109 break;
110 default:
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
112 break;
113 }
114 }
115
116 static void
dummy_notify(GObject * object,GParamSpec * pspec)117 dummy_notify (GObject *object,
118 GParamSpec *pspec)
119 {
120 GTest *test;
121
122 test = MY_TEST (object);
123
124 g_atomic_int_inc (&test->count);
125 }
126
127 static void
my_test_do_property(GTest * test)128 my_test_do_property (GTest * test)
129 {
130 gint dummy;
131
132 g_atomic_int_inc (&test->setcount);
133
134 g_object_get (test, "dummy", &dummy, NULL);
135 g_object_set (test, "dummy", dummy + 1, NULL);
136 }
137
138 static gpointer
run_thread(GTest * test)139 run_thread (GTest * test)
140 {
141 gint i = 1;
142
143 while (!g_atomic_int_get (&stopping)) {
144 my_test_do_property (test);
145 if ((i++ % 10000) == 0)
146 {
147 g_print (".%c", 'a' + test->id);
148 g_thread_yield(); /* force context switch */
149 }
150 }
151
152 return NULL;
153 }
154
155 int
main(int argc,char ** argv)156 main (int argc, char **argv)
157 {
158 gint i;
159 GTest *test;
160 GArray *test_threads;
161 const gint n_threads = 5;
162
163 g_print ("START: %s\n", argv[0]);
164 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
165
166 test = g_object_new (G_TYPE_TEST, NULL);
167
168 g_assert (test->count == test->dummy);
169 g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
170
171 test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
172
173 g_atomic_int_set (&stopping, 0);
174
175 for (i = 0; i < n_threads; i++) {
176 GThread *thread;
177
178 thread = g_thread_create ((GThreadFunc) run_thread, test, TRUE, NULL);
179 g_array_append_val (test_threads, thread);
180 }
181 g_usleep (30000000);
182
183 g_atomic_int_set (&stopping, 1);
184 g_print ("\nstopping\n");
185
186 /* join all threads */
187 for (i = 0; i < n_threads; i++) {
188 GThread *thread;
189
190 thread = g_array_index (test_threads, GThread *, i);
191 g_thread_join (thread);
192 }
193
194 g_print ("stopped\n");
195
196 g_print ("%d %d\n", test->setcount, test->count);
197
198 g_array_free (test_threads, TRUE);
199 g_object_unref (test);
200
201 return 0;
202 }
203