From: Andreas Fink Date: Sun, 15 Nov 2009 20:38:24 +0000 (+0000) Subject: *fix* added missing files X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Ftint2;a=commitdiff_plain;h=4ca207e8f31e8b608faf74eead20dd28eb91fc89 *fix* added missing files --- diff --git a/src/util/timer.c b/src/util/timer.c new file mode 100644 index 0000000..59064dd --- /dev/null +++ b/src/util/timer.c @@ -0,0 +1,67 @@ +/************************************************************************** +* +* Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com) +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License version 2 +* as published by the Free Software Foundation. +* +* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +**************************************************************************/ + +#include +#include +#include + +#include "timer.h" + +GSList* timer_list = 0; + +int install_timer(int value_sec, int value_nsec, int interval_sec, int interval_nsec, void (*_callback)()) +{ + if ( value_sec < 0 || interval_sec < 0 ) + return -1; + + int timer_fd; + struct itimerspec timer; + timer.it_value = (struct timespec){ .tv_sec=value_sec, .tv_nsec=value_nsec }; + timer.it_interval = (struct timespec){ .tv_sec=interval_sec, .tv_nsec=interval_nsec }; + timer_fd = timerfd_create(CLOCK_MONOTONIC, 0); + timerfd_settime(timer_fd, 0, &timer, 0); + struct timer* t = malloc(sizeof(struct timer)); + t->id=timer_fd; + t->_callback = _callback; + timer_list = g_slist_prepend(timer_list, t); + return timer_fd; +} + + +void reset_timer(int id, int value_sec, int value_nsec, int interval_sec, int interval_nsec) +{ + struct itimerspec timer; + timer.it_value = (struct timespec){ .tv_sec=value_sec, .tv_nsec=value_nsec }; + timer.it_interval = (struct timespec){ .tv_sec=interval_sec, .tv_nsec=interval_nsec }; + timerfd_settime(id, 0, &timer, 0); +} + + +void uninstall_timer(int id) +{ + close(id); + GSList* timer_iter = timer_list; + while (timer_iter) { + struct timer* t = timer_iter->data; + if (t->id == id) { + timer_list = g_slist_remove(timer_list, t); + free(t); + return; + } + timer_iter = timer_iter->next; + } +} diff --git a/src/util/timer.h b/src/util/timer.h new file mode 100644 index 0000000..fae0203 --- /dev/null +++ b/src/util/timer.h @@ -0,0 +1,48 @@ +/************************************************************************** +* +* Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com) +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License version 2 +* as published by the Free Software Foundation. +* +* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +**************************************************************************/ + + +#ifndef TIMER_H +#define TIMER_H + +#include + +extern GSList* timer_list; + + +struct timer { + int id; + void (*_callback)(); +}; + + +// timer functions +/** installs a timer with the first timeout 'value_sec' and then a periodic timeout with interval_sec + * '_callback' is the callback function when the timer reaches the timeout. + * If 'value_sec' == 0 then the timer is disabled (but not uninstalled) + * If 'interval_sec' == 0 the timer is a single shot timer, ie. no periodic timeout occur + * returns the 'id' of the timer, which is needed for uninstalling the timer **/ +int install_timer(int value_sec, int value_nsec, int interval_sec, int interval_nsec, void (*_callback)()); + +/** resets a timer to the new values. If 'id' does not exist nothing happens. + * If value_sec == 0 the timer is stopped **/ +void reset_timer(int id, int value_sec, int value_nsec, int interval_sec, int interval_nsec); + +/** uninstalls a timer with the given 'id'. If no timer is installed with this id nothing happens **/ +void uninstall_timer(int id); + +#endif // TIMER_H