11 #define CRAWLER_TIMEOUT_RANGE 40
12 #define RECURSER_TIMEOUT 50
14 G_LOCK_DEFINE_STATIC (context_array_lock);
15 GPtrArray *context_array;
18 G_LOCK_DEFINE_STATIC (crawler_array_lock);
19 GPtrArray *crawler_array;
21 typedef struct _AddrData AddrData;
22 typedef struct _TestData TestData;
38 static void cleanup_crawlers (GMainContext *context);
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 int 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 adder_callback (GIOChannel *source,
85 GIOCondition condition,
93 AddrData *addr_data = data;
95 if (!read_all (source, buf1, 32) ||
96 !read_all (source, buf2, 32))
98 g_main_loop_quit (addr_data->loop);
102 sprintf (result, "%d", atoi(buf1) + atoi(buf2));
103 write_all (addr_data->dest, result, 32);
109 timeout_callback (gpointer data)
111 AddrData *addr_data = data;
119 adder_thread (gpointer data)
121 GMainContext *context;
122 GSource *adder_source;
123 GSource *timeout_source;
125 GIOChannel **channels = data;
128 context = g_main_context_get (g_thread_self());
130 G_LOCK (context_array_lock);
131 g_ptr_array_add (context_array, context);
132 G_UNLOCK (context_array_lock);
134 addr_data.dest = channels[1];
135 addr_data.loop = g_main_loop_new (context, FALSE);
138 adder_source = g_io_create_watch (channels[0], G_IO_IN | G_IO_HUP);
139 g_source_set_callback (adder_source, (GSourceFunc)adder_callback, &addr_data, NULL);
140 g_source_attach (adder_source, context);
142 timeout_source = g_timeout_source_new (10);
143 g_source_set_callback (timeout_source, (GSourceFunc)timeout_callback, &addr_data, NULL);
144 g_source_set_priority (timeout_source, G_PRIORITY_HIGH);
145 g_source_attach (timeout_source, context);
147 g_main_run (addr_data.loop);
149 g_io_channel_close (channels[0]);
150 g_io_channel_close (channels[1]);
151 g_io_channel_unref (channels[0]);
152 g_io_channel_unref (channels[1]);
156 g_main_loop_destroy (addr_data.loop);
158 g_print ("Timeout run %d times\n", addr_data.count);
160 G_LOCK (context_array_lock);
161 g_ptr_array_remove (context_array, context);
162 if (context_array->len == 0)
163 g_main_loop_quit (main_loop);
164 G_UNLOCK (context_array_lock);
166 cleanup_crawlers (context);
170 io_pipe (GIOChannel **channels)
176 g_warning ("Cannot create pipe %s\n", g_strerror (errno));
180 channels[0] = g_io_channel_unix_new (fds[0]);
181 channels[1] = g_io_channel_unix_new (fds[1]);
185 do_add (GIOChannel *in, gint a, gint b)
190 sprintf (buf1, "%d", a);
191 sprintf (buf2, "%d", b);
193 write_all (in, buf1, 32);
194 write_all (in, buf2, 32);
198 adder_response (GIOChannel *source,
199 GIOCondition condition,
203 TestData *test_data = data;
205 if (!read_all (source, result, 32))
208 test_data->current_val = atoi (result);
211 if (test_data->iters == 0)
213 if (test_data->current_val != ITERS * INCREMENT)
215 g_print ("Addition failed: %d != %d\n",
216 test_data->current_val, ITERS * INCREMENT);
220 g_io_channel_close (source);
221 g_io_channel_close (test_data->in);
223 g_io_channel_unref (source);
224 g_io_channel_unref (test_data->in);
229 do_add (test_data->in, test_data->current_val, INCREMENT);
235 create_adder_thread (void)
240 GIOChannel *in_channels[2];
241 GIOChannel *out_channels[2];
243 GIOChannel **sub_channels;
245 sub_channels = g_new (GIOChannel *, 2);
247 io_pipe (in_channels);
248 io_pipe (out_channels);
250 sub_channels[0] = in_channels[0];
251 sub_channels[1] = out_channels[1];
253 g_thread_create (adder_thread, sub_channels, 0,
254 FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, &err);
258 g_warning ("Cannot create thread: %s", err->message);
262 test_data = g_new (TestData, 1);
263 test_data->in = in_channels[1];
264 test_data->current_val = 0;
265 test_data->iters = ITERS;
267 g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
268 adder_response, test_data);
270 do_add (test_data->in, test_data->current_val, INCREMENT);
273 static void create_crawler (void);
276 remove_crawler (void)
278 GSource *other_source;
280 if (crawler_array->len > 0)
282 other_source = crawler_array->pdata[g_random_int_range (0, crawler_array->len)];
283 g_source_destroy (other_source);
284 g_assert (g_ptr_array_remove_fast (crawler_array, other_source));
289 crawler_callback (gpointer data)
291 GSource *source = data;
293 G_LOCK (crawler_array_lock);
295 if (!g_ptr_array_remove_fast (crawler_array, source))
299 G_UNLOCK (crawler_array_lock);
308 create_crawler (void)
310 GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE));
311 g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL);
313 G_LOCK (context_array_lock);
314 g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
315 G_UNLOCK (context_array_lock);
317 G_LOCK (crawler_array_lock);
318 g_ptr_array_add (crawler_array, source);
319 G_UNLOCK (crawler_array_lock);
323 cleanup_crawlers (GMainContext *context)
327 G_LOCK (crawler_array_lock);
328 for (i=0; i < crawler_array->len; i++)
330 if (g_source_get_context (crawler_array->pdata[i]) == context)
332 g_source_destroy (g_ptr_array_remove_index (crawler_array, i));
336 G_UNLOCK (crawler_array_lock);
340 recurser_idle (gpointer data)
342 GMainContext *context = data;
345 for (i = 0; i < 10; i++)
346 g_main_context_iteration (context, TRUE);
352 recurser_start (gpointer data)
354 GMainContext *context;
357 G_LOCK (context_array_lock);
358 context = context_array->pdata[g_random_int_range (0, context_array->len)];
359 source = g_idle_source_new ();
360 g_source_set_callback (source, recurser_idle, context, NULL);
361 g_source_attach (source, context);
362 G_UNLOCK (context_array_lock);
371 /* Only run the test, if threads are enabled and a default thread
372 implementation is available */
373 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
376 g_thread_init (NULL);
378 context_array = g_ptr_array_new ();
379 crawler_array = g_ptr_array_new ();
381 main_loop = g_main_loop_new (NULL, FALSE);
383 for (i = 0; i < NTHREADS; i++)
384 create_adder_thread ();
386 for (i = 0; i < NCRAWLERS; i++)
389 g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL);
391 g_main_loop_run (main_loop);
392 g_main_loop_destroy (main_loop);