Deprecate g_thread_create_full()
[platform/upstream/glib.git] / tests / mainloop-test.c
index 2652d63..206f80e 100644 (file)
@@ -1,9 +1,20 @@
+#undef G_DISABLE_ASSERT
+#undef G_LOG_DOMAIN
+
 #include <errno.h>
 #include <glib.h>
+#ifdef G_OS_UNIX
 #include <unistd.h>
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 
+#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 INCREMENT 10
 #define NTHREADS 4
 #define CRAWLER_TIMEOUT_RANGE 40
 #define RECURSER_TIMEOUT 50
 
-G_LOCK_DEFINE_STATIC (context_array_lock);
+/* 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;
+
 GMainLoop *main_loop;
 
 G_LOCK_DEFINE_STATIC (crawler_array_lock);
@@ -38,10 +55,10 @@ struct _TestData
 static void cleanup_crawlers (GMainContext *context);
 
 gboolean
-read_all (GIOChannel *channel, char *buf, int len)
+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)
@@ -62,10 +79,10 @@ read_all (GIOChannel *channel, char *buf, int len)
 }
 
 gboolean
-write_all (GIOChannel *channel, char *buf, int len)
+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)
@@ -115,7 +132,7 @@ timeout_callback (gpointer data)
   return TRUE;
 }
 
-void
+gpointer
 adder_thread (gpointer data)
 {
   GMainContext *context;
@@ -125,47 +142,56 @@ adder_thread (gpointer data)
   GIOChannel **channels = data;
   AddrData addr_data;
 
-  context = g_main_context_get (g_thread_self());
+  context = g_main_context_new ();
 
-  G_LOCK (context_array_lock);
+  g_mutex_lock (context_array_mutex);
+  
   g_ptr_array_add (context_array, context);
-  G_UNLOCK (context_array_lock);
+
+  if (context_array->len == NTHREADS)
+    g_cond_broadcast (context_array_cond);
+  
+  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]);
 
   g_free (channels);
   
-  g_main_loop_destroy (addr_data.loop);
+  g_main_loop_unref (addr_data.loop);
 
+#ifdef VERBOSE
   g_print ("Timeout run %d times\n", addr_data.count);
+#endif
 
-  G_LOCK (context_array_lock);
+  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_UNLOCK (context_array_lock);
+  g_mutex_unlock (context_array_mutex);
 
   cleanup_crawlers (context);
+
+  return NULL;
 }
 
 void
@@ -181,6 +207,9 @@ 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
@@ -219,11 +248,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;
     }
@@ -252,8 +280,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)
     {
@@ -310,15 +337,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_LOCK (context_array_lock);
+  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_UNLOCK (context_array_lock);
+  g_mutex_unlock (context_array_mutex);
 
-  G_LOCK (crawler_array_lock);
-  g_ptr_array_add (crawler_array, source);
   G_UNLOCK (crawler_array_lock);
 }
 
@@ -346,7 +375,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;
 }
@@ -357,13 +386,14 @@ recurser_start (gpointer data)
   GMainContext *context;
   GSource *source;
   
-  G_LOCK (context_array_lock);
+  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_UNLOCK (context_array_lock);
+  g_mutex_unlock (context_array_mutex);
 
   return TRUE;
 }
@@ -372,14 +402,14 @@ 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 ();
 
   main_loop = g_main_loop_new (NULL, FALSE);
@@ -387,14 +417,22 @@ main (int   argc,
   for (i = 0; i < NTHREADS; i++)
     create_adder_thread ();
 
+  /* Wait for all threads to start
+   */
+  g_mutex_lock (context_array_mutex);
+  
+  if (context_array->len < NTHREADS)
+    g_cond_wait (context_array_cond, context_array_mutex);
+  
+  g_mutex_unlock (context_array_mutex);
+  
   for (i = 0; i < NCRAWLERS; i++)
     create_crawler ();
 
   g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL);
 
   g_main_loop_run (main_loop);
-  g_main_loop_destroy (main_loop);
+  g_main_loop_unref (main_loop);
 
-#endif
   return 0;
 }