Stop using GStaticMutex in two testcases
[platform/upstream/glib.git] / tests / thread-test.c
index 8abace2..fa4f02b 100644 (file)
@@ -1,3 +1,6 @@
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
 #include <glib.h>
 
 /* GMutex */
@@ -107,25 +110,25 @@ test_g_static_rec_mutex (void)
 
 static GStaticPrivate test_g_static_private_private1 = G_STATIC_PRIVATE_INIT;
 static GStaticPrivate test_g_static_private_private2 = G_STATIC_PRIVATE_INIT;
-static GStaticMutex test_g_static_private_mutex = G_STATIC_MUTEX_INIT;
+static GMutex test_g_static_private_mutex = G_MUTEX_INIT;
 static guint test_g_static_private_counter = 0;
 static guint test_g_static_private_ready = 0;
 
 static gpointer
 test_g_static_private_constructor (void)
 {
-  g_static_mutex_lock (&test_g_static_private_mutex);
+  g_mutex_lock (&test_g_static_private_mutex);
   test_g_static_private_counter++;
-  g_static_mutex_unlock (&test_g_static_private_mutex);  
+  g_mutex_unlock (&test_g_static_private_mutex);  
   return g_new (guint,1);
 }
 
 static void
 test_g_static_private_destructor (gpointer data)
 {
-  g_static_mutex_lock (&test_g_static_private_mutex);
+  g_mutex_lock (&test_g_static_private_mutex);
   test_g_static_private_counter--;
-  g_static_mutex_unlock (&test_g_static_private_mutex);  
+  g_mutex_unlock (&test_g_static_private_mutex);  
   g_free (data);
 }
 
@@ -159,9 +162,9 @@ test_g_static_private_thread (gpointer data)
       g_assert (number == *private1);
       g_assert (number * 2 == *private2);      
     }
-  g_static_mutex_lock (&test_g_static_private_mutex);
+  g_mutex_lock (&test_g_static_private_mutex);
   test_g_static_private_ready++;
-  g_static_mutex_unlock (&test_g_static_private_mutex);  
+  g_mutex_unlock (&test_g_static_private_mutex);  
 
   /* Busy wait is not nice but that's just a test */
   while (test_g_static_private_ready != 0)
@@ -294,6 +297,78 @@ test_g_static_rw_lock ()
   g_assert (test_g_static_rw_lock_state == 0);
 }
 
+#define G_ONCE_SIZE 100
+#define G_ONCE_THREADS 10
+
+G_LOCK_DEFINE (test_g_once);
+static guint test_g_once_guint_array[G_ONCE_SIZE];
+static GOnce test_g_once_array[G_ONCE_SIZE];
+
+static gpointer
+test_g_once_init_func(gpointer arg)
+{
+  guint *count = arg;
+  g_usleep (g_random_int_range (20,1000));
+  (*count)++;
+  g_usleep (g_random_int_range (20,1000));
+  return arg;
+}
+
+static gpointer
+test_g_once_thread (gpointer ignore)
+{
+  guint i;
+  G_LOCK (test_g_once);
+  /* Don't start before all threads are created */
+  G_UNLOCK (test_g_once);
+  for (i = 0; i < 1000; i++)
+    {
+      guint pos = g_random_int_range (0, G_ONCE_SIZE);
+      gpointer ret = g_once (test_g_once_array + pos, test_g_once_init_func, 
+                            test_g_once_guint_array + pos);
+      g_assert (ret == test_g_once_guint_array + pos);
+    }
+  
+  /* Make sure, that all counters are touched at least once */
+  for (i = 0; i < G_ONCE_SIZE; i++)
+    {
+      gpointer ret = g_once (test_g_once_array + i, test_g_once_init_func, 
+                            test_g_once_guint_array + i);
+      g_assert (ret == test_g_once_guint_array + i);
+    }
+
+  return NULL;
+}
+
+static void
+test_g_thread_once (void)
+{
+  static GOnce once_init = G_ONCE_INIT;
+  GThread *threads[G_ONCE_THREADS];
+  guint i;
+  for (i = 0; i < G_ONCE_SIZE; i++) 
+    {
+      test_g_once_array[i] = once_init;
+      test_g_once_guint_array[i] = i;
+    }
+  G_LOCK (test_g_once);
+  for (i = 0; i < G_ONCE_THREADS; i++)
+    {
+      threads[i] = g_thread_create (test_g_once_thread, GUINT_TO_POINTER(i%2), 
+                                   TRUE, NULL);
+    }
+  G_UNLOCK (test_g_once);
+  for (i = 0; i < G_ONCE_THREADS; i++)
+    {
+      g_thread_join (threads[i]);
+    }
+  
+  for (i = 0; i < G_ONCE_SIZE; i++) 
+    {
+      g_assert (test_g_once_guint_array[i] == i + 1);
+    }
+}
+
 /* run all the tests */
 void
 run_all_tests()
@@ -302,15 +377,13 @@ run_all_tests()
   test_g_static_rec_mutex ();
   test_g_static_private ();
   test_g_static_rw_lock ();
+  test_g_thread_once ();
 }
 
 int 
 main (int   argc,
       char *argv[])
 {
-  /* Only run the test, if threads are enabled and a default thread
-     implementation is available */
-#if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
   g_thread_init (NULL);
   run_all_tests ();
 
@@ -321,6 +394,5 @@ main (int   argc,
   g_thread_use_default_impl = FALSE;
   run_all_tests ();
   
-#endif
   return 0;
 }