#define N_THREADS (13)
-static GMutex *tmutex = NULL;
-static GCond *tcond = NULL;
+static GMutex tmutex;
+static GCond tcond;
static volatile int thread_call_count = 0;
static char dummy_value = 'x';
assert_singleton_execution1 (void)
{
static volatile int seen_execution = 0;
- int old_seen_execution = g_atomic_int_exchange_and_add (&seen_execution, 1);
+ int old_seen_execution = g_atomic_int_add (&seen_execution, 1);
if (old_seen_execution != 0)
g_error ("%s: function executed more than once", G_STRFUNC);
}
assert_singleton_execution2 (void)
{
static volatile int seen_execution = 0;
- int old_seen_execution = g_atomic_int_exchange_and_add (&seen_execution, 1);
+ int old_seen_execution = g_atomic_int_add (&seen_execution, 1);
if (old_seen_execution != 0)
g_error ("%s: function executed more than once", G_STRFUNC);
}
assert_singleton_execution3 (void)
{
static volatile int seen_execution = 0;
- int old_seen_execution = g_atomic_int_exchange_and_add (&seen_execution, 1);
+ int old_seen_execution = g_atomic_int_add (&seen_execution, 1);
if (old_seen_execution != 0)
g_error ("%s: function executed more than once", G_STRFUNC);
}
static gpointer
tmain_call_initializer3 (gpointer user_data)
{
- g_mutex_lock (tmutex);
- g_cond_wait (tcond, tmutex);
- g_mutex_unlock (tmutex);
+ g_mutex_lock (&tmutex);
+ g_cond_wait (&tcond, &tmutex);
+ g_mutex_unlock (&tmutex);
//g_printf ("[");
initializer3();
//g_printf ("]\n");
- g_atomic_int_exchange_and_add (&thread_call_count, 1);
+ g_atomic_int_add (&thread_call_count, 1);
return NULL;
}
main (int argc,
char *argv[])
{
- GThread *threads[N_THREADS];
+ G_GNUC_UNUSED GThread *threads[N_THREADS];
int i;
+ void *p;
+
/* test simple initializer */
initializer1();
initializer1();
/* test pointer initializer */
- void *p = initializer2();
+ p = initializer2();
g_assert (p == &dummy_value);
p = initializer2();
g_assert (p == &dummy_value);
- /* setup threads */
- g_thread_init (NULL);
- tmutex = g_mutex_new ();
- tcond = g_cond_new ();
/* start multiple threads for initializer3() */
- g_mutex_lock (tmutex);
+ g_mutex_lock (&tmutex);
for (i = 0; i < N_THREADS; i++)
threads[i] = g_thread_create (tmain_call_initializer3, 0, FALSE, NULL);
- g_mutex_unlock (tmutex);
+ g_mutex_unlock (&tmutex);
/* concurrently call initializer3() */
- g_cond_broadcast (tcond);
+ g_cond_broadcast (&tcond);
/* loop until all threads passed the call to initializer3() */
while (g_atomic_int_get (&thread_call_count) < i)
{
g_thread_yield(); /* concurrent shuffling for single core */
else
g_usleep (1000); /* concurrent shuffling for multi core */
- g_cond_broadcast (tcond);
+ g_cond_broadcast (&tcond);
}
/* call multiple (unoptimized) initializers from multiple threads */
- g_mutex_lock (tmutex);
+ g_mutex_lock (&tmutex);
g_atomic_int_set (&thread_call_count, 0);
for (i = 0; i < N_THREADS; i++)
g_thread_create (stress_concurrent_initializers, 0, FALSE, NULL);
- g_mutex_unlock (tmutex);
+ g_mutex_unlock (&tmutex);
while (g_atomic_int_get (&thread_call_count) < 256 * 4 * N_THREADS)
g_usleep (50 * 1000); /* wait for all 5 threads to complete */
return 0;
* to uncover possible races in the g_once_init_enter_impl()/
* g_once_init_leave() implementations
*/
-#define g_once_init_enter g_once_init_enter_impl
+#undef g_once_init_enter
+#undef g_once_init_leave
/* define 16 * 16 simple initializers */
#define DEFINE_TEST_INITIALIZER(N) \
};
int i;
/* sync to main thread */
- g_mutex_lock (tmutex);
- g_mutex_unlock (tmutex);
+ g_mutex_lock (&tmutex);
+ g_mutex_unlock (&tmutex);
/* initialize concurrently */
for (i = 0; i < G_N_ELEMENTS (initializers); i++)
{
initializers[i]();
- g_atomic_int_exchange_and_add (&thread_call_count, 1);
+ g_atomic_int_add (&thread_call_count, 1);
}
return NULL;
}