xref: /openbmc/linux/include/linux/completion.h (revision 65eb3dc6)
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.
81da177e4SLinus Torvalds  * See kernel/sched.c for details.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/wait.h>
121da177e4SLinus Torvalds 
1365eb3dc6SKevin Diggs /**
1465eb3dc6SKevin Diggs  * struct completion - structure used to maintain state for a "completion"
1565eb3dc6SKevin Diggs  *
1665eb3dc6SKevin Diggs  * This is the opaque structure used to maintain the state for a "completion".
1765eb3dc6SKevin Diggs  * Completions currently use a FIFO to queue threads that have to wait for
1865eb3dc6SKevin Diggs  * the "completion" event.
1965eb3dc6SKevin Diggs  *
2065eb3dc6SKevin Diggs  * See also:  complete(), wait_for_completion() (and friends _timeout,
2165eb3dc6SKevin Diggs  * _interruptible, _interruptible_timeout, and _killable), init_completion(),
2265eb3dc6SKevin Diggs  * and macros DECLARE_COMPLETION(), DECLARE_COMPLETION_ONSTACK(), and
2365eb3dc6SKevin Diggs  * INIT_COMPLETION().
2465eb3dc6SKevin Diggs  */
251da177e4SLinus Torvalds struct completion {
261da177e4SLinus Torvalds 	unsigned int done;
271da177e4SLinus Torvalds 	wait_queue_head_t wait;
281da177e4SLinus Torvalds };
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define COMPLETION_INITIALIZER(work) \
311da177e4SLinus Torvalds 	{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
321da177e4SLinus Torvalds 
33f86bf9b7SIngo Molnar #define COMPLETION_INITIALIZER_ONSTACK(work) \
34f86bf9b7SIngo Molnar 	({ init_completion(&work); work; })
35f86bf9b7SIngo Molnar 
3665eb3dc6SKevin Diggs /**
3765eb3dc6SKevin Diggs  * DECLARE_COMPLETION: - declare and initialize a completion structure
3865eb3dc6SKevin Diggs  * @work:  identifier for the completion structure
3965eb3dc6SKevin Diggs  *
4065eb3dc6SKevin Diggs  * This macro declares and initializes a completion structure. Generally used
4165eb3dc6SKevin Diggs  * for static declarations. You should use the _ONSTACK variant for automatic
4265eb3dc6SKevin Diggs  * variables.
4365eb3dc6SKevin Diggs  */
441da177e4SLinus Torvalds #define DECLARE_COMPLETION(work) \
451da177e4SLinus Torvalds 	struct completion work = COMPLETION_INITIALIZER(work)
461da177e4SLinus Torvalds 
478b3db9c5SIngo Molnar /*
488b3db9c5SIngo Molnar  * Lockdep needs to run a non-constant initializer for on-stack
498b3db9c5SIngo Molnar  * completions - so we use the _ONSTACK() variant for those that
508b3db9c5SIngo Molnar  * are on the kernel stack:
518b3db9c5SIngo Molnar  */
5265eb3dc6SKevin Diggs /**
5365eb3dc6SKevin Diggs  * DECLARE_COMPLETION_ONSTACK: - declare and initialize a completion structure
5465eb3dc6SKevin Diggs  * @work:  identifier for the completion structure
5565eb3dc6SKevin Diggs  *
5665eb3dc6SKevin Diggs  * This macro declares and initializes a completion structure on the kernel
5765eb3dc6SKevin Diggs  * stack.
5865eb3dc6SKevin Diggs  */
598b3db9c5SIngo Molnar #ifdef CONFIG_LOCKDEP
608b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) \
61f86bf9b7SIngo Molnar 	struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
628b3db9c5SIngo Molnar #else
638b3db9c5SIngo Molnar # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
648b3db9c5SIngo Molnar #endif
658b3db9c5SIngo Molnar 
6665eb3dc6SKevin Diggs /**
6765eb3dc6SKevin Diggs  * init_completion: - Initialize a dynamically allocated completion
6865eb3dc6SKevin Diggs  * @x:  completion structure that is to be initialized
6965eb3dc6SKevin Diggs  *
7065eb3dc6SKevin Diggs  * This inline function will initialize a dynamically created completion
7165eb3dc6SKevin Diggs  * structure.
7265eb3dc6SKevin Diggs  */
731da177e4SLinus Torvalds static inline void init_completion(struct completion *x)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	x->done = 0;
761da177e4SLinus Torvalds 	init_waitqueue_head(&x->wait);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
79b15136e9SIngo Molnar extern void wait_for_completion(struct completion *);
80b15136e9SIngo Molnar extern int wait_for_completion_interruptible(struct completion *x);
81009e577eSMatthew Wilcox extern int wait_for_completion_killable(struct completion *x);
82b15136e9SIngo Molnar extern unsigned long wait_for_completion_timeout(struct completion *x,
83b15136e9SIngo Molnar 						   unsigned long timeout);
84b15136e9SIngo Molnar extern unsigned long wait_for_completion_interruptible_timeout(
85b15136e9SIngo Molnar 			struct completion *x, unsigned long timeout);
86be4de352SDave Chinner extern bool try_wait_for_completion(struct completion *x);
87be4de352SDave Chinner extern bool completion_done(struct completion *x);
881da177e4SLinus Torvalds 
89b15136e9SIngo Molnar extern void complete(struct completion *);
90b15136e9SIngo Molnar extern void complete_all(struct completion *);
911da177e4SLinus Torvalds 
9265eb3dc6SKevin Diggs /**
9365eb3dc6SKevin Diggs  * INIT_COMPLETION: - reinitialize a completion structure
9465eb3dc6SKevin Diggs  * @x:  completion structure to be reinitialized
9565eb3dc6SKevin Diggs  *
9665eb3dc6SKevin Diggs  * This macro should be used to reinitialize a completion structure so it can
9765eb3dc6SKevin Diggs  * be reused. This is especially important after complete_all() is used.
9865eb3dc6SKevin Diggs  */
991da177e4SLinus Torvalds #define INIT_COMPLETION(x)	((x).done = 0)
1001da177e4SLinus Torvalds 
10139d2f1abSDavid Chinner 
1021da177e4SLinus Torvalds #endif
103