summaryrefslogtreecommitdiff
path: root/src/lib/ecore/ecore_timer.c
blob: 7bde05561c1ef3814fd5f87c9db16e83ee6837d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include <Eo.h>

#include "Ecore.h"
#include "ecore_private.h"

#define MY_CLASS ECORE_TIMER_CLASS
#define MY_CLASS_NAME "ecore_timer"

EAPI Eo_Op ECORE_TIMER_BASE_ID = EO_NOOP;

#define ECORE_TIMER_CHECK(obj)                       \
  if (!eo_isa((obj), ECORE_TIMER_CLASS)) \
    return

#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

struct _Ecore_Timer_Private_Data
{
   EINA_INLIST;
   Ecore_Timer         *obj;
   double              in;
   double              at;
   double              pending;
   Ecore_Task_Cb       func;
   void               *data;

#ifdef WANT_ECORE_TIMER_DUMP
   Ecore_Timer_Bt_Func timer_bt[ECORE_TIMER_DEBUG_BT_NUM];
   int                 timer_bt_num;
#endif

   int                 references;
   unsigned char       delete_me : 1;
   unsigned char       just_added : 1;
   unsigned char       frozen : 1;
};

typedef struct _Ecore_Timer_Private_Data Ecore_Timer_Private_Data;

static void _ecore_timer_set(Ecore_Timer *timer,
                             double        at,
                             double        in,
                             Ecore_Task_Cb func,
                             void         *data);
#ifdef WANT_ECORE_TIMER_DUMP
static int _ecore_timer_cmp(const void *d1,
                            const void *d2);
#endif

static int timers_added = 0;
static int timers_delete_me = 0;
static Ecore_Timer_Private_Data *timers = NULL;
static Ecore_Timer_Private_Data *timer_current = NULL;
static Ecore_Timer_Private_Data *suspended = NULL;
static double last_check = 0.0;
static double precision = 10.0 / 1000000.0;

/**
 * @addtogroup Ecore_Timer_Group
 *
 * @{
 */

/**
 * Retrieves the current precision used by timer infrastructure.
 * @return Current precision.
 * @see ecore_timer_precision_set()
 */
EAPI double
ecore_timer_precision_get(void)
{
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(0.0);
   return precision;
}

/**
 * @brief Sets the precision to be used by timer infrastructure.
 *
 * @param value allowed introduced timeout delay, in seconds.
 *
 * This sets the precision for @b all timers. The precision determines how much
 * of an difference from the requested interval is acceptable. One common reason
 * to use this function is to @b increase the allowed timeout and thus @b
 * decrease precision of the timers, this is because less precise the timers
 * result in the system waking up less often and thus consuming less resources.
 *
 * 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.
 */
EAPI void
ecore_timer_precision_set(double value)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   _ecore_lock();

   if (value < 0.0)
     {
        ERR("Precision %f less than zero, ignored", value);
        goto unlock;
     }
   precision = value;

unlock:
   _ecore_unlock();
}

/**
 * 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.
 *
 * This function adds a timer and returns its handle on success and NULL on
 * failure. The function @p func will be called every @p in seconds. The
 * function will be passed the @p data pointer as its parameter.
 *
 * 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).
 * 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.
 */
EAPI Ecore_Timer *
ecore_timer_add(double        in,
                Ecore_Task_Cb func,
                const void   *data)
{
   Ecore_Timer *timer = NULL;

   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   timer = eo_add_custom(MY_CLASS, _ecore_parent, ecore_timer_constructor(in, func, data));
   eo_unref(timer);
   return timer;
}

static Eina_Bool
_ecore_timer_add(Ecore_Timer *obj,
                 Ecore_Timer_Private_Data *timer,
                 double now,
                 double in,
                 Ecore_Task_Cb func,
                 const void *data)
{

   if (EINA_UNLIKELY(!eina_main_loop_is()))
     {
        eo_error_set(obj);
        EINA_MAIN_LOOP_CHECK_RETURN_VAL(EINA_FALSE);
     }

   timer->obj = obj;
   eo_do_super(obj, eo_constructor());
   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 EINA_FALSE;
     }

   if (in < 0.0) in = 0.0;

#ifdef WANT_ECORE_TIMER_DUMP
   timer->timer_bt_num = backtrace((void **)(timer->timer_bt),
                                   ECORE_TIMER_DEBUG_BT_NUM);
