mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
use timerfd instead of timer objects to simplify code
This commit is contained in:
parent
9e0153f0f8
commit
90db947fa4
1 changed files with 22 additions and 43 deletions
|
@ -8,31 +8,15 @@
|
|||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <sys/timerfd.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "utils.h"
|
||||
#include "msg.h"
|
||||
|
||||
#define CLOCKID CLOCK_REALTIME
|
||||
#define SIG SIGRTMIN
|
||||
|
||||
void tick(int sig, siginfo_t *si, void *ptr)
|
||||
{
|
||||
struct msg *m = (struct msg *) si->si_value.sival_ptr;
|
||||
|
||||
msg_random(m);
|
||||
msg_fprint(stdout, m);
|
||||
fflush(stdout);
|
||||
|
||||
m->sequence++;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
@ -49,44 +33,39 @@ int main(int argc, char *argv[])
|
|||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
uint64_t runs;
|
||||
int rate = atoi(argv[2]);
|
||||
struct msg m = MSG_INIT(atoi(argv[1]));
|
||||
|
||||
/* Setup signals */
|
||||
struct sigaction sa_tick = {
|
||||
.sa_flags = SA_SIGINFO,
|
||||
.sa_sigaction = tick
|
||||
};
|
||||
|
||||
sigemptyset(&sa_tick.sa_mask);
|
||||
sigaction(SIG, &sa_tick, NULL);
|
||||
|
||||
/* Setup timer */
|
||||
timer_t t;
|
||||
struct sigevent sev = {
|
||||
.sigev_notify = SIGEV_SIGNAL,
|
||||
.sigev_signo = SIG,
|
||||
.sigev_value.sival_ptr = &m
|
||||
};
|
||||
|
||||
double period = 1.0 / rate;
|
||||
long sec = floor(period);
|
||||
long nsec = (period - sec) * 1000000000;
|
||||
|
||||
struct itimerspec its = {
|
||||
.it_interval = { sec, nsec },
|
||||
.it_value = { 0, 1 }
|
||||
.it_interval = timespec_rate(rate),
|
||||
.it_value = { 1, 0 }
|
||||
};
|
||||
|
||||
int tfd = timerfd_create(CLOCK_REALTIME, 0);
|
||||
if (tfd < 0)
|
||||
serror("Failed to create timer");
|
||||
|
||||
if (timerfd_settime(tfd, 0, &its, NULL))
|
||||
serror("Failed to start timer");
|
||||
|
||||
/* Print header */
|
||||
fprintf(stderr, "# %-6s%-12s\n", "seq", "data");
|
||||
|
||||
timer_create(CLOCKID, &sev, &t);
|
||||
timer_settime(t, 0, &its, NULL);
|
||||
while (1) {
|
||||
/* Block until 1/p->rate seconds elapsed */
|
||||
read(tfd, &runs, sizeof(runs));
|
||||
|
||||
msg_random(&m);
|
||||
msg_fprint(stdout, &m);
|
||||
|
||||
fflush(stdout);
|
||||
|
||||
while (1) pause();
|
||||
m.sequence++;
|
||||
}
|
||||
|
||||
timer_delete(t);
|
||||
close(tfd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue