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>
13 #include <glib-object.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));
36 channels[0] = g_io_channel_unix_new (fds[0]);
37 channels[1] = g_io_channel_unix_new (fds[1]);
41 read_all (GIOChannel *channel, char *buf, int len)
47 while (bytes_read < len)
49 err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
52 if (err != G_IO_ERROR_AGAIN)
65 write_all (GIOChannel *channel, char *buf, int len)
67 gsize bytes_written = 0;
71 while (bytes_written < len)
73 err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
74 if (err && err != G_IO_ERROR_AGAIN)
77 bytes_written += count;
84 run_child (gpointer data)
88 GIOChannel *in_channel,*out_channel;
89 GTimer *timer = g_timer_new();
91 GIOChannel **channels = data;
93 in_channel = channels[0];
94 out_channel = channels[1];
96 for (i = 0; i < n_iters; i++)
98 write_all (out_channel, (char *)&val, sizeof (val));
99 read_all (in_channel, (char *)&val, sizeof (val));
103 write_all (out_channel, (char *)&val, sizeof (val));
105 val = g_timer_elapsed (timer, NULL) * 1000;
107 write_all (out_channel, (char *)&val, sizeof (val));
108 g_timer_destroy (timer);
114 input_callback (GIOChannel *source,
115 GIOCondition condition,
119 GIOChannel *dest = (GIOChannel *)data;
121 if (!read_all (source, (char *)&val, sizeof(val)))
123 g_print("Unexpected EOF\n");
129 write_all (dest, (char *)&val, sizeof(val));
135 g_io_channel_close (source);
136 g_io_channel_close (dest);
138 g_io_channel_unref (source);
139 g_io_channel_unref (dest);
142 if (n_active_children == 0)
143 g_main_loop_quit (loop);
153 GIOChannel *in_channels[2];
154 GIOChannel *out_channels[2];
155 GIOChannel **sub_channels;
158 sub_channels = g_new (GIOChannel *, 2);
160 io_pipe (in_channels);
161 io_pipe (out_channels);
163 sub_channels[0] = in_channels[0];
164 sub_channels[1] = out_channels[1];
166 source = g_io_create_watch (out_channels[0], G_IO_IN | G_IO_HUP);
167 g_assert(source != NULL);
168 g_source_set_closure (source,
169 g_cclosure_new (G_CALLBACK (input_callback), in_channels[1], NULL));
170 g_source_attach (source, NULL);
172 g_thread_create(run_child,sub_channels,FALSE,&err);
178 main (int argc, char **argv)
183 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 n_children = atoi(argv[1]);
194 n_iters = atoi(argv[2]);
196 n_active_children = n_children;
197 for (i = 0; i < n_children; i++)
200 loop = g_main_loop_new (NULL, FALSE);
202 g_assert(loop != NULL);
204 g_main_loop_run (loop);
206 g_assert(n_active_children == 0);
210 testResultXml("timeloop-closure");
211 #endif /* EMULATOR */