2023-08-28 12:31:18 +02:00
|
|
|
/* Run tasks periodically.
|
2018-08-22 11:29:39 +02:00
|
|
|
*
|
2023-08-31 11:17:07 +02:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2023-08-28 12:31:18 +02:00
|
|
|
*/
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-06-23 16:26:44 +02:00
|
|
|
#include <cstdint>
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <cstdio>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2019-06-23 16:26:44 +02:00
|
|
|
#include <ctime>
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-04-03 10:00:02 +00:00
|
|
|
// We can choose between two periodic task implementations
|
2018-08-22 11:29:39 +02:00
|
|
|
//#define PERIODIC_TASK_IMPL NANOSLEEP
|
2023-09-07 13:19:19 +02:00
|
|
|
#define TIMERFD 1
|
|
|
|
#define CLOCK_NANOSLEEP 2
|
|
|
|
#define NANOSLEEP 3
|
|
|
|
#define RDTSC 4
|
2018-08-22 11:29:39 +02:00
|
|
|
|
|
|
|
#if defined(__MACH__)
|
2023-09-07 13:19:19 +02:00
|
|
|
#define PERIODIC_TASK_IMPL NANOSLEEP
|
2018-08-22 11:29:39 +02:00
|
|
|
#elif defined(__linux__)
|
2023-09-07 13:19:19 +02:00
|
|
|
#define PERIODIC_TASK_IMPL TIMERFD
|
2018-08-22 11:29:39 +02:00
|
|
|
#else
|
2023-09-07 13:19:19 +02:00
|
|
|
#error "Platform not supported"
|
2018-08-22 11:29:39 +02:00
|
|
|
#endif
|
|
|
|
|
2018-12-02 02:49:53 +01:00
|
|
|
#if PERIODIC_TASK_IMPL == RDTSC
|
2023-09-07 13:19:19 +02:00
|
|
|
#include <villas/tsc.hpp>
|
2018-12-02 02:49:53 +01:00
|
|
|
#endif
|
|
|
|
|
2019-10-27 20:23:47 +01:00
|
|
|
struct Task {
|
2023-09-07 13:19:19 +02:00
|
|
|
int clock; // CLOCK_{MONOTONIC,REALTIME}
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
#if PERIODIC_TASK_IMPL == RDTSC // We use cycle counts in RDTSC mode
|
|
|
|
uint64_t period;
|
|
|
|
uint64_t next;
|
2018-08-22 11:29:39 +02:00
|
|
|
#else
|
2023-09-07 13:19:19 +02:00
|
|
|
struct timespec period; // The period of periodic invations of this task
|
|
|
|
struct timespec next; // The timer value for the next invocation
|
2018-08-22 11:29:39 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if PERIODIC_TASK_IMPL == TIMERFD
|
2023-09-07 13:19:19 +02:00
|
|
|
int fd; // The timerfd_create(2) file descriptior.
|
2018-08-22 11:29:39 +02:00
|
|
|
#elif PERIODIC_TASK_IMPL == RDTSC
|
2024-05-27 08:42:13 +02:00
|
|
|
struct Tsc tsc; // Initialized by tsc_init().
|
2018-08-22 11:29:39 +02:00
|
|
|
#endif
|
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Create a new task with the given rate.
|
|
|
|
Task(int clock = CLOCK_REALTIME);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
~Task();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Wait until task elapsed
|
|
|
|
//
|
|
|
|
// @retval 0 An error occured. Maybe the task was stopped.
|
|
|
|
// @retval >0 The nummer of runs this task already fired.
|
|
|
|
uint64_t wait();
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void setNext(const struct timespec *next);
|
|
|
|
void setTimeout(double to);
|
|
|
|
void setRate(double rate);
|
2018-08-22 11:29:39 +02:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
void stop();
|
2019-10-27 20:23:47 +01:00
|
|
|
|
2023-09-07 13:19:19 +02:00
|
|
|
// Returns a poll'able file descriptor which becomes readable when the timer expires.
|
|
|
|
//
|
|
|
|
// Note: currently not supported on all platforms.
|
|
|
|
int getFD() const;
|
2019-10-27 20:23:47 +01:00
|
|
|
};
|