Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / tests / refcount / properties3.c
blobbc8820661313ac15099fd1f3816faa3d61e4b390
1 #include <glib.h>
2 #include <glib-object.h>
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))
11 enum {
12 PROP_0,
13 PROP_DUMMY
16 typedef struct _GTest GTest;
17 typedef struct _GTestClass GTestClass;
19 struct _GTest
21 GObject object;
22 gint id;
23 gint dummy;
25 gint count;
26 gint setcount;
29 struct _GTestClass
31 GObjectClass parent_class;
34 static GType my_test_get_type (void);
35 G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
37 static volatile gboolean stopping;
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);
48 static void
49 my_test_class_init (GTestClass * klass)
51 GObjectClass *gobject_class;
53 gobject_class = (GObjectClass *) klass;
55 gobject_class->get_property = my_test_get_property;
56 gobject_class->set_property = my_test_set_property;
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));
67 static void
68 my_test_init (GTest * test)
70 static guint static_id = 1;
71 test->id = static_id++;
74 static void
75 my_test_get_property (GObject *object,
76 guint prop_id,
77 GValue *value,
78 GParamSpec *pspec)
80 GTest *test;
82 test = MY_TEST (object);
84 switch (prop_id)
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;
95 static void
96 my_test_set_property (GObject *object,
97 guint prop_id,
98 const GValue *value,
99 GParamSpec *pspec)
101 GTest *test;
103 test = MY_TEST (object);
105 switch (prop_id)
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;
116 static void
117 dummy_notify (GObject *object,
118 GParamSpec *pspec)
120 GTest *test;
122 test = MY_TEST (object);
124 g_atomic_int_inc (&test->count);
127 static void
128 my_test_do_property (GTest * test)
130 gint dummy;
132 g_atomic_int_inc (&test->setcount);
134 g_object_get (test, "dummy", &dummy, NULL);
135 g_object_set (test, "dummy", dummy + 1, NULL);
138 static gpointer
139 run_thread (GTest * test)
141 gint i = 1;
143 while (!stopping) {
144 my_test_do_property (test);
145 if ((i++ % 10000) == 0)
147 g_print (".%c", 'a' + test->id);
148 g_thread_yield(); /* force context switch */
152 return NULL;
156 main (int argc, char **argv)
158 gint i;
159 GTest *test;
160 GArray *test_threads;
161 const gint n_threads = 5;
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));
166 test = g_object_new (G_TYPE_TEST, NULL);
168 g_assert (test->count == test->dummy);
169 g_signal_connect (test, "notify::dummy", G_CALLBACK (dummy_notify), NULL);
171 test_threads = g_array_new (FALSE, FALSE, sizeof (GThread *));
173 stopping = FALSE;
175 for (i = 0; i < n_threads; i++) {
176 GThread *thread;
178 thread = g_thread_create ((GThreadFunc) run_thread, test, TRUE, NULL);
179 g_array_append_val (test_threads, thread);
181 g_usleep (30000000);
183 stopping = TRUE;
184 g_print ("\nstopping\n");
186 /* join all threads */
187 for (i = 0; i < n_threads; i++) {
188 GThread *thread;
190 thread = g_array_index (test_threads, GThread *, i);
191 g_thread_join (thread);
194 g_print ("stopped\n");
196 g_print ("%d %d\n", test->setcount, test->count);
198 g_array_free (test_threads, TRUE);
199 g_object_unref (test);
201 return 0;