[ecore] merged svn latest code (svn54830)
[profile/ivi/ecore.git] / src / lib / ecore / ecore_poll.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6
7 #include "Ecore.h"
8 #include "ecore_private.h"
9
10
11 struct _Ecore_Poller
12 {
13    EINA_INLIST;
14    ECORE_MAGIC;
15    int           ibit;
16    unsigned char delete_me : 1;
17    Ecore_Task_Cb func;
18    void          *data;
19 };
20
21
22 static Ecore_Timer    *timer = NULL;
23 static int             min_interval = -1;
24 static int             interval_incr = 0;
25 static int             at_tick = 0;
26 static int             just_added_poller = 0;
27 static int             poller_delete_count = 0;
28 static int             poller_walking = 0;
29 static double          poll_interval = 0.125;
30 static double          poll_cur_interval = 0.0;
31 static double          last_tick = 0.0;
32 static Ecore_Poller   *pollers[16] =
33 {
34    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
35    NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
36 };
37 static unsigned short  poller_counters[16] =
38 {
39    0,0,0,0,0,0,0,0,
40    0,0,0,0,0,0,0,0
41 };
42
43 static void _ecore_poller_next_tick_eval(void);
44 static Eina_Bool _ecore_poller_cb_timer(void *data);
45
46 static void
47 _ecore_poller_next_tick_eval(void)
48 {
49    int i;
50    double interval;
51
52    min_interval = -1;
53    for (i = 0; i < 15; i++)
54      {
55         if (pollers[i])
56           {
57              min_interval = i;
58              break;
59           }
60      }
61    if (min_interval < 0)
62      {
63         /* no pollers */
64         if (timer)
65           {
66              ecore_timer_del(timer);
67              timer = NULL;
68           }
69         return;
70      }
71    interval_incr = (1 << min_interval);
72    interval = interval_incr * poll_interval;
73    /* we are at the tick callback - so no need to do inter-tick adjustments
74     * so we can fasttrack this as t -= last_tick in theory is 0.0 (though
75     * in practice it will be a very very very small value. also the tick
76     * callback will adjust the timer interval at the end anyway */
77    if (at_tick)
78      {
79         if (!timer)
80           timer = ecore_timer_add(interval, _ecore_poller_cb_timer, NULL);
81      }
82    else
83      {
84         double t;
85
86         if (!timer)
87           timer = ecore_timer_add(interval, _ecore_poller_cb_timer, NULL);
88         else
89           {
90              t = ecore_time_get();
91              if (interval != poll_cur_interval)
92                {
93                   t -= last_tick; /* time since we last ticked */
94                   /* delete the timer and reset it to tick off in the new
95                    * time interval. at the tick this will be adjusted */
96                   ecore_timer_del(timer);
97                   timer = ecore_timer_add(interval - t,
98                                           _ecore_poller_cb_timer, NULL);
99                }
100           }
101      }
102    poll_cur_interval = interval;
103 }
104
105 static Eina_Bool
106 _ecore_poller_cb_timer(void *data __UNUSED__)
107 {
108    int i;
109    Ecore_Poller *poller, *l;
110    int changes = 0;
111
112    at_tick++;
113    last_tick = ecore_time_get();
114    /* we have 16 counters - each incriments every time the poller counter
115     * "ticks". it incriments by the minimum interval (which can be 1, 2, 4,
116     * 7, 16 etc. up to 32768) */
117    for (i = 0; i < 15; i++)
118      {
119         poller_counters[i] += interval_incr;
120         /* wrap back to 0 if we exceed out loop count for the counter */
121         if (poller_counters[i] >= (1 << i)) poller_counters[i] = 0;
122      }
123
124    just_added_poller = 0;
125    /* walk the pollers now */
126    poller_walking++;
127    for (i = 0; i < 15; i++)
128      {
129         /* if the counter is @ 0 - this means that counter "went off" this
130          * tick interval, so run all pollers hooked to that counter */
131         if (poller_counters[i] == 0)
132           {
133              EINA_INLIST_FOREACH(pollers[i], poller)
134                {
135                   if (!poller->delete_me)
136                     {
137                        if (!poller->func(poller->data))
138                          {
139                             if (!poller->delete_me)
140                               {
141                                  poller->delete_me = 1;
142                                  poller_delete_count++;
143                               }
144                          }
145                     }
146                }
147           }
148      }
149    poller_walking--;
150
151    /* handle deletes afterwards */
152    if (poller_delete_count > 0)
153      {
154         /* FIXME: walk all pollers and remove deleted ones */
155         for (i = 0; i < 15; i++)
156           {
157              for (l = pollers[i]; l;)
158                {
159                   poller = l;
160                   l = (Ecore_Poller *) EINA_INLIST_GET(l)->next;
161                   if (poller->delete_me)
162                     {
163                        pollers[i] = (Ecore_Poller *) eina_inlist_remove(EINA_INLIST_GET(pollers[i]), EINA_INLIST_GET(poller));
164                        free(poller);
165                        poller_delete_count--;
166                        changes++;
167                        if (poller_delete_count <= 0) break;
168                     }
169                }
170              if (poller_delete_count <= 0) break;
171           }
172      }
173    /* if we deleted or added any pollers, then we need to re-evaluate our
174     * minimum poll interval */
175    if ((changes > 0) || (just_added_poller > 0))
176      _ecore_poller_next_tick_eval();
177
178    just_added_poller = 0;
179    poller_delete_count = 0;
180
181    at_tick--;
182
183    /* if the timer was deleted then there is no point returning 1 - ambiguous
184     * if we do as it im plies "keep running me" but we have been deleted
185     * anyway */
186    if (!timer) return ECORE_CALLBACK_CANCEL;
187
188    /* adjust interval */
189    ecore_timer_interval_set(timer, poll_cur_interval);
190    return ECORE_CALLBACK_RENEW;
191 }
192
193 /**
194  * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
195  *
196  * @{
197  */
198
199 /**
200  * @addtogroup Ecore_Poller_Group Ecore Poll functions
201  *
202  * These functions are for the need to poll information, but provide a shared
203  * abstracted API to pool such polling to minimise wakeup and ensure all the
204  * polling happens in as few spots as possible areound a core poll interval.
205  * For now only 1 core poller type is supprted: ECORE_POLLER_CORE
206  *
207  * @{
208  */
209
210
211 /**
212  * Sets the time between ticks (in seconds) for the given ticker clock.
213  * @param   type The ticker type to adjust
214  * @param   poll_time The time (in seconds) between ticks of the clock
215  *
216  * This will adjust the time between ticks of the given ticker type defined
217  * by @p type to the time period defined by @p poll_time.
218  */
219 EAPI void
220 ecore_poller_poll_interval_set(Ecore_Poller_Type type __UNUSED__, double poll_time)
221 {
222    poll_interval = poll_time;
223    _ecore_poller_next_tick_eval();
224 }
225
226 /**
227  * Gets the time between ticks (in seconds) for the given ticker clock.
228  * @param   type The ticker type to query
229  * @return  The time in seconds between ticks of the ticker clock
230  *
231  * This will get the time between ticks of the specifider ticker clock.
232  */
233 EAPI double
234 ecore_poller_poll_interval_get(Ecore_Poller_Type type __UNUSED__)
235 {
236    return poll_interval;
237 }
238
239 /**
240  * Creates a poller to call the given function at a particular tick interval.
241  * @param   type The ticker type to attach the poller to
242  * @param   interval The poll interval
243  * @param   func The given function.  If @p func returns 1, the poller is
244  *               rescheduled for the next tick interval.
245  * @param   data Data to pass to @p func when it is called.
246  * @return  A poller object on success.  @c NULL on failure.
247  *
248  * This function adds a poller callback that is to be called regularly
249  * along with all other poller callbacks so the pollers are synchronized with
250  * all other pollers running off the same poller type and at the same tick
251  * interval. This should be used for polling things when polling is desired
252  * or required, and you do not have specific requirements on the exact times
253  * to poll and want to avoid extra process wakeups for polling. This will
254  * save power as the CPU has more of a chance to go into a low power state
255  * the longer it is asleep for, so this should be used if you are at all
256  * power conscious.
257  *
258  * The @p type parameter defines the poller tick type (there is a virtual
259  * clock ticking all the time - though ecore avoids making it tick when
260  * there will not be any work to do at that tick point). There is only one
261  * ticker at the moment - that is ECORE_POLLER_CORE. This is here for future
262  * expansion if multiple clocks with different frequencies are really required.
263  * The default time between ticks for the ECORE_POLLER_CORE ticker is 0.125
264  * seconds.
265  *
266  * The @p interval is the number of ticker ticks that will pass by in between
267  * invocations of the @p func callback. This must be between 1 and 32768
268  * inclusive, and must be a power of 2 (i.e. 1, 2, 4, 8, 16, ... 16384, 32768).
269  * If it is 1, then the function will be called every tick. if it is 2, then it
270  * will be called every 2nd tick, if it is 8, then every 8th tick etc. Exactly
271  * which tick is undefined, as only the interval between calls can be defined.
272  * Ecore will endeavour to keep pollers synchronised and to call as many in
273  * 1 wakeup event as possible.
274  *
275  * This function adds a poller and returns its handle on success and NULL on
276  * failure. The function @p func will be called at tick intervals described
277  * above. The function will be passed the @p data pointer as its parameter.
278  *
279  * When the poller @p func is called, it must return a value of either
280  * 1 (or ECORE_CALLBACK_RENEW) or 0 (or ECORE_CALLBACK_CANCEL). If it
281  * returns 1, it will be called again at the next tick, or if it returns
282  * 0 it will be deleted automatically making any references/handles for it
283  * invalid.
284  */
285 EAPI Ecore_Poller *
286 ecore_poller_add(Ecore_Poller_Type type __UNUSED__, int interval, Ecore_Task_Cb func, const void *data)
287 {
288    Ecore_Poller *poller;
289    int ibit;
290
291    if (!func) return NULL;
292    if (interval < 1) interval = 1;
293
294    poller = calloc(1, sizeof(Ecore_Poller));
295    if (!poller) return NULL;
296    ECORE_MAGIC_SET(poller, ECORE_MAGIC_POLLER);
297    /* interval MUST be a power of 2, so enforce it */
298    if (interval < 1) interval = 1;
299    ibit = -1;
300    while (interval != 0)
301      {
302         ibit++;
303         interval >>= 1;
304      }
305    /* only allow up to 32768 - i.e. ibit == 15, so limit it */
306    if (ibit > 15) ibit = 15;
307
308    poller->ibit = ibit;
309    poller->func = func;
310    poller->data = (void *)data;
311    pollers[poller->ibit] = (Ecore_Poller *) eina_inlist_prepend(EINA_INLIST_GET(pollers[poller->ibit]), EINA_INLIST_GET(poller));
312    if (poller_walking)
313      just_added_poller++;
314    else
315      _ecore_poller_next_tick_eval();
316    return poller;
317 }
318
319 /**
320  * Changes the polling interval rate of @p poller.
321  *
322  * @param poller The Ecore_Poller to change the interval of
323  * @param interval The tick interval to set; must be a power of 2 but <= 32768
324  * @return Returns true on success, false on failure
325  *
326  * This allows the changing of a poller's polling interval.  It is useful when you want to alter
327  * a poll rate without deleting and re-creating a poller.
328  */
329 EAPI Eina_Bool
330 ecore_poller_poller_interval_set(Ecore_Poller *poller, int interval)
331 {
332    int ibit;
333
334    if (!ECORE_MAGIC_CHECK(poller, ECORE_MAGIC_POLLER))
335      {
336         ECORE_MAGIC_FAIL(poller, ECORE_MAGIC_POLLER,
337            "ecore_poller_poller_interval_set");
338         return EINA_FALSE;
339      }
340
341    /* interval MUST be a power of 2, so enforce it */
342    if (interval < 1) interval = 1;
343    ibit = -1;
344    while (interval != 0)
345      {
346        ibit++;
347        interval >>= 1;
348      }
349    /* only allow up to 32768 - i.e. ibit == 15, so limit it */
350    if (ibit > 15) ibit = 15;
351    /* if interval specified is the same as interval set, return true without wasting time */
352    if (poller->ibit == ibit)
353      return EINA_TRUE;
354    pollers[poller->ibit] = (Ecore_Poller *) eina_inlist_remove(EINA_INLIST_GET(pollers[poller->ibit]), EINA_INLIST_GET(poller));
355    poller->ibit = ibit;
356    pollers[poller->ibit] = (Ecore_Poller *) eina_inlist_prepend(EINA_INLIST_GET(pollers[poller->ibit]), EINA_INLIST_GET(poller));
357    if (poller_walking)
358      just_added_poller++;
359    else
360      _ecore_poller_next_tick_eval();
361    return EINA_TRUE;
362 }
363
364 /**
365  * Gets the polling interval rate of @p poller.
366  *
367  * @param poller The Ecore_Poller to change the interval of
368  * @return Returns the interval, in ticks, that @p poller polls at
369  *
370  * This returns a poller's polling interval, or 0 on error.
371  */
372 EAPI int
373 ecore_poller_poller_interval_get(Ecore_Poller *poller)
374 {
375    int ibit, interval = 1;
376
377    if (!ECORE_MAGIC_CHECK(poller, ECORE_MAGIC_POLLER))
378      {
379         ECORE_MAGIC_FAIL(poller, ECORE_MAGIC_POLLER,
380            "ecore_poller_poller_interval_get");
381         return 0;
382      }
383
384    ibit = poller->ibit;
385    while (ibit != 0)
386      {
387        ibit--;
388        interval <<= 1;
389      }
390    return interval;
391 }
392
393 /**
394  * Delete the specified poller from the timer list.
395  * @param   poller The poller to delete.
396  * @return  The data pointer set for the timer when @ref ecore_poller_add was
397  *          called.  @c NULL is returned if the function is unsuccessful.
398  *
399  * Note: @p poller must be a valid handle. If the poller function has already
400  * returned 0, the handle is no longer valid (and does not need to be delete).
401  */
402 EAPI void *
403 ecore_poller_del(Ecore_Poller *poller)
404 {
405    void *data;
406
407    if (!ECORE_MAGIC_CHECK(poller, ECORE_MAGIC_POLLER))
408      {
409         ECORE_MAGIC_FAIL(poller, ECORE_MAGIC_POLLER,
410            "ecore_poller_del");
411         return NULL;
412      }
413    /* we are walking the poller list - a bad idea to remove from it while
414     * walking it, so just flag it as delete_me and come back to it after
415     * the loop has finished */
416    if (poller_walking > 0)
417      {
418         poller_delete_count++;
419         poller->delete_me = 1;
420         return poller->data;
421      }
422    /* not in loop so safe - delete immediately */
423    data = poller->data;
424    pollers[poller->ibit] = (Ecore_Poller *) eina_inlist_remove(EINA_INLIST_GET(pollers[poller->ibit]), EINA_INLIST_GET(poller));
425    free(poller);
426    _ecore_poller_next_tick_eval();
427    return data;
428 }
429
430 /**
431  * @}
432  */
433
434 /**
435  * @}
436  */
437
438 void
439 _ecore_poller_shutdown(void)
440 {
441    int i;
442    Ecore_Poller *poller;
443
444    for (i = 0; i < 15; i++)
445      {
446         while ((poller = pollers[i]))
447           {
448              pollers[i] = (Ecore_Poller *) eina_inlist_remove(EINA_INLIST_GET(pollers[i]), EINA_INLIST_GET(pollers[i]));
449              free(poller);
450           }
451      }
452 }