10 #include <fcntl.h> /* For _O_BINARY used by pipe() macro */
11 #include <io.h> /* for _pipe() */
18 #define CRAWLER_TIMEOUT_RANGE 40
19 #define RECURSER_TIMEOUT 50
21 /* The partial ordering between the context array mutex and
22 * crawler array mutex is that the crawler array mutex cannot
23 * be locked while the context array mutex is locked
25 GPtrArray *context_array;
26 GMutex *context_array_mutex;
27 GCond *context_array_cond;
31 G_LOCK_DEFINE_STATIC (crawler_array_lock);
32 GPtrArray *crawler_array;
34 typedef struct _AddrData AddrData;
35 typedef struct _TestData TestData;
51 static void cleanup_crawlers (GMainContext *context);
54 read_all (GIOChannel *channel, char *buf, int len)
60 while (bytes_read < len)
62 err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
65 if (err != G_IO_ERROR_AGAIN)
78 write_all (GIOChannel *channel, char *buf, int len)
80 int bytes_written = 0;
84 while (bytes_written < len)
86 err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
87 if (err && err != G_IO_ERROR_AGAIN)
90 bytes_written += count;
97 adder_callback (GIOChannel *source,
98 GIOCondition condition,
106 AddrData *addr_data = data;
108 if (!read_all (source, buf1, 32) ||
109 !read_all (source, buf2, 32))
111 g_main_loop_quit (addr_data->loop);
115 sprintf (result, "%d", atoi(buf1) + atoi(buf2));
116 write_all (addr_data->dest, result, 32);
122 timeout_callback (gpointer data)
124 AddrData *addr_data = data;
132 adder_thread (gpointer data)
134 GMainContext *context;
135 GSource *adder_source;
136 GSource *timeout_source;
138 GIOChannel **channels = data;
141 context = g_main_context_new ();
143 g_mutex_lock (context_array_mutex);
145 g_ptr_array_add (context_array, context);
147 if (context_array->len == NTHREADS)
148 g_cond_broadcast (context_array_cond);
150 g_mutex_unlock (context_array_mutex);
152 addr_data.dest = channels[1];
153 addr_data.loop = g_main_loop_new (context, FALSE);
156 adder_source = g_io_create_watch (channels[0], G_IO_IN | G_IO_HUP);
157 g_source_set_callback (adder_source, (GSourceFunc)adder_callback, &addr_data, NULL);
158 g_source_attach (adder_source, context);
159 g_source_unref (adder_source);
161 timeout_source = g_timeout_source_new (10);
162 g_source_set_callback (timeout_source, (GSourceFunc)timeout_callback, &addr_data, NULL);
163 g_source_set_priority (timeout_source, G_PRIORITY_HIGH);
164 g_source_attach (timeout_source, context);
165 g_source_unref (timeout_source);
167 g_main_run (addr_data.loop);
169 g_io_channel_close (channels[0]);
170 g_io_channel_close (channels[1]);
171 g_io_channel_unref (channels[0]);
172 g_io_channel_unref (channels[1]);
176 g_main_loop_unref (addr_data.loop);
178 g_print ("Timeout run %d times\n", addr_data.count);
180 g_mutex_lock (context_array_mutex);
181 g_ptr_array_remove (context_array, context);
182 if (context_array->len == 0)
183 g_main_loop_quit (main_loop);
184 g_mutex_unlock (context_array_mutex);
186 cleanup_crawlers (context);
192 io_pipe (GIOChannel **channels)
198 g_warning ("Cannot create pipe %s\n", g_strerror (errno));
202 channels[0] = g_io_channel_unix_new (fds[0]);
203 channels[1] = g_io_channel_unix_new (fds[1]);
207 do_add (GIOChannel *in, gint a, gint b)
212 sprintf (buf1, "%d", a);
213 sprintf (buf2, "%d", b);
215 write_all (in, buf1, 32);
216 write_all (in, buf2, 32);
220 adder_response (GIOChannel *source,
221 GIOCondition condition,
225 TestData *test_data = data;
227 if (!read_all (source, result, 32))
230 test_data->current_val = atoi (result);
233 if (test_data->iters == 0)
235 if (test_data->current_val != ITERS * INCREMENT)
237 g_print ("Addition failed: %d != %d\n",
238 test_data->current_val, ITERS * INCREMENT);
242 g_io_channel_close (source);
243 g_io_channel_close (test_data->in);
245 g_io_channel_unref (source);
246 g_io_channel_unref (test_data->in);
251 do_add (test_data->in, test_data->current_val, INCREMENT);
257 create_adder_thread (void)
262 GIOChannel *in_channels[2];
263 GIOChannel *out_channels[2];
265 GIOChannel **sub_channels;
267 sub_channels = g_new (GIOChannel *, 2);
269 io_pipe (in_channels);
270 io_pipe (out_channels);
272 sub_channels[0] = in_channels[0];
273 sub_channels[1] = out_channels[1];
275 g_thread_create (adder_thread, sub_channels, FALSE, &err);
279 g_warning ("Cannot create thread: %s", err->message);
283 test_data = g_new (TestData, 1);
284 test_data->in = in_channels[1];
285 test_data->current_val = 0;
286 test_data->iters = ITERS;
288 g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
289 adder_response, test_data);
291 do_add (test_data->in, test_data->current_val, INCREMENT);
294 static void create_crawler (void);
297 remove_crawler (void)
299 GSource *other_source;
301 if (crawler_array->len > 0)
303 other_source = crawler_array->pdata[g_random_int_range (0, crawler_array->len)];
304 g_source_destroy (other_source);
305 g_assert (g_ptr_array_remove_fast (crawler_array, other_source));
310 crawler_callback (gpointer data)
312 GSource *source = data;
314 G_LOCK (crawler_array_lock);
316 if (!g_ptr_array_remove_fast (crawler_array, source))
320 G_UNLOCK (crawler_array_lock);
329 create_crawler (void)
331 GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE));
332 g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL);
334 G_LOCK (crawler_array_lock);
335 g_ptr_array_add (crawler_array, source);
337 g_mutex_lock (context_array_mutex);
338 g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
339 g_source_unref (source);
340 g_mutex_unlock (context_array_mutex);
342 G_UNLOCK (crawler_array_lock);
346 cleanup_crawlers (GMainContext *context)
350 G_LOCK (crawler_array_lock);
351 for (i=0; i < crawler_array->len; i++)
353 if (g_source_get_context (crawler_array->pdata[i]) == context)
355 g_source_destroy (g_ptr_array_remove_index (crawler_array, i));
359 G_UNLOCK (crawler_array_lock);
363 recurser_idle (gpointer data)
365 GMainContext *context = data;
368 for (i = 0; i < 10; i++)
369 g_main_context_iteration (context, FALSE);
375 recurser_start (gpointer data)
377 GMainContext *context;
380 g_mutex_lock (context_array_mutex);
381 context = context_array->pdata[g_random_int_range (0, context_array->len)];
382 source = g_idle_source_new ();
383 g_source_set_callback (source, recurser_idle, context, NULL);
384 g_source_attach (source, context);
385 g_source_unref (source);
386 g_mutex_unlock (context_array_mutex);
395 /* Only run the test, if threads are enabled and a default thread
396 implementation is available */
397 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
400 g_thread_init (NULL);
402 context_array = g_ptr_array_new ();
403 context_array_mutex = g_mutex_new ();
404 context_array_cond = g_cond_new ();
406 crawler_array = g_ptr_array_new ();
408 main_loop = g_main_loop_new (NULL, FALSE);
410 for (i = 0; i < NTHREADS; i++)
411 create_adder_thread ();
413 /* Wait for all threads to start
415 g_mutex_lock (context_array_mutex);
417 if (context_array->len < NTHREADS)
418 g_cond_wait (context_array_cond, context_array_mutex);
420 g_mutex_unlock (context_array_mutex);
422 for (i = 0; i < NCRAWLERS; i++)
425 g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL);
427 g_main_loop_run (main_loop);
428 g_main_loop_unref (main_loop);