1999-10-04 15:06:00 -07:00
|
|
|
/*
|
2007-01-13 11:14:29 -08:00
|
|
|
* Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
|
2013-02-10 01:59:25 -08:00
|
|
|
* Copyright (C) 2006-2013 Kim Woelders
|
2000-04-05 16:22:56 -07:00
|
|
|
*
|
1999-10-04 15:06:00 -07:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2000-04-05 16:22:56 -07:00
|
|
|
*
|
1999-10-04 15:06:00 -07:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies of the Software, its documentation and marketing & publicity
|
|
|
|
* materials, and acknowledgment shall be given in the documentation, materials
|
|
|
|
* and software packages that this Software was used.
|
2000-04-05 16:22:56 -07:00
|
|
|
*
|
1999-10-04 15:06:00 -07:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
1999-08-17 15:56:46 -07:00
|
|
|
#include "E.h"
|
2013-06-06 13:37:06 -07:00
|
|
|
#include "list.h"
|
2006-04-16 06:27:38 -07:00
|
|
|
#include "timers.h"
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
struct _timer {
|
2011-01-05 10:05:52 -08:00
|
|
|
unsigned int in_time;
|
|
|
|
unsigned int at_time;
|
2008-05-24 11:13:17 -07:00
|
|
|
struct _timer *next;
|
|
|
|
int (*func) (void *data);
|
|
|
|
void *data;
|
|
|
|
char again;
|
2006-04-16 06:27:38 -07:00
|
|
|
};
|
|
|
|
|
2011-01-05 10:05:52 -08:00
|
|
|
static int
|
|
|
|
tdiff(unsigned int t1, unsigned int t2)
|
|
|
|
{
|
|
|
|
return (int)(t1 - t2);
|
|
|
|
}
|
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
static Timer *q_first = NULL;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
static void
|
|
|
|
_TimerSet(Timer * timer)
|
1999-08-17 15:56:46 -07:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
Timer *ptr, *pptr;
|
2007-01-28 17:26:12 -08:00
|
|
|
|
2012-07-15 09:17:11 -07:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS) > 1)
|
|
|
|
Eprintf("%s %p: func=%p data=%p\n", __func__, timer, timer->func,
|
2008-05-24 11:13:17 -07:00
|
|
|
timer->data);
|
1999-08-17 15:56:46 -07:00
|
|
|
|
|
|
|
/* if there is no queue it becomes the queue */
|
|
|
|
if (!q_first)
|
2006-01-08 08:13:21 -08:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
q_first = timer;
|
|
|
|
timer->next = NULL;
|
2006-01-08 08:13:21 -08:00
|
|
|
}
|
1999-08-17 15:56:46 -07:00
|
|
|
else
|
2000-07-26 11:00:47 -07:00
|
|
|
{
|
2003-05-22 12:15:03 -07:00
|
|
|
pptr = NULL;
|
2006-01-08 08:13:21 -08:00
|
|
|
for (ptr = q_first; ptr; pptr = ptr, ptr = ptr->next)
|
2003-05-22 12:15:03 -07:00
|
|
|
{
|
2011-01-05 10:05:52 -08:00
|
|
|
if (tdiff(ptr->at_time, timer->at_time) > 0)
|
2006-01-08 08:13:21 -08:00
|
|
|
break;
|
2003-05-22 12:15:03 -07:00
|
|
|
}
|
|
|
|
if (pptr)
|
2008-05-24 11:13:17 -07:00
|
|
|
pptr->next = timer;
|
2003-05-22 12:15:03 -07:00
|
|
|
else
|
2008-05-24 11:13:17 -07:00
|
|
|
q_first = timer;
|
|
|
|
timer->next = ptr;
|
2000-07-26 11:00:47 -07:00
|
|
|
}
|
1999-08-17 15:56:46 -07:00
|
|
|
}
|
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
static void
|
|
|
|
_TimerDel(Timer * timer)
|
|
|
|
{
|
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s %p: func=%p data=%p\n", __func__, timer, timer->func,
|
2008-05-24 11:13:17 -07:00
|
|
|
timer->data);
|
|
|
|
Efree(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer *
|
2011-01-05 10:05:57 -08:00
|
|
|
TimerAdd(int dt_ms, int (*func) (void *data), void *data)
|
2008-05-24 11:13:17 -07:00
|
|
|
{
|
|
|
|
Timer *timer;
|
|
|
|
|
|
|
|
timer = EMALLOC(Timer, 1);
|
|
|
|
if (!timer)
|
|
|
|
return NULL;
|
|
|
|
|
2011-01-05 10:05:52 -08:00
|
|
|
timer->in_time = (unsigned int)dt_ms;
|
|
|
|
timer->at_time = GetTimeMs() + dt_ms;
|
2008-05-24 11:13:17 -07:00
|
|
|
timer->func = func;
|
|
|
|
timer->data = data;
|
|
|
|
|
2009-03-22 05:40:20 -07:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s %p: func=%p data=%p: %8d\n", __func__, timer,
|
2011-01-05 10:05:52 -08:00
|
|
|
timer->func, timer->data, dt_ms);
|
2009-03-22 05:40:20 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
_TimerSet(timer); /* Add to timer queue */
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2012-07-15 09:17:13 -07:00
|
|
|
void
|
2011-01-05 10:05:52 -08:00
|
|
|
TimersRun(unsigned int t_ms)
|
1999-08-17 15:56:46 -07:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
Timer *timer, *q_old, *q_run;
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
timer = q_first;
|
2011-01-05 10:05:48 -08:00
|
|
|
if (!timer)
|
2012-07-15 09:17:13 -07:00
|
|
|
return; /* No timers pending */
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2011-01-05 10:05:48 -08:00
|
|
|
q_run = q_old = timer;
|
2008-05-24 11:13:17 -07:00
|
|
|
for (; timer; timer = q_first)
|
2007-01-28 17:26:12 -08:00
|
|
|
{
|
2012-07-15 09:17:11 -07:00
|
|
|
if (tdiff(timer->at_time, t_ms) > 0)
|
2007-01-28 17:26:12 -08:00
|
|
|
break;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s - run %p: func=%p data=%p: %8d\n", __func__, timer,
|
|
|
|
timer->func, timer->data, timer->at_time - t_ms);
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
q_first = timer->next;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
/* Run this callback */
|
|
|
|
timer->again = timer->func(timer->data);
|
|
|
|
q_run = timer;
|
|
|
|
}
|
2007-01-28 17:26:12 -08:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
if (q_old != q_first)
|
|
|
|
{
|
|
|
|
/* At least one timer has run */
|
|
|
|
q_run->next = NULL; /* Terminate expired timer list */
|
|
|
|
|
|
|
|
/* Re-schedule/remove timers that have run */
|
|
|
|
for (timer = q_old; timer; timer = q_old)
|
|
|
|
{
|
|
|
|
q_old = timer->next;
|
|
|
|
if (timer->again)
|
|
|
|
{
|
|
|
|
timer->at_time += timer->in_time;
|
|
|
|
_TimerSet(timer); /* Add to timer queue */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_TimerDel(timer);
|
|
|
|
}
|
|
|
|
}
|
2007-01-28 17:26:12 -08:00
|
|
|
}
|
|
|
|
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS) > 1)
|
2007-01-28 17:26:12 -08:00
|
|
|
{
|
2011-01-05 10:05:52 -08:00
|
|
|
for (timer = q_first; timer; timer = timer->next)
|
2012-07-15 09:17:11 -07:00
|
|
|
Eprintf("%s - pend %p: func=%p data=%p: %8d (%d)\n", __func__,
|
|
|
|
timer, timer->func, timer->data, timer->at_time - t_ms,
|
|
|
|
timer->in_time);
|
2007-01-28 17:26:12 -08:00
|
|
|
}
|
2012-07-15 09:17:13 -07:00
|
|
|
}
|
|
|
|
|
2013-02-10 01:59:25 -08:00
|
|
|
int
|
2012-07-15 09:17:13 -07:00
|
|
|
TimersRunNextIn(unsigned int t_ms)
|
|
|
|
{
|
|
|
|
Timer *timer;
|
2013-02-10 01:59:25 -08:00
|
|
|
int dt;
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2011-01-05 10:05:52 -08:00
|
|
|
timer = q_first;
|
2011-02-09 13:08:20 -08:00
|
|
|
|
|
|
|
/* If the next (rescheduled) timer is already expired, set timeout time
|
|
|
|
* to 1 ms. This avoids starving the fd's and should maintain the intended
|
|
|
|
* (mean) timer rate.
|
|
|
|
* The (mean) amount of work done in a timer function should of course not
|
|
|
|
* exceed the timeout time. */
|
|
|
|
if (timer)
|
2013-02-10 01:59:25 -08:00
|
|
|
dt = (int)(timer->at_time - t_ms) > 0 ? (int)(timer->at_time - t_ms) : 1;
|
2011-02-09 13:08:20 -08:00
|
|
|
else
|
2013-02-10 01:59:25 -08:00
|
|
|
dt = 0;
|
2006-04-16 06:27:38 -07:00
|
|
|
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_TIMERS))
|
2013-02-10 01:59:25 -08:00
|
|
|
Eprintf("%s - next in %8d\n", __func__, dt);
|
2007-01-28 17:26:12 -08:00
|
|
|
|
2013-02-10 01:59:25 -08:00
|
|
|
return dt;
|
2011-01-05 10:05:52 -08:00
|
|
|
}
|
|
|
|
|
2011-01-05 10:05:48 -08:00
|
|
|
void
|
2008-05-24 11:13:17 -07:00
|
|
|
TimerDel(Timer * timer)
|
1999-08-17 15:56:46 -07:00
|
|
|
{
|
2008-05-24 11:13:17 -07:00
|
|
|
Timer *qe, *ptr, *pptr;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
|
|
|
pptr = NULL;
|
2006-01-08 08:13:21 -08:00
|
|
|
for (ptr = q_first; ptr; pptr = ptr, ptr = ptr->next)
|
2000-07-26 11:00:47 -07:00
|
|
|
{
|
2003-05-22 12:15:03 -07:00
|
|
|
qe = ptr;
|
2008-05-24 11:13:17 -07:00
|
|
|
if (qe != timer)
|
2006-01-08 08:13:21 -08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Match - remove it from the queue */
|
|
|
|
if (pptr)
|
|
|
|
pptr->next = qe->next;
|
|
|
|
else
|
|
|
|
q_first = qe->next;
|
2008-05-24 11:13:17 -07:00
|
|
|
|
2006-01-08 08:13:21 -08:00
|
|
|
/* free it */
|
2008-05-24 11:13:17 -07:00
|
|
|
_TimerDel(timer);
|
2011-01-14 23:45:36 -08:00
|
|
|
break;
|
2000-07-26 11:00:47 -07:00
|
|
|
}
|
1999-08-17 15:56:46 -07:00
|
|
|
}
|
2006-04-09 03:18:34 -07:00
|
|
|
|
2008-05-24 11:13:17 -07:00
|
|
|
void
|
2011-01-05 10:05:57 -08:00
|
|
|
TimerSetInterval(Timer * timer, int dt_ms)
|
2008-05-24 11:13:17 -07:00
|
|
|
{
|
2011-01-05 10:05:52 -08:00
|
|
|
timer->in_time = (unsigned int)dt_ms;
|
2008-05-24 11:13:17 -07:00
|
|
|
}
|
|
|
|
|
2007-01-27 20:59:46 -08:00
|
|
|
/*
|
|
|
|
* Idlers
|
|
|
|
*/
|
2013-06-06 13:37:06 -07:00
|
|
|
static LIST_HEAD(idler_list);
|
2006-04-09 03:18:34 -07:00
|
|
|
|
|
|
|
typedef void (IdlerFunc) (void *data);
|
|
|
|
|
2008-03-23 04:54:24 -07:00
|
|
|
struct _idler {
|
2013-06-06 13:37:06 -07:00
|
|
|
dlist_t list;
|
2006-04-09 03:18:34 -07:00
|
|
|
IdlerFunc *func;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
2006-04-16 06:27:38 -07:00
|
|
|
Idler *
|
2008-05-10 16:15:32 -07:00
|
|
|
IdlerAdd(IdlerFunc * func, void *data)
|
2006-04-09 03:18:34 -07:00
|
|
|
{
|
|
|
|
Idler *id;
|
|
|
|
|
2007-01-16 17:10:44 -08:00
|
|
|
id = EMALLOC(Idler, 1);
|
2006-04-09 03:18:34 -07:00
|
|
|
if (!id)
|
2006-04-16 06:27:38 -07:00
|
|
|
return NULL;
|
2006-04-09 03:18:34 -07:00
|
|
|
|
|
|
|
id->func = func;
|
|
|
|
id->data = data;
|
|
|
|
|
2013-06-06 13:37:06 -07:00
|
|
|
LIST_APPEND(Idler, &idler_list, id);
|
2006-04-16 06:27:38 -07:00
|
|
|
|
|
|
|
return id;
|
2006-04-09 03:18:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
IdlerDel(Idler * id)
|
|
|
|
{
|
2013-06-06 13:37:06 -07:00
|
|
|
LIST_REMOVE(Idler, &idler_list, id);
|
2006-04-09 03:18:34 -07:00
|
|
|
Efree(id);
|
|
|
|
}
|
|
|
|
|
2008-03-22 23:49:50 -07:00
|
|
|
static void
|
|
|
|
_IdlerRun(void *_id, void *prm __UNUSED__)
|
|
|
|
{
|
|
|
|
Idler *id = (Idler *) _id;
|
|
|
|
|
2012-09-22 01:29:15 -07:00
|
|
|
if (EDebug(EDBUG_TYPE_IDLERS) > 1)
|
|
|
|
Eprintf("%s: func=%p\n", __func__, id->func);
|
2008-03-22 23:49:50 -07:00
|
|
|
id->func(id->data);
|
|
|
|
}
|
|
|
|
|
2006-04-09 03:18:34 -07:00
|
|
|
void
|
|
|
|
IdlersRun(void)
|
|
|
|
{
|
2013-06-06 13:37:06 -07:00
|
|
|
Idler *id;
|
|
|
|
|
2008-01-26 03:43:12 -08:00
|
|
|
if (EDebug(EDBUG_TYPE_IDLERS))
|
2012-09-22 01:29:15 -07:00
|
|
|
Eprintf("%s B\n", __func__);
|
2013-06-06 13:37:06 -07:00
|
|
|
LIST_FOR_EACH(Idler, &idler_list, id) _IdlerRun(id, NULL);
|
2012-09-22 01:29:15 -07:00
|
|
|
if (EDebug(EDBUG_TYPE_IDLERS))
|
|
|
|
Eprintf("%s E\n", __func__);
|
2006-04-09 03:18:34 -07:00
|
|
|
}
|