/* $Id$ * $URL$ * * generic timer handling * * Copyright (C) 2003, 2004 Michael Reinelt * Copyright (C) 2004 The LCD4Linux Team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * exported functions: * * int timer_add (void(*callback)(void *data), void *data, int interval, int one_shot); * adds a timer to the queue * * int timer_process (struct timespec *delay); * process timer queue * * void timer_exit(); * release all timers * */ #include "config.h" #include #include #include #include #include #include "debug.h" #include "cfg.h" #include "timer.h" #ifdef WITH_DMALLOC #include #endif typedef struct TIMER { void (*callback) (void *data); void *data; struct timeval when; int interval; int one_shot; int active; } TIMER; TIMER *Timers = NULL; int nTimers = 0; static void timer_inc(struct timeval *tv, const int msec) { tv->tv_sec += msec / 1000; tv->tv_usec += (msec - 1000 * (msec / 1000)) * 1000; if (tv->tv_usec >= 1000000) { tv->tv_usec -= 1000000; tv->tv_sec++; } } int timer_add(void (*callback) (void *data), void *data, const int interval, const int one_shot) { int i; struct timeval now; /* find a free slot */ for (i = 0; i < nTimers; i++) { if (Timers[i].active == 0) break; } /* none found, allocate a new slot */ if (i >= nTimers) { nTimers++; Timers = realloc(Timers, nTimers * sizeof(*Timers)); } gettimeofday(&now, NULL); /* fill slot */ Timers[i].callback = callback; Timers[i].data = data; Timers[i].when = now; Timers[i].interval = interval; Timers[i].one_shot = one_shot; Timers[i].active = 1; /* if one-shot timer, don't fire now */ if (one_shot) { timer_inc(&Timers[i].when, interval); } return 0; } int timer_process(struct timespec *delay) { int i, flag, min; struct timeval now; /* the current moment */ gettimeofday(&now, NULL); /* sanity check */ if (nTimers == 0) { error("huh? not one single timer to process? dazed and confused..."); return -1; } /* process expired timers */ flag = 0; for (i = 0; i < nTimers; i++) { if (Timers[i].active == 0) continue; if (timercmp(&Timers[i].when, &now, <=)) { flag = 1; /* callback */ if (Timers[i].callback != NULL) { Timers[i].callback(Timers[i].data); } /* respawn or delete timer */ if (Timers[i].one_shot) { Timers[i].active = 0; } else { Timers[i].when = now; timer_inc(&Timers[i].when, Timers[i].interval); } } } /* find next timer */ flag = 1; min = -1; for (i = 0; i < nTimers; i++) { if (Timers[i].active == 0) continue; if (flag || timercmp(&Timers[i].when, &Timers[min].when, <)) { flag = 0; min = i; } } if (min < 0) { error("huh? not one single timer left? dazed and confused..."); return -1; } /* delay until next timer event */ delay->tv_sec = Timers[min].when.tv_sec - now.tv_sec; delay->tv_nsec = Timers[min].when.tv_usec - now.tv_usec; if (delay->tv_nsec < 0) { delay->tv_sec--; delay->tv_nsec += 1000000; } /* nanoseconds!! */ delay->tv_nsec *= 1000; return 0; } void timer_exit(void) { nTimers = 0; if (Timers != NULL) { free(Timers);; Timers = NULL; } } 'deletions'>-238/+0 2005-05-05[lcd4linux @ 2005-05-05 08:36:12 by reinelt]reinelt6-15/+34 2005-05-04[lcd4linux @ 2005-05-04 07:18:44 by obconseil]obconseil1-36/+84 2005-05-04[lcd4linux @ 2005-05-04 06:13:05 by reinelt]reinelt2-12/+77 2005-05-04[lcd4linux @ 2005-05-04 05:42:37 by reinelt]reinelt7-23/+398 2005-05-04[lcd4linux @ 2005-05-04 05:22:12 by reinelt]reinelt1-28/+31 2005-05-03[lcd4linux @ 2005-05-03 11:13:23 by reinelt]reinelt6-30/+122 2005-05-02[lcd4linux @ 2005-05-02 10:29:20 by reinelt]reinelt9-30/+636 2005-05-02[lcd4linux @ 2005-05-02 05:15:46 by reinelt]reinelt4-9/+51 2005-04-30[lcd4linux @ 2005-04-30 06:02:09 by reinelt]reinelt3-15/+44 2005-04-24[lcd4linux @ 2005-04-24 05:27:09 by reinelt]reinelt2-7/+38 2005-04-24[lcd4linux @ 2005-04-24 04:41:28 by reinelt]reinelt1-0/+68 2005-04-24[lcd4linux @ 2005-04-24 04:33:46 by reinelt]reinelt9-10/+444 2005-04-20[lcd4linux @ 2005-04-20 05:49:21 by reinelt]reinelt1-44/+125 2005-04-09[lcd4linux @ 2005-04-09 07:36:42 by reinelt]reinelt2-17/+99 2005-04-05[lcd4linux @ 2005-04-05 06:57:39 by reinelt]reinelt3-34/+34 2005-04-05[lcd4linux @ 2005-04-05 05:12:48 by reinelt]reinelt4-407/+33 2005-04-05[lcd4linux @ 2005-04-05 04:46:06 by reinelt]reinelt1-1/+20 2005-04-04[lcd4linux @ 2005-04-04 20:11:14 by nicowallmeier]nicowallmeier1-2/+6 2005-04-03[lcd4linux @ 2005-04-03 07:07:43 by reinelt]reinelt8-9/+152 2005-04-02[lcd4linux @ 2005-04-02 05:28:58 by reinelt]reinelt3-19/+27 2005-04-01[lcd4linux @ 2005-04-01 05:16:04 by reinelt]reinelt5-584/+694 2005-03-30[lcd4linux @ 2005-03-30 04:57:50 by reinelt]reinelt3-23/+67 2005-03-28[lcd4linux @ 2005-03-28 22:29:23 by reinelt]reinelt1-250/+346 2005-03-28[lcd4linux @ 2005-03-28 19:39:14 by reinelt]reinelt9-256/+1147 2005-03-25[lcd4linux @ 2005-03-25 15:44:43 by reinelt]reinelt1-2/+5 2005-03-23[lcd4linux @ 2005-03-23 12:23:35 by reinelt]reinelt1-27/+32 2005-02-24[lcd4linux @ 2005-02-24 07:07:55 by reinelt]reinelt1-0/+53 2005-02-24[lcd4linux @ 2005-02-24 07:06:45 by reinelt]reinelt11-28/+347 2005-02-24[lcd4linux @ 2005-02-24 06:51:40 by reinelt]reinelt1-4/+5 2005-01-30[lcd4linux @ 2005-01-30 06:43:22 by reinelt]reinelt4-69/+95 2005-01-29[lcd4linux @ 2005-01-29 09:30:56 by reinelt]reinelt1-8/+11 2005-01-22[lcd4linux @ 2005-01-22 22:57:57 by reinelt]reinelt9-47/+382 2005-01-22[lcd4linux @ 2005-01-22 12:44:41 by reinelt]reinelt1-3/+6 2005-01-18[lcd4linux @ 2005-01-18 06:30:21 by reinelt]reinelt89-261/+603 2005-01-17[lcd4linux @ 2005-01-17 06:38:48 by reinelt]reinelt1-1/+6 2005-01-17[lcd4linux @ 2005-01-17 06:29:24 by reinelt]reinelt3-33/+85