[kdbus] Add SipHash algorithm
[platform/upstream/glib.git] / tests / mainloop-test.c
index 5aba25e..5d30550 100644 (file)
@@ -1,3 +1,6 @@
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
 #include <errno.h>
 #include <glib.h>
 #ifdef G_OS_UNIX
@@ -8,6 +11,8 @@
 
 #ifdef G_OS_WIN32
 #include <fcntl.h>             /* For _O_BINARY used by pipe() macro */
+#include <io.h>                        /* for _pipe() */
+#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
 #endif
 
 #define ITERS 10000
 #define CRAWLER_TIMEOUT_RANGE 40
 #define RECURSER_TIMEOUT 50
 
+/* The partial ordering between the context array mutex and
+ * crawler array mutex is that the crawler array mutex cannot
+ * be locked while the context array mutex is locked
+ */
 GPtrArray *context_array;
-GMutex *context_array_mutex;
-GCond *context_array_cond;
+GMutex context_array_mutex;
+GCond context_array_cond;
 
 GMainLoop *main_loop;
 
@@ -45,11 +54,11 @@ struct _TestData
 
 static void cleanup_crawlers (GMainContext *context);
 
-gboolean
-read_all (GIOChannel *channel, char *buf, int len)
+static gboolean
+read_all (GIOChannel *channel, char *buf, gsize len)
 {
-  int bytes_read = 0;
-  int count;
+  gsize bytes_read = 0;
+  gsize count;
   GIOError err;
 
   while (bytes_read < len)
@@ -69,11 +78,11 @@ read_all (GIOChannel *channel, char *buf, int len)
   return TRUE;
 }
 
-gboolean
-write_all (GIOChannel *channel, char *buf, int len)
+static gboolean
+write_all (GIOChannel *channel, char *buf, gsize len)
 {
-  int bytes_written = 0;
-  int count;
+  gsize bytes_written = 0;
+  gsize count;
   GIOError err;
 
   while (bytes_written < len)
@@ -88,7 +97,7 @@ write_all (GIOChannel *channel, char *buf, int len)
   return TRUE;
 }
 
-gboolean
+static gboolean
 adder_callback (GIOChannel   *source,
                GIOCondition  condition,
                gpointer      data)
@@ -96,7 +105,7 @@ adder_callback (GIOChannel   *source,
   char buf1[32];
   char buf2[32];
 
-  char result[32];
+  char result[32] = { 0, };
 
   AddrData *addr_data = data;
 
@@ -113,7 +122,7 @@ adder_callback (GIOChannel   *source,
   return TRUE;
 }
 
-gboolean
+static gboolean
 timeout_callback (gpointer data)
 {
   AddrData *addr_data = data;
@@ -123,7 +132,7 @@ timeout_callback (gpointer data)
   return TRUE;
 }
 
-void
+static gpointer
 adder_thread (gpointer data)
 {
   GMainContext *context;
@@ -133,36 +142,36 @@ adder_thread (gpointer data)
   GIOChannel **channels = data;
   AddrData addr_data;
 
-  context = g_main_context_get (g_thread_self());
+  context = g_main_context_new ();
 
-  g_mutex_lock (context_array_mutex);
+  g_mutex_lock (&context_array_mutex);
   
   g_ptr_array_add (context_array, context);
 
   if (context_array->len == NTHREADS)
-    g_cond_broadcast (context_array_cond);
+    g_cond_broadcast (&context_array_cond);
   
-  g_mutex_unlock (context_array_mutex);
+  g_mutex_unlock (&context_array_mutex);
 
   addr_data.dest = channels[1];
   addr_data.loop = g_main_loop_new (context, FALSE);
   addr_data.count = 0;
-  
+
   adder_source = g_io_create_watch (channels[0], G_IO_IN | G_IO_HUP);
+  g_source_set_name (adder_source, "Adder I/O");
   g_source_set_callback (adder_source, (GSourceFunc)adder_callback, &addr_data, NULL);
   g_source_attach (adder_source, context);
   g_source_unref (adder_source);
 
   timeout_source = g_timeout_source_new (10);
+  g_source_set_name (timeout_source, "Adder timeout");
   g_source_set_callback (timeout_source, (GSourceFunc)timeout_callback, &addr_data, NULL);
   g_source_set_priority (timeout_source, G_PRIORITY_HIGH);
   g_source_attach (timeout_source, context);
   g_source_unref (timeout_source);
 
-  g_main_run (addr_data.loop);
+  g_main_loop_run (addr_data.loop);
 
-  g_io_channel_close (channels[0]);
-  g_io_channel_close (channels[1]);
   g_io_channel_unref (channels[0]);
   g_io_channel_unref (channels[1]);
 
@@ -170,18 +179,23 @@ adder_thread (gpointer data)
   
   g_main_loop_unref (addr_data.loop);
 
+#ifdef VERBOSE
   g_print ("Timeout run %d times\n", addr_data.count);
+#endif
 
-  g_mutex_lock (context_array_mutex);
+  g_mutex_lock (&context_array_mutex);
   g_ptr_array_remove (context_array, context);
   if (context_array->len == 0)
     g_main_loop_quit (main_loop);
-  g_mutex_unlock (context_array_mutex);
+  g_mutex_unlock (&context_array_mutex);
 
   cleanup_crawlers (context);
+  g_main_context_unref (context);
+
+  return NULL;
 }
 
-void
+static void
 io_pipe (GIOChannel **channels)
 {
   gint fds[2];
@@ -194,13 +208,16 @@ io_pipe (GIOChannel **channels)
 
   channels[0] = g_io_channel_unix_new (fds[0]);
   channels[1] = g_io_channel_unix_new (fds[1]);
+
+  g_io_channel_set_close_on_unref (channels[0], TRUE);
+  g_io_channel_set_close_on_unref (channels[1], TRUE);
 }
 
-void
+static void
 do_add (GIOChannel *in, gint a, gint b)
 {
-  char buf1[32];
-  char buf2[32];
+  char buf1[32] = { 0, };
+  char buf2[32] = { 0, };
 
   sprintf (buf1, "%d", a);
   sprintf (buf2, "%d", b);
@@ -209,7 +226,7 @@ do_add (GIOChannel *in, gint a, gint b)
   write_all (in, buf2, 32);
 }
 
-gboolean
+static gboolean
 adder_response (GIOChannel   *source,
                GIOCondition  condition,
                gpointer      data)
@@ -232,11 +249,10 @@ adder_response (GIOChannel   *source,
          exit (1);
        }
 
-      g_io_channel_close (source);
-      g_io_channel_close (test_data->in);
-
       g_io_channel_unref (source);
       g_io_channel_unref (test_data->in);
+
+      g_free (test_data);
       
       return FALSE;
     }
