10 #include <fcntl.h> /* For _O_BINARY used by pipe() macro */
17 #define CRAWLER_TIMEOUT_RANGE 40
18 #define RECURSER_TIMEOUT 50
20 GPtrArray *context_array;
21 GMutex *context_array_mutex;
22 GCond *context_array_cond;
26 G_LOCK_DEFINE_STATIC (crawler_array_lock);
27 GPtrArray *crawler_array;
29 typedef struct _AddrData AddrData;
30 typedef struct _TestData TestData;
46 static void cleanup_crawlers (GMainContext *context);
49 read_all (GIOChannel *channel, char *buf, int len)
55 while (bytes_read < len)
57 err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
60 if (err != G_IO_ERROR_AGAIN)
73 write_all (GIOChannel *channel, char *buf, int len)
75 int 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 adder_callback (GIOChannel *source,
93 GIOCondition condition,
101 AddrData *addr_data = data;
103 if (!read_all (source, buf1, 32) ||
104 !read_all (source, buf2, 32))
106 g_main_loop_quit (addr_data->loop);
110 sprintf (result, "%d", atoi(buf1) + atoi(buf2));
111 write_all (addr_data->dest, result, 32);
117 timeout_callback (gpointer data)
119 AddrData *addr_data = data;
127 adder_thread (gpointer data)
129 GMainContext *context;
130 GSource *adder_source;
131 GSource *timeout_source;
133 GIOChannel **channels = data;
136 context = g_main_context_new ();
138 g_mutex_lock (context_array_mutex);
140 g_ptr_array_add (context_array, context);
142 if (context_array->len == NTHREADS)
143 g_cond_broadcast (context_array_cond);
145 g_mutex_unlock (context_array_mutex);
147 addr_data.dest = channels[1];
148 addr_data.loop = g_main_loop_new (context, FALSE);
151 adder_source = g_io_create_watch (channels[0], G_IO_IN | G_IO_HUP);
152 g_source_set_callback (adder_source, (GSourceFunc)adder_callback, &addr_data, NULL);
153 g_source_attach (adder_source, context);
154 g_source_unref (adder_source);
156 timeout_source = g_timeout_source_new (10);
157 g_source_set_callback (timeout_source, (GSourceFunc)timeout_callback, &addr_data, NULL);
158 g_source_set_priority (timeout_source, G_PRIORITY_HIGH);
159 g_source_attach (timeout_source, context);
160 g_source_unref (timeout_source);
162 g_main_run (addr_data.loop);
164 g_io_channel_close (channels[0]);
165 g_io_channel_close (channels[1]);
166 g_io_channel_unref (channels[0]);
167 g_io_channel_unref (channels[1]);
171 g_main_loop_unref (addr_data.loop);
173 g_print ("Timeout run %d times\n", addr_data.count);
175 g_mutex_lock (context_array_mutex);
176 g_ptr_array_remove (context_array, context);
177 if (context_array->len == 0)
178 g_main_loop_quit (main_loop);
179 g_mutex_unlock (context_array_mutex);
181 cleanup_crawlers (context);
187 io_pipe (GIOChannel **channels)
193 g_warning ("Cannot create pipe %s\n", g_strerror (errno));
197 channels[0] = g_io_channel_unix_new (fds[0]);
198 channels[1] = g_io_channel_unix_new (fds[1]);
202 do_add (GIOChannel *in, gint a, gint b)
207 sprintf (buf1, "%d", a);
208 sprintf (buf2, "%d", b);
210 write_all (in, buf1, 32);
211 write_all (in, buf2, 32);
215 adder_response (GIOChannel *source,
216 GIOCondition condition,
220 TestData *test_data = data;
222 if (!read_all (source, result, 32))
225 test_data->current_val = atoi (result);
228 if (test_data->iters == 0)
230 if (test_data->current_val != ITERS * INCREMENT)
232 g_print ("Addition failed: %d != %d\n",
233 test_data->current_val, ITERS * INCREMENT);
237 g_io_channel_close (source);
238 g_io_channel_close (test_data->in);
240 g_io_channel_unref (source);
241 g_io_channel_unref (test_data->in);
246 do_add (test_data->in, test_data->current_val, INCREMENT);
252 create_adder_thread (void)
257 GIOChannel *in_channels[2];
258 GIOChannel *out_channels[2];
260 GIOChannel **sub_channels;
262 sub_channels = g_new (GIOChannel *, 2);
264 io_pipe (in_channels);
265 io_pipe (out_channels);
267 sub_channels[0] = in_channels[0];
268 sub_channels[1] = out_channels[1];
270 g_thread_create (adder_thread, sub_channels, FALSE, &err);
274 g_warning ("Cannot create thread: %s", err->message);
278 test_data = g_new (TestData, 1);
279 test_data->in = in_channels[1];
280 test_data->current_val = 0;
281 test_data->iters = ITERS;
283 g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
284 adder_response, test_data);
286 do_add (test_data->in, test_data->current_val, INCREMENT);
289 static void create_crawler (void);
292 remove_crawler (void)
294 GSource *other_source;
296 if (crawler_array->len > 0)
298 other_source = crawler_array->pdata[g_random_int_range (0, crawler_array->len)];
299 g_source_destroy (other_source);
300 g_assert (g_ptr_array_remove_fast (crawler_array, other_source));
305 crawler_callback (gpointer data)
307 GSource *source = data;
309 G_LOCK (crawler_array_lock);
311 if (!g_ptr_array_remove_fast (crawler_array, source))
315 G_UNLOCK (crawler_array_lock);
324 create_crawler (void)
326 GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE));
327 g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL);
329 g_mutex_lock (context_array_mutex);
330 g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
331 g_source_unref (source);
332 g_mutex_unlock (context_array_mutex);
334 G_LOCK (crawler_array_lock);
335 g_ptr_array_add (crawler_array, source);
336 G_UNLOCK (crawler_array_lock);
340 cleanup_crawlers (GMainContext *context)
344 G_LOCK (crawler_array_lock);
345 for (i=0; i < crawler_array->len; i++)
347 if (g_source_get_context (crawler_array->pdata[i]) == context)
349 g_source_destroy (g_ptr_array_remove_index (crawler_array, i));
353 G_UNLOCK (crawler_array_lock);
357 recurser_idle (gpointer data)
359 GMainContext *context = data;
362 for (i = 0; i < 10; i++)
363 g_main_context_iteration (context, TRUE);
369 recurser_start (gpointer data)
371 GMainContext *context;
374 g_mutex_lock (context_array_mutex);
375 context = context_array->pdata[g_random_int_range (0, context_array->len)];
376 source = g_idle_source_new ();
377 g_source_set_callback (source, recurser_idle, context, NULL);
378 g_source_attach (source, context);
379 g_source_unref (source);
380 g_mutex_unlock (context_array_mutex);
389 /* Only run the test, if threads are enabled and a default thread
390 implementation is available */
391 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
394 g_thread_init (NULL);
396 context_array = g_ptr_array_new ();
397 context_array_mutex = g_mutex_new ();
398 context_array_cond = g_cond_new ();
400 crawler_array = g_ptr_array_new ();
402 main_loop = g_main_loop_new (NULL, FALSE);
404 for (i = 0; i < NTHREADS; i++)
405 create_adder_thread ();
407 /* Wait for all threads to start
409 g_mutex_lock (context_array_mutex);
411 if (context_array->len < NTHREADS)
412 g_cond_wait (context_array_cond, context_array_mutex);
414 g_mutex_unlock (context_array_mutex);
416 for (i = 0; i < NCRAWLERS; i++)
419 g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL);
421 g_main_loop_run (main_loop);
422 g_main_loop_unref (main_loop);