Update Friulian translation
[glib.git] / tests / refcount / closures.c
blob25b0a27799cfa3ff5cd61f8686e20540595767f6
1 /* Copyright (C) 2005 Imendio AB
3 * This software is provided "as is"; redistribution and modification
4 * is permitted, provided that the following disclaimer is retained.
6 * This software is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9 * In no event shall the authors or contributors be liable for any
10 * direct, indirect, incidental, special, exemplary, or consequential
11 * damages (including, but not limited to, procurement of substitute
12 * goods or services; loss of use, data, or profits; or business
13 * interruption) however caused and on any theory of liability, whether
14 * in contract, strict liability, or tort (including negligence or
15 * otherwise) arising in any way out of the use of this software, even
16 * if advised of the possibility of such damage.
18 #include <glib-object.h>
20 #ifdef G_OS_UNIX
21 #include <unistd.h>
22 #endif
24 #define TEST_POINTER1 ((gpointer) 47)
25 #define TEST_POINTER2 ((gpointer) 49)
26 #define TEST_INT1 (-77)
27 #define TEST_INT2 (78)
29 /* --- GTest class --- */
30 typedef struct {
31 GObject object;
32 gint value;
33 gpointer test_pointer1;
34 gpointer test_pointer2;
35 } GTest;
36 typedef struct {
37 GObjectClass parent_class;
38 void (*test_signal1) (GTest * test, gint an_int);
39 void (*test_signal2) (GTest * test, gint an_int);
40 } GTestClass;
42 #define G_TYPE_TEST (my_test_get_type ())
43 #define MY_TEST(test) (G_TYPE_CHECK_INSTANCE_CAST ((test), G_TYPE_TEST, GTest))
44 #define MY_IS_TEST(test) (G_TYPE_CHECK_INSTANCE_TYPE ((test), G_TYPE_TEST))
45 #define MY_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_CAST ((tclass), G_TYPE_TEST, GTestClass))
46 #define MY_IS_TEST_CLASS(tclass) (G_TYPE_CHECK_CLASS_TYPE ((tclass), G_TYPE_TEST))
47 #define MY_TEST_GET_CLASS(test) (G_TYPE_INSTANCE_GET_CLASS ((test), G_TYPE_TEST, GTestClass))
49 static GType my_test_get_type (void);
50 G_DEFINE_TYPE (GTest, my_test, G_TYPE_OBJECT)
52 /* --- variables --- */
53 static volatile gboolean stopping = FALSE;
54 static guint test_signal1 = 0;
55 static guint test_signal2 = 0;
56 static gboolean seen_signal_handler = FALSE;
57 static gboolean seen_cleanup = FALSE;
58 static gboolean seen_test_int1 = FALSE;
59 static gboolean seen_test_int2 = FALSE;
60 static gboolean seen_thread1 = FALSE;
61 static gboolean seen_thread2 = FALSE;
63 /* --- functions --- */
64 static void
65 my_test_init (GTest * test)
67 g_print ("init %p\n", test);
69 test->value = 0;
70 test->test_pointer1 = TEST_POINTER1;
71 test->test_pointer2 = TEST_POINTER2;
74 enum {
75 ARG_0,
76 ARG_TEST_PROP
79 static void
80 my_test_set_property (GObject *object,
81 guint prop_id,
82 const GValue *value,
83 GParamSpec *pspec)
85 GTest *test = MY_TEST (object);
86 switch (prop_id)
88 case ARG_TEST_PROP:
89 test->value = g_value_get_int (value);
90 break;
91 default:
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 break;
97 static void
98 my_test_get_property (GObject *object,
99 guint prop_id,
100 GValue *value,
101 GParamSpec *pspec)
103 GTest *test = MY_TEST (object);
104 switch (prop_id)
106 case ARG_TEST_PROP:
107 g_value_set_int (value, test->value);
108 break;
109 default:
110 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
111 break;
115 static void
116 my_test_test_signal2 (GTest *test,
117 gint an_int)
121 static void
122 my_test_emit_test_signal1 (GTest *test,
123 gint vint)
125 g_signal_emit (G_OBJECT (test), test_signal1, 0, vint);
128 static void
129 my_test_emit_test_signal2 (GTest *test,
130 gint vint)
132 g_signal_emit (G_OBJECT (test), test_signal2, 0, vint);
135 static void
136 my_test_class_init (GTestClass *klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 gobject_class->set_property = my_test_set_property;
141 gobject_class->get_property = my_test_get_property;
143 test_signal1 = g_signal_new ("test-signal1", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
144 G_STRUCT_OFFSET (GTestClass, test_signal1), NULL, NULL,
145 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
146 test_signal2 = g_signal_new ("test-signal2", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
147 G_STRUCT_OFFSET (GTestClass, test_signal2), NULL, NULL,
148 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
150 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TEST_PROP,
151 g_param_spec_int ("test-prop", "Test Prop", "Test property",
152 0, 1, 0, G_PARAM_READWRITE));
153 klass->test_signal2 = my_test_test_signal2;
156 static inline guint32
157 quick_rand32 (void)
159 static guint32 accu = 2147483563;
160 accu = 1664525 * accu + 1013904223;
161 return accu;
164 static void
165 test_closure (GClosure *closure)
167 /* try to produce high contention in closure->ref_count */
168 guint i = 0, n = quick_rand32() % 199;
169 for (i = 0; i < n; i++)
170 g_closure_ref (closure);
171 g_closure_sink (closure); /* NOP */
172 for (i = 0; i < n; i++)
173 g_closure_unref (closure);
176 static gpointer
177 thread1_main (gpointer data)
179 GClosure *closure = data;
180 while (!stopping)
182 static guint count = 0;
183 test_closure (closure);
184 if (++count % 10000 == 0)
186 g_printerr ("c");
187 g_thread_yield(); /* force context switch */
188 seen_thread1 = TRUE;
191 return NULL;
194 static gpointer
195 thread2_main (gpointer data)
197 GClosure *closure = data;
198 while (!stopping)
200 static guint count = 0;
201 test_closure (closure);
202 if (++count % 10000 == 0)
204 g_printerr ("C");
205 g_thread_yield(); /* force context switch */
206 seen_thread2 = TRUE;
209 return NULL;
212 static void
213 test_signal_handler (GTest *test,
214 gint vint,
215 gpointer data)
217 g_assert (data == TEST_POINTER2);
218 g_assert (test->test_pointer1 == TEST_POINTER1);
219 seen_signal_handler = TRUE;
220 seen_test_int1 |= vint == TEST_INT1;
221 seen_test_int2 |= vint == TEST_INT2;
224 static void
225 destroy_data (gpointer data,
226 GClosure *closure)
228 seen_cleanup = data == TEST_POINTER2;
229 g_assert (closure->ref_count == 0);
232 static void
233 test_emissions (GTest *test)
235 my_test_emit_test_signal1 (test, TEST_INT1);
236 my_test_emit_test_signal2 (test, TEST_INT2);
240 main (int argc,
241 char **argv)
243 GThread *thread1, *thread2;
244 GClosure *closure;
245 GTest *object;
246 guint i;
248 g_print ("START: %s\n", argv[0]);
249 g_log_set_always_fatal (G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL | g_log_set_always_fatal (G_LOG_FATAL_MASK));
251 object = g_object_new (G_TYPE_TEST, NULL);
252 closure = g_cclosure_new (G_CALLBACK (test_signal_handler), TEST_POINTER2, destroy_data);
254 g_signal_connect_closure (object, "test-signal1", closure, FALSE);
255 g_signal_connect_closure (object, "test-signal2", closure, FALSE);
257 stopping = FALSE;
259 thread1 = g_thread_create (thread1_main, closure, TRUE, NULL);
260 thread2 = g_thread_create (thread2_main, closure, TRUE, NULL);
262 for (i = 0; i < 1000000; i++)
264 static guint count = 0;
265 test_emissions (object);
266 if (++count % 10000 == 0)
268 g_printerr (".\n");
269 g_thread_yield(); /* force context switch */
273 stopping = TRUE;
274 g_print ("\nstopping\n");
276 /* wait for thread shutdown */
277 g_thread_join (thread1);
278 g_thread_join (thread2);
280 /* finalize object, destroy signals, run cleanup code */
281 g_object_unref (object);
283 g_print ("stopped\n");
285 g_assert (seen_thread1 != FALSE);
286 g_assert (seen_thread2 != FALSE);
287 g_assert (seen_test_int1 != FALSE);
288 g_assert (seen_test_int2 != FALSE);
289 g_assert (seen_signal_handler != FALSE);
290 g_assert (seen_cleanup != FALSE);
292 return 0;