2009-01-31 10:33:39 -08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
#include "Ecore.h"
|
2009-12-22 13:15:12 -08:00
|
|
|
#include "ecore_private.h"
|
2003-09-23 01:09:32 -07:00
|
|
|
|
2011-05-17 10:32:32 -07:00
|
|
|
#ifdef WANT_ECORE_TIMER_DUMP
|
|
|
|
# include <string.h>
|
|
|
|
# include <execinfo.h>
|
|
|
|
# define ECORE_TIMER_DEBUG_BT_NUM 64
|
|
|
|
typedef void (*Ecore_Timer_Bt_Func) ();
|
|
|
|
#endif
|
2010-01-03 13:55:50 -08:00
|
|
|
|
|
|
|
struct _Ecore_Timer
|
|
|
|
{
|
|
|
|
EINA_INLIST;
|
|
|
|
ECORE_MAGIC;
|
|
|
|
double in;
|
|
|
|
double at;
|
|
|
|
double pending;
|
2010-08-03 19:55:20 -07:00
|
|
|
Ecore_Task_Cb func;
|
2010-01-03 13:55:50 -08:00
|
|
|
void *data;
|
|
|
|
|
2011-05-17 10:32:32 -07:00
|
|
|
#ifdef WANT_ECORE_TIMER_DUMP
|
|
|
|
Ecore_Timer_Bt_Func timer_bt[ECORE_TIMER_DEBUG_BT_NUM];
|
|
|
|
int timer_bt_num;
|
|
|
|
#endif
|
|
|
|
|
2010-02-23 16:27:04 -08:00
|
|
|
int references;
|
2010-01-03 13:55:50 -08:00
|
|
|
unsigned char delete_me : 1;
|
|
|
|
unsigned char just_added : 1;
|
|
|
|
unsigned char frozen : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-08-03 19:55:20 -07:00
|
|
|
static void _ecore_timer_set(Ecore_Timer *timer, double at, double in, Ecore_Task_Cb func, void *data);
|
2011-05-17 10:32:32 -07:00
|
|
|
static int _ecore_timer_cmp(const void *d1, const void *d2);
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
static int timers_added = 0;
|
|
|
|
static int timers_delete_me = 0;
|
|
|
|
static Ecore_Timer *timers = NULL;
|
2010-02-23 16:27:04 -08:00
|
|
|
static Ecore_Timer *timer_current = NULL;
|
2008-03-09 08:56:17 -07:00
|
|
|
static Ecore_Timer *suspended = NULL;
|
2005-04-04 09:01:39 -07:00
|
|
|
static double last_check = 0.0;
|
2008-11-13 12:42:01 -08:00
|
|
|
static double precision = 10.0 / 1000000.0;
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
/**
|
2010-10-17 00:03:28 -07:00
|
|
|
* @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Ecore_Time_Group Ecore Time functions
|
2004-09-22 21:58:16 -07:00
|
|
|
*
|
|
|
|
* Functions that deal with time. These functions include those that simply
|
|
|
|
* retrieve it in a given format, and those that create events based on it.
|
2010-10-17 00:03:28 -07:00
|
|
|
*
|
|
|
|
* The timer allows callbacks to be called at specific intervals.
|
|
|
|
*
|
|
|
|
* @{
|
2004-09-22 21:58:16 -07:00
|
|
|
*/
|
|
|
|
|
2008-11-13 12:42:01 -08:00
|
|
|
/**
|
|
|
|
* Retrieves the current precision used by timer infrastructure.
|
|
|
|
*
|
|
|
|
* @see ecore_timer_precision_set()
|
|
|
|
*/
|
|
|
|
EAPI double
|
|
|
|
ecore_timer_precision_get(void)
|
|
|
|
{
|
|
|
|
return precision;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the precision to be used by timer infrastructure.
|
|
|
|
*
|
|
|
|
* When system calculates time to expire the next timer we'll be able
|
|
|
|
* to delay the timer by the given amount so more timers will fit in
|
|
|
|
* the same dispatch, waking up the system less often and thus being
|
|
|
|
* able to save power.
|
|
|
|
*
|
|
|
|
* Be aware that kernel may delay delivery even further, these delays
|
|
|
|
* are always possible due other tasks having higher priorities or
|
|
|
|
* other scheduler policies.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* We have 2 timers, one that expires in a 2.0s and another that
|
|
|
|
* expires in 2.1s, if precision is 0.1s, then the Ecore will request
|
|
|
|
* for the next expire to happen in 2.1s and not 2.0s and another one
|
|
|
|
* of 0.1 as it would before.
|
|
|
|
*
|
|
|
|
* @note Ecore is smart enough to see if there are timers in the
|
|
|
|
* precision range, if it does not, in our example if no second timer
|
|
|
|
* in (T + precision) existed, then it would use the minimum timeout.
|
|
|
|
*
|
|
|
|
* @param value allowed introduced timeout delay, in seconds.
|
|
|
|
*/
|
|
|
|
EAPI void
|
|
|
|
ecore_timer_precision_set(double value)
|
|
|
|
{
|
|
|
|
if (value < 0.0)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ERR("Precision %f less than zero, ignored", value);
|
|
|
|
return;
|
2008-11-13 12:42:01 -08:00
|
|
|
}
|
|
|
|
precision = value;
|
|
|
|
}
|
|
|
|
|
2004-09-22 21:58:16 -07:00
|
|
|
/**
|
|
|
|
* Creates a timer to call the given function in the given period of time.
|
|
|
|
* @param in The interval in seconds.
|
|
|
|
* @param func The given function. If @p func returns 1, the timer is
|
|
|
|
* rescheduled for the next interval @p in.
|
|
|
|
* @param data Data to pass to @p func when it is called.
|
|
|
|
* @return A timer object on success. @c NULL on failure.
|
2006-12-27 19:31:44 -08:00
|
|
|
*
|
|
|
|
* This function adds a timer and returns its handle on success and NULL on
|
2009-03-13 02:38:46 -07:00
|
|
|
* failure. The function @p func will be called every @p in seconds. The
|
2006-12-27 19:31:44 -08:00
|
|
|
* function will be passed the @p data pointer as its parameter.
|
2008-11-29 03:23:17 -08:00
|
|
|
*
|
|
|
|
* When the timer @p func is called, it must return a value of either 1
|
|
|
|
* (or ECORE_CALLBACK_RENEW) or 0 (or ECORE_CALLBACK_CANCEL).
|
2006-12-27 19:31:44 -08:00
|
|
|
* If it returns 1, it will be called again at the next tick, or if it returns
|
|
|
|
* 0 it will be deleted automatically making any references/handles for it
|
|
|
|
* invalid.
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI Ecore_Timer *
|
2010-08-03 19:55:20 -07:00
|
|
|
ecore_timer_add(double in, Ecore_Task_Cb func, const void *data)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
|
|
|
double now;
|
|
|
|
Ecore_Timer *timer;
|
|
|
|
|
|
|
|
if (!func) return NULL;
|
|
|
|
if (in < 0.0) in = 0.0;
|
|
|
|
timer = calloc(1, sizeof(Ecore_Timer));
|
|
|
|
if (!timer) return NULL;
|
|
|
|
ECORE_MAGIC_SET(timer, ECORE_MAGIC_TIMER);
|
|
|
|
now = ecore_time_get();
|
2011-05-17 10:32:32 -07:00
|
|
|
|
|
|
|
#ifdef WANT_ECORE_TIMER_DUMP
|
|
|
|
timer->timer_bt_num = backtrace((void**) (timer->timer_bt),
|
|
|
|
ECORE_TIMER_DEBUG_BT_NUM);
|
|
|
|
#endif
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
_ecore_timer_set(timer, now + in, in, func, (void *)data);
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2009-03-22 19:13:50 -07:00
|
|
|
/**
|
|
|
|
* Creates a timer to call the given function in the given period of time.
|
|
|
|
* @param in The interval in seconds from current loop time.
|
|
|
|
* @param func The given function. If @p func returns 1, the timer is
|
|
|
|
* rescheduled for the next interval @p in.
|
|
|
|
* @param data Data to pass to @p func when it is called.
|
|
|
|
* @return A timer object on success. @c NULL on failure.
|
|
|
|
*
|
|
|
|
* This is the same as ecore_timer_add(), but "now" is the time from
|
|
|
|
* ecore_loop_time_get() not ecore_time_get() as ecore_timer_add() uses. See
|
|
|
|
* ecore_timer_add() for more details.
|
|
|
|
*/
|
|
|
|
EAPI Ecore_Timer *
|
2010-08-03 19:55:20 -07:00
|
|
|
ecore_timer_loop_add(double in, Ecore_Task_Cb func, const void *data)
|
2009-03-22 19:13:50 -07:00
|
|
|
{
|
|
|
|
double now;
|
|
|
|
Ecore_Timer *timer;
|
|
|
|
|
|
|
|
if (!func) return NULL;
|
|
|
|
if (in < 0.0) in = 0.0;
|
|
|
|
timer = calloc(1, sizeof(Ecore_Timer));
|
|
|
|
if (!timer) return NULL;
|
|
|
|
ECORE_MAGIC_SET(timer, ECORE_MAGIC_TIMER);
|
|
|
|
now = ecore_loop_time_get();
|
2011-05-19 08:38:30 -07:00
|
|
|
|
|
|
|
#ifdef WANT_ECORE_TIMER_DUMP
|
|
|
|
timer->timer_bt_num = backtrace((void**) (timer->timer_bt),
|
|
|
|
ECORE_TIMER_DEBUG_BT_NUM);
|
|
|
|
#endif
|
|
|
|
|
2009-03-22 19:13:50 -07:00
|
|
|
_ecore_timer_set(timer, now + in, in, func, (void *)data);
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
/**
|
|
|
|
* Delete the specified timer from the timer list.
|
2004-09-22 21:58:16 -07:00
|
|
|
* @param timer The timer to delete.
|
|
|
|
* @return The data pointer set for the timer when @ref ecore_timer_add was
|
|
|
|
* called. @c NULL is returned if the function is unsuccessful.
|
2006-12-27 19:31:44 -08:00
|
|
|
*
|
|
|
|
* Note: @p timer must be a valid handle. If the timer function has already
|
|
|
|
* returned 0, the handle is no longer valid (and does not need to be delete).
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void *
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_timer_del(Ecore_Timer *timer)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
|
|
|
|
"ecore_timer_del");
|
|
|
|
return NULL;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2009-11-13 08:57:12 -08:00
|
|
|
|
2010-02-23 16:27:04 -08:00
|
|
|
if (timer->frozen && !timer->references)
|
2009-11-13 08:57:12 -08:00
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
void *data = timer->data;
|
2009-11-13 08:57:12 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
suspended = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
|
2009-11-13 08:57:12 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
if (timer->delete_me)
|
|
|
|
timers_delete_me--;
|
2009-11-13 08:57:12 -08:00
|
|
|
|
2010-09-29 23:09:20 -07:00
|
|
|
free(timer);
|
|
|
|
return data;
|
2009-11-13 08:57:12 -08:00
|
|
|
}
|
|
|
|
|
2010-07-09 07:52:45 -07:00
|
|
|
EINA_SAFETY_ON_TRUE_RETURN_VAL(timer->delete_me, NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
timer->delete_me = 1;
|
2010-07-09 07:52:45 -07:00
|
|
|
timers_delete_me++;
|
2003-09-23 01:09:32 -07:00
|
|
|
return timer->data;
|
|
|
|
}
|
|
|
|
|
2005-02-22 04:39:50 -08:00
|
|
|
/**
|
|
|
|
* Change the interval the timer ticks of. If set during
|
|
|
|
* a timer call, this will affect the next interval.
|
|
|
|
*
|
|
|
|
* @param timer The timer to change.
|
|
|
|
* @param in The interval in seconds.
|
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void
|
2005-02-22 04:39:50 -08:00
|
|
|
ecore_timer_interval_set(Ecore_Timer *timer, double in)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
|
|
|
|
"ecore_timer_interval_set");
|
|
|
|
return;
|
2005-02-22 04:39:50 -08:00
|
|
|
}
|
|
|
|
timer->in = in;
|
|
|
|
}
|
|
|
|
|
2009-04-26 10:16:33 -07:00
|
|
|
/**
|
|
|
|
* Get the interval the timer ticks on.
|
|
|
|
*
|
|
|
|
* @param timer The timer to retrieve the interval from
|
|
|
|
* @return The interval on success. -1 on failure.
|
|
|
|
*/
|
|
|
|
EAPI double
|
|
|
|
ecore_timer_interval_get(Ecore_Timer *timer)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
|
|
|
|
"ecore_timer_interval_get");
|
|
|
|
return -1.0;
|
2009-04-26 10:16:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return timer->in;
|
|
|
|
}
|
|
|
|
|
2008-03-09 08:56:17 -07:00
|
|
|
/**
|
2010-09-08 04:23:42 -07:00
|
|
|
* Add some delay for the next occurrence of a timer.
|
2008-04-11 16:39:55 -07:00
|
|
|
* This doesn't affect the interval of a timer.
|
|
|
|
*
|
|
|
|
* @param timer The timer to change.
|
|
|
|
* @param add The dalay to add to the next iteration.
|
|
|
|
*/
|
|
|
|
EAPI void
|
|
|
|
ecore_timer_delay(Ecore_Timer *timer, double add)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
|
|
|
|
"ecore_timer_delay");
|
|
|
|
return;
|
2008-04-11 16:39:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timer->frozen)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
timer->pending += add;
|
2008-04-11 16:39:55 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
timers = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
|
|
|
|
_ecore_timer_set(timer, timer->at + add, timer->in, timer->func, timer->data);
|
2008-04-11 16:39:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the pending time regarding a timer.
|
|
|
|
*
|
2010-09-29 23:09:20 -07:00
|
|
|
* @param timer The timer to learn from.
|
|
|
|
* @ingroup Ecore_Time_Group
|
2008-04-11 16:39:55 -07:00
|
|
|
*/
|
|
|
|
EAPI double
|
|
|
|
ecore_timer_pending_get(Ecore_Timer *timer)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
double now;
|
2008-04-11 16:39:55 -07:00
|
|
|
|
|
|
|
if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
|
|
|
|
"ecore_timer_pending_get");
|
|
|
|
return 0;
|
2008-04-11 16:39:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
now = ecore_time_get();
|
|
|
|
|
|
|
|
if (timer->frozen)
|
|
|
|
return timer->pending;
|
|
|
|
return timer->at - now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2008-03-09 08:56:17 -07:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
EAPI void
|
|
|
|
ecore_timer_freeze(Ecore_Timer *timer)
|
|
|
|
{
|
|
|
|
double now;
|
|
|
|
|
|
|
|
if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
|
2008-03-09 08:56:17 -07:00
|
|
|
"ecore_timer_freeze");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Timer already frozen */
|
|
|
|
if (timer->frozen)
|
|
|
|
return ;
|
|
|
|
|
2010-02-23 16:27:04 -08:00
|
|
|
timers = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
|
2009-06-17 04:39:37 -07:00
|
|
|
suspended = (Ecore_Timer *) eina_inlist_prepend(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
|
2008-03-09 08:56:17 -07:00
|
|
|
|
|
|
|
now = ecore_time_get();
|
|
|
|
|
|
|
|
timer->pending = timer->at - now;
|
|
|
|
timer->at = 0.0;
|
|
|
|
timer->frozen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EAPI void
|
|
|
|
ecore_timer_thaw(Ecore_Timer *timer)
|
|
|
|
{
|
|
|
|
double now;
|
|
|
|
|
|
|
|
if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
|
2008-03-09 08:56:17 -07:00
|
|
|
"ecore_timer_thaw");
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Timer not frozen */
|
|
|
|
if (!timer->frozen)
|
|
|
|
return ;
|
|
|
|
|
2009-06-17 04:39:37 -07:00
|
|
|
suspended = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
|
2008-03-09 08:56:17 -07:00
|
|
|
now = ecore_time_get();
|
|
|
|
|
|
|
|
_ecore_timer_set(timer, timer->pending + now, timer->in, timer->func, timer->data);
|
|
|
|
}
|
|
|
|
|
2011-05-17 10:32:32 -07:00
|
|
|
EAPI char *
|
|
|
|
ecore_timer_dump(void)
|
|
|
|
{
|
|
|
|
#ifdef WANT_ECORE_TIMER_DUMP
|
|
|
|
Eina_Strbuf *result;
|
|
|
|
char *out;
|
|
|
|
Ecore_Timer *tm;
|
|
|
|
Eina_List *tmp = NULL;
|
2011-05-19 08:38:30 -07:00
|
|
|
int living_timer = 0;
|
|
|
|
int unknow_timer = 0;
|
2011-05-17 10:32:32 -07:00
|
|
|
|
|
|
|
result = eina_strbuf_new();
|
|
|
|
|
|
|
|
EINA_INLIST_FOREACH(timers, tm)
|
|
|
|
tmp = eina_list_sorted_insert(tmp, _ecore_timer_cmp, tm);
|
|
|
|
|
|
|
|
EINA_LIST_FREE(tmp, tm)
|
|
|
|
{
|
|
|
|
char **strings;
|
|
|
|
int j;
|
|
|
|
|
2011-05-19 08:38:30 -07:00
|
|
|
if (!tm->frozen && !tm->delete_me)
|
|
|
|
living_timer++;
|
|
|
|
|
2011-05-18 02:12:07 -07:00
|
|
|
strings = backtrace_symbols((void**) tm->timer_bt, tm->timer_bt_num);
|
2011-05-19 08:38:30 -07:00
|
|
|
if (tm->timer_bt_num <= 0 || strings == NULL)
|
|
|
|
{
|
|
|
|
unknow_timer++;
|
|
|
|
continue ;
|
|
|
|
}
|
2011-05-17 10:32:32 -07:00
|
|
|
|
|
|
|
eina_strbuf_append_printf(result, "*** timer: %f ***\n", tm->in);
|
|
|
|
if (tm->frozen)
|
|
|
|
eina_strbuf_append(result, "FROZEN\n");
|
|
|
|
if (tm->delete_me)
|
|
|
|
eina_strbuf_append(result, "DELETED\n");
|
2011-05-18 02:12:07 -07:00
|
|
|
for (j = 0; j < tm->timer_bt_num; j++)
|
2011-05-17 10:32:32 -07:00
|
|
|
eina_strbuf_append_printf(result, "%s\n", strings[j]);
|
|
|
|
|
|
|
|
free(strings);
|
|
|
|
}
|
|
|
|
|
2011-05-19 08:38:30 -07:00
|
|
|
eina_strbuf_append_printf(result, "\n***\nThere is %i living timer.\nWe did lost track of %i timers.\n", living_timer, unknow_timer);
|
|
|
|
|
2011-05-17 10:32:32 -07:00
|
|
|
out = eina_strbuf_string_steal(result);
|
|
|
|
eina_strbuf_free(result);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-10-17 00:03:28 -07:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
void
|
|
|
|
_ecore_timer_shutdown(void)
|
|
|
|
{
|
2009-06-17 04:39:37 -07:00
|
|
|
Ecore_Timer *timer;
|
2008-11-29 03:23:17 -08:00
|
|
|
|
2009-06-17 04:39:37 -07:00
|
|
|
while ((timer = timers))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
timers = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timers));
|
|
|
|
ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
|
|
|
|
free(timer);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2008-03-09 08:56:17 -07:00
|
|
|
|
2009-06-17 04:39:37 -07:00
|
|
|
while ((timer = suspended))
|
2008-03-09 08:56:17 -07:00
|
|
|
{
|
2009-06-17 04:39:37 -07:00
|
|
|
suspended = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(suspended));
|
2008-03-09 08:56:17 -07:00
|
|
|
ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
|
|
|
|
free(timer);
|
|
|
|
}
|
2010-02-23 16:27:04 -08:00
|
|
|
|
|
|
|
timer_current = NULL;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_ecore_timer_cleanup(void)
|
|
|
|
{
|
2009-06-17 04:39:37 -07:00
|
|
|
Ecore_Timer *l;
|
2010-07-09 07:52:45 -07:00
|
|
|
int in_use = 0, todo = timers_delete_me, done = 0;
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
if (!timers_delete_me) return;
|
2009-06-17 04:39:37 -07:00
|
|
|
for (l = timers; l;)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
Ecore_Timer *timer = l;
|
|
|
|
|
|
|
|
l = (Ecore_Timer *) EINA_INLIST_GET(l)->next;
|
|
|
|
if (timer->delete_me)
|
|
|
|
{
|
|
|
|
if (timer->references)
|
|
|
|
{
|
|
|
|
in_use++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
timers = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
|
|
|
|
ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
|
|
|
|
free(timer);
|
|
|
|
timers_delete_me--;
|
|
|
|
done++;
|
|
|
|
if (timers_delete_me == 0) return;
|
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2009-06-17 04:39:37 -07:00
|
|
|
for (l = suspended; l;)
|
2008-03-09 08:56:17 -07:00
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
Ecore_Timer *timer = l;
|
|
|
|
|
|
|
|
l = (Ecore_Timer *) EINA_INLIST_GET(l)->next;
|
|
|
|
if (timer->delete_me)
|
|
|
|
{
|
|
|
|
if (timer->references)
|
|
|
|
{
|
|
|
|
in_use++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
suspended = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
|
|
|
|
ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
|
|
|
|
free(timer);
|
|
|
|
timers_delete_me--;
|
|
|
|
done++;
|
|
|
|
if (timers_delete_me == 0) return;
|
|
|
|
}
|
2008-03-09 08:56:17 -07:00
|
|
|
}
|
2010-02-23 16:27:04 -08:00
|
|
|
|
|
|
|
if ((!in_use) && (timers_delete_me))
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
ERR("%d timers to delete, but they were not found!"
|
|
|
|
"Stats: todo=%d, done=%d, pending=%d, in_use=%d. "
|
|
|
|
"reset counter.",
|
|
|
|
timers_delete_me, todo, done, todo - done, in_use);
|
|
|
|
timers_delete_me = 0;
|
2010-02-23 16:27:04 -08:00
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_ecore_timer_enable_new(void)
|
|
|
|
{
|
2009-06-17 04:39:37 -07:00
|
|
|
Ecore_Timer *timer;
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
if (!timers_added) return;
|
|
|
|
timers_added = 0;
|
2010-02-16 08:52:02 -08:00
|
|
|
EINA_INLIST_FOREACH(timers, timer) timer->just_added = 0;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
|
Fix the bug of the first timer being added from idler.
We should start doing unit-test for ecore, accumulating these
problems. Follows the test case:
#include <Ecore.h>
#include <Eina.h>
static int _log_dom;
#define INF(...) EINA_LOG_DOM_INFO(_log_dom, __VA_ARGS__)
static int quiter(void *data)
{
INF("quit!");
ecore_main_loop_quit();
return 1;
}
static int idler(void *data)
{
INF("idler");
return 1;
}
static int cb1(void *data)
{
INF("cb1");
ecore_timer_add(0.0, quiter, NULL);
return 0;
}
int main(void)
{
ecore_init();
_log_dom = eina_log_domain_register("test", EINA_COLOR_CYAN);
/*
* Create a main loop with just idlers, there is a special case
* for just idlers without timers in ecore.
*
* From idler, add a timer that quits the application. It should
* always quit.
*
* If it does not quit, then there is a bug of new timers not
* being immediately detected and system never exits idle.
*/
INF("main: begin");
ecore_idler_add(cb1, NULL);
ecore_idler_add(idler, NULL);
ecore_main_loop_begin();
INF("main: end");
return 0;
}
SVN revision: 46405
2010-02-23 13:04:38 -08:00
|
|
|
int
|
|
|
|
_ecore_timers_exists(void)
|
|
|
|
{
|
|
|
|
Ecore_Timer *timer = timers;
|
|
|
|
|
|
|
|
while ((timer) && (timer->delete_me))
|
|
|
|
timer = (Ecore_Timer *)EINA_INLIST_GET(timer)->next;
|
|
|
|
|
|
|
|
return !!timer;
|
|
|
|
}
|
|
|
|
|
2008-11-13 12:42:01 -08:00
|
|
|
static inline Ecore_Timer *
|
|
|
|
_ecore_timer_first_get(void)
|
|
|
|
{
|
2009-06-17 04:39:37 -07:00
|
|
|
Ecore_Timer *timer = timers;
|
2008-11-13 12:42:01 -08:00
|
|
|
|
|
|
|
while ((timer) && ((timer->delete_me) || (timer->just_added)))
|
2009-06-17 04:39:37 -07:00
|
|
|
timer = (Ecore_Timer *) EINA_INLIST_GET(timer)->next;
|
2008-11-13 12:42:01 -08:00
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline Ecore_Timer *
|
|
|
|
_ecore_timer_after_get(Ecore_Timer *base)
|
|
|
|
{
|
2009-06-17 04:39:37 -07:00
|
|
|
Ecore_Timer *timer = (Ecore_Timer *) EINA_INLIST_GET(base)->next;
|
2008-11-13 12:42:01 -08:00
|
|
|
double maxtime = base->at + precision;
|
|
|
|
|
|
|
|
while ((timer) && ((timer->delete_me) || (timer->just_added)) && (timer->at <= maxtime))
|
2009-06-17 04:39:37 -07:00
|
|
|
timer = (Ecore_Timer *) EINA_INLIST_GET(timer)->next;
|
2008-11-13 12:42:01 -08:00
|
|
|
|
|
|
|
if ((!timer) || (timer->at > maxtime))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
double
|
|
|
|
_ecore_timer_next_get(void)
|
|
|
|
{
|
|
|
|
double now;
|
|
|
|
double in;
|
2008-11-13 12:42:01 -08:00
|
|
|
Ecore_Timer *first, *second;
|
|
|
|
|
|
|
|
first = _ecore_timer_first_get();
|
|
|
|
if (!first) return -1;
|
|
|
|
|
|
|
|
second = _ecore_timer_after_get(first);
|
|
|
|
if (second)
|
|
|
|
first = second;
|
|
|
|
|
2008-11-01 19:19:18 -07:00
|
|
|
now = ecore_loop_time_get();
|
2008-11-13 12:42:01 -08:00
|
|
|
in = first->at - now;
|
2003-09-23 01:09:32 -07:00
|
|
|
if (in < 0) in = 0;
|
|
|
|
return in;
|
2008-11-13 12:42:01 -08:00
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
|
Reschedule timer_current in case of inner mainloop
Timers' list is and *ordered list*. Therefore, timers can be added
before timer_current in an inner mainloop. Reschedule timer_current in
this case before looping through timers' list.
Thanks to Barbieri for the insight.
The following test didn't work before and it's ok now (I'm adding it to
ecore_suite too).
static int _timer3(void *data)
{
printf("timer 3, do nothing\n");
return 0;
}
static int _timer2(void *data)
{
printf("timer 2, quit inner\n");
ecore_main_loop_quit();
return 0;
}
static int _timer1(void *data)
{
int *times = data;
(*times)++;
printf("BEGIN: inner\n");
ecore_timer_add(0.3, _timer2, NULL);
ecore_timer_add(0.1, _timer3, NULL);
ecore_main_loop_begin();
printf("END: inner\n");
ecore_main_loop_quit();
return 0;
}
int main(void)
{
int times = 0;
ecore_init();
ecore_timer_add(1.0, _timer1, ×);
printf("BEGIN: main\n");
ecore_main_loop_begin();
assert(times == 1);
printf("timer1 called %d times \n", times);
printf("END: main\n");
return 0;
}
By: Lucas De Marchi <lucas.demarchi@profusion.mobi>
SVN revision: 49245
2010-05-27 19:31:02 -07:00
|
|
|
static inline void
|
|
|
|
_ecore_timer_reschedule(Ecore_Timer *timer, double when)
|
|
|
|
{
|
|
|
|
if ((timer->delete_me) || (timer->frozen)) return;
|
|
|
|
|
|
|
|
timers = (Ecore_Timer *) eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
|
|
|
|
|
|
|
|
/* if the timer would have gone off more than 15 seconds ago,
|
|
|
|
* assume that the system hung and set the timer to go off
|
|
|
|
* timer->in from now. this handles system hangs, suspends
|
|
|
|
* and more, so ecore will only "replay" the timers while
|
|
|
|
* the system is suspended if it is suspended for less than
|
|
|
|
* 15 seconds (basically). this also handles if the process
|
|
|
|
* is stopped in a debugger or IO and other handling gets
|
|
|
|
* really slow within the main loop.
|
|
|
|
*/
|
|
|
|
if ((timer->at + timer->in) < (when - 15.0))
|
|
|
|
_ecore_timer_set(timer, when + timer->in, timer->in, timer->func, timer->data);
|
|
|
|
else
|
|
|
|
_ecore_timer_set(timer, timer->at + timer->in, timer->in, timer->func, timer->data);
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
int
|
|
|
|
_ecore_timer_call(double when)
|
|
|
|
{
|
|
|
|
if (!timers) return 0;
|
2005-04-04 09:01:39 -07:00
|
|
|
if (last_check > when)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
Ecore_Timer *timer;
|
|
|
|
/* User set time backwards */
|
|
|
|
EINA_INLIST_FOREACH(timers, timer) timer->at -= (last_check - when);
|
2005-04-04 09:01:39 -07:00
|
|
|
}
|
|
|
|
last_check = when;
|
2010-02-23 16:27:04 -08:00
|
|
|
|
|
|
|
if (!timer_current)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
/* regular main loop, start from head */
|
|
|
|
timer_current = timers;
|
2010-02-23 16:27:04 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
/* recursive main loop, continue from where we were */
|
|
|
|
Ecore_Timer *timer_old = timer_current;
|
|
|
|
timer_current = (Ecore_Timer *)EINA_INLIST_GET(timer_current)->next;
|
|
|
|
_ecore_timer_reschedule(timer_old, when);
|
2010-02-23 16:27:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (timer_current)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
Ecore_Timer *timer = timer_current;
|
|
|
|
|
|
|
|
if (timer->at > when)
|
|
|
|
{
|
|
|
|
timer_current = NULL; /* ended walk, next should restart. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((timer->just_added) || (timer->delete_me))
|
|
|
|
{
|
|
|
|
timer_current = (Ecore_Timer*)EINA_INLIST_GET(timer_current)->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
timer->references++;
|
|
|
|
if (!timer->func(timer->data))
|
|
|
|
{
|
|
|
|
if (!timer->delete_me) ecore_timer_del(timer);
|
|
|
|
}
|
|
|
|
timer->references--;
|
|
|
|
|
|
|
|
if (timer_current) /* may have changed in recursive main loops */
|
|
|
|
timer_current = (Ecore_Timer *)EINA_INLIST_GET(timer_current)->next;
|
|
|
|
|
|
|
|
_ecore_timer_reschedule(timer, when);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
return 0;
|
2008-11-29 03:23:17 -08:00
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
static void
|
2010-08-03 19:55:20 -07:00
|
|
|
_ecore_timer_set(Ecore_Timer *timer, double at, double in, Ecore_Task_Cb func, void *data)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
2009-06-17 04:39:37 -07:00
|
|
|
Ecore_Timer *t2;
|
2008-11-29 03:23:17 -08:00
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
timers_added = 1;
|
|
|
|
timer->at = at;
|
|
|
|
timer->in = in;
|
|
|
|
timer->func = func;
|
|
|
|
timer->data = data;
|
|
|
|
timer->just_added = 1;
|
2008-03-09 08:56:17 -07:00
|
|
|
timer->frozen = 0;
|
|
|
|
timer->pending = 0.0;
|
2003-09-23 01:09:32 -07:00
|
|
|
if (timers)
|
|
|
|
{
|
2010-09-29 23:09:20 -07:00
|
|
|
EINA_INLIST_REVERSE_FOREACH(EINA_INLIST_GET(timers), t2)
|
|
|
|
{
|
|
|
|
if (timer->at > t2->at)
|
|
|
|
{
|
|
|
|
timers = (Ecore_Timer *) eina_inlist_append_relative(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer), EINA_INLIST_GET(t2));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2009-06-17 04:39:37 -07:00
|
|
|
timers = (Ecore_Timer *) eina_inlist_prepend(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2011-05-17 10:32:32 -07:00
|
|
|
|
|
|
|
static int
|
|
|
|
_ecore_timer_cmp(const void *d1, const void *d2)
|
|
|
|
{
|
|
|
|
const Ecore_Timer *t1 = d1;
|
|
|
|
const Ecore_Timer *t2 = d2;
|
|
|
|
|
2011-05-19 08:38:30 -07:00
|
|
|
return (int) ((t1->in - t2->in) * 100);
|
2011-05-17 10:32:32 -07:00
|
|
|
}
|