glib/tsrc/BC/tests/timeloop.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 <stdlib.h>
       
     7 #include <unistd.h>
       
     8 #include <stdio.h>
       
     9 #include <sys/time.h>
       
    10 #include <sys/resource.h>
       
    11 
       
    12 #include <glib.h>
       
    13 
       
    14 #ifdef SYMBIAN
       
    15 #include <glib_global.h>
       
    16 #include "mrt2_glib2_test.h"
       
    17 #endif /*SYMBIAN*/
       
    18 
       
    19 
       
    20 static int n_children = 4;
       
    21 static int n_active_children;
       
    22 static int n_iters = 5;
       
    23 static GMainLoop *loop;
       
    24 
       
    25 static void
       
    26 io_pipe (GIOChannel **channels)
       
    27 {
       
    28   int fds[2];
       
    29 
       
    30   if (pipe(fds) < 0)
       
    31     {
       
    32       g_print ("Cannot create pipe %s\n", g_strerror (errno));
       
    33       g_assert(FALSE && "timeloop failed");  
       
    34     
       
    35 #ifdef SYMBIAN
       
    36   testResultXml("timeloop");
       
    37 #endif /* EMULATOR */
       
    38       exit (1);
       
    39     }
       
    40 
       
    41   channels[0] = g_io_channel_unix_new (fds[0]);
       
    42   channels[1] = g_io_channel_unix_new (fds[1]);
       
    43 }
       
    44 
       
    45 static gboolean
       
    46 read_all (GIOChannel *channel, char *buf, int len)
       
    47 {
       
    48   gsize bytes_read = 0;
       
    49   gsize count;
       
    50   GIOError err;
       
    51 
       
    52   while (bytes_read < len)
       
    53     {
       
    54       err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
       
    55       if (err)
       
    56 	{
       
    57 	  if (err != G_IO_ERROR_AGAIN)
       
    58 	  {
       
    59 	  	g_assert(FALSE && "timeloop failed");
       
    60 	    return FALSE;
       
    61 	  }
       
    62 	}
       
    63       else if (count == 0)
       
    64 	return FALSE;
       
    65 
       
    66       bytes_read += count;
       
    67     }
       
    68 
       
    69   return TRUE;
       
    70 }
       
    71 
       
    72 static gboolean
       
    73 write_all (GIOChannel *channel, char *buf, int len)
       
    74 {
       
    75   gsize bytes_written = 0;
       
    76   gsize count;
       
    77   GIOError err;
       
    78 
       
    79   while (bytes_written < len)
       
    80     {
       
    81       err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
       
    82       if (err && err != G_IO_ERROR_AGAIN)
       
    83 	return FALSE;
       
    84 
       
    85       bytes_written += count;
       
    86     }
       
    87 
       
    88   return TRUE;
       
    89 }
       
    90 
       
    91 gpointer
       
    92 run_child (gpointer data)
       
    93 {
       
    94   int i;
       
    95   int val = 1;
       
    96   GIOChannel *in_channel,*out_channel;
       
    97   GTimer *timer = g_timer_new();
       
    98   
       
    99   GIOChannel **channels = data;
       
   100   
       
   101   in_channel = channels[0];
       
   102   out_channel = channels[1];
       
   103 
       
   104   for (i = 0; i < n_iters; i++)
       
   105     {
       
   106       write_all (out_channel, (char *)&val, sizeof (val));
       
   107       read_all (in_channel, (char *)&val, sizeof (val));
       
   108     }
       
   109 
       
   110   val = 0;
       
   111   write_all (out_channel, (char *)&val, sizeof (val));
       
   112 
       
   113   val = g_timer_elapsed (timer, NULL) * 1000;
       
   114   
       
   115   write_all (out_channel, (char *)&val, sizeof (val));
       
   116   g_timer_destroy (timer);
       
   117 
       
   118   return NULL;
       
   119 }
       
   120 
       
   121 static gboolean
       
   122 input_callback (GIOChannel   *source,
       
   123 		GIOCondition  condition,
       
   124 		gpointer      data)
       
   125 {
       
   126   int val;
       
   127   GIOChannel *dest = (GIOChannel *)data;
       
   128   
       
   129   if (!read_all (source, (char *)&val, sizeof(val)))
       
   130     {
       
   131       g_print("Unexpected EOF\n");
       
   132       g_assert(FALSE && "timeloop failed");
       
   133     #ifdef SYMBIAN
       
   134   testResultXml("timeloop");
       
   135 #endif /* EMULATOR */
       
   136       exit (1);
       
   137     }
       
   138 
       
   139   if (val)
       
   140     {
       
   141       write_all (dest, (char *)&val, sizeof(val));
       
   142       
       
   143       return TRUE;
       
   144     }
       
   145   else
       
   146     {
       
   147       g_io_channel_close (source);
       
   148       g_io_channel_close (dest);
       
   149       
       
   150       g_io_channel_unref (source);
       
   151       g_io_channel_unref (dest);
       
   152 
       
   153       n_active_children--;
       
   154       if (n_active_children == 0)
       
   155 	g_main_loop_quit (loop);
       
   156       
       
   157       return FALSE;
       
   158     }
       
   159 }
       
   160 
       
   161 static void
       
   162 create_child (void)
       
   163 {
       
   164   GError *err = NULL;
       
   165   GIOChannel *in_channels[2];
       
   166   GIOChannel *out_channels[2];
       
   167   
       
   168   GIOChannel **sub_channels;
       
   169   
       
   170   sub_channels = g_new (GIOChannel *, 2);
       
   171   
       
   172   io_pipe (in_channels);
       
   173   io_pipe (out_channels);
       
   174   
       
   175   sub_channels[0] = in_channels[0];
       
   176   sub_channels[1] = out_channels[1];
       
   177 
       
   178   g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
       
   179 		      input_callback, in_channels[1]);
       
   180   
       
   181   g_thread_create(run_child,sub_channels,FALSE,&err);
       
   182 }
       
   183 
       
   184 int 
       
   185 main (int argc, char **argv)
       
   186 {
       
   187   int i;
       
   188   
       
   189   #ifdef SYMBIAN
       
   190   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);
       
   191   #endif /*SYMBIAN*/
       
   192 	  
       
   193   g_thread_init(NULL);
       
   194   
       
   195   if (argc > 1)
       
   196     n_children = atoi(argv[1]);
       
   197 
       
   198   if (argc > 2)
       
   199     n_iters = atoi(argv[2]);
       
   200 
       
   201   //printf ("Children: %d     Iters: %d\n", n_children, n_iters);
       
   202 
       
   203   n_active_children = n_children;
       
   204   for (i = 0; i < n_children; i++)
       
   205     create_child ();
       
   206 
       
   207   loop = g_main_loop_new (NULL, FALSE);
       
   208   
       
   209   g_assert(loop != NULL);
       
   210   
       
   211   g_main_loop_run (loop);
       
   212   #ifdef SYMBIAN
       
   213   testResultXml("timeloop");
       
   214   #endif /* EMULATOR */
       
   215   return 0;
       
   216 }