btrfs-progs: check: enhanced progress indicator
[btrfs-progs-unstable/devel.git] / task-utils.c
bloba9bee8f4486d8ccdd902f23f7435442b1e7b6c04
1 /*
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.
17 #include <pthread.h>
18 #include <sys/timerfd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <time.h>
24 #include "task-utils.h"
26 struct task_info *task_init(void *(*threadfn)(void *), int (*postfn)(void *),
27 void *thread_private)
29 struct task_info *info = calloc(1, sizeof(struct task_info));
31 if (!info)
32 return NULL;
34 info->private_data = thread_private;
35 info->threadfn = threadfn;
36 info->postfn = postfn;
38 return info;
41 int task_start(struct task_info *info, time_t *start_time, u64 *item_count)
43 int ret;
45 if (!info)
46 return -1;
48 if (!info->threadfn)
49 return -1;
51 if (start_time)
52 *start_time = time(NULL);
53 if (item_count)
54 *item_count = 0;
56 ret = pthread_create(&info->id, NULL, info->threadfn,
57 info->private_data);
59 if (ret)
60 info->id = 0;
62 return ret;
65 void task_stop(struct task_info *info)
67 if (!info)
68 return;
70 if (info->id > 0) {
71 pthread_cancel(info->id);
72 pthread_join(info->id, NULL);
73 info->id = 0;
76 if (info->periodic.timer_fd) {
77 close(info->periodic.timer_fd);
78 info->periodic.timer_fd = 0;
81 if (info->postfn)
82 info->postfn(info->private_data);
85 void task_deinit(struct task_info *info)
87 if (!info)
88 return;
90 free(info);
93 int task_period_start(struct task_info *info, unsigned int period_ms)
95 unsigned int ns;
96 unsigned int sec;
97 struct itimerspec itval;
99 if (!info)
100 return -1;
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;
123 int ret;
125 if (!info)
126 return;
128 if (info->periodic.timer_fd == 0)
129 return;
131 ret = read(info->periodic.timer_fd, &missed, sizeof (missed));
132 if (ret == -1)
133 return;
135 if (missed > 0)
136 info->periodic.wakeups_missed += (missed - 1);
139 void task_period_stop(struct task_info *info)
141 if (!info)
142 return;
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;