1 #ifndef __LINUX_COMPLETION_H
2 #define __LINUX_COMPLETION_H
5 * (C) Copyright 2001 Linus Torvalds
7 * Atomic wait-for-completion handler data structures.
8 * See kernel/sched/completion.c for details.
11 #include <linux/wait.h>
12 #ifdef CONFIG_LOCKDEP_COMPLETIONS
13 #include <linux/lockdep.h>
17 * struct completion - structure used to maintain state for a "completion"
19 * This is the opaque structure used to maintain the state for a "completion".
20 * Completions currently use a FIFO to queue threads that have to wait for
21 * the "completion" event.
23 * See also: complete(), wait_for_completion() (and friends _timeout,
24 * _interruptible, _interruptible_timeout, and _killable), init_completion(),
25 * reinit_completion(), and macros DECLARE_COMPLETION(),
26 * DECLARE_COMPLETION_ONSTACK().
30 wait_queue_head_t wait;
31 #ifdef CONFIG_LOCKDEP_COMPLETIONS
32 struct lockdep_map_cross map;
36 #ifdef CONFIG_LOCKDEP_COMPLETIONS
37 static inline void complete_acquire(struct completion *x)
39 lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_);
42 static inline void complete_release(struct completion *x)
44 lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_);
47 static inline void complete_release_commit(struct completion *x)
49 lock_commit_crosslock((struct lockdep_map *)&x->map);
52 #define init_completion(x) \
54 static struct lock_class_key __key; \
55 lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \
58 __init_completion(x); \
61 #define init_completion(x) __init_completion(x)
62 static inline void complete_acquire(struct completion *x) {}
63 static inline void complete_release(struct completion *x) {}
64 static inline void complete_release_commit(struct completion *x) {}
67 #ifdef CONFIG_LOCKDEP_COMPLETIONS
68 #define COMPLETION_INITIALIZER(work) \
69 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \
70 STATIC_CROSS_LOCKDEP_MAP_INIT("(complete)" #work, &(work)) }
72 #define COMPLETION_INITIALIZER(work) \
73 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
76 #define COMPLETION_INITIALIZER_ONSTACK(work) \
77 (*({ init_completion(&work); &work; }))
80 * DECLARE_COMPLETION - declare and initialize a completion structure
81 * @work: identifier for the completion structure
83 * This macro declares and initializes a completion structure. Generally used
84 * for static declarations. You should use the _ONSTACK variant for automatic
87 #define DECLARE_COMPLETION(work) \
88 struct completion work = COMPLETION_INITIALIZER(work)
91 * Lockdep needs to run a non-constant initializer for on-stack
92 * completions - so we use the _ONSTACK() variant for those that
93 * are on the kernel stack:
96 * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
97 * @work: identifier for the completion structure
99 * This macro declares and initializes a completion structure on the kernel
102 #ifdef CONFIG_LOCKDEP
103 # define DECLARE_COMPLETION_ONSTACK(work) \
104 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
106 # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
110 * init_completion - Initialize a dynamically allocated completion
111 * @x: pointer to completion structure that is to be initialized
113 * This inline function will initialize a dynamically created completion
116 static inline void __init_completion(struct completion *x)
119 init_waitqueue_head(&x->wait);
123 * reinit_completion - reinitialize a completion structure
124 * @x: pointer to completion structure that is to be reinitialized
126 * This inline function should be used to reinitialize a completion structure so it can
127 * be reused. This is especially important after complete_all() is used.
129 static inline void reinit_completion(struct completion *x)
134 extern void wait_for_completion(struct completion *);
135 extern void wait_for_completion_io(struct completion *);
136 extern int wait_for_completion_interruptible(struct completion *x);
137 extern int wait_for_completion_killable(struct completion *x);
138 extern unsigned long wait_for_completion_timeout(struct completion *x,
139 unsigned long timeout);
140 extern unsigned long wait_for_completion_io_timeout(struct completion *x,
141 unsigned long timeout);
142 extern long wait_for_completion_interruptible_timeout(
143 struct completion *x, unsigned long timeout);
144 extern long wait_for_completion_killable_timeout(
145 struct completion *x, unsigned long timeout);
146 extern bool try_wait_for_completion(struct completion *x);
147 extern bool completion_done(struct completion *x);
149 extern void complete(struct completion *);
150 extern void complete_all(struct completion *);