Commit 558f0fa2 authored by Joanne Hugé's avatar Joanne Hugé

Add polling timerthread

parent 52325bb4
......@@ -7,7 +7,7 @@ SRCS = main.c
OBJS = $(SRCS:%.c=%.o)
CFLAGS = -Og -g -Wall -Werror -Wextra
CFLAGS = -Og -g -Wall -Wextra
CFLAGS += -MD -MP
CFLAGS += -I $(SRCDIR)
CFLAGS += -std=gnu99
......
......@@ -59,6 +59,55 @@ static inline struct timespec add_ns(struct timespec t, int64_t ns) {
return ret;
}
// Real-time thread
// Sends packets at a regular intervall
static void *timerthread_poll(void *p) {
struct timespec next;
struct timespec current;
struct sched_param priority;
thread_param_t * param = (thread_param_t *)p;
thread_stat_t * stat = &param->stat;
priority.sched_priority = param->priority;
int err = sched_setscheduler(0, SCHED_FIFO, &priority);
if (err)
error(EXIT_FAILURE, errno, "Couldn't set priority");
stat->max_diff = 0;
stat->min_diff = 1000000;
clock_gettime(CLOCK_ID, &next);
next = add_ns(next, param->interval);
for(stat->nb_cycles = 0;; stat->nb_cycles++) {
if(param->max_cycles)
if(stat->nb_cycles >= param->max_cycles)
break;
for(;;) {
clock_gettime(CLOCK_ID, &current);
if( current.tv_nsec == next.tv_nsec)
break;
}
int64_t diff = diff_ns(current, next);
stat->max_diff = max(stat->max_diff, diff);
stat->min_diff = min(stat->min_diff, diff);
next = add_ns(current, param->interval);
}
printf("Done\n");
return NULL;
}
// Real-time thread
// Sends packets at a regular intervall
......@@ -154,7 +203,7 @@ int main(int argc, char *argv[]) {
sleep(1);
pthread_create(&thread, NULL, timerthread, (void *)&param);
pthread_create(&thread, NULL, timerthread_poll, (void *)&param);
sleep(10);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment