btrfs-progs: resst info->periodic.timer_fd's value after free
[platform/upstream/btrfs-progs.git] / task-utils.c
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.
5  *
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.
10  *
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.
15  */
16
17 #include <pthread.h>
18 #include <sys/timerfd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22
23 #include "task-utils.h"
24
25 struct task_info *task_init(void *(*threadfn)(void *), int (*postfn)(void *),
26                             void *thread_private)
27 {
28         struct task_info *info = calloc(1, sizeof(struct task_info));
29
30         if (!info)
31                 return NULL;
32
33         info->private_data = thread_private;
34         info->threadfn = threadfn;
35         info->postfn = postfn;
36
37         return info;
38 }
39
40 int task_start(struct task_info *info)
41 {
42         int ret;
43
44         if (!info)
45                 return -1;
46
47         if (!info->threadfn)
48                 return -1;
49
50         ret = pthread_create(&info->id, NULL, info->threadfn,
51                              info->private_data);
52
53         if (ret)
54                 info->id = -1;
55
56         return ret;
57 }
58
59 void task_stop(struct task_info *info)
60 {
61         if (!info)
62                 return;
63
64         if (info->id > 0) {
65                 pthread_cancel(info->id);
66                 pthread_join(info->id, NULL);
67                 info->id = -1;
68         }
69
70         if (info->periodic.timer_fd) {
71                 close(info->periodic.timer_fd);
72                 info->periodic.timer_fd = 0;
73         }
74
75         if (info->postfn)
76                 info->postfn(info->private_data);
77 }
78
79 void task_deinit(struct task_info *info)
80 {
81         if (!info)
82                 return;
83
84         free(info);
85 }
86
87 int task_period_start(struct task_info *info, unsigned int period_ms)
88 {
89         unsigned int ns;
90         unsigned int sec;
91         struct itimerspec itval;
92
93         if (!info)
94                 return -1;
95
96         info->periodic.timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
97         if (info->periodic.timer_fd == -1)
98                 return info->periodic.timer_fd;
99
100         info->periodic.wakeups_missed = 0;
101
102         sec = period_ms / 1000;
103         ns = (period_ms - (sec * 1000)) * 1000;
104         itval.it_interval.tv_sec = sec;
105         itval.it_interval.tv_nsec = ns;
106         itval.it_value.tv_sec = sec;
107         itval.it_value.tv_nsec = ns;
108
109         return timerfd_settime(info->periodic.timer_fd, 0, &itval, NULL);
110 };
111
112 void task_period_wait(struct task_info *info)
113 {
114         unsigned long long missed;
115         int ret;
116
117         if (!info)
118                 return;
119
120         ret = read(info->periodic.timer_fd, &missed, sizeof (missed));
121         if (ret == -1)
122                 return;
123
124         if (missed > 0)
125                 info->periodic.wakeups_missed += (missed - 1);
126 }
127
128 void task_period_stop(struct task_info *info)
129 {
130         if (!info)
131                 return;
132
133         if (info->periodic.timer_fd) {
134                 timerfd_settime(info->periodic.timer_fd, 0, NULL, NULL);
135                 close(info->periodic.timer_fd);
136                 info->periodic.timer_fd = -1;
137         }
138 }