1999-10-04 15:06:00 -07:00
|
|
|
/*
|
2006-01-06 23:20:58 -08:00
|
|
|
* Copyright (C) 2000-2006 Carsten Haitzler, Geoff Harrison and various contributors
|
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"
|
2003-11-09 15:17:15 -08:00
|
|
|
#include <sys/time.h>
|
1999-08-17 15:56:46 -07:00
|
|
|
|
1999-08-26 11:56:06 -07:00
|
|
|
double
|
1999-08-23 13:58:31 -07:00
|
|
|
GetTime(void)
|
|
|
|
{
|
|
|
|
struct timeval timev;
|
1999-08-26 11:56:06 -07:00
|
|
|
|
1999-08-23 13:58:31 -07:00
|
|
|
gettimeofday(&timev, NULL);
|
|
|
|
return (double)timev.tv_sec + (((double)timev.tv_usec) / 1000000);
|
|
|
|
}
|
|
|
|
|
1999-08-17 15:56:46 -07:00
|
|
|
static Qentry *q_first = NULL;
|
|
|
|
|
|
|
|
void
|
2004-03-20 17:39:40 -08:00
|
|
|
DoIn(const char *name, double in_time, void (*func) (int val, void *data),
|
1999-08-17 15:56:46 -07:00
|
|
|
int runtime_val, void *runtime_data)
|
|
|
|
{
|
|
|
|
Qentry *qe, *ptr, *pptr;
|
|
|
|
|
1999-09-21 19:13:51 -07:00
|
|
|
RemoveTimerEvent(name);
|
1999-08-17 15:56:46 -07:00
|
|
|
qe = Emalloc(sizeof(Qentry));
|
|
|
|
if (!qe)
|
2005-01-25 13:58:28 -08:00
|
|
|
return;
|
|
|
|
|
2006-01-08 08:13:21 -08:00
|
|
|
if (EventDebug(EDBUG_TYPE_EVENTS))
|
|
|
|
Eprintf("DoIn %8.3f: %s\n", in_time, name);
|
|
|
|
|
2004-02-28 07:14:02 -08:00
|
|
|
qe->name = Estrdup(name);
|
1999-08-17 15:56:46 -07:00
|
|
|
qe->func = func;
|
2006-01-08 08:13:21 -08:00
|
|
|
qe->at_time = GetTime() + in_time;
|
1999-08-17 15:56:46 -07:00
|
|
|
qe->runtime_val = runtime_val;
|
|
|
|
qe->runtime_data = runtime_data;
|
|
|
|
|
|
|
|
/* if there is no queue it becomes the queue */
|
|
|
|
if (!q_first)
|
2006-01-08 08:13:21 -08:00
|
|
|
{
|
|
|
|
q_first = qe;
|
|
|
|
qe->next = NULL;
|
|
|
|
}
|
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
|
|
|
{
|
2006-01-08 08:13:21 -08:00
|
|
|
if (ptr->at_time > qe->at_time)
|
|
|
|
break;
|
2003-05-22 12:15:03 -07:00
|
|
|
}
|
|
|
|
if (pptr)
|
|
|
|
pptr->next = qe;
|
|
|
|
else
|
|
|
|
q_first = qe;
|
2006-01-08 08:13:21 -08:00
|
|
|
qe->next = ptr;
|
2000-07-26 11:00:47 -07:00
|
|
|
}
|
1999-08-17 15:56:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Qentry *
|
|
|
|
GetHeadTimerQueue(void)
|
|
|
|
{
|
2005-01-25 13:58:28 -08:00
|
|
|
return q_first;
|
1999-08-17 15:56:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HandleTimerEvent(void)
|
|
|
|
{
|
|
|
|
Qentry *qe;
|
|
|
|
|
|
|
|
/* no timers - exit */
|
|
|
|
if (!q_first)
|
2005-01-25 13:58:28 -08:00
|
|
|
return;
|
1999-08-17 15:56:46 -07:00
|
|
|
|
|
|
|
/* get the first timer */
|
|
|
|
qe = q_first;
|
|
|
|
/* remove it */
|
|
|
|
q_first = q_first->next;
|
|
|
|
/* run this callback */
|
2006-01-08 08:13:21 -08:00
|
|
|
qe->func(qe->runtime_val, qe->runtime_data);
|
1999-08-17 15:56:46 -07:00
|
|
|
/* free the timer */
|
|
|
|
if (qe->name)
|
|
|
|
Efree(qe->name);
|
|
|
|
if (qe)
|
|
|
|
Efree(qe);
|
|
|
|
}
|
|
|
|
|
2005-03-06 13:29:46 -08:00
|
|
|
int
|
2004-03-20 17:39:40 -08:00
|
|
|
RemoveTimerEvent(const char *name)
|
1999-08-17 15:56:46 -07:00
|
|
|
{
|
|
|
|
Qentry *qe, *ptr, *pptr;
|
|
|
|
|
|
|
|
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;
|
2006-01-08 08:13:21 -08:00
|
|
|
if (strcmp(qe->name, name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Match - remove it from the queue */
|
|
|
|
if (pptr)
|
|
|
|
pptr->next = qe->next;
|
|
|
|
else
|
|
|
|
q_first = qe->next;
|
|
|
|
/* free it */
|
|
|
|
if (qe->name)
|
|
|
|
Efree(qe->name);
|
|
|
|
if (qe)
|
|
|
|
Efree(qe);
|
|
|
|
/* done */
|
|
|
|
return 1;
|
2000-07-26 11:00:47 -07:00
|
|
|
}
|
2005-03-06 13:29:46 -08:00
|
|
|
|
|
|
|
return 0;
|
1999-08-17 15:56:46 -07:00
|
|
|
}
|