2 * Copyright © 2008 Ryan Lortie
3 * Copyright © 2010 Codethink Limited
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * See the included COPYING file for more information.
13 /* LOCKS should be more than the number of contention
14 * counters in gthread.c in order to ensure we exercise
15 * the case where they overlap.
18 #define ITERATIONS 10000
23 #if TEST_EMULATED_FUTEX
25 #pragma GCC diagnostic push
26 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
28 /* this is defined for the 1bit-mutex-emufutex test.
30 * we want to test the emulated futex even if futex(2) is available.
33 /* side-step some glib build stuff */
34 #define GLIB_COMPILATION
36 /* rebuild gbitlock.c without futex support,
37 defining our own version of the g_bit_*lock symbols
39 #undef g_pointer_bit_lock
40 #undef g_pointer_bit_trylock
41 #undef g_pointer_bit_unlock
43 #define g_bit_lock _emufutex_g_bit_lock
44 #define g_bit_trylock _emufutex_g_bit_trylock
45 #define g_bit_unlock _emufutex_g_bit_unlock
46 #define g_pointer_bit_lock _emufutex_g_pointer_bit_lock
47 #define g_pointer_bit_trylock _emufutex_g_pointer_bit_trylock
48 #define g_pointer_bit_unlock _emufutex_g_pointer_bit_unlock
50 #define G_BIT_LOCK_FORCE_FUTEX_EMULATION
52 #include <glib/gbitlock.c>
54 #pragma GCC diagnostic pop
57 volatile GThread *owners[LOCKS];
58 volatile gint locks[LOCKS];
59 volatile gpointer ptrs[LOCKS];
60 volatile gint bits[LOCKS];
64 gboolean use_pointers)
68 self = g_thread_self ();
70 g_assert_cmpint (((gsize) ptrs) % sizeof(gint), ==, 0);
73 g_pointer_bit_trylock (&ptrs[nr], bits[nr])
74 : g_bit_trylock (&locks[nr], bits[nr])))
76 if (g_test_verbose ())
77 g_print ("thread %p going to block on lock %d\n", self, nr);
80 g_pointer_bit_lock (&ptrs[nr], bits[nr]);
82 g_bit_lock (&locks[nr], bits[nr]);
85 g_assert (owners[nr] == NULL); /* hopefully nobody else is here */
88 /* let some other threads try to ruin our day */
93 g_assert (owners[nr] == self); /* hopefully this is still us... */
94 owners[nr] = NULL; /* make way for the next guy */
97 g_pointer_bit_unlock (&ptrs[nr], bits[nr]);
99 g_bit_unlock (&locks[nr], bits[nr]);
103 thread_func (gpointer data)
105 gboolean use_pointers = GPOINTER_TO_INT (data);
109 rand = g_rand_new ();
111 for (i = 0; i < ITERATIONS; i++)
112 acquire (g_rand_int_range (rand, 0, LOCKS), use_pointers);
120 testcase (gconstpointer data)
122 gboolean use_pointers = GPOINTER_TO_INT (data);
123 GThread *threads[THREADS];
126 #ifdef TEST_EMULATED_FUTEX
127 #define SUFFIX "-emufutex"
129 /* ensure that we are using the emulated futex by checking
130 * (at compile-time) for the existance of 'g_futex_address_list'
132 g_assert (g_futex_address_list == NULL);
137 for (i = 0; i < LOCKS; i++)
138 bits[i] = g_random_int () % 32;
140 for (i = 0; i < THREADS; i++)
141 threads[i] = g_thread_new ("foo", thread_func,
142 GINT_TO_POINTER (use_pointers));
144 for (i = 0; i < THREADS; i++)
145 g_thread_join (threads[i]);
147 for (i = 0; i < LOCKS; i++)
149 g_assert (owners[i] == NULL);
150 g_assert (locks[i] == 0);
155 main (int argc, char **argv)
157 g_test_init (&argc, &argv, NULL);
159 g_test_add_data_func ("/glib/1bit-mutex" SUFFIX "/int", (gpointer) 0, testcase);
160 g_test_add_data_func ("/glib/1bit-mutex" SUFFIX "/pointer", (gpointer) 1, testcase);
162 return g_test_run ();