Increase the timeout for some GLib tests
[glib.git] / glib / tests / private.c
blob6e6eba167afeaa70382f991e0d796f1624a769cd
1 /* Unit tests for GPrivate and friends
2 * Copyright (C) 2011 Red Hat, Inc
3 * Author: Matthias Clasen
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 /* We are testing some deprecated APIs here */
24 #define GLIB_DISABLE_DEPRECATION_WARNINGS
26 #include <glib.h>
28 /* test basics:
29 * - initial value is NULL
30 * - set/get work repeatedly
32 static void
33 test_private1 (void)
35 static GPrivate private = G_PRIVATE_INIT (NULL);
36 gpointer value;
38 value = g_private_get (&private);
39 g_assert (value == NULL);
41 g_private_set (&private, GINT_TO_POINTER(1));
42 value = g_private_get (&private);
43 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 1);
45 g_private_set (&private, GINT_TO_POINTER(2));
46 value = g_private_get (&private);
47 g_assert_cmpint (GPOINTER_TO_INT (value), ==, 2);
50 static gint private2_destroy_count;
52 static void
53 private2_destroy (gpointer data)
55 g_atomic_int_inc (&private2_destroy_count);
58 static GPrivate private2 = G_PRIVATE_INIT (private2_destroy);
60 static gpointer
61 private2_func (gpointer data)
63 gint value = GPOINTER_TO_INT (data);
64 gint i;
65 gint v, v2;
67 for (i = 0; i < 1000; i++)
69 v = value + (i % 5);
70 g_private_set (&private2, GINT_TO_POINTER (v));
71 g_usleep (1000);
72 v2 = GPOINTER_TO_INT (g_private_get (&private2));
73 g_assert_cmpint (v, ==, v2);
76 if (value % 2 == 0)
77 g_thread_exit (NULL);
79 return NULL;
82 /* test that
83 * - threads do not interfere with each other
84 * - destroy notifies are called for each thread exit
85 * - destroy notifies are called for g_thread_exit() too
86 * - destroy notifies are not called on g_private_set()
87 * - destroy notifies are called on g_private_replace()
89 static void
90 test_private2 (void)
92 GThread *thread[10];
93 gint i;
95 g_private_set (&private2, GINT_TO_POINTER (234));
96 g_private_replace (&private2, GINT_TO_POINTER (123));
98 for (i = 0; i < 10; i++)
99 thread[i] = g_thread_create (private2_func, GINT_TO_POINTER (i), TRUE, NULL);
101 for (i = 0; i < 10; i++)
102 g_thread_join (thread[i]);
104 g_assert_cmpint (private2_destroy_count, ==, 11);
107 static gboolean private3_freed;
109 static void
110 private3_free (gpointer data)
112 g_assert (data == (void*) 0x1234);
113 private3_freed = TRUE;
116 #ifdef G_OS_WIN32
117 #include <windows.h>
118 #include <process.h>
120 static guint __stdcall
121 #else
122 #include <pthread.h>
124 static gpointer
125 #endif
126 private3_func (gpointer data)
128 static GPrivate key = G_PRIVATE_INIT (private3_free);
130 g_private_set (&key, (void *) 0x1234);
132 return 0;
135 static void
136 test_private3 (void)
138 g_assert (!private3_freed);
140 #ifdef G_OS_WIN32
142 HANDLE thread;
143 guint ignore;
144 thread = (HANDLE) _beginthreadex (NULL, 0, private3_func, NULL, 0, &ignore);
145 WaitForSingleObject (thread, INFINITE);
146 CloseHandle (thread);
148 #else
150 pthread_t thread;
152 pthread_create (&thread, NULL, private3_func, NULL);
153 pthread_join (thread, NULL);
155 #endif
156 g_assert (private3_freed);
159 /* test basics:
160 * - static initialization works
161 * - initial value is NULL
162 * - get/set works repeatedly
164 static GStaticPrivate sp1 = G_STATIC_PRIVATE_INIT;
166 static void
167 test_static_private1 (void)
169 gpointer value;
171 value = g_static_private_get (&sp1);
172 g_assert (value == NULL);
174 g_static_private_set (&sp1, GINT_TO_POINTER(1), NULL);
175 value = g_static_private_get (&sp1);
176 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
178 g_static_private_set (&sp1, GINT_TO_POINTER(2), NULL);
179 value = g_static_private_get (&sp1);
180 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
182 g_static_private_free (&sp1);
184 value = g_static_private_get (&sp1);
185 g_assert (value == NULL);
188 static gint sp2_destroy_count;
190 static void
191 sp2_destroy (gpointer data)
193 sp2_destroy_count++;
196 static void
197 sp2_destroy2 (gpointer data)
199 gint value = GPOINTER_TO_INT (data);
201 g_assert_cmpint (value, ==, 2);
204 /* test that destroy notifies are called as expected
205 * and on the right values
207 static void
208 test_static_private2 (void)
210 GStaticPrivate sp2;
211 gpointer value;
213 g_static_private_init (&sp2);
215 value = g_static_private_get (&sp2);
216 g_assert (value == NULL);
218 g_static_private_set (&sp2, GINT_TO_POINTER(1), sp2_destroy);
219 g_assert_cmpint (sp2_destroy_count, ==, 0);
220 value = g_static_private_get (&sp2);
221 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
223 g_static_private_set (&sp2, GINT_TO_POINTER(2), sp2_destroy2);
224 g_assert_cmpint (sp2_destroy_count, ==, 1);
225 value = g_static_private_get (&sp2);
226 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
228 g_static_private_set (&sp2, GINT_TO_POINTER(3), sp2_destroy);
229 g_assert_cmpint (sp2_destroy_count, ==, 1);
230 value = g_static_private_get (&sp2);
231 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 3);
233 g_static_private_free (&sp2);
235 value = g_static_private_get (&sp2);
236 g_assert (value == NULL);
239 /* test that freeing and reinitializing a static private
240 * drops previous value
242 static void
243 test_static_private3 (void)
245 GStaticPrivate sp3;
246 gpointer value;
248 g_static_private_init (&sp3);
250 value = g_static_private_get (&sp3);
251 g_assert (value == NULL);
253 g_static_private_set (&sp3, GINT_TO_POINTER(1), NULL);
254 value = g_static_private_get (&sp3);
255 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 1);
257 g_static_private_free (&sp3);
258 g_static_private_init (&sp3);
260 value = g_static_private_get (&sp3);
261 g_assert (value == NULL);
263 g_static_private_set (&sp3, GINT_TO_POINTER(2), NULL);
264 value = g_static_private_get (&sp3);
265 g_assert_cmpint (GPOINTER_TO_INT(value), ==, 2);
267 g_static_private_free (&sp3);
270 static GStaticPrivate sp4 = G_STATIC_PRIVATE_INIT;
272 static gpointer
273 sp4_func (gpointer data)
275 gint value = GPOINTER_TO_INT (data);
276 gint i;
277 gint v, v2;
279 for (i = 0; i < 1000; i++)
281 v = value + (i % 5);
282 g_static_private_set (&sp4, GINT_TO_POINTER(v), NULL);
283 g_usleep (1000);
284 v2 = GPOINTER_TO_INT(g_static_private_get (&sp4));
285 g_assert_cmpint (v, ==, v2);
288 if (value % 2 == 0)
289 g_thread_exit (NULL);
291 return NULL;
294 /* test that threads do not interfere with each other
296 static void
297 test_static_private4 (void)
299 GThread *thread[10];
300 gint i;
302 for (i = 0; i < 10; i++)
303 thread[i] = g_thread_create (sp4_func, GINT_TO_POINTER (i), TRUE, NULL);
305 for (i = 0; i < 10; i++)
306 g_thread_join (thread[i]);
308 g_static_private_free (&sp4);
311 static GStaticPrivate sp5 = G_STATIC_PRIVATE_INIT;
312 static GMutex m5;
313 static GCond c5a;
314 static GCond c5b;
315 static gint count5;
317 static gpointer
318 sp5_func (gpointer data)
320 gint v = GPOINTER_TO_INT (data);
321 gpointer value;
323 value = g_static_private_get (&sp5);
324 g_assert (value == NULL);
326 g_static_private_set (&sp5, GINT_TO_POINTER (v), NULL);
327 value = g_static_private_get (&sp5);
328 g_assert_cmpint (GPOINTER_TO_INT (value), ==, v);
330 if (g_test_verbose ())
331 g_printerr ("thread %d set sp5\n", v);
332 g_mutex_lock (&m5);
333 g_atomic_int_inc (&count5);
334 g_cond_signal (&c5a);
335 g_cond_wait (&c5b, &m5);
336 g_mutex_unlock (&m5);
338 if (g_test_verbose ())
339 g_printerr ("thread %d get sp5\n", v);
340 value = g_static_private_get (&sp5);
341 g_assert (value == NULL);
343 return NULL;
346 static void
347 test_static_private5 (void)
349 GThread *thread[10];
350 gint i;
352 g_atomic_int_set (&count5, 0);
354 for (i = 0; i < 10; i++)
355 thread[i] = g_thread_create (sp5_func, GINT_TO_POINTER (i), TRUE, NULL);
357 g_mutex_lock (&m5);
358 while (g_atomic_int_get (&count5) < 10)
359 g_cond_wait (&c5a, &m5);
361 if (g_test_verbose ())
362 g_printerr ("sp5 gets nuked\n");
364 g_static_private_free (&sp5);
366 g_cond_broadcast (&c5b);
367 g_mutex_unlock (&m5);
369 for (i = 0; i < 10; i++)
370 g_thread_join (thread[i]);
374 main (int argc, char *argv[])
376 g_test_init (&argc, &argv, NULL);
378 g_test_add_func ("/thread/private1", test_private1);
379 g_test_add_func ("/thread/private2", test_private2);
380 g_test_add_func ("/thread/private3", test_private3);
381 g_test_add_func ("/thread/staticprivate1", test_static_private1);
382 g_test_add_func ("/thread/staticprivate2", test_static_private2);
383 g_test_add_func ("/thread/staticprivate3", test_static_private3);
384 g_test_add_func ("/thread/staticprivate4", test_static_private4);
385 g_test_add_func ("/thread/staticprivate5", test_static_private5);
387 return g_test_run ();