#endif
   _ecore_timer_set(obj, now + in, in, func, (void *)data);
   return EINA_TRUE;
}

static void
_timer_constructor(Eo *obj, void *_pd, va_list *list)
{
   double in = va_arg(*list, double);
   Ecore_Task_Cb func = va_arg(*list, Ecore_Task_Cb);
   const void *data = va_arg(*list, const void *);
   double now;

   _ecore_lock();
   now = ecore_time_get();

   Ecore_Timer_Private_Data *timer = _pd;
   _ecore_timer_add(obj, timer, now, in, func, data);
   _ecore_unlock();
}

static void
_timer_loop_constructor(Eo *obj, void *_pd, va_list *list)
{
   double in = va_arg(*list, double);
   Ecore_Task_Cb func = va_arg(*list, Ecore_Task_Cb);
   const void *data = va_arg(*list, const void *);
   double now;

   now = ecore_loop_time_get();

   Ecore_Timer_Private_Data *timer = _pd;
   _ecore_timer_add(obj, timer, now, in, func, data);
}

static void
_constructor(Eo *obj, void *_pd EINA_UNUSED, va_list *list EINA_UNUSED)
{
   eo_error_set(obj);
   ERR("only custom constructor can be used with '%s' class", MY_CLASS_NAME);
}


/**
 * 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 *
ecore_timer_loop_add(double        in,
                     Ecore_Task_Cb func,
                     const void   *data)
{
   Ecore_Timer *timer;

   _ecore_lock();
   timer = _ecore_timer_loop_add(in, func, data);
   _ecore_unlock();

   return timer;
}

/**
 * Delete the specified timer from the timer list.
 * @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.
 *
 * 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).
 */
EAPI void *
ecore_timer_del(Ecore_Timer *timer)
{
   void *data = NULL;

   if (!timer) return NULL;
   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   _ecore_lock();

   data = _ecore_timer_del(timer);

   _ecore_unlock();
   return data;
}

/**
 * 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.
 */
EAPI void
ecore_timer_interval_set(Ecore_Timer *timer,
                         double       in)
{
   ECORE_TIMER_CHECK(timer);
   eo_do(timer, ecore_obj_timer_interval_set(in));
}

