#include <errno.h>
#include <glib.h>
+#ifdef G_OS_UNIX
#include <unistd.h>
+#endif
#include <stdio.h>
#include <stdlib.h>
+#ifdef G_OS_WIN32
+#include <fcntl.h> /* For _O_BINARY used by pipe() macro */
+#include <io.h> /* for _pipe() */
+#endif
+
#define ITERS 10000
#define INCREMENT 10
#define NTHREADS 4
#define CRAWLER_TIMEOUT_RANGE 40
#define RECURSER_TIMEOUT 50
-G_LOCK_DEFINE_STATIC (context_array_lock);
GPtrArray *context_array;
+GMutex *context_array_mutex;
+GCond *context_array_cond;
+
GMainLoop *main_loop;
G_LOCK_DEFINE_STATIC (crawler_array_lock);
return TRUE;
}
-void
+gpointer
adder_thread (gpointer data)
{
GMainContext *context;
GIOChannel **channels = data;
AddrData addr_data;
- context = g_main_context_get (g_thread_self());
+ context = g_main_context_new ();
- G_LOCK (context_array_lock);
+ g_mutex_lock (context_array_mutex);
+
g_ptr_array_add (context_array, context);
- G_UNLOCK (context_array_lock);
+
+ if (context_array->len == NTHREADS)
+ g_cond_broadcast (context_array_cond);
+
+ g_mutex_unlock (context_array_mutex);
addr_data.dest = channels[1];
addr_data.loop = g_main_loop_new (context, FALSE);
g_free (channels);
- g_main_loop_destroy (addr_data.loop);
+ g_main_loop_unref (addr_data.loop);
g_print ("Timeout run %d times\n", addr_data.count);
- G_LOCK (context_array_lock);
+ g_mutex_lock (context_array_mutex);
g_ptr_array_remove (context_array, context);
if (context_array->len == 0)
g_main_loop_quit (main_loop);
- G_UNLOCK (context_array_lock);
+ g_mutex_unlock (context_array_mutex);
cleanup_crawlers (context);
+
+ return NULL;
}
void
sub_channels[0] = in_channels[0];
sub_channels[1] = out_channels[1];
- g_thread_create (adder_thread, sub_channels, 0,
- FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, &err);
+ g_thread_create (adder_thread, sub_channels, FALSE, &err);
if (err)
{
GSource *source = g_timeout_source_new (g_random_int_range (0, CRAWLER_TIMEOUT_RANGE));
g_source_set_callback (source, (GSourceFunc)crawler_callback, source, NULL);
- G_LOCK (context_array_lock);
+ g_mutex_lock (context_array_mutex);
g_source_attach (source, context_array->pdata[g_random_int_range (0, context_array->len)]);
g_source_unref (source);
- G_UNLOCK (context_array_lock);
+ g_mutex_unlock (context_array_mutex);
G_LOCK (crawler_array_lock);
g_ptr_array_add (crawler_array, source);
GMainContext *context;
GSource *source;
- G_LOCK (context_array_lock);
+ g_mutex_lock (context_array_mutex);
context = context_array->pdata[g_random_int_range (0, context_array->len)];
source = g_idle_source_new ();
g_source_set_callback (source, recurser_idle, context, NULL);
g_source_attach (source, context);
g_source_unref (source);
- G_UNLOCK (context_array_lock);
+ g_mutex_unlock (context_array_mutex);
return TRUE;
}
g_thread_init (NULL);
context_array = g_ptr_array_new ();
+ context_array_mutex = g_mutex_new ();
+ context_array_cond = g_cond_new ();
+
crawler_array = g_ptr_array_new ();
main_loop = g_main_loop_new (NULL, FALSE);
for (i = 0; i < NTHREADS; i++)
create_adder_thread ();
+ /* Wait for all threads to start
+ */
+ g_mutex_lock (context_array_mutex);
+
+ if (context_array->len < NTHREADS)
+ g_cond_wait (context_array_cond, context_array_mutex);
+
+ g_mutex_unlock (context_array_mutex);
+
for (i = 0; i < NCRAWLERS; i++)
create_crawler ();
g_timeout_add (RECURSER_TIMEOUT, recurser_start, NULL);
g_main_loop_run (main_loop);
- g_main_loop_destroy (main_loop);
+ g_main_loop_unref (main_loop);
#endif
return 0;