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 #define GLIB_DISABLE_DEPRECATION_WARNINGS
17 /* On smcv's laptop, 1e4 iterations didn't always exhibit the bug, but 1e5
18 * iterations exhibited it 10/10 times in practice. YMMV. */
19 #define ITERATIONS 100000
21 static GStaticPrivate sp
;
26 static volatile gint freed
= 0;
31 if (!g_atomic_int_compare_and_exchange (&freed
, 0, 1))
33 g_error ("someone already freed it after %u iterations", i
);
37 static gpointer
thread_func (gpointer nil
)
39 /* wait for main thread to reach its g_cond_wait call */
42 g_static_private_set (&sp
, &sp
, notify
);
43 g_cond_broadcast (cond
);
44 g_mutex_unlock (mutex
);
52 g_test_bug ("642026");
54 mutex
= g_mutex_new ();
59 for (i
= 0; i
< ITERATIONS
; i
++)
63 g_static_private_init (&sp
);
66 t1
= g_thread_create (thread_func
, NULL
, TRUE
, NULL
);
67 g_assert (t1
!= NULL
);
69 /* wait for t1 to set up its thread-private data */
70 g_cond_wait (cond
, mutex
);
72 /* exercise the bug, by racing with t1 to free the private data */
73 g_static_private_free (&sp
);
78 g_mutex_unlock (mutex
);
86 g_test_init (&argc
, &argv
, NULL
);
87 g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
89 g_test_add_func ("/glib/642026", testcase
);