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