os/ossrv/glib/tsrc/BC/tests/timeloop.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/timeloop.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,216 @@
     1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     1.5 +#undef G_DISABLE_ASSERT
     1.6 +#undef G_LOG_DOMAIN
     1.7 +
     1.8 +#include <errno.h>
     1.9 +#include <stdlib.h>
    1.10 +#include <unistd.h>
    1.11 +#include <stdio.h>
    1.12 +#include <sys/time.h>
    1.13 +#include <sys/resource.h>
    1.14 +
    1.15 +#include <glib.h>
    1.16 +
    1.17 +#ifdef SYMBIAN
    1.18 +#include <glib_global.h>
    1.19 +#include "mrt2_glib2_test.h"
    1.20 +#endif /*SYMBIAN*/
    1.21 +
    1.22 +
    1.23 +static int n_children = 4;
    1.24 +static int n_active_children;
    1.25 +static int n_iters = 5;
    1.26 +static GMainLoop *loop;
    1.27 +
    1.28 +static void
    1.29 +io_pipe (GIOChannel **channels)
    1.30 +{
    1.31 +  int fds[2];
    1.32 +
    1.33 +  if (pipe(fds) < 0)
    1.34 +    {
    1.35 +      g_print ("Cannot create pipe %s\n", g_strerror (errno));
    1.36 +      g_assert(FALSE && "timeloop failed");  
    1.37 +    
    1.38 +#ifdef SYMBIAN
    1.39 +  testResultXml("timeloop");
    1.40 +#endif /* EMULATOR */
    1.41 +      exit (1);
    1.42 +    }
    1.43 +
    1.44 +  channels[0] = g_io_channel_unix_new (fds[0]);
    1.45 +  channels[1] = g_io_channel_unix_new (fds[1]);
    1.46 +}
    1.47 +
    1.48 +static gboolean
    1.49 +read_all (GIOChannel *channel, char *buf, int len)
    1.50 +{
    1.51 +  gsize bytes_read = 0;
    1.52 +  gsize count;
    1.53 +  GIOError err;
    1.54 +
    1.55 +  while (bytes_read < len)
    1.56 +    {
    1.57 +      err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
    1.58 +      if (err)
    1.59 +	{
    1.60 +	  if (err != G_IO_ERROR_AGAIN)
    1.61 +	  {
    1.62 +	  	g_assert(FALSE && "timeloop failed");
    1.63 +	    return FALSE;
    1.64 +	  }
    1.65 +	}
    1.66 +      else if (count == 0)
    1.67 +	return FALSE;
    1.68 +
    1.69 +      bytes_read += count;
    1.70 +    }
    1.71 +
    1.72 +  return TRUE;
    1.73 +}
    1.74 +
    1.75 +static gboolean
    1.76 +write_all (GIOChannel *channel, char *buf, int len)
    1.77 +{
    1.78 +  gsize bytes_written = 0;
    1.79 +  gsize count;
    1.80 +  GIOError err;
    1.81 +
    1.82 +  while (bytes_written < len)
    1.83 +    {
    1.84 +      err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
    1.85 +      if (err && err != G_IO_ERROR_AGAIN)
    1.86 +	return FALSE;
    1.87 +
    1.88 +      bytes_written += count;
    1.89 +    }
    1.90 +
    1.91 +  return TRUE;
    1.92 +}
    1.93 +
    1.94 +gpointer
    1.95 +run_child (gpointer data)
    1.96 +{
    1.97 +  int i;
    1.98 +  int val = 1;
    1.99 +  GIOChannel *in_channel,*out_channel;
   1.100 +  GTimer *timer = g_timer_new();
   1.101 +  
   1.102 +  GIOChannel **channels = data;
   1.103 +  
   1.104 +  in_channel = channels[0];
   1.105 +  out_channel = channels[1];
   1.106 +
   1.107 +  for (i = 0; i < n_iters; i++)
   1.108 +    {
   1.109 +      write_all (out_channel, (char *)&val, sizeof (val));
   1.110 +      read_all (in_channel, (char *)&val, sizeof (val));
   1.111 +    }
   1.112 +
   1.113 +  val = 0;
   1.114 +  write_all (out_channel, (char *)&val, sizeof (val));
   1.115 +
   1.116 +  val = g_timer_elapsed (timer, NULL) * 1000;
   1.117 +  
   1.118 +  write_all (out_channel, (char *)&val, sizeof (val));
   1.119 +  g_timer_destroy (timer);
   1.120 +
   1.121 +  return NULL;
   1.122 +}
   1.123 +
   1.124 +static gboolean
   1.125 +input_callback (GIOChannel   *source,
   1.126 +		GIOCondition  condition,
   1.127 +		gpointer      data)
   1.128 +{
   1.129 +  int val;
   1.130 +  GIOChannel *dest = (GIOChannel *)data;
   1.131 +  
   1.132 +  if (!read_all (source, (char *)&val, sizeof(val)))
   1.133 +    {
   1.134 +      g_print("Unexpected EOF\n");
   1.135 +      g_assert(FALSE && "timeloop failed");
   1.136 +    #ifdef SYMBIAN
   1.137 +  testResultXml("timeloop");
   1.138 +#endif /* EMULATOR */
   1.139 +      exit (1);
   1.140 +    }
   1.141 +
   1.142 +  if (val)
   1.143 +    {
   1.144 +      write_all (dest, (char *)&val, sizeof(val));
   1.145 +      
   1.146 +      return TRUE;
   1.147 +    }
   1.148 +  else
   1.149 +    {
   1.150 +      g_io_channel_close (source);
   1.151 +      g_io_channel_close (dest);
   1.152 +      
   1.153 +      g_io_channel_unref (source);
   1.154 +      g_io_channel_unref (dest);
   1.155 +
   1.156 +      n_active_children--;
   1.157 +      if (n_active_children == 0)
   1.158 +	g_main_loop_quit (loop);
   1.159 +      
   1.160 +      return FALSE;
   1.161 +    }
   1.162 +}
   1.163 +
   1.164 +static void
   1.165 +create_child (void)
   1.166 +{
   1.167 +  GError *err = NULL;
   1.168 +  GIOChannel *in_channels[2];
   1.169 +  GIOChannel *out_channels[2];
   1.170 +  
   1.171 +  GIOChannel **sub_channels;
   1.172 +  
   1.173 +  sub_channels = g_new (GIOChannel *, 2);
   1.174 +  
   1.175 +  io_pipe (in_channels);
   1.176 +  io_pipe (out_channels);
   1.177 +  
   1.178 +  sub_channels[0] = in_channels[0];
   1.179 +  sub_channels[1] = out_channels[1];
   1.180 +
   1.181 +  g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
   1.182 +		      input_callback, in_channels[1]);
   1.183 +  
   1.184 +  g_thread_create(run_child,sub_channels,FALSE,&err);
   1.185 +}
   1.186 +
   1.187 +int 
   1.188 +main (int argc, char **argv)
   1.189 +{
   1.190 +  int i;
   1.191 +  
   1.192 +  #ifdef SYMBIAN
   1.193 +  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);
   1.194 +  #endif /*SYMBIAN*/
   1.195 +	  
   1.196 +  g_thread_init(NULL);
   1.197 +  
   1.198 +  if (argc > 1)
   1.199 +    n_children = atoi(argv[1]);
   1.200 +
   1.201 +  if (argc > 2)
   1.202 +    n_iters = atoi(argv[2]);
   1.203 +
   1.204 +  //printf ("Children: %d     Iters: %d\n", n_children, n_iters);
   1.205 +
   1.206 +  n_active_children = n_children;
   1.207 +  for (i = 0; i < n_children; i++)
   1.208 +    create_child ();
   1.209 +
   1.210 +  loop = g_main_loop_new (NULL, FALSE);
   1.211 +  
   1.212 +  g_assert(loop != NULL);
   1.213 +  
   1.214 +  g_main_loop_run (loop);
   1.215 +  #ifdef SYMBIAN
   1.216 +  testResultXml("timeloop");
   1.217 +  #endif /* EMULATOR */
   1.218 +  return 0;
   1.219 +}