License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / include / linux / completion.h
CommitLineData
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
29struct 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
38static inline void complete_acquire(struct completion *x)
39{
40 lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_);
41}
42
43static inline void complete_release(struct completion *x)
44{
45 lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_);
46}
47
48static inline void complete_release_commit(struct completion *x)
49{
50 lock_commit_crosslock((struct lockdep_map *)&x->map);
51}
52
53#define init_completion(x) \
54do { \
55 static struct lock_class_key __key; \
56 lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \
57 "(complete)" #x, \
58 &__key, 0); \
59 __init_completion(x); \
60} while (0)
61#else
62#define init_completion(x) __init_completion(x)
63static inline void complete_acquire(struct completion *x) {}
64static inline void complete_release(struct completion *x) {}
65static inline void complete_release_commit(struct completion *x) {}
66#endif
67
ea3f2c0f 68#ifdef CONFIG_LOCKDEP_COMPLETIONS
cd8084f9
BP
69#define COMPLETION_INITIALIZER(work) \
70 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \
71 STATIC_CROSS_LOCKDEP_MAP_INIT("(complete)" #work, &(work)) }
72#else
1da177e4
LT
73#define COMPLETION_INITIALIZER(work) \
74 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
cd8084f9 75#endif
1da177e4 76
f86bf9b7 77#define COMPLETION_INITIALIZER_ONSTACK(work) \
ec81048c 78 (*({ init_completion(&work); &work; }))
f86bf9b7 79
65eb3dc6 80/**
ee2f154a 81 * DECLARE_COMPLETION - declare and initialize a completion structure
65eb3dc6
KD
82 * @work: identifier for the completion structure
83 *
84 * This macro declares and initializes a completion structure. Generally used
85 * for static declarations. You should use the _ONSTACK variant for automatic
86 * variables.
87 */
1da177e4
LT
88#define DECLARE_COMPLETION(work) \
89 struct completion work = COMPLETION_INITIALIZER(work)
90
8b3db9c5
IM
91/*
92 * Lockdep needs to run a non-constant initializer for on-stack
93 * completions - so we use the _ONSTACK() variant for those that
94 * are on the kernel stack:
95 */
65eb3dc6 96/**
ee2f154a 97 * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
65eb3dc6
KD
98 * @work: identifier for the completion structure
99 *
100 * This macro declares and initializes a completion structure on the kernel
101 * stack.
102 */
8b3db9c5
IM
103#ifdef CONFIG_LOCKDEP
104# define DECLARE_COMPLETION_ONSTACK(work) \
f86bf9b7 105 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
8b3db9c5
IM
106#else
107# define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
108#endif
109
65eb3dc6 110/**
ee2f154a 111 * init_completion - Initialize a dynamically allocated completion
c32f74ab 112 * @x: pointer to completion structure that is to be initialized
65eb3dc6
KD
113 *
114 * This inline function will initialize a dynamically created completion
115 * structure.
116 */
cd8084f9 117static inline void __init_completion(struct completion *x)
1da177e4
LT
118{
119 x->done = 0;
120 init_waitqueue_head(&x->wait);
121}
122
c32f74ab
WS
123/**
124 * reinit_completion - reinitialize a completion structure
125 * @x: pointer to completion structure that is to be reinitialized
126 *
127 * This inline function should be used to reinitialize a completion structure so it can
128 * be reused. This is especially important after complete_all() is used.
129 */
130static inline void reinit_completion(struct completion *x)
131{
132 x->done = 0;
133}
134
b15136e9 135extern void wait_for_completion(struct completion *);
686855f5 136extern void wait_for_completion_io(struct completion *);
b15136e9 137extern int wait_for_completion_interruptible(struct completion *x);
009e577e 138extern int wait_for_completion_killable(struct completion *x);
b15136e9
IM
139extern unsigned long wait_for_completion_timeout(struct completion *x,
140 unsigned long timeout);
686855f5
VD
141extern unsigned long wait_for_completion_io_timeout(struct completion *x,
142 unsigned long timeout);
6bf41237
N
143extern long wait_for_completion_interruptible_timeout(
144 struct completion *x, unsigned long timeout);
145extern long wait_for_completion_killable_timeout(
146 struct completion *x, unsigned long timeout);
be4de352
DC
147extern bool try_wait_for_completion(struct completion *x);
148extern bool completion_done(struct completion *x);
b15136e9
IM
149
150extern void complete(struct completion *);
151extern void complete_all(struct completion *);
1da177e4 152
1da177e4 153#endif