1 #undef G_DISABLE_ASSERT
13 #include <fcntl.h> /* For _O_BINARY used by pipe() macro */
14 #include <io.h> /* for _pipe() */
21 #define CRAWLER_TIMEOUT_RANGE 40
22 #define RECURSER_TIMEOUT 50
24 /* The partial ordering between the context array mutex and
25 * crawler array mutex is that the crawler array mutex cannot
26 * be locked while the context array mutex is locked
28 GPtrArray *context_array;
29 GMutex *context_array_mutex;
30 GCond *context_array_cond;
34 G_LOCK_DEFINE_STATIC (crawler_array_lock);
35 GPtrArray *crawler_array;
37 typedef struct _AddrData AddrData;
38 typedef struct _TestData TestData;
54 static void cleanup_crawlers (GMainContext *context);
57 read_all (GIOChannel *channel, char *buf, gsize len)
63 while (bytes_read < len)
65 err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
68 if (err != G_IO_ERROR_AGAIN)
81 write_all (GIOChannel *channel, char *buf, gsize len)
83 gsize bytes_written = 0;
87 while (bytes_written < len)
89 err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
90 if (err && err != G_IO_ERROR_AGAIN)
93 bytes_written += count;
100 adder_callback (GIOChannel *source,
101 GIOCondition condition,
109 AddrData *addr_data = data;
111 if (!read_all (source, buf1, 32) ||
112 !read_all (source, buf2, 32))
114 g_main_loop_quit (addr_data->loop);
118 sprintf (result, "%d", atoi(buf1) + atoi(buf2));
119 write_all (addr_data->dest, result, 32);
125 timeout_callback (gpointer data)
127 AddrData *addr_data = data;
135 adder_thread (gpointer data)
137 GMainContext *context;
138 GSource *adder_source;
139 GSource *timeout_source;
141 GIOChannel **channels = data;
144 context = g_main_context_new ();
146 g_mutex_lock (context_array_mutex);
148 g_ptr_array_add (context_array, context);
150 if (context_array->len == NTHREADS)
151 g_cond_broadcast (context_array_cond);
153 g_mutex_unlock (context_array_mutex);
155 addr_data.dest = channels[1];
156 addr_data.loop = g_main_loop_new (context, FALSE);
159 adder_source = g_io_create_watch (channels[0], G_IO_IN | G_IO_HUP);
160 g_source_set_callback (adder_source, (GSourceFunc)adder_callback, &addr_data, NULL);
161 g_source_attach (adder_source, context);
162 g_source_unref (adder_source);
164 timeout_source = g_timeout_source_new (10);
165 g_source_set_callback (timeout_source, (GSourceFunc)timeout_callback, &addr_data, NULL);
166 g_source_set_priority (timeout_source, G_PRIORITY_HIGH);
167 g_source_attach (timeout_source, context);
168 g_source_unref (timeout_source);
170 g_main_run (addr_data.loop);
172 g_io_channel_close (channels[0]);
173 g_io_channel_close (channels[1]);
174 g_io_channel_unref (channels[0]);
175 g_io_channel_unref (channels[1]);
179 g_main_loop_unref (addr_data.loop);
181 g_print ("Timeout run %d times\n", addr_data.count);
183 g_mutex_lock (context_array_mutex);
184 g_ptr_array_remove (context_array, context);
185 if (context_array->len == 0)
186 g_main_loop_quit (main_loop);
187 g_mutex_unlock (context_array_mutex);
189 cleanup_crawlers (context);
195 io_pipe (GIOChannel **channels)
201 g_warning ("Cannot create pipe %s\n", g_strerror (errno));
205 channels[0] = g_io_channel_unix_new (fds[0]);
206 channels[1] = g_io_channel_unix_new (fds[1]);
210 do_add (GIOChannel *in, gint a, gint b)
215 sprintf (buf1, "%d", a);
216 sprintf (buf2, "%d", b);
218 write_all (in, buf1, 32);
219 write_all (in, buf2, 32);
223 adder_response (GIOChannel *source,
224 GIOCondition condition,
228 TestData *test_data = data;
230 if (!read_all (source, result, 32))
233 test_data->current_val = atoi (result);
236 if (test_data->iters == 0)
238 if (test_data->current_val != ITERS * INCREMENT)
240 g_print ("Addition failed: %d != %d\n",
241 test_data->current_val, ITERS * INCREMENT);
245 g_io_channel_close (source);
246 g_io_channel_close (test_data->in);
248 g_io_channel_unref (source);
249 g_io_channel_unref (test_data->in);
256 do_add (test_data->in, test_data->current_val, INCREMENT);
262 create_adder_thread (void)
267 GIOChannel *in_channels[2];
268 GIOChannel *out_channels[2];
270 GIOChannel **sub_channels;
272 sub_channels = g_new (GIOChannel *, 2);
274 io_pipe (in_channels);
275 io_pipe (out_channels);
277 sub_channels[0] = in_channels[0];
278 sub_channels[1] = out_channels[1];
280 g_thread_create (adder_thread, sub_channels, FALSE, &err);
284 g_warning ("Cannot create thread: %s", err->message);
288 test_data = g_new (TestData, 1);
289 test_data->in = in_channels[1];
290 test_data->current_val = 0;
291 test_data->iters = ITERS;
293 g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
294 adder_response, test_data);
296 do_add (test_data->in, test_data->current_val, INCREMENT);
299 static void create_crawler (void);
302 remove_crawler (void)
304 GSource *other_source;
306 if (crawler_array->len > 0)
308 other_source = crawler_array->pdata[g_random_int_range (0, crawler_array->len)];
309 g_source_destroy (other_source);
310 g_assert (g_ptr_array_remove_fast (crawler_array, other_source));
315 crawler_callback (gpointer data)
317 GSource *source = data;
319 G_LOCK (crawler_array_lock);
321 if (!g_ptr_array_remove_fast (crawler_array, source))
325 G_UNLOCK (crawler_array_lock);
334 create_crawler (void)
336 GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE));
337 g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL);
339 G_LOCK (crawler_array_lock);
340 g_ptr_array_add (crawler_array, source);
342 g_mutex_lock (context_array_mutex);
343 g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
344 g_source_unref (source);
345 g_mutex_unlock (context_array_mutex);
347 G_UNLOCK (crawler_array_lock);
351 cleanup_crawlers (GMainContext *context)
355 G_LOCK (crawler_array_lock);
356 for (i=0; i < crawler_array->len; i++)
358 if (g_source_get_context (crawler_array->pdata[i]) == context)
360 g_source_destroy (g_ptr_array_remove_index (crawler_array, i));
364 G_UNLOCK (crawler_array_lock);
368 recurser_idle (gpointer data)
370 GMainContext *context = data;
373 for (i = 0; i < 10; i++)
374 g_main_context_iteration (context, FALSE);
380 recurser_start (gpointer data)
382 GMainContext *context;
385 g_mutex_lock (context_array_mutex);
386 context = context_array->pdata[g_random_int_range (0, context_array->len)];
387 source = g_idle_source_new ();
388 g_source_set_callback (source, recurser_idle, context, NULL);
389 g_source_attach (source, context);
390 g_source_unref (source);
391 g_mutex_unlock (context_array_mutex);
400 /* Only run the test, if threads are enabled and a default thread
401 implementation is available */
402 #if defined(G_THREADS_ENABLED) && ! defined(G_THREADS_IMPL_NONE)
405 g_thread_init (NULL);
407 context_array = g_ptr_array_new ();
408 context_array_mutex = g_mutex_new ();
409 context_array_cond = g_cond_new ();
411 crawler_array = g_ptr_array_new ();
413 main_loop = g_main_loop_new (NULL, FALSE);
415 for (i = 0; i < NTHREADS; i++)
416 create_adder_thread ();
418 /* Wait for all threads to start
420 g_mutex_lock (context_array_mutex);
422 if (context_array->len < NTHREADS)
423 g_cond_wait (context_array_cond, context_array_mutex);
425 g_mutex_unlock (context_array_mutex);
427 for (i = 0; i < NCRAWLERS; i++)
430 g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL);
432 g_main_loop_run (main_loop);
433 g_main_loop_unref (main_loop);