diff options
author | Peng Huang <shawn.p.huang@gmail.com> | 2010-10-29 18:46:07 +0900 |
---|---|---|
committer | Peng Huang <shawn.p.huang@gmail.com> | 2010-10-29 18:46:07 +0900 |
commit | fd7e899c327ba9e1bf33fa79ce6027e35ac3bccc (patch) | |
tree | 04fa26a6203e27dd8cf596628086c34192842121 | |
parent | 9cf6eed3c3fc7a40d0c6f75117fd2c4784b60536 (diff) | |
download | ibus-fd7e899c327ba9e1bf33fa79ce6027e35ac3bccc.tar.gz |
Use block mode of g_main_context_iteration to avoid consuming too much cpu time.
BUG=none
TEST=manual
Review URL: http://codereview.appspot.com/2768043
-rw-r--r-- | bus/ibusimpl.c | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/bus/ibusimpl.c b/bus/ibusimpl.c index 410cdb1b..da243548 100644 --- a/bus/ibusimpl.c +++ b/bus/ibusimpl.c @@ -812,25 +812,38 @@ _ibus_get_address (BusIBusImpl *ibus, return reply; } + +static gboolean +_timeout_cb (gpointer data) +{ + return TRUE; +} + static BusFactoryProxy * _get_factory_proxy(IBusEngineDesc *engine_desc) { - BusFactoryProxy *factory; - GTimeVal t1, t2; - g_get_current_time (&t1); - while (1) { - if (g_main_context_pending (NULL)) { - g_main_context_iteration (NULL, FALSE); + BusFactoryProxy *factory = NULL; + + /* Add a timeout to wake up g_main_context_iteration in every 0.5 second, + * and then to check the factory assocated with the engine_desc */ + guint timeout_id = g_timeout_add (500, _timeout_cb, NULL); + + GTimer *timer = g_timer_new (); + + /* Leave the loop, if it spends more than 5 seconds */ + while (g_timer_elapsed (timer, NULL) <= 5.0) { + if (g_main_context_iteration (NULL, TRUE)) { factory = bus_factory_proxy_get_from_engine (engine_desc); if (factory != NULL) { - return factory; + break; } } - g_get_current_time (&t2); - if (t2.tv_sec - t1.tv_sec >= 5) - break; } - return NULL; + + g_source_remove (timeout_id); + g_timer_destroy (timer); + + return factory; } static BusEngineProxy * |