The main event loop manages all the available sources of events for GLib and GTK + applications. These events can come from any number of different types of sources such as file descriptors (plain files, pipes or sockets s) and timeouts. New types of event sources can also be added using G_source_attach ().
To allow multiple independent sets of sources to being handled in different threads, each source is associated with a Gmainco ntext. A gmaincontext can is running in a single thread, but sources can is added to it and removed from it from other Threa Ds.
Each event source is assigned a priority. The default priority, G_priority_default, is 0. Values less than 0 denote higher priorities. Values greater than 0 denote lower priorities. Events from high priority sources is always processed before events from lower to priority sources.
Idle functions can also is added, and assigned a priority. These'll be run whenever no events with a higher priority is ready to be processed.
The Gmainloop data type represents a main event loop. A Gmainloop is created with G_main_loop_new (). After adding the initial event sources, G_main_loop_run () is called. This continuously checks for new events from each of the event sources and dispatches them. Finally, the processing of an event from one of the sources leads to a call to G_main_loop_quit () to exit the main loop, a nd G_main_loop_run () returns.
It is possible to create new instances of Gmainloop recursively. This was often used in GTK + applications when showing modal dialog boxes. Note that event sources is associated with a particular gmaincontext, and would be checked and dispatched for all main loo PS associated with that gmaincontext. explain
There are n context, and there is a Adder in each context. Each of the crawler is randomly associated with a context. Crawler does not affect Adder.
#undef g_disable_assert #undef g_log_domain #include <errno.h> #include <glib.h> #include <unistd.h> # Include <stdio.h> #include <stdlib.h> #define ITERS 10000 #define INCREMENT #define NTHREADS 4 #define NC Rawlers 4 #define CRAWLER_TIMEOUT_RANGE #define RECURSER_TIMEOUT/* The partial ordering between the context array Mutex and * Crawler array mutex is, the crawler array mutex cannot * be locked while the context array mutex is LOC
ked */Gptrarray *context_array;
Gmutex *context_array_mutex;
Gcond *context_array_cond;
Gmainloop *main_loop;
G_lock_define_static (Crawler_array_lock);
Gptrarray *crawler_array;
typedef struct _ADDRDATA Addrdata;
typedef struct _TESTDATA TestData;
struct _addrdata {gmainloop *loop;
Giochannel *dest;
Gint count;
};
struct _testdata {gint current_val;
Gint iters;
Giochannel *in;
};
static void Cleanup_crawlers (Gmaincontext *context); Gboolean Read_all (Giochannel *channel, Char *buf, Gsize len) {gsize bytes_read = 0;
Gsize count;
Gioerror err;
while (Bytes_read < len) {err = G_io_channel_read (channel, buf + Bytes_read, Len-bytes_read, &count);
if (err) {if (err! = G_io_error_again) return FALSE;
} else if (count = = 0) return FALSE;
Bytes_read + = count;
} return TRUE;
} Gboolean Write_all (Giochannel *channel, Char *buf, Gsize len) {gsize Bytes_written = 0;
Gsize count;
Gioerror err; while (Bytes_written < len) {err = G_io_channel_write (channel, buf + Bytes_written, Len-bytes_written, &A
Mp;count);
if (err && err! = G_io_error_again) return FALSE;
Bytes_written + = count;
} return TRUE;
} Gboolean Adder_callback (Giochannel *source, giocondition condition, gpointer data) {char buf1[32];
Char buf2[32];
Char result[32];
Addrdata *addr_data = data;
if (!read_all (source, BUF1, 32) | | !read_all (SoUrce, Buf2, ()) {g_main_loop_quit (addr_data->loop);
return FALSE;
} sprintf (Result, "%d", atoi (BUF1) + atoi (BUF2));
Write_all (Addr_data->dest, result, 32);
return TRUE;
} Gboolean Timeout_callback (Gpointer data) {Addrdata *addr_data = data;
addr_data->count++;
return TRUE;
} gpointer Adder_thread (Gpointer data) {Gmaincontext *context;
Gsource *adder_source;
Gsource *timeout_source;
Giochannel **channels = data;
Addrdata Addr_data;
context = G_main_context_new ();
G_mutex_lock (Context_array_mutex);
G_ptr_array_add (Context_array, context);
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);
Addr_data.count = 0; Adder_source = G_io_create_watch (channels[0], g_io_in |
G_IO_HUP); G_source_set_callback (Adder_source, (Gsourcefunc) adder_Callback, &addr_data, NULL);
G_source_attach (Adder_source, context);
G_source_unref (Adder_source);
Timeout_source = G_timeout_source_new (10);
G_source_set_callback (Timeout_source, (Gsourcefunc) Timeout_callback, &addr_data, NULL);
G_source_set_priority (Timeout_source, G_priority_high);
G_source_attach (Timeout_source, context);
G_source_unref (Timeout_source);
G_main_loop_run (Addr_data.loop);
G_io_channel_unref (Channels[0]);
G_io_channel_unref (Channels[1]);
G_free (Channels);
G_main_loop_unref (Addr_data.loop);
#ifdef VERBOSE g_print ("Timeout Run%d times\n", addr_data.count);
#endif 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_mutex_unlock (Context_array_mutex);
Cleanup_crawlers (context);
return NULL;
} void Io_pipe (Giochannel **channels) {gint fds[2]; if (pipe (FDS) < 0) {g_warning ("CanNot create pipe%s\n ", G_strerror (errno));
Exit (1);
} Channels[0] = G_io_channel_unix_new (fds[0]);
CHANNELS[1] = g_io_channel_unix_new (fds[1]);
G_io_channel_set_close_on_unref (Channels[0], TRUE);
G_io_channel_set_close_on_unref (Channels[1], TRUE);
} void Do_add (Giochannel *in, gint A, gint b) {char buf1[32];
Char buf2[32];
sprintf (BUF1, "%d", a);
sprintf (Buf2, "%d", b);
Write_all (in, Buf1, 32);
Write_all (in, Buf2, 32);
} Gboolean Adder_response (Giochannel *source, giocondition condition, gpointer data) {char result[32];
TestData *test_data = data;
if (!read_all (source, result,)) return FALSE;
Test_data->current_val = atoi (result);
test_data->iters--; if (test_data->iters = = 0) {if (Test_data->current_val! = iters * INCREMENT) {g_print ("addition FAI
LED:%d! =%d\n ", Test_data->current_val, Iters * INCREMENT);
Exit (1); } g_io_channel_unref (SourCE);
G_io_channel_unref (Test_data->in);
G_free (Test_data);
return FALSE;
} do_add (Test_data->in, Test_data->current_val, INCREMENT);
return TRUE;
} void Create_adder_thread (void) {Gerror *err = NULL;
TestData *test_data;
Giochannel *in_channels[2];
Giochannel *out_channels[2];
Giochannel **sub_channels;
Sub_channels = G_new (Giochannel *, 2);
Io_pipe (In_channels);
Io_pipe (Out_channels);
Sub_channels[0] = in_channels[0];
SUB_CHANNELS[1] = out_channels[1];
G_thread_create (Adder_thread, Sub_channels, FALSE, &err);
if (err) {g_warning ("Cannot create thread:%s", err->message);
Exit (1);
} Test_data = G_new (TestData, 1);
Test_data->in = in_channels[1];
Test_data->current_val = 0;
Test_data->iters = iters; G_io_add_watch (Out_channels[0], g_io_in |
G_io_hup, Adder_response, Test_data); Do_add (Test_data->in, Test_data->current_val, INCREment);
} static void Create_crawler (void);
static void Remove_crawler (void) {Gsource *other_source; if (Crawler_array->len > 0) {other_source = Crawler_array->pdata[g_random_int_range (0, crawler_array-
>len)];
G_source_destroy (Other_source);
G_assert (G_ptr_array_remove_fast (Crawler_array, Other_source));
}} static Gint Crawler_callback (Gpointer data) {Gsource *source = data; G_lock (Crawler_array_lock); /* <--Note, before this crawler lock the * array, it could be removed during the Interva L/* If my corresponding source is all ready been removed, remove another one */if (!g_ptr_array_remove_fast (CR
Awler_array, source)) Remove_crawler ();
Remove_crawler ();
G_unlock (Crawler_array_lock);
Create_crawler ();
Create_crawler ();
return FALSE; } static void Create_crawler (void) {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 (Crawler_array_lock);
G_ptr_array_add (Crawler_array, source);
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_mutex_unlock (Context_array_mutex);
G_unlock (Crawler_array_lock);
} static void Cleanup_crawlers (Gmaincontext *context) {gint i;
G_lock (Crawler_array_lock);
for (i=0; i < crawler_array->len; i++) {if (G_source_get_context (crawler_array->pdata[i]) = = context)
{G_source_destroy (G_ptr_array_remove_index (Crawler_array, i));
i--;
}} g_unlock (Crawler_array_lock);
} static Gboolean Recurser_idle (Gpointer data) {Gmaincontext *context = data;
Gint i;
for (i = 0; i < i++) g_main_context_iteration (context, FALSE);
return FALSE;
} static Gboolean Recurser_start (Gpointer data){Gmaincontext *context;
Gsource *source;
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_mutex_unlock (Context_array_mutex);
return TRUE;
} int main (int argc, char *argv[]) {gint i;
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_unref (Main_loop);
return 0; }