static void
_timer_interval_set(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
{
   EINA_MAIN_LOOP_CHECK_RETURN;
   double in = va_arg(*list, double);
   if (in < 0.0) in = 0.0;
   Ecore_Timer_Private_Data *timer = _pd;

   _ecore_lock();
   timer->in = in;
   _ecore_unlock();
}

/**
 * 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)
{
   double interval = -1.0;

   ECORE_TIMER_CHECK(timer) interval;
   eo_do(timer, ecore_obj_timer_interval_get(&interval));
   return interval;
}

static void
_timer_interval_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
{
   double *ret = va_arg(*list, double *);
   *ret = -1.0;

   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Timer_Private_Data *timer = _pd;
   _ecore_lock();
   *ret = timer->in;
   _ecore_unlock();
}

/**
 * Add some delay for the next occurrence of a timer.
 * This doesn't affect the interval of a timer.
 *
 * @param   timer The timer to change.
 * @param   add   The delay to add to the next iteration.
 */
EAPI void
ecore_timer_delay(Ecore_Timer *timer,
                  double       add)
{
   ECORE_TIMER_CHECK(timer);
   eo_do(timer, ecore_obj_timer_delay(add));
}

static void
_timer_delay(Eo *obj, void *_pd EINA_UNUSED, va_list *list)
{
   double add = va_arg(*list, double);
   EINA_MAIN_LOOP_CHECK_RETURN;

   _ecore_lock();
   _ecore_timer_delay(obj, add);
   _ecore_unlock();
}

/**
 * Reset a timer to its full interval
 * This doesn't affect the interval of a timer
 * @param timer The timer
 * @since 1.2
 * @note This is equivalent to (but faster than)
 * @code
 * ecore_timer_delay(timer, ecore_timer_interval_get(timer) - ecore_timer_pending_get(timer));
 * @endcode
 */
EAPI void
ecore_timer_reset(Ecore_Timer *timer)
{
   ECORE_TIMER_CHECK(timer);
   eo_do(timer, ecore_obj_timer_reset());
}

static void
_timer_reset(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
{
   double now, add;
   EINA_MAIN_LOOP_CHECK_RETURN;

   Ecore_Timer_Private_Data *timer = _pd;

   _ecore_lock();
   now = ecore_time_get();

   if (timer->frozen)
     add = timer->pending;
   else
     add = timer->at - now;
   _ecore_timer_delay(obj, timer->in - add);
   _ecore_unlock();
}

/**
 * Get the pending time regarding a timer.
 *
 * @param        timer The timer to learn from.
 * @return The pending time.
 * @ingroup        Ecore_Timer_Group
 */
EAPI double
ecore_timer_pending_get(Ecore_Timer *timer)
{
   double ret = 0.0;

   ECORE_TIMER_CHECK(timer) ret;
   eo_do(timer, ecore_obj_timer_pending_get(&ret));
   return ret;
}

static void
_timer_pending_get(Eo *obj EINA_UNUSED, void *_pd, va_list *list)
{
   double now;
   double *ret = va_arg(*list, double *);
   *ret = 0.0;

   EINA_MAIN_LOOP_CHECK_RETURN;

   _ecore_lock();
   Ecore_Timer_Private_Data *timer = _pd;

   now = ecore_time_get();

   if (timer->frozen)
     *ret = timer->pending;
   else
     *ret = timer->at - now;
   _ecore_unlock();
}
/**
 * Pauses a running timer.
 *
 * @param timer The timer to be paused.
 *
 * The timer callback won't be called while the timer is paused. The remaining
 * time until the timer expires will be saved, so the timer can be resumed with
 * that same remaining time to expire, instead of expiring instantly.  Use
 * ecore_timer_thaw() to resume it.
 *
 * @note Nothing happens if the timer was already paused.
 *
 * @see ecore_timer_thaw()
 */
EAPI void
ecore_timer_freeze(Ecore_Timer *timer)
{
   ECORE_TIMER_CHECK(timer);
   eo_do(timer, eo_event_freeze());
}

static void
_timer_freeze(Eo *obj EINA_UNUSED, void *_pd, va_list *list EINA_UNUSED)
{
   double now;

   EINA_MAIN_LOOP_CHECK_RETURN;

   Ecore_Timer_Private_Data *timer = _pd;
   _ecore_lock();

   /* Timer already frozen */
   if (timer->frozen)
     goto unlock;

   timers = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
   suspended = (Ecore_Timer_Private_Data *)eina_inlist_prepend(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));

   now = ecore_time_get();

   timer->pending = timer->at - now;
   timer->at = 0.0;
   timer->frozen = 1;
unlock:
   _ecore_unlock();
}

/**
 * Resumes a frozen (paused) timer.
 *
 * @param timer The timer to be resumed.
 *
 * The timer will be resumed from its previous relative position in time. That
 * means, if it had X seconds remaining until expire when it was paused, it will
 * be started now with those same X seconds remaining to expire again. But
 * notice that the interval time won't be touched by this call or by
 * ecore_timer_freeze().
 *
 * @see ecore_timer_freeze()
 */
EAPI void
ecore_timer_thaw(Ecore_Timer *timer)
{
   ECORE_TIMER_CHECK(timer);
   eo_do(timer, eo_event_thaw());
}

static void
_timer_thaw(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
{
   double now;

   EINA_MAIN_LOOP_CHECK_RETURN;
   Ecore_Timer_Private_Data *timer = _pd;

   _ecore_lock();

   /* Timer not frozen */
   if (!timer->frozen)
     goto unlock;

   suspended = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));
   now = ecore_time_get();

   _ecore_timer_set(obj, timer->pending + now, timer->in, timer->func, timer->data);
unlock:
   _ecore_unlock();
}

EAPI char *
ecore_timer_dump(void)
{
#ifdef WANT_ECORE_TIMER_DUMP
   Eina_Strbuf *result;
   char *out;
   Ecore_Timer_Private_Data *tm;
   Eina_List *tmp = NULL;
   int living_timer = 0;
   int unknow_timer = 0;

   EINA_MAIN_LOOP_CHECK_RETURN_VAL(NULL);
   _ecore_lock();
   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;

        if (!tm->frozen && !tm->delete_me)
          living_timer++;

        strings = backtrace_symbols((void **)tm->timer_bt, tm->timer_bt_num);
        if (tm->timer_bt_num <= 0 || strings == NULL)
          {
             unknow_timer++;
             continue;
          }

        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");
        for (j = 0; j < tm->timer_bt_num; j++)
          eina_strbuf_append_printf(result, "%s\n", strings[j]);

        free(strings);
     }

   eina_strbuf_append_printf(result, "\n***\nThere is %i living timer.\nWe did lost track of %i timers.\n", living_timer, unknow_timer);

   out = eina_strbuf_string_steal(result);
   eina_strbuf_free(result);
   _ecore_unlock();

   return out;
