glib/tests/mainloop-test.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #undef G_DISABLE_ASSERT
       
    19 #undef G_LOG_DOMAIN
       
    20 
       
    21 #include <errno.h>
       
    22 #include <glib.h>
       
    23 #if (defined G_OS_UNIX)||(defined __SYMBIAN32__)
       
    24 #include <unistd.h>
       
    25 #endif
       
    26 #include <stdio.h>
       
    27 #include <stdlib.h>
       
    28 
       
    29 #ifdef G_OS_WIN32
       
    30 #include <fcntl.h>		/* For _O_BINARY used by pipe() macro */
       
    31 #include <io.h>			/* for _pipe() */
       
    32 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
       
    33 #endif
       
    34 
       
    35 
       
    36 
       
    37 #ifdef __SYMBIAN32__
       
    38 #include <glib_global.h>
       
    39 #include "mrt2_glib2_test.h"
       
    40 #endif /*__SYMBIAN32__*/
       
    41 
       
    42 #ifndef __SYMBIAN32__
       
    43 #define ITERS 10000
       
    44 #else
       
    45 #define ITERS 100
       
    46 #endif
       
    47 #define INCREMENT 10
       
    48 #define NTHREADS 4
       
    49 #define NCRAWLERS 4
       
    50 #define CRAWLER_TIMEOUT_RANGE 40
       
    51 #define RECURSER_TIMEOUT 50
       
    52 
       
    53 /* The partial ordering between the context array mutex and
       
    54  * crawler array mutex is that the crawler array mutex cannot
       
    55  * be locked while the context array mutex is locked
       
    56  */
       
    57 GPtrArray *context_array;
       
    58 GMutex *context_array_mutex;
       
    59 GCond *context_array_cond;
       
    60 
       
    61 GMainLoop *main_loop;
       
    62 
       
    63 G_LOCK_DEFINE_STATIC (crawler_array_lock);
       
    64 GPtrArray *crawler_array;
       
    65 
       
    66 typedef struct _AddrData AddrData;
       
    67 typedef struct _TestData TestData;
       
    68 
       
    69 struct _AddrData
       
    70 {
       
    71   GMainLoop *loop;
       
    72   GIOChannel *dest;
       
    73   gint count;
       
    74 };
       
    75 
       
    76 struct _TestData
       
    77 {
       
    78   gint current_val;
       
    79   gint iters;
       
    80   GIOChannel *in;
       
    81 };
       
    82 
       
    83 static void cleanup_crawlers (GMainContext *context);
       
    84 
       
    85 gboolean
       
    86 read_all (GIOChannel *channel, char *buf, gsize len)
       
    87 {
       
    88   gsize bytes_read = 0;
       
    89   gsize count;
       
    90   GIOError err;
       
    91 
       
    92   while (bytes_read < len)
       
    93     {
       
    94       err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
       
    95       if (err)
       
    96 	{
       
    97 	  if (err != G_IO_ERROR_AGAIN)
       
    98 	    return FALSE;
       
    99 	}
       
   100       else if (count == 0)
       
   101 	return FALSE;
       
   102 
       
   103       bytes_read += count;
       
   104     }
       
   105 
       
   106   return TRUE;
       
   107 }
       
   108 
       
   109 gboolean
       
   110 write_all (GIOChannel *channel, char *buf, gsize len)
       
   111 {
       
   112   gsize bytes_written = 0;
       
   113   gsize count;
       
   114   GIOError err;
       
   115 
       
   116   while (bytes_written < len)
       
   117     {
       
   118       err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
       
   119       if (err && err != G_IO_ERROR_AGAIN)
       
   120 	return FALSE;
       
   121 
       
   122       bytes_written += count;
       
   123     }
       
   124 
       
   125   return TRUE;
       
   126 }
       
   127 
       
   128 gboolean
       
   129 adder_callback (GIOChannel   *source,
       
   130 		GIOCondition  condition,
       
   131 		gpointer      data)
       
   132 {
       
   133   char buf1[32];
       
   134   char buf2[32];
       
   135 
       
   136   char result[32];
       
   137 
       
   138   AddrData *addr_data = data;
       
   139 
       
   140   if (!read_all (source, buf1, 32) ||
       
   141       !read_all (source, buf2, 32))
       
   142     {
       
   143       g_main_loop_quit (addr_data->loop);
       
   144       return FALSE;
       
   145     }
       
   146 
       
   147   sprintf (result, "%d", atoi(buf1) + atoi(buf2));
       
   148   write_all (addr_data->dest, result, 32);
       
   149   
       
   150   return TRUE;
       
   151 }
       
   152 
       
   153 gboolean
       
   154 timeout_callback (gpointer data)
       
   155 {
       
   156   AddrData *addr_data = data;
       
   157 
       
   158   addr_data->count++;
       
   159   
       
   160   return TRUE;
       
   161 }
       
   162 
       
   163 gpointer
       
   164 adder_thread (gpointer data)
       
   165 {
       
   166   GMainContext *context;
       
   167   GSource *adder_source;
       
   168   GSource *timeout_source;
       
   169 
       
   170   GIOChannel **channels = data;
       
   171   AddrData addr_data;
       
   172 
       
   173   context = g_main_context_new ();
       
   174 
       
   175   g_mutex_lock (context_array_mutex);
       
   176   
       
   177   g_ptr_array_add (context_array, context);
       
   178 
       
   179   if (context_array->len == NTHREADS)
       
   180     g_cond_broadcast (context_array_cond);
       
   181   
       
   182   g_mutex_unlock (context_array_mutex);
       
   183 
       
   184   addr_data.dest = channels[1];
       
   185   addr_data.loop = g_main_loop_new (context, FALSE);
       
   186   addr_data.count = 0;
       
   187   
       
   188   adder_source = g_io_create_watch (channels[0], G_IO_IN | G_IO_HUP | G_IO_PRI);
       
   189   g_source_set_callback (adder_source, (GSourceFunc)adder_callback, &addr_data, NULL);
       
   190   g_source_attach (adder_source, context);
       
   191   g_source_unref (adder_source);
       
   192 
       
   193   timeout_source = g_timeout_source_new (10);
       
   194   g_source_set_callback (timeout_source, (GSourceFunc)timeout_callback, &addr_data, NULL);
       
   195   g_source_set_priority (timeout_source, G_PRIORITY_HIGH);
       
   196   g_source_attach (timeout_source, context);
       
   197   g_source_unref (timeout_source);
       
   198 
       
   199   g_main_loop_run (addr_data.loop);
       
   200 
       
   201   g_io_channel_unref (channels[0]);
       
   202   g_io_channel_unref (channels[1]);
       
   203 
       
   204   g_free (channels);
       
   205   
       
   206   g_main_loop_unref (addr_data.loop);
       
   207 
       
   208 #ifdef VERBOSE
       
   209   g_print ("Timeout run %d times\n", addr_data.count);
       
   210 #endif
       
   211 
       
   212   g_mutex_lock (context_array_mutex);
       
   213   g_ptr_array_remove (context_array, context);
       
   214   if (context_array->len == 0)
       
   215     g_main_loop_quit (main_loop);
       
   216   g_mutex_unlock (context_array_mutex);
       
   217 
       
   218   cleanup_crawlers (context);
       
   219 
       
   220   return NULL;
       
   221 }
       
   222 
       
   223 void
       
   224 io_pipe (GIOChannel **channels)
       
   225 {
       
   226   gint fds[2];
       
   227 
       
   228   if (pipe(fds) < 0)
       
   229     {
       
   230       g_warning ("Cannot create pipe %s\n", g_strerror (errno));
       
   231       exit (1);
       
   232     }
       
   233 
       
   234   channels[0] = g_io_channel_unix_new (fds[0]);
       
   235   channels[1] = g_io_channel_unix_new (fds[1]);
       
   236 
       
   237   g_io_channel_set_close_on_unref (channels[0], TRUE);
       
   238   g_io_channel_set_close_on_unref (channels[1], TRUE);
       
   239 }
       
   240 
       
   241 void
       
   242 do_add (GIOChannel *in, gint a, gint b)
       
   243 {
       
   244   char buf1[32];
       
   245   char buf2[32];
       
   246 
       
   247   sprintf (buf1, "%d", a);
       
   248   sprintf (buf2, "%d", b);
       
   249 
       
   250   write_all (in, buf1, 32);
       
   251   write_all (in, buf2, 32);
       
   252 }
       
   253 
       
   254 gboolean
       
   255 adder_response (GIOChannel   *source,
       
   256 		GIOCondition  condition,
       
   257 		gpointer      data)
       
   258 {
       
   259   char result[32];
       
   260   TestData *test_data = data;
       
   261   
       
   262   if (!read_all (source, result, 32))
       
   263     return FALSE;
       
   264 
       
   265   test_data->current_val = atoi (result);
       
   266   test_data->iters--;
       
   267 
       
   268   if (test_data->iters == 0)
       
   269     {
       
   270       if (test_data->current_val != ITERS * INCREMENT)
       
   271 	{
       
   272 	  g_print ("Addition failed: %d != %d\n",
       
   273 		   test_data->current_val, ITERS * INCREMENT);
       
   274 	  exit (1);
       
   275 	}
       
   276 
       
   277       g_io_channel_unref (source);
       
   278       g_io_channel_unref (test_data->in);
       
   279 
       
   280       g_free (test_data);
       
   281       
       
   282       return FALSE;
       
   283     }
       
   284   
       
   285   do_add (test_data->in, test_data->current_val, INCREMENT);
       
   286 
       
   287   return TRUE;
       
   288 }
       
   289 
       
   290 void
       
   291 create_adder_thread (void)
       
   292 {
       
   293   GError *err = NULL;
       
   294   TestData *test_data;
       
   295   
       
   296   GIOChannel *in_channels[2];
       
   297   GIOChannel *out_channels[2];
       
   298 
       
   299   GIOChannel **sub_channels;
       
   300 
       
   301   sub_channels = g_new (GIOChannel *, 2);
       
   302 
       
   303   io_pipe (in_channels);
       
   304   io_pipe (out_channels);
       
   305 
       
   306   sub_channels[0] = in_channels[0];
       
   307   sub_channels[1] = out_channels[1];
       
   308 
       
   309   g_thread_create (adder_thread, sub_channels, FALSE, &err);
       
   310 
       
   311   if (err)
       
   312     {
       
   313       g_warning ("Cannot create thread: %s", err->message);
       
   314       exit (1);
       
   315     }
       
   316 
       
   317   test_data = g_new (TestData, 1);
       
   318   test_data->in = in_channels[1];
       
   319   test_data->current_val = 0;
       
   320   test_data->iters = ITERS;
       
   321 
       
   322   g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP | G_IO_PRI,
       
   323 		  adder_response, test_data);
       
   324   
       
   325   do_add (test_data->in, test_data->current_val, INCREMENT);
       
   326 }
       
   327 
       
   328 static void create_crawler (void);
       
   329 
       
   330 static void
       
   331 remove_crawler (void)
       
   332 {
       
   333   GSource *other_source;
       
   334 
       
   335   if (crawler_array->len > 0)
       
   336     {
       
   337       other_source = crawler_array->pdata[g_random_int_range (0, crawler_array->len)];
       
   338       g_source_destroy (other_source);
       
   339       g_assert (g_ptr_array_remove_fast (crawler_array, other_source));
       
   340     }
       
   341 }
       
   342 
       
   343 static gint
       
   344 crawler_callback (gpointer data)
       
   345 {
       
   346   GSource *source = data;
       
   347 
       
   348   G_LOCK (crawler_array_lock);
       
   349   
       
   350   if (!g_ptr_array_remove_fast (crawler_array, source))
       
   351     remove_crawler();
       
   352 
       
   353   remove_crawler();
       
   354   G_UNLOCK (crawler_array_lock);
       
   355 	    
       
   356   create_crawler();
       
   357   create_crawler();
       
   358 
       
   359   return FALSE;
       
   360 }
       
   361 
       
   362 static void
       
   363 create_crawler (void)
       
   364 {
       
   365   GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE));
       
   366   g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL);
       
   367 
       
   368   G_LOCK (crawler_array_lock);
       
   369   g_ptr_array_add (crawler_array, source);
       
   370   
       
   371   g_mutex_lock (context_array_mutex);
       
   372   g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
       
   373   g_source_unref (source);
       
   374   g_mutex_unlock (context_array_mutex);
       
   375 
       
   376   G_UNLOCK (crawler_array_lock);
       
   377 }
       
   378 
       
   379 static void
       
   380 cleanup_crawlers (GMainContext *context)
       
   381 {
       
   382   gint i;
       
   383   
       
   384   G_LOCK (crawler_array_lock);
       
   385   for (i=0; i < crawler_array->len; i++)
       
   386     {
       
   387       if (g_source_get_context (crawler_array->pdata[i]) == context)
       
   388 	{
       
   389 	  g_source_destroy (g_ptr_array_remove_index (crawler_array, i));
       
   390 	  i--;
       
   391 	}
       
   392     }
       
   393   G_UNLOCK (crawler_array_lock);
       
   394 }
       
   395 
       
   396 static gboolean
       
   397 recurser_idle (gpointer data)
       
   398 {
       
   399   GMainContext *context = data;
       
   400   gint i;
       
   401 
       
   402   for (i = 0; i < 10; i++)
       
   403     g_main_context_iteration (context, FALSE);
       
   404 
       
   405   return FALSE;
       
   406 }
       
   407 
       
   408 static gboolean
       
   409 recurser_start (gpointer data)
       
   410 {
       
   411   GMainContext *context;
       
   412   GSource *source;
       
   413   
       
   414   g_mutex_lock (context_array_mutex);
       
   415   context = context_array->pdata[g_random_int_range (0, context_array->len)];
       
   416   source = g_idle_source_new ();
       
   417   g_source_set_callback (source, recurser_idle, context, NULL);
       
   418   g_source_attach (source, context);
       
   419   g_source_unref (source);
       
   420   g_mutex_unlock (context_array_mutex);
       
   421 
       
   422   return TRUE;
       
   423 }
       
   424 
       
   425 int 
       
   426 main (int   argc,
       
   427       char *argv[])
       
   428 {
       
   429   /* Only run the test, if threads are enabled and a default thread
       
   430      implementation is available */
       
   431 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
       
   432   gint i;
       
   433 
       
   434   #ifdef __SYMBIAN32__
       
   435   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   436   g_set_print_handler(mrtPrintHandler);
       
   437   #endif /*__SYMBIAN32__*/
       
   438   g_thread_init (NULL);
       
   439 
       
   440   context_array = g_ptr_array_new ();
       
   441   context_array_mutex = g_mutex_new ();
       
   442   context_array_cond = g_cond_new (); 
       
   443 
       
   444   crawler_array = g_ptr_array_new ();
       
   445 
       
   446   main_loop = g_main_loop_new (NULL, FALSE);
       
   447 
       
   448   for (i = 0; i < NTHREADS; i++)
       
   449     create_adder_thread ();
       
   450 
       
   451   /* Wait for all threads to start
       
   452    */
       
   453   g_mutex_lock (context_array_mutex);
       
   454   
       
   455   if (context_array->len < NTHREADS)
       
   456     g_cond_wait (context_array_cond, context_array_mutex);
       
   457   
       
   458   g_mutex_unlock (context_array_mutex);
       
   459   
       
   460   for (i = 0; i < NCRAWLERS; i++)
       
   461     create_crawler ();
       
   462 
       
   463   g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL);
       
   464 
       
   465   g_main_loop_run (main_loop);
       
   466   g_main_loop_unref (main_loop);
       
   467 
       
   468 #endif
       
   469   #ifdef __SYMBIAN32__
       
   470   testResultXml("mainloop-test");
       
   471   #endif /* EMULATOR */
       
   472 
       
   473   return 0;
       
   474 }