2 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
3 * Copyright © 2011 Nokia Corporation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * See the included COPYING file for more information.
13 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
14 #define GLIB_DISABLE_DEPRECATION_WARNINGS
19 /* On smcv's laptop, 1e4 iterations didn't always exhibit the bug, but 1e5
20 * iterations exhibited it 10/10 times in practice. YMMV. */
21 #define ITERATIONS 100000
23 static GStaticPrivate sp;
28 static gint freed = 0; /* (atomic) */
33 if (!g_atomic_int_compare_and_exchange (&freed, 0, 1))
35 g_error ("someone already freed it after %u iterations", i);
39 static gpointer thread_func (gpointer nil)
41 /* wait for main thread to reach its g_cond_wait call */
44 g_static_private_set (&sp, &sp, notify);
45 g_cond_broadcast (cond);
46 g_mutex_unlock (mutex);
54 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=642026");
56 mutex = g_mutex_new ();
61 for (i = 0; i < ITERATIONS; i++)
65 g_static_private_init (&sp);
66 g_atomic_int_set (&freed, 0);
68 t1 = g_thread_create (thread_func, NULL, TRUE, NULL);
69 g_assert (t1 != NULL);
71 /* wait for t1 to set up its thread-private data */
72 g_cond_wait (cond, mutex);
74 /* exercise the bug, by racing with t1 to free the private data */
75 g_static_private_free (&sp);
80 g_mutex_unlock (mutex);
88 g_test_init (&argc, &argv, NULL);
90 g_test_add_func ("/glib/642026", testcase);