9 #include "ecore_private.h"
11 #ifdef WANT_ECORE_TIMER_DUMP
13 # include <execinfo.h>
14 # define ECORE_TIMER_DEBUG_BT_NUM 64
15 typedef void (*Ecore_Timer_Bt_Func)();
28 #ifdef WANT_ECORE_TIMER_DUMP
29 Ecore_Timer_Bt_Func timer_bt[ECORE_TIMER_DEBUG_BT_NUM];
34 unsigned char delete_me : 1;
35 unsigned char just_added : 1;
36 unsigned char frozen : 1;
38 GENERIC_ALLOC_SIZE_DECLARE(Ecore_Timer);
40 static void _ecore_timer_set(Ecore_Timer *timer,
45 #ifdef WANT_ECORE_TIMER_DUMP
46 static int _ecore_timer_cmp(const void *d1,
50 static int timers_added = 0;
51 static int timers_delete_me = 0;
52 static Ecore_Timer *timers = NULL;
53 static Ecore_Timer *timer_current = NULL;
54 static Ecore_Timer *suspended = NULL;
55 static double last_check = 0.0;
56 static double precision = 10.0 / 1000000.0;
59 * @addtogroup Ecore_Timer_Group
65 * Retrieves the current precision used by timer infrastructure.
66 * @return Current precision.
67 * @see ecore_timer_precision_set()
70 ecore_timer_precision_get(void)
72 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0.0);
77 * @brief Sets the precision to be used by timer infrastructure.
79 * @param value allowed introduced timeout delay, in seconds.
81 * This sets the precision for @b all timers. The precision determines how much
82 * of an difference from the requested interval is acceptable. One common reason
83 * to use this function is to @b increase the allowed timeout and thus @b
84 * decrease precision of the timers, this is because less precise the timers
85 * result in the system waking up less often and thus consuming less resources.
87 * Be aware that kernel may delay delivery even further, these delays
88 * are always possible due other tasks having higher priorities or
89 * other scheduler policies.
92 * We have 2 timers, one that expires in a 2.0s and another that
93 * expires in 2.1s, if precision is 0.1s, then the Ecore will request
94 * for the next expire to happen in 2.1s and not 2.0s and another one
95 * of 0.1 as it would before.
97 * @note Ecore is smart enough to see if there are timers in the
98 * precision range, if it does not, in our example if no second timer
99 * in (T + precision) existed, then it would use the minimum timeout.
102 ecore_timer_precision_set(double value)
104 EINA_MAIN_LOOP_CHECK_RETURN;
109 ERR("Precision %f less than zero, ignored", value);
119 * Creates a timer to call the given function in the given period of time.
120 * @param in The interval in seconds.
121 * @param func The given function. If @p func returns 1, the timer is
122 * rescheduled for the next interval @p in.
123 * @param data Data to pass to @p func when it is called.
124 * @return A timer object on success. @c NULL on failure.
126 * This function adds a timer and returns its handle on success and NULL on
127 * failure. The function @p func will be called every @p in seconds. The
128 * function will be passed the @p data pointer as its parameter.
130 * When the timer @p func is called, it must return a value of either 1
131 * (or ECORE_CALLBACK_RENEW) or 0 (or ECORE_CALLBACK_CANCEL).
132 * If it returns 1, it will be called again at the next tick, or if it returns
133 * 0 it will be deleted automatically making any references/handles for it
137 ecore_timer_add(double in,
142 Ecore_Timer *timer = NULL;
144 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
146 if (!func) goto unlock;
147 if (in < 0.0) in = 0.0;
148 timer = ecore_timer_calloc(1);
149 if (!timer) goto unlock;
150 ECORE_MAGIC_SET(timer, ECORE_MAGIC_TIMER);
151 now = ecore_time_get();
153 #ifdef WANT_ECORE_TIMER_DUMP
154 timer->timer_bt_num = backtrace((void **)(timer->timer_bt),
155 ECORE_TIMER_DEBUG_BT_NUM);
158 _ecore_timer_set(timer, now + in, in, func, (void *)data);
165 ecore_timer_loop_add(double in,
171 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
173 timer = _ecore_timer_loop_add(in, func, data);
180 * Delete the specified timer from the timer list.
181 * @param timer The timer to delete.
182 * @return The data pointer set for the timer when @ref ecore_timer_add was
183 * called. @c NULL is returned if the function is unsuccessful.
185 * Note: @p timer must be a valid handle. If the timer function has already
186 * returned 0, the handle is no longer valid (and does not need to be delete).
189 ecore_timer_del(Ecore_Timer *timer)
192 if (!timer) return NULL;
194 EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
197 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
199 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
204 data = _ecore_timer_del(timer);
212 * Change the interval the timer ticks of. If set during
213 * a timer call, this will affect the next interval.
215 * @param timer The timer to change.
216 * @param in The interval in seconds.
219 ecore_timer_interval_set(Ecore_Timer *timer,
222 EINA_MAIN_LOOP_CHECK_RETURN;
224 if (in < 0.0) in = 0.0;
225 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
227 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
228 "ecore_timer_interval_set");
237 * Get the interval the timer ticks on.
239 * @param timer The timer to retrieve the interval from
240 * @return The interval on success. -1 on failure.
243 ecore_timer_interval_get(Ecore_Timer *timer)
247 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0.0);
250 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
252 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
253 "ecore_timer_interval_get");
258 interval = timer->in;
265 * Add some delay for the next occurrence of a timer.
266 * This doesn't affect the interval of a timer.
268 * @param timer The timer to change.
269 * @param add The delay to add to the next iteration.
272 ecore_timer_delay(Ecore_Timer *timer,
275 EINA_MAIN_LOOP_CHECK_RETURN;
276 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
278 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
279 "ecore_timer_delay");
284 _ecore_timer_delay(timer, add);
289 * Reset a timer to its full interval
290 * This doesn't affect the interval of a timer
291 * @param timer The timer
293 * @note This is equivalent to (but faster than)
295 * ecore_timer_delay(timer, ecore_timer_interval_get(timer) - ecore_timer_pending_get(timer));
299 ecore_timer_reset(Ecore_Timer *timer)
302 EINA_MAIN_LOOP_CHECK_RETURN;
303 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
305 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
310 now = ecore_time_get();
313 add = timer->pending;
315 add = timer->at - now;
316 _ecore_timer_delay(timer, timer->in - add);
321 * Get the pending time regarding a timer.
323 * @param timer The timer to learn from.
324 * @return The pending time.
325 * @ingroup Ecore_Timer_Group
328 ecore_timer_pending_get(Ecore_Timer *timer)
333 EINA_MAIN_LOOP_CHECK_RETURN_VAL(0.0);
336 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
338 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
339 "ecore_timer_pending_get");
343 now = ecore_time_get();
346 ret = timer->pending;
348 ret = timer->at - now;
355 * Pauses a running timer.
357 * @param timer The timer to be paused.
359 * The timer callback won't be called while the timer is paused. The remaining
360 * time until the timer expires will be saved, so the timer can be resumed with
361 * that same remaining time to expire, instead of expiring instantly. Use
362 * ecore_timer_thaw() to resume it.
364 * @note Nothing happens if the timer was already paused.
366 * @see ecore_timer_thaw()
369 ecore_timer_freeze(Ecore_Timer *timer)
373 EINA_MAIN_LOOP_CHECK_RETURN;
376 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
378 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
379 "ecore_timer_freeze");
383 /* Timer already frozen */
387 timers = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
388 suspended = (Ecore_Timer *)eina_inlist_prepend(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
390 now = ecore_time_get();
392 timer->pending = timer->at - now;
400 * Resumes a frozen (paused) timer.
402 * @param timer The timer to be resumed.
404 * The timer will be resumed from its previous relative position in time. That
405 * means, if it had X seconds remaining until expire when it was paused, it will
406 * be started now with those same X seconds remaining to expire again. But
407 * notice that the interval time won't be touched by this call or by
408 * ecore_timer_freeze().
410 * @see ecore_timer_freeze()
413 ecore_timer_thaw(Ecore_Timer *timer)
417 EINA_MAIN_LOOP_CHECK_RETURN;
420 if (!ECORE_MAGIC_CHECK(timer, ECORE_MAGIC_TIMER))
422 ECORE_MAGIC_FAIL(timer, ECORE_MAGIC_TIMER,
427 /* Timer not frozen */
431 suspended = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
432 now = ecore_time_get();
434 _ecore_timer_set(timer, timer->pending + now, timer->in, timer->func, timer->data);
440 ecore_timer_dump(void)
442 #ifdef WANT_ECORE_TIMER_DUMP
446 Eina_List *tmp = NULL;
447 int living_timer = 0;
448 int unknow_timer = 0;
450 EINA_MAIN_LOOP_CHECK_RETURN(NULL);
452 result = eina_strbuf_new();
454 EINA_INLIST_FOREACH(timers, tm)
455 tmp = eina_list_sorted_insert(tmp, _ecore_timer_cmp, tm);
457 EINA_LIST_FREE(tmp, tm)
462 if (!tm->frozen && !tm->delete_me)
465 strings = backtrace_symbols((void **)tm->timer_bt, tm->timer_bt_num);
466 if (tm->timer_bt_num <= 0 || strings == NULL)
472 eina_strbuf_append_printf(result, "*** timer: %f ***\n", tm->in);
474 eina_strbuf_append(result, "FROZEN\n");
476 eina_strbuf_append(result, "DELETED\n");
477 for (j = 0; j < tm->timer_bt_num; j++)
478 eina_strbuf_append_printf(result, "%s\n", strings[j]);
483 eina_strbuf_append_printf(result, "\n***\nThere is %i living timer.\nWe did lost track of %i timers.\n", living_timer, unknow_timer);
485 out = eina_strbuf_string_steal(result);
486 eina_strbuf_free(result);
500 _ecore_timer_loop_add(double in,
505 Ecore_Timer *timer = NULL;
507 if (!func) return timer;
508 if (in < 0.0) in = 0.0;
509 timer = ecore_timer_calloc(1);
510 if (!timer) return timer;
511 ECORE_MAGIC_SET(timer, ECORE_MAGIC_TIMER);
512 now = ecore_loop_time_get();
514 #ifdef WANT_ECORE_TIMER_DUMP
515 timer->timer_bt_num = backtrace((void **)(timer->timer_bt),
516 ECORE_TIMER_DEBUG_BT_NUM);
518 _ecore_timer_set(timer, now + in, in, func, (void *)data);
523 _ecore_timer_delay(Ecore_Timer *timer,
528 timer->pending += add;
532 timers = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
533 _ecore_timer_set(timer, timer->at + add, timer->in, timer->func, timer->data);
538 _ecore_timer_del(Ecore_Timer *timer)
540 if (timer->frozen && !timer->references)
542 void *data = timer->data;
544 suspended = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
546 if (timer->delete_me)
549 ecore_timer_mp_free(timer);
553 EINA_SAFETY_ON_TRUE_RETURN_VAL(timer->delete_me, NULL);
554 timer->delete_me = 1;
560 _ecore_timer_shutdown(void)
564 while ((timer = timers))
566 timers = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timers));
567 ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
568 ecore_timer_mp_free(timer);
571 while ((timer = suspended))
573 suspended = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(suspended));
574 ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
575 ecore_timer_mp_free(timer);
578 timer_current = NULL;
582 _ecore_timer_cleanup(void)
585 int in_use = 0, todo = timers_delete_me, done = 0;
587 if (!timers_delete_me) return;
588 for (l = timers; l; )
590 Ecore_Timer *timer = l;
592 l = (Ecore_Timer *)EINA_INLIST_GET(l)->next;
593 if (timer->delete_me)
595 if (timer->references)
600 timers = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
601 ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
602 ecore_timer_mp_free(timer);
605 if (timers_delete_me == 0) return;
608 for (l = suspended; l; )
610 Ecore_Timer *timer = l;
612 l = (Ecore_Timer *)EINA_INLIST_GET(l)->next;
613 if (timer->delete_me)
615 if (timer->references)
620 suspended = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
621 ECORE_MAGIC_SET(timer, ECORE_MAGIC_NONE);
622 ecore_timer_mp_free(timer);
625 if (timers_delete_me == 0) return;
629 if ((!in_use) && (timers_delete_me))
631 ERR("%d timers to delete, but they were not found!"
632 "Stats: todo=%d, done=%d, pending=%d, in_use=%d. "
634 timers_delete_me, todo, done, todo - done, in_use);
635 timers_delete_me = 0;
640 _ecore_timer_enable_new(void)
644 if (!timers_added) return;
646 EINA_INLIST_FOREACH(timers, timer) timer->just_added = 0;
650 _ecore_timers_exists(void)
652 Ecore_Timer *timer = timers;
654 while ((timer) && (timer->delete_me))
655 timer = (Ecore_Timer *)EINA_INLIST_GET(timer)->next;
660 static inline Ecore_Timer *
661 _ecore_timer_first_get(void)
663 Ecore_Timer *timer = timers;
665 while ((timer) && ((timer->delete_me) || (timer->just_added)))
666 timer = (Ecore_Timer *)EINA_INLIST_GET(timer)->next;
671 static inline Ecore_Timer *
672 _ecore_timer_after_get(Ecore_Timer *base)
674 Ecore_Timer *timer = (Ecore_Timer *)EINA_INLIST_GET(base)->next;
675 Ecore_Timer *valid_timer = NULL;
676 double maxtime = base->at + precision;
678 while ((timer) && (timer->at < maxtime))
680 if (!((timer->delete_me) || (timer->just_added)))
682 timer = (Ecore_Timer *)EINA_INLIST_GET(timer)->next;
689 _ecore_timer_next_get(void)
693 Ecore_Timer *first, *second;
695 first = _ecore_timer_first_get();
696 if (!first) return -1;
698 second = _ecore_timer_after_get(first);
699 if (second) first = second;
701 now = ecore_loop_time_get();
702 in = first->at - now;
708 _ecore_timer_reschedule(Ecore_Timer *timer,
711 if ((timer->delete_me) || (timer->frozen)) return;
713 timers = (Ecore_Timer *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
715 /* if the timer would have gone off more than 15 seconds ago,
716 * assume that the system hung and set the timer to go off
717 * timer->in from now. this handles system hangs, suspends
718 * and more, so ecore will only "replay" the timers while
719 * the system is suspended if it is suspended for less than
720 * 15 seconds (basically). this also handles if the process
721 * is stopped in a debugger or IO and other handling gets
722 * really slow within the main loop.
724 if ((timer->at + timer->in) < (when - 15.0))
725 _ecore_timer_set(timer, when + timer->in, timer->in, timer->func, timer->data);
727 _ecore_timer_set(timer, timer->at + timer->in, timer->in, timer->func, timer->data);
730 /* assume that we hold the ecore lock when entering this function */
732 _ecore_timer_expired_timers_call(double when)
734 /* call the first expired timer until no expired timers exist */
735 while (_ecore_timer_expired_call(when)) ;
738 /* assume that we hold the ecore lock when entering this function */
740 _ecore_timer_expired_call(double when)
742 if (!timers) return 0;
743 if (last_check > when)
746 /* User set time backwards */
747 EINA_INLIST_FOREACH(timers, timer) timer->at -= (last_check - when);
753 /* regular main loop, start from head */
754 timer_current = timers;
758 /* recursive main loop, continue from where we were */
759 Ecore_Timer *timer_old = timer_current;
760 timer_current = (Ecore_Timer *)EINA_INLIST_GET(timer_current)->next;
761 _ecore_timer_reschedule(timer_old, when);
764 while (timer_current)
766 Ecore_Timer *timer = timer_current;
768 if (timer->at > when)
770 timer_current = NULL; /* ended walk, next should restart. */
774 if ((timer->just_added) || (timer->delete_me))
776 timer_current = (Ecore_Timer *)EINA_INLIST_GET(timer_current)->next;
781 if (!_ecore_call_task_cb(timer->func, timer->data))
783 if (!timer->delete_me) _ecore_timer_del(timer);
787 if (timer_current) /* may have changed in recursive main loops */
788 timer_current = (Ecore_Timer *)EINA_INLIST_GET(timer_current)->next;
790 _ecore_timer_reschedule(timer, when);
796 _ecore_timer_set(Ecore_Timer *timer,
809 timer->just_added = 1;
811 timer->pending = 0.0;
814 EINA_INLIST_REVERSE_FOREACH(EINA_INLIST_GET(timers), t2)
816 if (timer->at > t2->at)
818 timers = (Ecore_Timer *)eina_inlist_append_relative(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer), EINA_INLIST_GET(t2));
823 timers = (Ecore_Timer *)eina_inlist_prepend(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
826 #ifdef WANT_ECORE_TIMER_DUMP
828 _ecore_timer_cmp(const void *d1,
831 const Ecore_Timer *t1 = d1;
832 const Ecore_Timer *t2 = d2;
834 return (int)((t1->in - t2->in) * 100);