2017-08-14 14:35:16 +02:00
|
|
|
/** Run tasks periodically.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* 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 3 of the License, or
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/task.h>
|
|
|
|
#include <villas/timing.h>
|
2017-08-14 14:35:16 +02:00
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
#include <sys/timerfd.h>
|
|
|
|
#endif
|
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
int task_init(struct task *t, double rate, int clock)
|
2017-08-14 14:35:16 +02:00
|
|
|
{
|
2017-09-05 16:41:05 +02:00
|
|
|
int ret;
|
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
t->clock = clock;
|
2017-09-05 16:41:05 +02:00
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
t->fd = timerfd_create(t->clock, 0);
|
|
|
|
if (t->fd < 0)
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = task_set_rate(t, rate);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
int task_set_timeout(struct task *t, double to)
|
|
|
|
{
|
|
|
|
struct timespec now;
|
|
|
|
|
|
|
|
clock_gettime(t->clock, &now);
|
|
|
|
|
|
|
|
struct timespec timeout = time_from_double(to);
|
|
|
|
struct timespec next = time_add(&now, &timeout);
|
|
|
|
|
|
|
|
return task_set_next(t, &next);
|
|
|
|
}
|
|
|
|
|
|
|
|
int task_set_next(struct task *t, struct timespec *next)
|
|
|
|
{
|
|
|
|
t->next = *next;
|
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
int ret;
|
|
|
|
struct itimerspec its = {
|
|
|
|
.it_interval = (struct timespec) { 0, 0 },
|
|
|
|
.it_value = t->next
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = timerfd_settime(t->fd, TFD_TIMER_ABSTIME, &its, NULL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-05 16:41:05 +02:00
|
|
|
int task_set_rate(struct task *t, double rate)
|
|
|
|
{
|
2017-09-16 15:33:01 +02:00
|
|
|
/* A rate of 0 will disarm the timer */
|
2017-08-20 10:48:44 +02:00
|
|
|
t->period = rate ? time_from_double(1.0 / rate) : (struct timespec) { 0, 0 };
|
2017-08-14 14:35:16 +02:00
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP
|
2017-09-16 15:33:01 +02:00
|
|
|
struct timespec now, next;
|
2017-09-05 16:41:05 +02:00
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
clock_gettime(t->clock, &now);
|
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
next = time_add(&now, &t->period);
|
|
|
|
|
2017-12-08 13:10:08 +08:00
|
|
|
return task_set_next(t, &next);
|
2017-08-14 14:35:16 +02:00
|
|
|
#elif PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
int ret;
|
|
|
|
struct itimerspec its = {
|
|
|
|
.it_interval = t->period,
|
|
|
|
.it_value = t->period
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = timerfd_settime(t->fd, 0, &its, NULL);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
int task_destroy(struct task *t)
|
2017-08-14 14:35:16 +02:00
|
|
|
{
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
return close(t->fd);
|
|
|
|
#endif
|
2017-09-05 16:41:05 +02:00
|
|
|
|
2017-08-14 14:35:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP
|
|
|
|
static int time_lt(const struct timespec *lhs, const struct timespec *rhs)
|
|
|
|
{
|
|
|
|
if (lhs->tv_sec == rhs->tv_sec)
|
|
|
|
return lhs->tv_nsec < rhs->tv_nsec;
|
|
|
|
else
|
|
|
|
return lhs->tv_sec < rhs->tv_sec;
|
2017-09-05 16:41:05 +02:00
|
|
|
|
2017-08-14 14:35:16 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
uint64_t task_wait(struct task *t)
|
2017-08-14 14:35:16 +02:00
|
|
|
{
|
|
|
|
uint64_t runs;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP || PERIODIC_TASK_IMPL == NANOSLEEP
|
|
|
|
struct timespec now;
|
2017-09-05 16:41:05 +02:00
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
#if PERIODIC_TASK_IMPL == CLOCK_NANOSLEEP
|
|
|
|
do {
|
|
|
|
ret = clock_nanosleep(t->clock, TIMER_ABSTIME, &t->next, NULL);
|
|
|
|
} while (ret == EINTR);
|
|
|
|
#elif PERIODIC_TASK_IMPL == NANOSLEEP
|
|
|
|
struct timespec delta;
|
2017-08-14 14:35:16 +02:00
|
|
|
|
2017-08-20 10:48:44 +02:00
|
|
|
ret = clock_gettime(t->clock, &now);
|
2017-08-14 14:35:16 +02:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
delta = time_diff(&now, &t->next);
|
2017-08-14 14:35:16 +02:00
|
|
|
|
|
|
|
ret = nanosleep(&delta, NULL);
|
2017-09-16 15:33:01 +02:00
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
2017-09-05 16:41:05 +02:00
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
ret = clock_gettime(t->clock, &now);
|
2017-08-14 14:35:16 +02:00
|
|
|
if (ret)
|
|
|
|
return 0;
|
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
for (runs = 0; time_lt(&t->next, &now); runs++)
|
|
|
|
t->next = time_add(&t->next, &t->period);
|
|
|
|
#elif PERIODIC_TASK_IMPL == TIMERFD
|
2017-08-14 14:35:16 +02:00
|
|
|
ret = read(t->fd, &runs, sizeof(runs));
|
|
|
|
if (ret < 0)
|
2017-09-16 15:33:01 +02:00
|
|
|
return 0;
|
2017-08-14 14:35:16 +02:00
|
|
|
#else
|
|
|
|
#error "Invalid period task implementation"
|
|
|
|
#endif
|
2017-09-05 16:41:05 +02:00
|
|
|
|
2017-09-16 15:33:01 +02:00
|
|
|
return runs;
|
2017-08-30 00:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int task_fd(struct task *t)
|
|
|
|
{
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
|
|
|
return t->fd;
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2017-09-05 16:41:05 +02:00
|
|
|
}
|