xref: /openbmc/linux/include/linux/completion.h (revision 6f63904c)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __LINUX_COMPLETION_H
31da177e4SLinus Torvalds #define __LINUX_COMPLETION_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds /*
61da177e4SLinus Torvalds  * (C) Copyright 2001 Linus Torvalds
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Atomic wait-for-completion handler data structures.
9b8a21626SPeter Zijlstra  * See kernel/sched/completion.c for details.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
12a5c6234eSThomas Gleixner #include <linux/swait.h>
131da177e4SLinus Torvalds 
14ee2f154aSRandy Dunlap /*
1565eb3dc6SKevin Diggs  * struct completion - structure used to maintain state for a "completion"
1665eb3dc6SKevin Diggs  *
1765eb3dc6SKevin Diggs  * This is the opaque structure used to maintain the state for a "completion".
1865eb3dc6SKevin Diggs  * Completions currently use a FIFO to queue threads that have to wait for
1965eb3dc6SKevin Diggs  * the "completion" event.
2065eb3dc6SKevin Diggs  *
2165eb3dc6SKevin Diggs  * See also:  complete(), wait_for_completion() (and friends _timeout,
2265eb3dc6SKevin Diggs  * _interruptible, _interruptible_timeout, and _killable), init_completion(),
23c32f74abSWolfram Sang  * reinit_completion(), and macros DECLARE_COMPLETION(),
24c32f74abSWolfram Sang  * DECLARE_COMPLETION_ONSTACK().
2565eb3dc6SKevin Diggs  */
261da177e4SLinus Torvalds struct completion {
271da177e4SLinus Torvalds 	unsigned int done;
28a5c6234eSThomas Gleixner 	struct swait_queue_head wait;
291da177e4SLinus Torvalds };
301da177e4SLinus Torvalds 
31b6498aadSMauro Carvalho Chehab #define init_completion_map(x, m) init_completion(x)
complete_acquire(struct completion * x)32cd8084f9SByungchul Park static inline void complete_acquire(struct completion *x) {}
complete_release(struct completion * x)33cd8084f9SByungchul Park static inline void complete_release(struct completion *x) {}
34cd8084f9SByungchul Park 
351da177e4SLinus Torvalds #define COMPLETION_INITIALIZER(work) \
36a5c6234eSThomas Gleixner 	{ 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
371da177e4SLinus Torvalds 
38a7967bc3SByungchul Park #define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \
39a7967bc3SByungchul Park 	(*({ init_completion_map(&(work), &(map)); &(work); }))
40a7967bc3SByungchul Park 
41f86bf9b7SIngo Molnar #define COMPLETION_INITIALIZER_ONSTACK(work) \
42ec81048cSBoqun Feng 	(*({ init_completion(&work); &work; }))
43f86bf9b7SIngo Molnar 
4465eb3dc6SKevin Diggs /**
45ee2f154aSRandy Dunlap  * DECLARE_COMPLETION - declare and initialize a completion structure
4665eb3dc6SKevin Diggs  * @work:  identifier for the completion structure
4765eb3dc6SKevin Diggs  *
4865eb3dc6SKevin Diggs  * This macro declares and initializes a completion structure. Generally used
4965eb3dc6SKevin Diggs  * for static declarations. You should use the _ONSTACK variant for automatic
5065eb3dc6SKevin Diggs  * variables.
5165eb3dc6SKevin Diggs  */
521da177e4SLinus Torvalds #define DECLARE_COMPLETION(work) \
531da177e4SLinus Torvalds 	struct completion work = COMPLETION_INITIALIZER(work)
541da177e4SLinus Torvalds 
558b3db9c5SIngo Molnar /*
568b3db9c5SIngo Molnar  * Lockdep needs to run a non-constant initializer for on-stack
578b3db9c5SIngo Molnar  * completions - so we use the _ONSTACK() variant for those that
588b3db9c5SIngo Molnar  * are on the kernel stack:
598b3db9c5SIngo Molnar  */
6065eb3dc6SKevin Diggs /**
61ee2f154aSRandy Dunlap  * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
6265eb3dc6SKevin Diggs  * @work:  identifier for the completion structure
6365eb3dc6SKevin Diggs  *
6465eb3dc6SKevin Diggs  * This macro declares and initializes a completion structure on the kernel
6565eb3dc6SKevin Diggs  * stack.
6665eb3dc6SKevin Diggs  */
678b3db9c5SIngo Molnar #ifdef CONFIG_LOCKDEP
688b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) \
69f86bf9b7SIngo Molnar 	struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
70a7967bc3SByungchul Park # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \
71a7967bc3SByungchul Park 	struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map)
728b3db9c5SIngo Molnar #else
738b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
74a7967bc3SByungchul Park # define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work)
758b3db9c5SIngo Molnar #endif
768b3db9c5SIngo Molnar 
7765eb3dc6SKevin Diggs /**
78ee2f154aSRandy Dunlap  * init_completion - Initialize a dynamically allocated completion
79c32f74abSWolfram Sang  * @x:  pointer to completion structure that is to be initialized
8065eb3dc6SKevin Diggs  *
8165eb3dc6SKevin Diggs  * This inline function will initialize a dynamically created completion
8265eb3dc6SKevin Diggs  * structure.
8365eb3dc6SKevin Diggs  */
init_completion(struct completion * x)84b6498aadSMauro Carvalho Chehab static inline void init_completion(struct completion *x)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	x->done = 0;
87a5c6234eSThomas Gleixner 	init_swait_queue_head(&x->wait);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
90c32f74abSWolfram Sang /**
91c32f74abSWolfram Sang  * reinit_completion - reinitialize a completion structure
92c32f74abSWolfram Sang  * @x:  pointer to completion structure that is to be reinitialized
93c32f74abSWolfram Sang  *
94c32f74abSWolfram Sang  * This inline function should be used to reinitialize a completion structure so it can
95c32f74abSWolfram Sang  * be reused. This is especially important after complete_all() is used.
96c32f74abSWolfram Sang  */
reinit_completion(struct completion * x)97c32f74abSWolfram Sang static inline void reinit_completion(struct completion *x)
98c32f74abSWolfram Sang {
99c32f74abSWolfram Sang 	x->done = 0;
100c32f74abSWolfram Sang }
101c32f74abSWolfram Sang 
102b15136e9SIngo Molnar extern void wait_for_completion(struct completion *);
103686855f5SVladimir Davydov extern void wait_for_completion_io(struct completion *);
104b15136e9SIngo Molnar extern int wait_for_completion_interruptible(struct completion *x);
105009e577eSMatthew Wilcox extern int wait_for_completion_killable(struct completion *x);
106929659acSPeter Zijlstra extern int wait_for_completion_state(struct completion *x, unsigned int state);
107b15136e9SIngo Molnar extern unsigned long wait_for_completion_timeout(struct completion *x,
108b15136e9SIngo Molnar 						   unsigned long timeout);
109686855f5SVladimir Davydov extern unsigned long wait_for_completion_io_timeout(struct completion *x,
110686855f5SVladimir Davydov 						    unsigned long timeout);
1116bf41237SNeilBrown extern long wait_for_completion_interruptible_timeout(
112b15136e9SIngo Molnar 	struct completion *x, unsigned long timeout);
1136bf41237SNeilBrown extern long wait_for_completion_killable_timeout(
1140aa12fb4SSage Weil 	struct completion *x, unsigned long timeout);
115be4de352SDave Chinner extern bool try_wait_for_completion(struct completion *x);
116be4de352SDave Chinner extern bool completion_done(struct completion *x);
1171da177e4SLinus Torvalds 
118b15136e9SIngo Molnar extern void complete(struct completion *);
119*6f63904cSAndrei Vagin extern void complete_on_current_cpu(struct completion *x);
120b15136e9SIngo Molnar extern void complete_all(struct completion *);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds #endif
123