[glib/gsettings: 2/327] Add test case for 1-bit mutex locks



commit a11df12673b8d45f984f48511fdb4a67ad35e376
Author: Ryan Lortie <desrt desrt ca>
Date:   Sun Apr 5 22:42:19 2009 -0400

    Add test case for 1-bit mutex locks

 configure.in               |    1 +
 glib/gthread.c             |    4 +-
 gthread/Makefile.am        |    3 +
 gthread/tests/.gitignore   |    1 +
 gthread/tests/1bit-mutex.c |   90 ++++++++++++++++++++++++++++++++++++++++++++
 gthread/tests/Makefile.am  |   10 +++++
 6 files changed, 107 insertions(+), 2 deletions(-)
---
diff --git a/configure.in b/configure.in
index 185bbe2..f3dd878 100644
--- a/configure.in
+++ b/configure.in
@@ -3379,6 +3379,7 @@ gobject/Makefile
 gobject/glib-mkenums
 gobject/tests/Makefile
 gthread/Makefile
+gthread/tests/Makefile
 gio/Makefile
 gio/xdgmime/Makefile
 gio/inotify/Makefile
diff --git a/glib/gthread.c b/glib/gthread.c
index 38c8472..05fe3f9 100644
--- a/glib/gthread.c
+++ b/glib/gthread.c
@@ -1137,7 +1137,7 @@ static volatile gint g_bit_lock_contended[CONTENTION_CLASSES];
 /**
  * g_bit_lock:
  * @address: a pointer to an integer
- * @bit: a bit value between 0 and 31
+ * @lock_bit: a bit value between 0 and 31
  *
  * Sets the indicated @lock_bit in @address.  If the bit is already
  * set, this call will block until g_bit_unlock() unsets the
@@ -1180,7 +1180,7 @@ g_bit_lock (volatile gint *address,
 /**
  * g_bit_unlock:
  * @address: a pointer to an integer
- * @bit: a bit value between 0 and 31
+ * @lock_bit: a bit value between 0 and 31
  *
  * Clears the indicated @lock_bit in @address.  If another thread is
  * currently blocked in g_bit_lock() on this same bit then it will be
diff --git a/gthread/Makefile.am b/gthread/Makefile.am
index a7b53cf..3afd84e 100644
--- a/gthread/Makefile.am
+++ b/gthread/Makefile.am
@@ -1,6 +1,9 @@
 ## Process this file with automake to produce Makefile.in
 include $(top_srcdir)/Makefile.decl
 
+SUBDIRS = . tests
+DIST_SUBDIRS = tests
+
 AM_CPPFLAGS = 				\
 	-I$(top_srcdir) 		\
 	-I$(top_srcdir)/glib 		\
diff --git a/gthread/tests/.gitignore b/gthread/tests/.gitignore
new file mode 100644
index 0000000..2e74bc7
--- /dev/null
+++ b/gthread/tests/.gitignore
@@ -0,0 +1 @@
+1bit-mutex
diff --git a/gthread/tests/1bit-mutex.c b/gthread/tests/1bit-mutex.c
new file mode 100644
index 0000000..3a767ce
--- /dev/null
+++ b/gthread/tests/1bit-mutex.c
@@ -0,0 +1,90 @@
+/* 
+ * Copyright © 2008 Ryan Lortie
+ * 
+ * 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>
+
+/* LOCKS should be more than the number of condition
+ * counters in gthread.c in order to ensure we exercise
+ * the case where they overlap.
+ */
+#define LOCKS      48
+#define ITERATIONS 10000
+#define THREADS    100
+
+volatile GThread *owners[LOCKS];
+volatile gint     locks[LOCKS];
+volatile gint     bits[LOCKS];
+
+void
+acquire (int nr)
+{
+  GThread *self;
+
+  self = g_thread_self ();
+
+  g_bit_lock (&locks[nr], bits[nr]);
+  g_assert (owners[nr] == NULL);   /* hopefully nobody else is here */
+  owners[nr] = self;
+
+  /* let some other threads try to ruin our day */
+  g_thread_yield ();
+  g_thread_yield ();
+  g_thread_yield ();
+
+  g_assert (owners[nr] == self);   /* hopefully this is still us... */
+  owners[nr] = NULL;               /* make way for the next guy */
+  g_bit_unlock (&locks[nr], bits[nr]);
+}
+
+gpointer
+thread_func (gpointer data)
+{
+  gint i;
+
+  for (i = 0; i < ITERATIONS; i++)
+    acquire (g_random_int () % LOCKS);
+
+  return NULL;
+}
+
+void
+testcase (void)
+{
+  GThread *threads[THREADS];
+  int i;
+
+  g_thread_init (NULL);
+
+  for (i = 0; i < LOCKS; i++)
+    bits[i] = g_random_int () % 32;
+
+  for (i = 0; i < THREADS; i++)
+    threads[i] = g_thread_create (thread_func, NULL, TRUE, NULL);
+
+  for (i = 0; i < THREADS; i++)
+    g_thread_join (threads[i]);
+
+  for (i = 0; i < LOCKS; i++)
+    {
+      g_assert (owners[i] == NULL);
+      g_assert (locks[i] == 0);
+    }
+}
+
+int
+main (int argc, char **argv)
+{
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/glib/1bit-mutex", testcase);
+
+  return g_test_run ();
+}
diff --git a/gthread/tests/Makefile.am b/gthread/tests/Makefile.am
new file mode 100644
index 0000000..45d3ae8
--- /dev/null
+++ b/gthread/tests/Makefile.am
@@ -0,0 +1,10 @@
+include $(top_srcdir)/Makefile.decl
+
+INCLUDES = -g -I$(top_srcdir) -I$(top_srcdir)/glib $(GLIB_DEBUG_FLAGS)
+
+noinst_PROGRAMS = $(TEST_PROGS)
+progs_ldadd     = $(top_builddir)/glib/libglib-2.0.la \
+		  $(top_builddir)/gthread/libgthread-2.0.la
+
+TEST_PROGS	 += 1bit-mutex
+1bit_mutex_LDADD  = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]