mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-25 06:56:14 +01:00
tests: merge gthread/ and glib/ atomic tests
This commit is contained in:
parent
f5e8694782
commit
fb4e120d88
@ -1,3 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2011 Red Hat, Inc.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as
|
||||||
|
* published by the Free Software Foundation; either version 2 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* See the included COPYING file for more information.
|
||||||
|
*/
|
||||||
|
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -184,8 +195,54 @@ test_types (void)
|
|||||||
gs2 = g_atomic_pointer_xor (&gs, 4);
|
gs2 = g_atomic_pointer_xor (&gs, 4);
|
||||||
g_assert (gs2 == 12);
|
g_assert (gs2 == 12);
|
||||||
g_assert (gs == 8);
|
g_assert (gs == 8);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
#define THREADS 10
|
||||||
|
#define ROUNDS 10000
|
||||||
|
|
||||||
|
volatile gint bucket[THREADS];
|
||||||
|
volatile gint atomic;
|
||||||
|
|
||||||
|
static gpointer
|
||||||
|
thread_func (gpointer data)
|
||||||
|
{
|
||||||
|
gint idx = GPOINTER_TO_INT (data);
|
||||||
|
gint i;
|
||||||
|
gint d;
|
||||||
|
|
||||||
|
for (i = 0; i < ROUNDS; i++)
|
||||||
|
{
|
||||||
|
d = g_random_int_range (-10, 100);
|
||||||
|
bucket[idx] += d;
|
||||||
|
g_atomic_int_add (&atomic, d);
|
||||||
|
g_thread_yield ();
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_threaded (void)
|
||||||
|
{
|
||||||
|
gint sum;
|
||||||
|
gint i;
|
||||||
|
GThread *threads[THREADS];
|
||||||
|
|
||||||
|
atomic = 0;
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
bucket[i] = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
threads[i] = g_thread_new ("atomic", thread_func, GINT_TO_POINTER (i));
|
||||||
|
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
g_thread_join (threads[i]);
|
||||||
|
|
||||||
|
sum = 0;
|
||||||
|
for (i = 0; i < THREADS; i++)
|
||||||
|
sum += bucket[i];
|
||||||
|
|
||||||
|
g_assert_cmpint (sum, ==, atomic);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -194,6 +251,7 @@ main (int argc, char **argv)
|
|||||||
g_test_init (&argc, &argv, NULL);
|
g_test_init (&argc, &argv, NULL);
|
||||||
|
|
||||||
g_test_add_func ("/atomic/types", test_types);
|
g_test_add_func ("/atomic/types", test_types);
|
||||||
|
g_test_add_func ("/atomic/threaded", test_threaded);
|
||||||
|
|
||||||
return g_test_run ();
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
@ -32,10 +32,6 @@ unix_multithreaded_CFLAGS = -DTEST_THREADED
|
|||||||
unix_multithreaded_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
|
unix_multithreaded_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
|
||||||
endif
|
endif
|
||||||
|
|
||||||
TEST_PROGS += atomic
|
|
||||||
atomic_SOURCES = atomic.c
|
|
||||||
atomic_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
|
|
||||||
|
|
||||||
TEST_PROGS += spawn-multithreaded
|
TEST_PROGS += spawn-multithreaded
|
||||||
spawn_multithreaded_SOURCES = spawn-multithreaded.c
|
spawn_multithreaded_SOURCES = spawn-multithreaded.c
|
||||||
spawn_multithreaded_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
|
spawn_multithreaded_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
|
||||||
|
@ -1,70 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2011 Red Hat, Inc.
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Lesser General Public License as
|
|
||||||
* published by the Free Software Foundation; either version 2 of the
|
|
||||||
* License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* See the included COPYING file for more information.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
#define THREADS 10
|
|
||||||
#define ROUNDS 10000
|
|
||||||
|
|
||||||
volatile gint bucket[THREADS];
|
|
||||||
volatile gint atomic;
|
|
||||||
|
|
||||||
static gpointer
|
|
||||||
thread_func (gpointer data)
|
|
||||||
{
|
|
||||||
gint idx = GPOINTER_TO_INT (data);
|
|
||||||
gint i;
|
|
||||||
gint d;
|
|
||||||
|
|
||||||
for (i = 0; i < ROUNDS; i++)
|
|
||||||
{
|
|
||||||
d = g_random_int_range (-10, 100);
|
|
||||||
bucket[idx] += d;
|
|
||||||
g_atomic_int_add (&atomic, d);
|
|
||||||
g_thread_yield ();
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
test_atomic (void)
|
|
||||||
{
|
|
||||||
gint sum;
|
|
||||||
gint i;
|
|
||||||
GThread *threads[THREADS];
|
|
||||||
|
|
||||||
atomic = 0;
|
|
||||||
for (i = 0; i < THREADS; i++)
|
|
||||||
bucket[i] = 0;
|
|
||||||
|
|
||||||
for (i = 0; i < THREADS; i++)
|
|
||||||
threads[i] = g_thread_new ("atomic", thread_func, GINT_TO_POINTER (i));
|
|
||||||
|
|
||||||
for (i = 0; i < THREADS; i++)
|
|
||||||
g_thread_join (threads[i]);
|
|
||||||
|
|
||||||
sum = 0;
|
|
||||||
for (i = 0; i < THREADS; i++)
|
|
||||||
sum += bucket[i];
|
|
||||||
|
|
||||||
g_assert_cmpint (sum, ==, atomic);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
main (int argc, char *argv[])
|
|
||||||
{
|
|
||||||
g_test_init (&argc, &argv, NULL);
|
|
||||||
|
|
||||||
g_test_add_func ("/glib/atomic/add", test_atomic);
|
|
||||||
|
|
||||||
return g_test_run ();
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user