efl/src/lib/ecore/ecore_idler.c

195 lines
4.5 KiB
C
Raw Normal View History

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <Eo.h>
#include "Ecore.h"
#include "ecore_private.h"
#define MY_CLASS ECORE_IDLER_CLASS
#define MY_CLASS_NAME "Ecore_Idler"
2014-03-23 07:20:37 -07:00
struct _Ecore_Idler_Data
{
EINA_INLIST;
Ecore_Idler *obj;
Ecore_Task_Cb func;
void *data;
int references;
Eina_Bool delete_me : 1;
};
2014-03-23 07:20:37 -07:00
typedef struct _Ecore_Idler_Data Ecore_Idler_Data;
static Ecore_Idler_Data *idlers = NULL;
static Ecore_Idler_Data *idler_current = NULL;
static int idlers_delete_me = 0;
static void *
_ecore_idler_del(Ecore_Idler *idler);
EAPI Ecore_Idler *
ecore_idler_add(Ecore_Task_Cb func,
const void *data)
{
Ecore_Idler *ie = NULL;
_ecore_lock();
ie = eo_add(MY_CLASS, _ecore_parent, ecore_idler_constructor(func, data));
_ecore_unlock();
return ie;
}
2014-03-23 07:20:37 -07:00
EOLIAN static void
_ecore_idler_constructor(Eo *obj, Ecore_Idler_Data *ie, Ecore_Task_Cb func, const void *data)
{
if (EINA_UNLIKELY(!eina_main_loop_is()))
{
eo_error_set(obj);
EINA_MAIN_LOOP_CHECK_RETURN;
}
ie->obj = obj;
eo_manual_free_set(obj, EINA_TRUE);
if (!func)
{
eo_error_set(obj);
ERR("callback function must be set up for an object of class: '%s'", MY_CLASS_NAME);
return;
}
ie->func = func;
ie->data = (void *)data;
2014-03-23 07:20:37 -07:00
idlers = (Ecore_Idler_Data *)eina_inlist_append(EINA_INLIST_GET(idlers), EINA_INLIST_GET(ie));
}
EAPI void *
ecore_idler_del(Ecore_Idler *idler)
{
void *data = NULL;
if (!idler) return NULL;
EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
_ecore_lock();
data = _ecore_idler_del(idler);
_ecore_unlock();
return data;
}
static void *
_ecore_idler_del(Ecore_Idler *obj)
{
2014-03-23 07:20:37 -07:00
Ecore_Idler_Data *idler = eo_data_scope_get(obj, MY_CLASS);
2015-03-27 23:43:50 -07:00
if (!idler) return NULL;
EINA_SAFETY_ON_TRUE_RETURN_VAL(idler->delete_me, NULL);
idler->delete_me = 1;
idlers_delete_me = 1;
return idler->data;
}
2014-03-23 07:20:37 -07:00
EOLIAN static void
_ecore_idler_eo_base_destructor(Eo *obj, Ecore_Idler_Data *idler)
{
idler->delete_me = 1;
idlers_delete_me = 1;
eo_do_super(obj, MY_CLASS, eo_destructor());
}
void
_ecore_idler_shutdown(void)
{
2014-03-23 07:20:37 -07:00
Ecore_Idler_Data *ie;
while ((ie = idlers))
{
2014-03-23 07:20:37 -07:00
idlers = (Ecore_Idler_Data *)eina_inlist_remove(EINA_INLIST_GET(idlers), EINA_INLIST_GET(idlers));
eo_do(ie->obj, eo_parent_set(NULL));
if (eo_destructed_is(ie->obj))
eo_manual_free(ie->obj);
else
eo_manual_free_set(ie->obj, EINA_FALSE);
}
idlers_delete_me = 0;
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 <Ecore.h> #include <Eina.h> 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; } SVN revision: 46406
2010-02-23 13:27:04 -08:00
idler_current = NULL;
}
int
_ecore_idler_all_call(void)
{
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 <Ecore.h> #include <Eina.h> 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; } SVN revision: 46406
2010-02-23 13:27:04 -08:00
if (!idler_current)
{
2010-09-29 23:09:20 -07:00
/* regular main loop, start from head */
idler_current = idlers;
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 <Ecore.h> #include <Eina.h> 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; } SVN revision: 46406
2010-02-23 13:27:04 -08:00
}
else
{
2010-09-29 23:09:20 -07:00
/* recursive main loop, continue from where we were */
2014-03-23 07:20:37 -07:00
idler_current = (Ecore_Idler_Data *)EINA_INLIST_GET(idler_current)->next;
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 <Ecore.h> #include <Eina.h> 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; } SVN revision: 46406
2010-02-23 13:27:04 -08:00
}
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 <Ecore.h> #include <Eina.h> 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; } SVN revision: 46406
2010-02-23 13:27:04 -08:00
while (idler_current)
{
2014-03-23 07:20:37 -07:00
Ecore_Idler_Data *ie = (Ecore_Idler_Data *)idler_current;
2010-09-29 23:09:20 -07:00
if (!ie->delete_me)
{
ie->references++;
eina_evlog("+idler", ie, 0.0, NULL);
if (!_ecore_call_task_cb(ie->func, ie->data))
2010-09-29 23:09:20 -07:00
{
if (!ie->delete_me) _ecore_idler_del(ie->obj);
2010-09-29 23:09:20 -07:00
}
eina_evlog("-idler", ie, 0.0, NULL);
2010-09-29 23:09:20 -07:00
ie->references--;
}
if (idler_current) /* may have changed in recursive main loops */
2014-03-23 07:20:37 -07:00
idler_current = (Ecore_Idler_Data *)EINA_INLIST_GET(idler_current)->next;
}
if (idlers_delete_me)
{
2014-03-23 07:20:37 -07:00
Ecore_Idler_Data *l;
2010-09-29 23:09:20 -07:00
int deleted_idlers_in_use = 0;
for (l = idlers; l; )
2010-09-29 23:09:20 -07:00
{
2014-03-23 07:20:37 -07:00
Ecore_Idler_Data *ie = l;
l = (Ecore_Idler_Data *)EINA_INLIST_GET(l)->next;
2010-09-29 23:09:20 -07:00
if (ie->delete_me)
{
if (ie->references)
{
deleted_idlers_in_use++;
continue;
}
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 <Ecore.h> #include <Eina.h> 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; } SVN revision: 46406
2010-02-23 13:27:04 -08:00
2014-03-23 07:20:37 -07:00
idlers = (Ecore_Idler_Data *)eina_inlist_remove(EINA_INLIST_GET(idlers), EINA_INLIST_GET(ie));
eo_do(ie->obj, eo_parent_set(NULL));
if (eo_destructed_is(ie->obj))
eo_manual_free(ie->obj);
else
eo_manual_free_set(ie->obj, EINA_FALSE);
2010-09-29 23:09:20 -07:00
}
}
if (!deleted_idlers_in_use)
idlers_delete_me = 0;
}
if (idlers) return 1;
return 0;
}
int
_ecore_idler_exist(void)
{
if (idlers) return 1;
return 0;
}
#include "ecore_idler.eo.c"