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))
16 typedef struct _GTest GTest
;
17 typedef struct _GTestClass 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
,
43 static void my_test_set_property (GObject
*object
,
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
,
60 g_param_spec_int ("dummy",
68 my_test_init (GTest
* test
)
70 static guint static_id
= 1;
71 test
->id
= static_id
++;
75 my_test_get_property (GObject
*object
,
82 test
= MY_TEST (object
);
87 g_value_set_int (value
, g_atomic_int_get (&test
->dummy
));
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
96 my_test_set_property (GObject
*object
,
103 test
= MY_TEST (object
);
108 g_atomic_int_set (&test
->dummy
, g_value_get_int (value
));
111 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
117 dummy_notify (GObject
*object
,
122 test
= MY_TEST (object
);
124 g_atomic_int_inc (&test
->count
);
128 my_test_do_property (GTest
* test
)
132 g_atomic_int_inc (&test
->setcount
);
134 g_object_get (test
, "dummy", &dummy
, NULL
);
135 g_object_set (test
, "dummy", dummy
+ 1, NULL
);
139 run_thread (GTest
* test
)
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 */
156 main (int argc
, char **argv
)
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
*));
175 for (i
= 0; i
< n_threads
; i
++) {
178 thread
= g_thread_create ((GThreadFunc
) run_thread
, test
, TRUE
, NULL
);
179 g_array_append_val (test_threads
, thread
);
184 g_print ("\nstopping\n");
186 /* join all threads */
187 for (i
= 0; i
< n_threads
; i
++) {
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
);