@@ -246,7 +262,7 @@ adder_response (GIOChannel   *source,
   return TRUE;
 }
 
-void
+static void
 create_adder_thread (void)
 {
   GError *err = NULL;
@@ -265,8 +281,7 @@ create_adder_thread (void)
   sub_channels[0] = in_channels[0];
   sub_channels[1] = out_channels[1];
 
-  g_thread_create (adder_thread, sub_channels, 0,
-                  FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, &err);
+  g_thread_create (adder_thread, sub_channels, FALSE, &err);
 
   if (err)
     {
@@ -323,15 +338,17 @@ static void
 create_crawler (void)
 {
   GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE));
+  g_source_set_name (source, "Crawler timeout");
   g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL);
 
-  g_mutex_lock (context_array_mutex);
+  G_LOCK (crawler_array_lock);
+  g_ptr_array_add (crawler_array, source);
+  
+  g_mutex_lock (&context_array_mutex);
   g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
   g_source_unref (source);
-  g_mutex_unlock (context_array_mutex);
+  g_mutex_unlock (&context_array_mutex);
 
-  G_LOCK (crawler_array_lock);
-  g_ptr_array_add (crawler_array, source);
   G_UNLOCK (crawler_array_lock);
 }
 
@@ -359,7 +376,7 @@ recurser_idle (gpointer data)
   gint i;
 
   for (i = 0; i < 10; i++)
-    g_main_context_iteration (context, TRUE);
+    g_main_context_iteration (context, FALSE);
 
   return FALSE;
 }
@@ -370,31 +387,25 @@ recurser_start (gpointer data)
   GMainContext *context;
   GSource *source;
   
-  g_mutex_lock (context_array_mutex);
+  g_mutex_lock (&context_array_mutex);
   context = context_array->pdata[g_random_int_range (0, context_array->len)];
   source = g_idle_source_new ();
+  g_source_set_name (source, "Recursing idle source");
   g_source_set_callback (source, recurser_idle, context, NULL);
   g_source_attach (source, context);
   g_source_unref (source);
-  g_mutex_unlock (context_array_mutex);
+  g_mutex_unlock (&context_array_mutex);
 
   return TRUE;
 }
 
-int 
+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)
   gint i;
 
-  g_thread_init (NULL);
-
   context_array = g_ptr_array_new ();
-  context_array_mutex = g_mutex_new ();
-  context_array_cond = g_cond_new (); 
 
   crawler_array = g_ptr_array_new ();
 
@@ -405,12 +416,12 @@ main (int   argc,
 
   /* Wait for all threads to start
    */
-  g_mutex_lock (context_array_mutex);
+  g_mutex_lock (&context_array_mutex);
   
   if (context_array->len < NTHREADS)
-    g_cond_wait (context_array_cond, context_array_mutex);
+    g_cond_wait (&context_array_cond, &context_array_mutex);
   
-  g_mutex_unlock (context_array_mutex);
+  g_mutex_unlock (&context_array_mutex);
   
   for (i = 0; i < NCRAWLERS; i++)
     create_crawler ();
@@ -420,6 +431,8 @@ main (int   argc,
   g_main_loop_run (main_loop);
   g_main_loop_unref (main_loop);
 
-#endif
+  g_ptr_array_unref (crawler_array);
+  g_ptr_array_unref (context_array);
+
   return 0;
 }