Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef __LINUX_COMPLETION_H |
3 | #define __LINUX_COMPLETION_H | |
4 | ||
5 | /* | |
6 | * (C) Copyright 2001 Linus Torvalds | |
7 | * | |
8 | * Atomic wait-for-completion handler data structures. | |
b8a21626 | 9 | * See kernel/sched/completion.c for details. |
1da177e4 LT |
10 | */ |
11 | ||
12 | #include <linux/wait.h> | |
ea3f2c0f | 13 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
cd8084f9 BP |
14 | #include <linux/lockdep.h> |
15 | #endif | |
1da177e4 | 16 | |
ee2f154a | 17 | /* |
65eb3dc6 KD |
18 | * struct completion - structure used to maintain state for a "completion" |
19 | * | |
20 | * This is the opaque structure used to maintain the state for a "completion". | |
21 | * Completions currently use a FIFO to queue threads that have to wait for | |
22 | * the "completion" event. | |
23 | * | |
24 | * See also: complete(), wait_for_completion() (and friends _timeout, | |
25 | * _interruptible, _interruptible_timeout, and _killable), init_completion(), | |
c32f74ab WS |
26 | * reinit_completion(), and macros DECLARE_COMPLETION(), |
27 | * DECLARE_COMPLETION_ONSTACK(). | |
65eb3dc6 | 28 | */ |
1da177e4 LT |
29 | struct completion { |
30 | unsigned int done; | |
31 | wait_queue_head_t wait; | |
ea3f2c0f | 32 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
cd8084f9 BP |
33 | struct lockdep_map_cross map; |
34 | #endif | |
1da177e4 LT |
35 | }; |
36 | ||
ea3f2c0f | 37 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
cd8084f9 BP |
38 | static inline void complete_acquire(struct completion *x) |
39 | { | |
40 | lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_); | |
41 | } | |
42 | ||
43 | static inline void complete_release(struct completion *x) | |
44 | { | |
45 | lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_); | |
46 | } | |
47 | ||
48 | static inline void complete_release_commit(struct completion *x) | |
49 | { | |
50 | lock_commit_crosslock((struct lockdep_map *)&x->map); | |
51 | } | |
52 | ||
a7967bc3 BP |
53 | #define init_completion_map(x, m) \ |
54 | do { \ | |
55 | lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \ | |
56 | (m)->name, (m)->key, 0); \ | |
57 | __init_completion(x); \ | |
58 | } while (0) | |
59 | ||
cd8084f9 BP |
60 | #define init_completion(x) \ |
61 | do { \ | |
62 | static struct lock_class_key __key; \ | |
63 | lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \ | |
24208435 | 64 | "(completion)" #x, \ |
cd8084f9 BP |
65 | &__key, 0); \ |
66 | __init_completion(x); \ | |
67 | } while (0) | |
68 | #else | |
a7967bc3 | 69 | #define init_completion_map(x, m) __init_completion(x) |
cd8084f9 BP |
70 | #define init_completion(x) __init_completion(x) |
71 | static inline void complete_acquire(struct completion *x) {} | |
72 | static inline void complete_release(struct completion *x) {} | |
73 | static inline void complete_release_commit(struct completion *x) {} | |
74 | #endif | |
75 | ||
ea3f2c0f | 76 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
cd8084f9 BP |
77 | #define COMPLETION_INITIALIZER(work) \ |
78 | { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \ | |
24208435 | 79 | STATIC_CROSS_LOCKDEP_MAP_INIT("(completion)" #work, &(work)) } |
cd8084f9 | 80 | #else |
1da177e4 LT |
81 | #define COMPLETION_INITIALIZER(work) \ |
82 | { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) } | |
cd8084f9 | 83 | #endif |
1da177e4 | 84 | |
a7967bc3 BP |
85 | #define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \ |
86 | (*({ init_completion_map(&(work), &(map)); &(work); })) | |
87 | ||
f86bf9b7 | 88 | #define COMPLETION_INITIALIZER_ONSTACK(work) \ |
ec81048c | 89 | (*({ init_completion(&work); &work; })) |
f86bf9b7 | 90 | |
65eb3dc6 | 91 | /** |
ee2f154a | 92 | * DECLARE_COMPLETION - declare and initialize a completion structure |
65eb3dc6 KD |
93 | * @work: identifier for the completion structure |
94 | * | |
95 | * This macro declares and initializes a completion structure. Generally used | |
96 | * for static declarations. You should use the _ONSTACK variant for automatic | |
97 | * variables. | |
98 | */ | |
1da177e4 LT |
99 | #define DECLARE_COMPLETION(work) \ |
100 | struct completion work = COMPLETION_INITIALIZER(work) | |
101 | ||
8b3db9c5 IM |
102 | /* |
103 | * Lockdep needs to run a non-constant initializer for on-stack | |
104 | * completions - so we use the _ONSTACK() variant for those that | |
105 | * are on the kernel stack: | |
106 | */ | |
65eb3dc6 | 107 | /** |
ee2f154a | 108 | * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure |
65eb3dc6 KD |
109 | * @work: identifier for the completion structure |
110 | * | |
111 | * This macro declares and initializes a completion structure on the kernel | |
112 | * stack. | |
113 | */ | |
8b3db9c5 IM |
114 | #ifdef CONFIG_LOCKDEP |
115 | # define DECLARE_COMPLETION_ONSTACK(work) \ | |
f86bf9b7 | 116 | struct completion work = COMPLETION_INITIALIZER_ONSTACK(work) |
a7967bc3 BP |
117 | # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \ |
118 | struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) | |
8b3db9c5 IM |
119 | #else |
120 | # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work) | |
a7967bc3 | 121 | # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work) |
8b3db9c5 IM |
122 | #endif |
123 | ||
65eb3dc6 | 124 | /** |
ee2f154a | 125 | * init_completion - Initialize a dynamically allocated completion |
c32f74ab | 126 | * @x: pointer to completion structure that is to be initialized |
65eb3dc6 KD |
127 | * |
128 | * This inline function will initialize a dynamically created completion | |
129 | * structure. | |
130 | */ | |
cd8084f9 | 131 | static inline void __init_completion(struct completion *x) |
1da177e4 LT |
132 | { |
133 | x->done = 0; | |
134 | init_waitqueue_head(&x->wait); | |
135 | } | |
136 | ||
c32f74ab WS |
137 | /** |
138 | * reinit_completion - reinitialize a completion structure | |
139 | * @x: pointer to completion structure that is to be reinitialized | |
140 | * | |
141 | * This inline function should be used to reinitialize a completion structure so it can | |
142 | * be reused. This is especially important after complete_all() is used. | |
143 | */ | |
144 | static inline void reinit_completion(struct completion *x) | |
145 | { | |
146 | x->done = 0; | |
147 | } | |
148 | ||
b15136e9 | 149 | extern void wait_for_completion(struct completion *); |
686855f5 | 150 | extern void wait_for_completion_io(struct completion *); |
b15136e9 | 151 | extern int wait_for_completion_interruptible(struct completion *x); |
009e577e | 152 | extern int wait_for_completion_killable(struct completion *x); |
b15136e9 IM |
153 | extern unsigned long wait_for_completion_timeout(struct completion *x, |
154 | unsigned long timeout); | |
686855f5 VD |
155 | extern unsigned long wait_for_completion_io_timeout(struct completion *x, |
156 | unsigned long timeout); | |
6bf41237 N |
157 | extern long wait_for_completion_interruptible_timeout( |
158 | struct completion *x, unsigned long timeout); | |
159 | extern long wait_for_completion_killable_timeout( | |
160 | struct completion *x, unsigned long timeout); | |
be4de352 DC |
161 | extern bool try_wait_for_completion(struct completion *x); |
162 | extern bool completion_done(struct completion *x); | |
b15136e9 IM |
163 | |
164 | extern void complete(struct completion *); | |
165 | extern void complete_all(struct completion *); | |
1da177e4 | 166 | |
1da177e4 | 167 | #endif |