-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoinable.h
92 lines (77 loc) · 2.48 KB
/
joinable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Joinable wrapper for kthread.
*
* Copyright (c) 2020-2023 Jiansheng Qiu <[email protected]>
*
* This file is licensed under the terms of the GNU General Public
* License version 2. This program is licensed "as is" without any
* warranty of any kind, whether express or implied.
*/
#ifndef __JOINABLE_H
#define __JOINABLE_H
#include <linux/fs.h>
#include <linux/kthread.h>
struct joinable_kthread {
// The caller should set these
int (*threadfn)(void *data);
void *data;
// The user should not touch these
struct task_struct *task;
struct completion entered;
};
int __joinable_kthreadfn(void *data);
// The caller should set t->threadfn and t->data
// TODO: Make this an ordinary function if kthread_create has a version that
// accepts va_list to hide __joinable_kthreadfn
#define joinable_kthread_create(t, namefmt, ...) ({ \
int ret; \
typecheck(struct joinable_kthread *, (t)); \
init_completion(&(t)->entered); \
(t)->task = kthread_create(__joinable_kthreadfn, (t), (namefmt), \
__VA_ARGS__); \
if (IS_ERR((t)->task)) \
ret = PTR_ERR((t)->task); \
else \
ret = 0; \
ret; \
})
static inline void joinable_kthread_bind(struct joinable_kthread *t,
unsigned int cpu)
{
kthread_bind(t->task, cpu);
}
static inline int joinable_kthread_stop(struct joinable_kthread *t)
{
return kthread_stop(t->task);
}
static inline void joinable_kthread_abort(struct joinable_kthread *t)
{
BUG_ON(joinable_kthread_stop(t) != -EINTR);
}
static inline void joinable_kthread_wake_up(struct joinable_kthread *t)
{
wake_up_process(t->task);
}
// The caller should make sure that the thread has been waken up.
static inline int __joinable_kthread_join(struct joinable_kthread *t)
{
wait_for_completion(&t->entered);
return kthread_stop(t->task);
}
int joinable_kthreads_create(struct joinable_kthread *ts, int num,
const char *basename);
// The caller should set threadfn and data of ts[0..num]
int joinable_kthreads_run(struct joinable_kthread *ts, int num,
const char *basename);
// The caller should make sure that the threads has been waken up.
int __joinable_kthreads_join_check_lt_zero(struct joinable_kthread *ts, int num,
const char *basename);
static inline int joinable_kthreads_run_join_check_lt_zero(
struct joinable_kthread *ts, int num, const char *basename)
{
int ret = joinable_kthreads_run(ts, num, basename);
if (ret < 0)
return ret;
return __joinable_kthreads_join_check_lt_zero(ts, num, basename);
}
#endif // __JOINABLE_H