[glib] Add some atomic ops tests
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] Add some atomic ops tests
- Date: Sun, 22 May 2011 05:29:39 +0000 (UTC)
commit 9255350a70ea53f719b395d200b9e35a5dcb6d3c
Author: Matthias Clasen <mclasen redhat com>
Date: Sun May 22 01:29:22 2011 -0400
Add some atomic ops tests
gthread/tests/Makefile.am | 4 ++
gthread/tests/atomic.c | 72 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 76 insertions(+), 0 deletions(-)
---
diff --git a/gthread/tests/Makefile.am b/gthread/tests/Makefile.am
index 436498a..0317cf2 100644
--- a/gthread/tests/Makefile.am
+++ b/gthread/tests/Makefile.am
@@ -18,3 +18,7 @@ TEST_PROGS += unix-multithreaded
unix_multithreaded_SOURCES = $(top_srcdir)/glib/tests/unix.c
unix_multithreaded_CFLAGS = -DTEST_THREADED
unix_multithreaded_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
+
+TEST_PROGS += atomic
+atomic_SOURCES = atomic.c
+atomic_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
diff --git a/gthread/tests/atomic.c b/gthread/tests/atomic.c
new file mode 100644
index 0000000..c5c1c78
--- /dev/null
+++ b/gthread/tests/atomic.c
@@ -0,0 +1,72 @@
+/*
+ * 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_create (thread_func, GINT_TO_POINTER (i), TRUE, NULL);
+
+ 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_thread_init (NULL);
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/glib/atomic/add", test_atomic);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]