Update contrib.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
10 #include <sys/resource.h>
15 #include <glib_global.h>
16 #include "mrt2_glib2_test.h"
20 static int n_children = 4;
21 static int n_active_children;
22 static int n_iters = 5;
23 static GMainLoop *loop;
26 io_pipe (GIOChannel **channels)
32 g_print ("Cannot create pipe %s\n", g_strerror (errno));
33 g_assert(FALSE && "timeloop failed");
36 testResultXml("timeloop");
41 channels[0] = g_io_channel_unix_new (fds[0]);
42 channels[1] = g_io_channel_unix_new (fds[1]);
46 read_all (GIOChannel *channel, char *buf, int len)
52 while (bytes_read < len)
54 err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
57 if (err != G_IO_ERROR_AGAIN)
59 g_assert(FALSE && "timeloop failed");
73 write_all (GIOChannel *channel, char *buf, int len)
75 gsize bytes_written = 0;
79 while (bytes_written < len)
81 err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
82 if (err && err != G_IO_ERROR_AGAIN)
85 bytes_written += count;
92 run_child (gpointer data)
96 GIOChannel *in_channel,*out_channel;
97 GTimer *timer = g_timer_new();
99 GIOChannel **channels = data;
101 in_channel = channels[0];
102 out_channel = channels[1];
104 for (i = 0; i < n_iters; i++)
106 write_all (out_channel, (char *)&val, sizeof (val));
107 read_all (in_channel, (char *)&val, sizeof (val));
111 write_all (out_channel, (char *)&val, sizeof (val));
113 val = g_timer_elapsed (timer, NULL) * 1000;
115 write_all (out_channel, (char *)&val, sizeof (val));
116 g_timer_destroy (timer);
122 input_callback (GIOChannel *source,
123 GIOCondition condition,
127 GIOChannel *dest = (GIOChannel *)data;
129 if (!read_all (source, (char *)&val, sizeof(val)))
131 g_print("Unexpected EOF\n");
132 g_assert(FALSE && "timeloop failed");
134 testResultXml("timeloop");
135 #endif /* EMULATOR */
141 write_all (dest, (char *)&val, sizeof(val));
147 g_io_channel_close (source);
148 g_io_channel_close (dest);
150 g_io_channel_unref (source);
151 g_io_channel_unref (dest);
154 if (n_active_children == 0)
155 g_main_loop_quit (loop);
165 GIOChannel *in_channels[2];
166 GIOChannel *out_channels[2];
168 GIOChannel **sub_channels;
170 sub_channels = g_new (GIOChannel *, 2);
172 io_pipe (in_channels);
173 io_pipe (out_channels);
175 sub_channels[0] = in_channels[0];
176 sub_channels[1] = out_channels[1];
178 g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
179 input_callback, in_channels[1]);
181 g_thread_create(run_child,sub_channels,FALSE,&err);
185 main (int argc, char **argv)
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);
196 n_children = atoi(argv[1]);
199 n_iters = atoi(argv[2]);
201 //printf ("Children: %d Iters: %d\n", n_children, n_iters);
203 n_active_children = n_children;
204 for (i = 0; i < n_children; i++)
207 loop = g_main_loop_new (NULL, FALSE);
209 g_assert(loop != NULL);
211 g_main_loop_run (loop);
213 testResultXml("timeloop");
214 #endif /* EMULATOR */