From 2af66414676f112177a0e325e675fee00154d74d Mon Sep 17 00:00:00 2001 From: barbieri Date: Tue, 23 Feb 2010 21:27:04 +0000 Subject: [PATCH] Fix idlers when using recursive main loops. If an idler created a recursive main loop (just called ecore_main_loop_begin()), then this recursive main loop should continue to process idlers from there and on, thus idler_current was added. When going back from recursion, the current iterator should be updated properly. This patch also fixes the deletion of idlers from recursive main loops by reference counting them. This way, the node will not be free()d inside inner loop cleanups and then crash when going back to outer loop. The following test case used to crash but not anymore: #include #include static int _log_dom; #define INF(...) EINA_LOG_DOM_INFO(_log_dom, __VA_ARGS__) static Ecore_Idler *handle; static int idler(void *data) { INF("idler"); return 1; } static int cb2(void *data) { INF("cb2 - delete cb1 handle"); ecore_idler_del(handle); ecore_main_loop_quit(); /* quits inner main loop */ return 0; } static int cb1(void *data) { INF("cb1: begin"); INF(" add cb2"); ecore_idler_add(cb2, NULL); INF(" inner main loop begin (recurse)"); ecore_main_loop_begin(); /* will it crash due ecore_idler_del(handle) * inside cb2()? It used to! */ INF("cb1: end"); ecore_main_loop_quit(); /* quits outer main loop */ return 0; } int main(void) { ecore_init(); _log_dom = eina_log_domain_register("test", EINA_COLOR_CYAN); /* * Creating a new main loop from inside an idler callback, and inside * this new (inner) main loop deleting the caller callback used to * crash since the handle would be effectively free()d, but when the * recursion is over the pointer would be used. */ INF("main: begin"); handle = ecore_idler_add(cb1, NULL); ecore_idler_add(idler, NULL); ecore_main_loop_begin(); INF("main: end"); return 0; } git-svn-id: http://svn.enlightenment.org/svn/e/trunk/ecore@46406 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/lib/ecore/ecore_idler.c | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/lib/ecore/ecore_idler.c b/src/lib/ecore/ecore_idler.c index 76b68ba..6df9ad0 100644 --- a/src/lib/ecore/ecore_idler.c +++ b/src/lib/ecore/ecore_idler.c @@ -16,13 +16,15 @@ struct _Ecore_Idler { EINA_INLIST; ECORE_MAGIC; - int delete_me : 1; int (*func) (void *data); void *data; + int references; + Eina_Bool delete_me : 1; }; static Ecore_Idler *idlers = NULL; +static Ecore_Idler *idler_current = NULL; static int idlers_delete_me = 0; /** @@ -87,35 +89,58 @@ _ecore_idler_shutdown(void) free(ie); } idlers_delete_me = 0; + idler_current = NULL; } int _ecore_idler_call(void) { - Ecore_Idler *ie; + if (!idler_current) + { + /* regular main loop, start from head */ + idler_current = idlers; + } + else + { + /* recursive main loop, continue from where we were */ + idler_current = (Ecore_Idler *)EINA_INLIST_GET(idler_current)->next; + } - EINA_INLIST_FOREACH(idlers, ie) + while (idler_current) { + Ecore_Idler *ie = (Ecore_Idler *)idler_current; if (!ie->delete_me) { + ie->references++; if (!ie->func(ie->data)) ecore_idler_del(ie); + ie->references--; } + if (idler_current) /* may have changed in recursive main loops */ + idler_current = (Ecore_Idler *)EINA_INLIST_GET(idler_current)->next; } if (idlers_delete_me) { - Ecore_Idler *l; + Ecore_Idler *l; + int deleted_idlers_in_use = 0; for (l = idlers; l;) { - ie = l; + Ecore_Idler *ie = l; l = (Ecore_Idler *) EINA_INLIST_GET(l)->next; if (ie->delete_me) { + if (ie->references) + { + deleted_idlers_in_use++; + continue; + } + idlers = (Ecore_Idler *) eina_inlist_remove(EINA_INLIST_GET(idlers), EINA_INLIST_GET(ie)); ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE); free(ie); } } - idlers_delete_me = 0; + if (!deleted_idlers_in_use) + idlers_delete_me = 0; } if (idlers) return 1; return 0; -- 2.7.4