#else
   return NULL;
#endif
}

/**
 * @}
 */

Ecore_Timer *
_ecore_timer_loop_add(double        in,
                      Ecore_Task_Cb func,
                      const void   *data)
{
   Ecore_Timer *timer = NULL;
   timer = eo_add_custom(MY_CLASS, _ecore_parent, ecore_timer_loop_constructor(in, func, data));
   eo_unref(timer);

   return timer;
}

EAPI void
_ecore_timer_delay(Ecore_Timer *obj,
                   double       add)
{
   Ecore_Timer_Private_Data *timer = eo_data_get(obj, MY_CLASS);

   if (timer->frozen)
     {
        timer->pending += add;
     }
   else
     {
        timers = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
        _ecore_timer_set(obj, timer->at + add, timer->in, timer->func, timer->data);
     }
}

void *
_ecore_timer_del(Ecore_Timer *obj)
{
   Ecore_Timer_Private_Data *timer = eo_data_get(obj, MY_CLASS);

   if (timer->frozen && !timer->references)
     {
        void *data = timer->data;

        suspended = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));

        if (timer->delete_me)
          timers_delete_me--;

        eo_parent_set(obj, NULL);

        if (eo_destructed_is(obj))
          eo_manual_free(obj);
        else
          eo_manual_free_set(obj, EINA_FALSE);
        return data;
     }

   EINA_SAFETY_ON_TRUE_RETURN_VAL(timer->delete_me, NULL);
   timer->delete_me = 1;
   timers_delete_me++;
   return timer->data;
}

static void
_destructor(Eo *obj, void *_pd, va_list *list EINA_UNUSED)
{
   Ecore_Timer_Private_Data *pd = _pd;

   if (!pd->delete_me)
   {
     pd->delete_me = 1;
     timers_delete_me++;
   }

   eo_do_super(obj, eo_destructor());
}

void
_ecore_timer_shutdown(void)
{
   Ecore_Timer_Private_Data *timer;

   while ((timer = timers))
     {
        timers = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timers));

        eo_parent_set(timer->obj, NULL);
        if (eo_destructed_is(timer->obj))
          eo_manual_free(timer->obj);
        else
          eo_manual_free_set(timer->obj, EINA_FALSE);
     }

   while ((timer = suspended))
     {
        suspended = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(suspended));

        eo_parent_set(timer->obj, NULL);
        if (eo_destructed_is(timer->obj))
          eo_manual_free(timer->obj);
        else
          eo_manual_free_set(timer->obj, EINA_FALSE);
     }

   timer_current = NULL;
}

void
_ecore_timer_cleanup(void)
{
   Ecore_Timer_Private_Data *l;
   int in_use = 0, todo = timers_delete_me, done = 0;

   if (!timers_delete_me) return;
   for (l = timers; l; )
     {
        Ecore_Timer_Private_Data *timer = l;

        l = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(l)->next;
        if (timer->delete_me)
          {
             if (timer->references)
               {
                  in_use++;
                  continue;
               }
             timers = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));

             eo_parent_set(timer->obj, NULL);
             if (eo_destructed_is(timer->obj))
               eo_manual_free(timer->obj);
             else
               eo_manual_free_set(timer->obj, EINA_FALSE);
             timers_delete_me--;
             done++;
             if (timers_delete_me == 0) return;
          }
     }
   for (l = suspended; l; )
     {
        Ecore_Timer_Private_Data *timer = l;

        l = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(l)->next;
        if (timer->delete_me)
          {
             if (timer->references)
               {
                  in_use++;
                  continue;
               }
             suspended = (Ecore_Timer_Private_Data *)eina_inlist_remove(EINA_INLIST_GET(suspended), EINA_INLIST_GET(timer));

             eo_parent_set(timer->obj, NULL);
             if (eo_destructed_is(timer->obj))
                eo_manual_free(timer->obj);
             else
                eo_manual_free_set(timer->obj, EINA_FALSE);
             timers_delete_me--;
             done++;
             if (timers_delete_me == 0) return;
          }
     }

   if ((!in_use) && (timers_delete_me))
     {
        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;
     }
}

