xref: /openbmc/linux/include/linux/completion.h (revision cd8084f9)
11da177e4SLinus Torvalds #ifndef __LINUX_COMPLETION_H
21da177e4SLinus Torvalds #define __LINUX_COMPLETION_H
31da177e4SLinus Torvalds 
41da177e4SLinus Torvalds /*
51da177e4SLinus Torvalds  * (C) Copyright 2001 Linus Torvalds
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Atomic wait-for-completion handler data structures.
8b8a21626SPeter Zijlstra  * See kernel/sched/completion.c for details.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/wait.h>
12cd8084f9SByungchul Park #ifdef CONFIG_LOCKDEP_COMPLETE
13cd8084f9SByungchul Park #include <linux/lockdep.h>
14cd8084f9SByungchul Park #endif
151da177e4SLinus Torvalds 
16ee2f154aSRandy Dunlap /*
1765eb3dc6SKevin Diggs  * struct completion - structure used to maintain state for a "completion"
1865eb3dc6SKevin Diggs  *
1965eb3dc6SKevin Diggs  * This is the opaque structure used to maintain the state for a "completion".
2065eb3dc6SKevin Diggs  * Completions currently use a FIFO to queue threads that have to wait for
2165eb3dc6SKevin Diggs  * the "completion" event.
2265eb3dc6SKevin Diggs  *
2365eb3dc6SKevin Diggs  * See also:  complete(), wait_for_completion() (and friends _timeout,
2465eb3dc6SKevin Diggs  * _interruptible, _interruptible_timeout, and _killable), init_completion(),
25c32f74abSWolfram Sang  * reinit_completion(), and macros DECLARE_COMPLETION(),
26c32f74abSWolfram Sang  * DECLARE_COMPLETION_ONSTACK().
2765eb3dc6SKevin Diggs  */
281da177e4SLinus Torvalds struct completion {
291da177e4SLinus Torvalds 	unsigned int done;
301da177e4SLinus Torvalds 	wait_queue_head_t wait;
31cd8084f9SByungchul Park #ifdef CONFIG_LOCKDEP_COMPLETE
32cd8084f9SByungchul Park 	struct lockdep_map_cross map;
33cd8084f9SByungchul Park #endif
341da177e4SLinus Torvalds };
351da177e4SLinus Torvalds 
36cd8084f9SByungchul Park #ifdef CONFIG_LOCKDEP_COMPLETE
37cd8084f9SByungchul Park static inline void complete_acquire(struct completion *x)
38cd8084f9SByungchul Park {
39cd8084f9SByungchul Park 	lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_);
40cd8084f9SByungchul Park }
41cd8084f9SByungchul Park 
42cd8084f9SByungchul Park static inline void complete_release(struct completion *x)
43cd8084f9SByungchul Park {
44cd8084f9SByungchul Park 	lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_);
45cd8084f9SByungchul Park }
46cd8084f9SByungchul Park 
47cd8084f9SByungchul Park static inline void complete_release_commit(struct completion *x)
48cd8084f9SByungchul Park {
49cd8084f9SByungchul Park 	lock_commit_crosslock((struct lockdep_map *)&x->map);
50cd8084f9SByungchul Park }
51cd8084f9SByungchul Park 
52cd8084f9SByungchul Park #define init_completion(x)						\
53cd8084f9SByungchul Park do {									\
54cd8084f9SByungchul Park 	static struct lock_class_key __key;				\
55cd8084f9SByungchul Park 	lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map,	\
56cd8084f9SByungchul Park 			"(complete)" #x,				\
57cd8084f9SByungchul Park 			&__key, 0);					\
58cd8084f9SByungchul Park 	__init_completion(x);						\
59cd8084f9SByungchul Park } while (0)
60cd8084f9SByungchul Park #else
61cd8084f9SByungchul Park #define init_completion(x) __init_completion(x)
62cd8084f9SByungchul Park static inline void complete_acquire(struct completion *x) {}
63cd8084f9SByungchul Park static inline void complete_release(struct completion *x) {}
64cd8084f9SByungchul Park static inline void complete_release_commit(struct completion *x) {}
65cd8084f9SByungchul Park #endif
66cd8084f9SByungchul Park 
67cd8084f9SByungchul Park #ifdef CONFIG_LOCKDEP_COMPLETE
68cd8084f9SByungchul Park #define COMPLETION_INITIALIZER(work) \
69cd8084f9SByungchul Park 	{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \
70cd8084f9SByungchul Park 	STATIC_CROSS_LOCKDEP_MAP_INIT("(complete)" #work, &(work)) }
71cd8084f9SByungchul Park #else
721da177e4SLinus Torvalds #define COMPLETION_INITIALIZER(work) \
731da177e4SLinus Torvalds 	{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
74cd8084f9SByungchul Park #endif
751da177e4SLinus Torvalds 
76f86bf9b7SIngo Molnar #define COMPLETION_INITIALIZER_ONSTACK(work) \
77f86bf9b7SIngo Molnar 	({ init_completion(&work); work; })
78f86bf9b7SIngo Molnar 
7965eb3dc6SKevin Diggs /**
80ee2f154aSRandy Dunlap  * DECLARE_COMPLETION - declare and initialize a completion structure
8165eb3dc6SKevin Diggs  * @work:  identifier for the completion structure
8265eb3dc6SKevin Diggs  *
8365eb3dc6SKevin Diggs  * This macro declares and initializes a completion structure. Generally used
8465eb3dc6SKevin Diggs  * for static declarations. You should use the _ONSTACK variant for automatic
8565eb3dc6SKevin Diggs  * variables.
8665eb3dc6SKevin Diggs  */
871da177e4SLinus Torvalds #define DECLARE_COMPLETION(work) \
881da177e4SLinus Torvalds 	struct completion work = COMPLETION_INITIALIZER(work)
891da177e4SLinus Torvalds 
908b3db9c5SIngo Molnar /*
918b3db9c5SIngo Molnar  * Lockdep needs to run a non-constant initializer for on-stack
928b3db9c5SIngo Molnar  * completions - so we use the _ONSTACK() variant for those that
938b3db9c5SIngo Molnar  * are on the kernel stack:
948b3db9c5SIngo Molnar  */
9565eb3dc6SKevin Diggs /**
96ee2f154aSRandy Dunlap  * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
9765eb3dc6SKevin Diggs  * @work:  identifier for the completion structure
9865eb3dc6SKevin Diggs  *
9965eb3dc6SKevin Diggs  * This macro declares and initializes a completion structure on the kernel
10065eb3dc6SKevin Diggs  * stack.
10165eb3dc6SKevin Diggs  */
1028b3db9c5SIngo Molnar #ifdef CONFIG_LOCKDEP
1038b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) \
104f86bf9b7SIngo Molnar 	struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
1058b3db9c5SIngo Molnar #else
1068b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
1078b3db9c5SIngo Molnar #endif
1088b3db9c5SIngo Molnar 
10965eb3dc6SKevin Diggs /**
110ee2f154aSRandy Dunlap  * init_completion - Initialize a dynamically allocated completion
111c32f74abSWolfram Sang  * @x:  pointer to completion structure that is to be initialized
11265eb3dc6SKevin Diggs  *
11365eb3dc6SKevin Diggs  * This inline function will initialize a dynamically created completion
11465eb3dc6SKevin Diggs  * structure.
11565eb3dc6SKevin Diggs  */
116cd8084f9SByungchul Park static inline void __init_completion(struct completion *x)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	x->done = 0;
1191da177e4SLinus Torvalds 	init_waitqueue_head(&x->wait);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
122c32f74abSWolfram Sang /**
123c32f74abSWolfram Sang  * reinit_completion - reinitialize a completion structure
124c32f74abSWolfram Sang  * @x:  pointer to completion structure that is to be reinitialized
125c32f74abSWolfram Sang  *
126c32f74abSWolfram Sang  * This inline function should be used to reinitialize a completion structure so it can
127c32f74abSWolfram Sang  * be reused. This is especially important after complete_all() is used.
128c32f74abSWolfram Sang  */
129c32f74abSWolfram Sang static inline void reinit_completion(struct completion *x)
130c32f74abSWolfram Sang {
131c32f74abSWolfram Sang 	x->done = 0;
132c32f74abSWolfram Sang }
133c32f74abSWolfram Sang 
134b15136e9SIngo Molnar extern void wait_for_completion(struct completion *);
135686855f5SVladimir Davydov extern void wait_for_completion_io(struct completion *);
136b15136e9SIngo Molnar extern int wait_for_completion_interruptible(struct completion *x);
137009e577eSMatthew Wilcox extern int wait_for_completion_killable(struct completion *x);
138b15136e9SIngo Molnar extern unsigned long wait_for_completion_timeout(struct completion *x,
139b15136e9SIngo Molnar 						   unsigned long timeout);
140686855f5SVladimir Davydov extern unsigned long wait_for_completion_io_timeout(struct completion *x,
141686855f5SVladimir Davydov 						    unsigned long timeout);
1426bf41237SNeilBrown extern long wait_for_completion_interruptible_timeout(
143b15136e9SIngo Molnar 	struct completion *x, unsigned long timeout);
1446bf41237SNeilBrown extern long wait_for_completion_killable_timeout(
1450aa12fb4SSage Weil 	struct completion *x, unsigned long timeout);
146be4de352SDave Chinner extern bool try_wait_for_completion(struct completion *x);
147be4de352SDave Chinner extern bool completion_done(struct completion *x);
1481da177e4SLinus Torvalds 
149b15136e9SIngo Molnar extern void complete(struct completion *);
150b15136e9SIngo Molnar extern void complete_all(struct completion *);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds #endif
153