Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / tests / refcount / properties.c
blobc682342166ab55b150965b620eaf117352dc7734
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 enum {
16 PROP_0,
17 PROP_DUMMY
20 typedef struct _GTest GTest;
21 typedef struct _GTestClass GTestClass;
23 struct _GTest
25 GObject object;
26 gint id;
27 gint dummy;
29 gint count;
32 struct _GTestClass
34 GObjectClass parent_class;
37 static GType my_test_get_type (void);
38 static volatile gboolean stopping;
40 static void my_test_class_init (GTestClass * klass);
41 static void my_test_init (GTest * test);
42 static void my_test_dispose (GObject * object);
43 static void my_test_get_property (GObject *object,
44 guint prop_id,
45 GValue *value,
46 GParamSpec *pspec);
47 static void my_test_set_property (GObject *object,
48 guint prop_id,
49 const GValue *value,
50 GParamSpec *pspec);
52 static GObjectClass *parent_class = NULL;
54 static GType
55 my_test_get_type (void)
57 static GType test_type = 0;
59 if (!test_type) {
60 const GTypeInfo test_info = {
61 sizeof (GTestClass),
62 NULL,
63 NULL,
64 (GClassInitFunc) my_test_class_init,
65 NULL,
66 NULL,
67 sizeof (GTest),
69 (GInstanceInitFunc) my_test_init,
70 NULL
73 test_type = g_type_register_static (G_TYPE_OBJECT, "GTest", &test_info, 0);
75 return test_type;
78 static void
79 my_test_class_init (GTestClass * klass)
81 GObjectClass *gobject_class;
83 gobject_class = (GObjectClass *) klass;
85 parent_class = g_type_class_ref (G_TYPE_OBJECT);
87 gobject_class->dispose = my_test_dispose;
88 gobject_class->get_property = my_test_get_property;
89 gobject_class->set_property = my_test_set_property;
91 g_object_class_install_property (gobject_class,
92 PROP_DUMMY,
93 g_param_spec_int ("dummy",
94 NULL,
95 NULL,
96 0, G_MAXINT, 0,
97 G_PARAM_READWRITE));
100 static void
101 my_test_init (GTest * test)
103 static guint static_id = 1;
104 test->id = static_id++;
107 static void
108 my_test_dispose (GObject * object)
110 G_OBJECT_CLASS (parent_class)->dispose (object);
113 static void
114 my_test_get_property (GObject *object,
115 guint prop_id,
116 GValue *value,
117 GParamSpec *pspec)
119 GTest *test;
121 test = MY_TEST (object);
123 switch (prop_id)
125 case PROP_DUMMY:
126 g_value_set_int (value, test->dummy);
127 break;
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 break;
134 static void
135 my_test_set_property (GObject *object,
136 guint prop_id,
137 const GValue *value,
138 GParamSpec *pspec)
140 GTest *test;
142 test = MY_TEST (object);
144 switch (prop_id)
146 case PROP_DUMMY:
147 test->dummy = g_value_get_int (value);
148 break;
149 default:
150 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151 break;
155 static void
156 dummy_notify (GObject *object,
157 GParamSpec *pspec)
159 GTest *test;
161 test = MY_TEST (object);
163 test->count++;
166 static void
167 my_test_do_property (GTest * test)
169 gint dummy;
171 g_object_get (test, "dummy", &dummy, NULL);
172 g_object_set (test, "dummy", dummy + 1, NULL);
175 static gpointer
176 run_thread (GTest * test)
178 gint i = 1;
180 while (!stopping) {
181 my_test_do_property (test);
182 if ((i++ % 10000) == 0)
184 g_print (".%c", 'a' + test->id);
185 g_thread_yield(); /* force context switch */
189 return NULL;
193 main (int argc, char **argv)
195 #define N_THREADS 5
196 GThread *test_threads[N_THREADS];
197 GTest *test_objects[N_THREADS];
198 gint i;
200 g_print ("START: %s\n", argv[0]);
201 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
203 for (i = 0; i < N_THREADS; i++) {
204 GTest *test;
206 test = g_object_new (G_TYPE_TEST, NULL);
207 test_objects[i] = test;
209 g_assert (test->count == test->dummy);
210 g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
213 stopping = FALSE;
215 for (i = 0; i < N_THREADS; i++)
216 test_threads[i] = g_thread_create ((GThreadFunc) run_thread, test_objects[i], TRUE, NULL);
218 g_usleep (3000000);
220 stopping = TRUE;
221 g_print ("\nstopping\n");
223 /* join all threads */
224 for (i = 0; i < N_THREADS; i++)
225 g_thread_join (test_threads[i]);
227 g_print ("stopped\n");
229 for (i = 0; i < N_THREADS; i++) {
230 GTest *test = test_objects[i];
232 g_assert (test->count == test->dummy);
233 g_object_unref (test);
236 return 0;