void
_ecore_timer_enable_new(void)
{
   Ecore_Timer_Private_Data *timer;

   if (!timers_added) return;
   timers_added = 0;
   EINA_INLIST_FOREACH(timers, timer) timer->just_added = 0;
}

int
_ecore_timers_exists(void)
{
   Ecore_Timer_Private_Data *timer = timers;

   while ((timer) && (timer->delete_me))
     timer = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(timer)->next;

   return !!timer;
}

static inline Ecore_Timer *
_ecore_timer_first_get(void)
{
   Ecore_Timer *ret = NULL;
   Ecore_Timer_Private_Data *timer = timers;

   while ((timer) && ((timer->delete_me) || (timer->just_added)))
     timer = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(timer)->next;

   if (timer)
     ret = timer->obj;
   return ret;
}

static inline Ecore_Timer *
_ecore_timer_after_get(Ecore_Timer *obj)
{
   Ecore_Timer *ret = NULL;
   Ecore_Timer_Private_Data *base = eo_data_get(obj, MY_CLASS);

   Ecore_Timer_Private_Data *timer = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(base)->next;
   Ecore_Timer_Private_Data *valid_timer = NULL;
   double maxtime = base->at + precision;

   while ((timer) && (timer->at < maxtime))
     {
        if (!((timer->delete_me) || (timer->just_added)))
          valid_timer = timer;
        timer = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(timer)->next;
     }

   if (valid_timer)
     ret = valid_timer->obj;
   return ret;
}

double
_ecore_timer_next_get(void)
{
   double now;
   double in;
   Ecore_Timer *first_obj, *second_obj;
   Ecore_Timer_Private_Data *first;

   first_obj = _ecore_timer_first_get();
   if (!first_obj) return -1;

   second_obj = _ecore_timer_after_get(first_obj);
   if (second_obj) first_obj = second_obj;

   first = eo_data_get(first_obj, MY_CLASS);

   now = ecore_loop_time_get();
   in = first->at - now;
   if (in < 0) in = 0;
   return in;
}

static inline void
_ecore_timer_reschedule(Ecore_Timer *obj,
                        double       when)
{
   Ecore_Timer_Private_Data *timer = eo_data_get(obj, MY_CLASS);
   if ((timer->delete_me) || (timer->frozen)) return;

   timers = (Ecore_Timer_Private_Data *)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(obj, when + timer->in, timer->in, timer->func, timer->data);
   else
     _ecore_timer_set(obj, timer->at + timer->in, timer->in, timer->func, timer->data);
}

/* assume that we hold the ecore lock when entering this function */
void
_ecore_timer_expired_timers_call(double when)
{
   /* call the first expired timer until no expired timers exist */
    while (_ecore_timer_expired_call(when)) ;
}

/* assume that we hold the ecore lock when entering this function */
int
_ecore_timer_expired_call(double when)
{
   if (!timers) return 0;
   if (last_check > when)
     {
        Ecore_Timer_Private_Data *timer;
        /* User set time backwards */
        EINA_INLIST_FOREACH(timers, timer) timer->at -= (last_check - when);
     }
   last_check = when;

   if (!timer_current)
     {
        /* regular main loop, start from head */
        timer_current = timers;
     }
   else
     {
        /* recursive main loop, continue from where we were */
        Ecore_Timer_Private_Data *timer_old = timer_current;
        timer_current = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(timer_current)->next;
        _ecore_timer_reschedule(timer_old->obj, when);
     }

   while (timer_current)
     {
        Ecore_Timer_Private_Data *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_Private_Data *)EINA_INLIST_GET(timer_current)->next;
             continue;
          }

        timer->references++;
        if (!_ecore_call_task_cb(timer->func, timer->data))
          {
             if (!timer->delete_me) _ecore_timer_del(timer->obj);
          }
        timer->references--;

        if (timer_current) /* may have changed in recursive main loops */
          timer_current = (Ecore_Timer_Private_Data *)EINA_INLIST_GET(timer_current)->next;

        _ecore_timer_reschedule(timer->obj, when);
     }
   return 0;
}

