Fix idle_exiters when using recursive main loops.

If an idle_exiter created a recursive main loop (just called
ecore_main_loop_begin()), then this recursive main loop should
continue to process idle_exiters from there and on, thus
idle_exiter_current was added. When going back from recursion, the
current iterator should be updated properly.

This patch also fixes the deletion of idle_exiters 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 <Ecore.h>
#include <Eina.h>

static int _log_dom;
#define INF(...) EINA_LOG_DOM_INFO(_log_dom, __VA_ARGS__)

static Ecore_Idle_Exiter *handle;

static int idler(void *data)
{
    INF("idler");
    return 1;
}

static int timer(void *data)
{
    INF("timer (exited idle!)");
    return 0;
}

static int exit_idle(void *data)
{
    INF("add request (timer) to exit idle");
    ecore_timer_add(0.0, timer, NULL);
    return 0;
}

static int cb2(void *data)
{
    INF("cb2 - delete cb1 handle");
    ecore_idle_exiter_del(handle);
    ecore_main_loop_quit(); /* quits inner main loop */
    return 0;
}

static int cb1(void *data)
{
    INF("cb1: begin");
    INF("    add cb2");
    ecore_idle_exiter_add(cb2, NULL);
    INF("    add exit idler");
    ecore_idler_add(exit_idle, NULL);
    INF("    inner main loop begin (recurse)");
    ecore_main_loop_begin(); /* will it crash due ecore_idle_exiter_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 idle_exiter 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_idle_exiter_add(cb1, NULL);
    ecore_idler_add(idler, NULL);
    ecore_idler_add(exit_idle, NULL);
    ecore_main_loop_begin();
    INF("main: end");
    return 0;
}



SVN revision: 46410
This commit is contained in:
Gustavo Sverzut Barbieri 2010-02-23 22:25:35 +00:00
parent 1c3348513a
commit 0c24c76c5a
1 changed files with 34 additions and 6 deletions

View File

@ -16,13 +16,15 @@ struct _Ecore_Idle_Exiter
{
EINA_INLIST;
ECORE_MAGIC;
int delete_me : 1;
int (*func) (void *data);
void *data;
int references;
Eina_Bool delete_me : 1;
};
static Ecore_Idle_Exiter *idle_exiters = NULL;
static Ecore_Idle_Exiter *idle_exiter_current = NULL;
static int idle_exiters_delete_me = 0;
/**
@ -79,36 +81,62 @@ _ecore_idle_exiter_shutdown(void)
free(ie);
}
idle_exiters_delete_me = 0;
idle_exiter_current = NULL;
}
void
_ecore_idle_exiter_call(void)
{
Ecore_Idle_Exiter *ie;
EINA_INLIST_FOREACH(idle_exiters, ie)
if (!idle_exiter_current)
{
/* regular main loop, start from head */
idle_exiter_current = idle_exiters;
}
else
{
/* recursive main loop, continue from where we were */
idle_exiter_current =
(Ecore_Idle_Exiter *)EINA_INLIST_GET(idle_exiter_current)->next;
}
while (idle_exiter_current)
{
Ecore_Idle_Exiter *ie = (Ecore_Idle_Exiter *)idle_exiter_current;
if (!ie->delete_me)
{
ie->references++;
if (!ie->func(ie->data)) ecore_idle_exiter_del(ie);
ie->references--;
}
if (idle_exiter_current) /* may have changed in recursive main loops */
idle_exiter_current =
(Ecore_Idle_Exiter *)EINA_INLIST_GET(idle_exiter_current)->next;
}
if (idle_exiters_delete_me)
{
Ecore_Idle_Exiter *l;
int deleted_idler_exiters_in_use = 0;
for (l = idle_exiters; l;)
{
ie = l;
Ecore_Idle_Exiter *ie = l;
l = (Ecore_Idle_Exiter *) EINA_INLIST_GET(l)->next;
if (ie->delete_me)
{
if (ie->references)
{
deleted_idler_exiters_in_use++;
continue;
}
idle_exiters = (Ecore_Idle_Exiter *) eina_inlist_remove(EINA_INLIST_GET(idle_exiters), EINA_INLIST_GET(ie));
ECORE_MAGIC_SET(ie, ECORE_MAGIC_NONE);
free(ie);
}
}
idle_exiters_delete_me = 0;
if (!deleted_idler_exiters_in_use)
idle_exiters_delete_me = 0;
}
}