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>
24 #include "task-utils.h"
26 struct task_info
*task_init(void *(*threadfn
)(void *), int (*postfn
)(void *),
29 struct task_info
*info
= calloc(1, sizeof(struct task_info
));
34 info
->private_data
= thread_private
;
35 info
->threadfn
= threadfn
;
36 info
->postfn
= postfn
;
41 int task_start(struct task_info
*info
, time_t *start_time
, u64
*item_count
)
52 *start_time
= time(NULL
);
56 ret
= pthread_create(&info
->id
, NULL
, info
->threadfn
,
65 void task_stop(struct task_info
*info
)
71 pthread_cancel(info
->id
);
72 pthread_join(info
->id
, NULL
);
76 if (info
->periodic
.timer_fd
) {
77 close(info
->periodic
.timer_fd
);
78 info
->periodic
.timer_fd
= 0;
82 info
->postfn(info
->private_data
);
85 void task_deinit(struct task_info
*info
)
93 int task_period_start(struct task_info
*info
, unsigned int period_ms
)
97 struct itimerspec itval
;
102 info
->periodic
.timer_fd
= timerfd_create(CLOCK_MONOTONIC
, 0);
103 if (info
->periodic
.timer_fd
== -1) {
104 info
->periodic
.timer_fd
= 0;
105 return info
->periodic
.timer_fd
;
108 info
->periodic
.wakeups_missed
= 0;
110 sec
= period_ms
/ 1000;
111 ns
= (period_ms
- (sec
* 1000)) * 1000 * 1000;
112 itval
.it_interval
.tv_sec
= sec
;
113 itval
.it_interval
.tv_nsec
= ns
;
114 itval
.it_value
.tv_sec
= sec
;
115 itval
.it_value
.tv_nsec
= ns
;
117 return timerfd_settime(info
->periodic
.timer_fd
, 0, &itval
, NULL
);
120 void task_period_wait(struct task_info
*info
)
122 unsigned long long missed
;
128 if (info
->periodic
.timer_fd
== 0)
131 ret
= read(info
->periodic
.timer_fd
, &missed
, sizeof (missed
));
136 info
->periodic
.wakeups_missed
+= (missed
- 1);
139 void task_period_stop(struct task_info
*info
)
144 if (info
->periodic
.timer_fd
) {
145 timerfd_settime(info
->periodic
.timer_fd
, 0, NULL
, NULL
);
146 close(info
->periodic
.timer_fd
);
147 info
->periodic
.timer_fd
= -1;