static void
_ecore_timer_set(Ecore_Timer  *obj,
                 double        at,
                 double        in,
                 Ecore_Task_Cb func,
                 void         *data)
{
   Ecore_Timer_Private_Data *t2;

   Ecore_Timer_Private_Data *timer = eo_data_get(obj, MY_CLASS);

   timers_added = 1;
   timer->at = at;
   timer->in = in;
   timer->func = func;
   timer->data = data;
   timer->just_added = 1;
   timer->frozen = 0;
   timer->pending = 0.0;
   if (timers)
     {
        EINA_INLIST_REVERSE_FOREACH(EINA_INLIST_GET(timers), t2)
          {
             if (timer->at > t2->at)
               {
                  timers = (Ecore_Timer_Private_Data *)eina_inlist_append_relative(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer), EINA_INLIST_GET(t2));
                  return;
               }
          }
     }
   timers = (Ecore_Timer_Private_Data *)eina_inlist_prepend(EINA_INLIST_GET(timers), EINA_INLIST_GET(timer));
}

#ifdef WANT_ECORE_TIMER_DUMP
static int
_ecore_timer_cmp(const void *d1,
                 const void *d2)
{
   const Ecore_Timer_Private_Data *t1 = d1;
   const Ecore_Timer_Private_Data *t2 = d2;

   return (int)((t1->in - t2->in) * 100);
}
#endif

static void
_class_constructor(Eo_Class *klass)
{
   const Eo_Op_Func_Description func_desc[] = {
        EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_CONSTRUCTOR), _constructor),
        EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_DESTRUCTOR), _destructor),

        EO_OP_FUNC(ECORE_TIMER_ID(ECORE_TIMER_SUB_ID_CONSTRUCTOR), _timer_constructor),
        EO_OP_FUNC(ECORE_TIMER_ID(ECORE_TIMER_SUB_ID_LOOP_CONSTRUCTOR), _timer_loop_constructor),
        EO_OP_FUNC(ECORE_TIMER_ID(ECORE_TIMER_SUB_ID_INTERVAL_SET), _timer_interval_set),
        EO_OP_FUNC(ECORE_TIMER_ID(ECORE_TIMER_SUB_ID_INTERVAL_GET), _timer_interval_get),

        EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_FREEZE), _timer_freeze),
        EO_OP_FUNC(EO_BASE_ID(EO_BASE_SUB_ID_EVENT_THAW), _timer_thaw),

        EO_OP_FUNC(ECORE_TIMER_ID(ECORE_TIMER_SUB_ID_DELAY), _timer_delay),
        EO_OP_FUNC(ECORE_TIMER_ID(ECORE_TIMER_SUB_ID_RESET), _timer_reset),
        EO_OP_FUNC(ECORE_TIMER_ID(ECORE_TIMER_SUB_ID_PENDING_GET), _timer_pending_get),
        EO_OP_FUNC_SENTINEL
   };

   eo_class_funcs_set(klass, func_desc);
}

static const Eo_Op_Description op_desc[] = {
     EO_OP_DESCRIPTION(ECORE_TIMER_SUB_ID_CONSTRUCTOR, "Creates a timer to call the given function in the given period of time."),
     EO_OP_DESCRIPTION(ECORE_TIMER_SUB_ID_LOOP_CONSTRUCTOR, "Creates a timer to call the given function in the given period of time."),
     EO_OP_DESCRIPTION(ECORE_TIMER_SUB_ID_INTERVAL_SET, "Change the interval the timer ticks of."),
     EO_OP_DESCRIPTION(ECORE_TIMER_SUB_ID_INTERVAL_GET, "Get the interval the timer ticks on."),
     EO_OP_DESCRIPTION(ECORE_TIMER_SUB_ID_DELAY, "Add some delay for the next occurrence of a timer."),
     EO_OP_DESCRIPTION(ECORE_TIMER_SUB_ID_RESET, "Reset a timer to its full interval"),
     EO_OP_DESCRIPTION(ECORE_TIMER_SUB_ID_PENDING_GET, "Get the pending time regarding a timer."),
     EO_OP_DESCRIPTION_SENTINEL
};
static const Eo_Class_Description class_desc = {
     EO_VERSION,
     MY_CLASS_NAME,
     EO_CLASS_TYPE_REGULAR,
     EO_CLASS_DESCRIPTION_OPS(&ECORE_TIMER_BASE_ID, op_desc, ECORE_TIMER_SUB_ID_LAST),
     NULL,
     sizeof(Ecore_Timer_Private_Data),
     _class_constructor,
     NULL
};

EO_DEFINE_CLASS(ecore_timer_class_get, &class_desc, EO_BASE_CLASS, NULL)