2 /* Unit tests for GOnce and friends
3 * Copyright (C) 2011 Red Hat, Inc
4 * Author: Matthias Clasen
6 * This work is provided "as is"; redistribution and modification
7 * in whole or in part, in any medium, physical or electronic is
8 * permitted without restriction.
10 * This work is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * In no event shall the authors or contributors be liable for any
15 * direct, indirect, incidental, special, exemplary, or consequential
16 * damages (including, but not limited to, procurement of substitute
17 * goods or services; loss of use, data, or profits; or business
18 * interruption) however caused and on any theory of liability, whether
19 * in contract, strict liability, or tort (including negligence or
20 * otherwise) arising in any way out of the use of this software, even
21 * if advised of the possibility of such damage.
26 #if GLIB_SIZEOF_VOID_P > 4
33 do_once (gpointer data)
39 return GINT_TO_POINTER (i);
43 test_once_single_threaded (void)
45 GOnce once = G_ONCE_INIT;
48 g_test_summary ("Test g_once() usage from a single thread");
50 g_assert (once.status == G_ONCE_STATUS_NOTCALLED);
52 res = g_once (&once, do_once, NULL);
53 g_assert_cmpint (GPOINTER_TO_INT (res), ==, 1);
55 g_assert (once.status == G_ONCE_STATUS_READY);
57 res = g_once (&once, do_once, NULL);
58 g_assert_cmpint (GPOINTER_TO_INT (res), ==, 1);
61 static GOnce once_multi_threaded = G_ONCE_INIT;
62 static gint once_multi_threaded_counter = 0;
63 static GCond once_multi_threaded_cond;
64 static GMutex once_multi_threaded_mutex;
65 static guint once_multi_threaded_n_threads_waiting = 0;
68 do_once_multi_threaded (gpointer data)
72 /* While this function should only ever be executed once, by one thread,
73 * we should use atomics to ensure that if there were a bug, writes to
74 * `once_multi_threaded_counter` from multiple threads would not get lost and
75 * mean the test erroneously succeeded. */
76 old_value = g_atomic_int_add (&once_multi_threaded_counter, 1);
78 return GINT_TO_POINTER (old_value + 1);
82 once_thread_func (gpointer data)
85 guint n_threads_expected = GPOINTER_TO_UINT (data);
87 /* Don’t immediately call g_once(), otherwise the first thread to be created
88 * will end up calling the once-function, and there will be very little
90 g_mutex_lock (&once_multi_threaded_mutex);
92 once_multi_threaded_n_threads_waiting++;
93 g_cond_broadcast (&once_multi_threaded_cond);
95 while (once_multi_threaded_n_threads_waiting < n_threads_expected)
96 g_cond_wait (&once_multi_threaded_cond, &once_multi_threaded_mutex);
97 g_mutex_unlock (&once_multi_threaded_mutex);
99 /* Actually run the test. */
100 res = g_once (&once_multi_threaded, do_once_multi_threaded, NULL);
101 g_assert_cmpint (GPOINTER_TO_INT (res), ==, 1);
107 test_once_multi_threaded (void)
110 GThread *threads[THREADS];
112 g_test_summary ("Test g_once() usage from multiple threads");
114 for (i = 0; i < G_N_ELEMENTS (threads); i++)
115 threads[i] = g_thread_new ("once-multi-threaded",
117 GUINT_TO_POINTER (G_N_ELEMENTS (threads)));
119 /* All threads have started up, so start the test. */
120 g_cond_broadcast (&once_multi_threaded_cond);
122 for (i = 0; i < G_N_ELEMENTS (threads); i++)
123 g_thread_join (threads[i]);
125 g_assert_cmpint (g_atomic_int_get (&once_multi_threaded_counter), ==, 1);
129 test_once_init_single_threaded (void)
131 static gsize init = 0;
133 g_test_summary ("Test g_once_init_{enter,leave}() usage from a single thread");
135 if (g_once_init_enter (&init))
138 g_once_init_leave (&init, 1);
141 g_assert_cmpint (init, ==, 1);
142 if (g_once_init_enter (&init))
144 g_assert_not_reached ();
145 g_once_init_leave (&init, 2);
147 g_assert_cmpint (init, ==, 1);
150 static gint64 shared;
155 static gsize init = 0;
157 if (g_once_init_enter (&init))
161 g_once_init_leave (&init, 1);
166 thread_func (gpointer data)
174 test_once_init_multi_threaded (void)
177 GThread *threads[THREADS];
179 g_test_summary ("Test g_once_init_{enter,leave}() usage from multiple threads");
183 for (i = 0; i < G_N_ELEMENTS (threads); i++)
184 threads[i] = g_thread_new ("once-init-multi-threaded", thread_func, NULL);
186 for (i = 0; i < G_N_ELEMENTS (threads); i++)
187 g_thread_join (threads[i]);
189 g_assert_cmpint (shared, ==, 42);
193 test_once_init_string (void)
195 static const gchar *val;
197 g_test_summary ("Test g_once_init_{enter,leave}() usage with a string");
199 if (g_once_init_enter (&val))
200 g_once_init_leave (&val, "foo");
202 g_assert_cmpstr (val, ==, "foo");
206 main (int argc, char *argv[])
208 g_test_init (&argc, &argv, NULL);
210 g_test_add_func ("/once/single-threaded", test_once_single_threaded);
211 g_test_add_func ("/once/multi-threaded", test_once_multi_threaded);
212 g_test_add_func ("/once-init/single-threaded", test_once_init_single_threaded);
213 g_test_add_func ("/once-init/multi-threaded", test_once_init_multi_threaded);
214 g_test_add_func ("/once-init/string", test_once_init_string);
216 return g_test_run ();