2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
18 #include <sys/timerfd.h>
23 #include "task-utils.h"
25 struct task_info *task_init(void *(*threadfn)(void *), int (*postfn)(void *),
28 struct task_info *info = calloc(1, sizeof(struct task_info));
33 info->private_data = thread_private;
34 info->threadfn = threadfn;
35 info->postfn = postfn;
40 int task_start(struct task_info *info)
50 ret = pthread_create(&info->id, NULL, info->threadfn,
54 pthread_detach(info->id);
61 void task_stop(struct task_info *info)
66 if (info->periodic.timer_fd)
67 close(info->periodic.timer_fd);
70 pthread_cancel(info->id);
73 info->postfn(info->private_data);
76 void task_deinit(struct task_info *info)
84 int task_period_start(struct task_info *info, unsigned int period_ms)
88 struct itimerspec itval;
93 info->periodic.timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
94 if (info->periodic.timer_fd == -1)
95 return info->periodic.timer_fd;
97 info->periodic.wakeups_missed = 0;
99 sec = period_ms / 1000;
100 ns = (period_ms - (sec * 1000)) * 1000;
101 itval.it_interval.tv_sec = sec;
102 itval.it_interval.tv_nsec = ns;
103 itval.it_value.tv_sec = sec;
104 itval.it_value.tv_nsec = ns;
106 return timerfd_settime(info->periodic.timer_fd, 0, &itval, NULL);
109 void task_period_wait(struct task_info *info)
111 unsigned long long missed;
117 ret = read(info->periodic.timer_fd, &missed, sizeof (missed));
122 info->periodic.wakeups_missed += (missed - 1);
125 void task_period_stop(struct task_info *info)
130 if (info->periodic.timer_fd) {
131 timerfd_settime(info->periodic.timer_fd, 0, NULL, NULL);
132 close(info->periodic.timer_fd);