[glib] Add a non-trivial GRecMutex test



commit 0a9885ebd84b9e7762346be3153c2428bc95ec50
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Sep 23 07:14:13 2011 -0400

    Add a non-trivial GRecMutex test
    
    Adapted from the corresponding GMutex test.

 glib/tests/rec-mutex.c |   86 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 0 deletions(-)
---
diff --git a/glib/tests/rec-mutex.c b/glib/tests/rec-mutex.c
index e416d31..055ab07 100644
--- a/glib/tests/rec-mutex.c
+++ b/glib/tests/rec-mutex.c
@@ -64,6 +64,91 @@ test_rec_mutex3 (void)
   g_rec_mutex_clear (&mutex);
 }
 
+#define LOCKS      48
+#define ITERATIONS 10000
+#define THREADS    100
+
+
+GThread   *owners[LOCKS];
+GRecMutex  locks[LOCKS];
+
+static void
+acquire (gint nr)
+{
+  GThread *self;
+
+  self = g_thread_self ();
+
+  if (owners[nr] != NULL && owners[nr] != self)
+    {
+      if (g_test_verbose ())
+        g_print ("thread %p going to block on lock %d\n", self, nr);
+
+    }
+
+  g_rec_mutex_lock (&locks[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_assert (owners[nr] == self);   /* hopefully this is still us... */
+
+  g_rec_mutex_lock (&locks[nr]);  /* we're recursive, after all */
+
+  g_assert (owners[nr] == self);   /* hopefully this is still us... */
+
+  g_rec_mutex_unlock (&locks[nr]);
+
+  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_rec_mutex_unlock (&locks[nr]);
+}
+
+static gpointer
+thread_func (gpointer data)
+{
+  gint i;
+  GRand *rand;
+
+  rand = g_rand_new ();
+
+  for (i = 0; i < ITERATIONS; i++)
+    acquire (g_rand_int_range (rand, 0, LOCKS));
+
+  g_rand_free (rand);
+
+  return NULL;
+}
+
+static void
+test_rec_mutex4 (void)
+{
+  gint i;
+  GThread *threads[THREADS];
+
+  for (i = 0; i < LOCKS; i++)
+    g_rec_mutex_init (&locks[i]);
+
+  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_rec_mutex_clear (&locks[i]);
+
+  for (i = 0; i < LOCKS; i++)
+    g_assert (owners[i] == NULL);
+}
 
 int
 main (int argc, char *argv[])
@@ -73,6 +158,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/thread/rec-mutex1", test_rec_mutex1);
   g_test_add_func ("/thread/rec-mutex2", test_rec_mutex2);
   g_test_add_func ("/thread/rec-mutex3", test_rec_mutex3);
+  g_test_add_func ("/thread/rec-mutex4", test_rec_mutex4);
 
   return g_test_run ();
 }



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