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