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