11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifndef __LINUX_COMPLETION_H
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define __LINUX_COMPLETION_H
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (C) Copyright 2001 Linus Torvalds
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Atomic wait-for-completion handler data structures.
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * See kernel/sched.c for details.
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/wait.h>
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13ee2f154a598e96df2ebb01648a7699373bc085c7Randy Dunlap/*
1465eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * struct completion - structure used to maintain state for a "completion"
1565eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs *
1665eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * This is the opaque structure used to maintain the state for a "completion".
1765eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * Completions currently use a FIFO to queue threads that have to wait for
1865eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * the "completion" event.
1965eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs *
2065eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * See also:  complete(), wait_for_completion() (and friends _timeout,
2165eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * _interruptible, _interruptible_timeout, and _killable), init_completion(),
2265eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * and macros DECLARE_COMPLETION(), DECLARE_COMPLETION_ONSTACK(), and
2365eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * INIT_COMPLETION().
2465eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs */
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct completion {
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int done;
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	wait_queue_head_t wait;
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define COMPLETION_INITIALIZER(work) \
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
33f86bf9b7bcc5d325687a8b80da8ee3eb56e02da7Ingo Molnar#define COMPLETION_INITIALIZER_ONSTACK(work) \
34f86bf9b7bcc5d325687a8b80da8ee3eb56e02da7Ingo Molnar	({ init_completion(&work); work; })
35f86bf9b7bcc5d325687a8b80da8ee3eb56e02da7Ingo Molnar
3665eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs/**
37ee2f154a598e96df2ebb01648a7699373bc085c7Randy Dunlap * DECLARE_COMPLETION - declare and initialize a completion structure
3865eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * @work:  identifier for the completion structure
3965eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs *
4065eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * This macro declares and initializes a completion structure. Generally used
4165eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * for static declarations. You should use the _ONSTACK variant for automatic
4265eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * variables.
4365eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs */
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DECLARE_COMPLETION(work) \
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct completion work = COMPLETION_INITIALIZER(work)
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
478b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar/*
488b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar * Lockdep needs to run a non-constant initializer for on-stack
498b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar * completions - so we use the _ONSTACK() variant for those that
508b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar * are on the kernel stack:
518b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar */
5265eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs/**
53ee2f154a598e96df2ebb01648a7699373bc085c7Randy Dunlap * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
5465eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * @work:  identifier for the completion structure
5565eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs *
5665eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * This macro declares and initializes a completion structure on the kernel
5765eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * stack.
5865eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs */
598b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar#ifdef CONFIG_LOCKDEP
608b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar# define DECLARE_COMPLETION_ONSTACK(work) \
61f86bf9b7bcc5d325687a8b80da8ee3eb56e02da7Ingo Molnar	struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
628b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar#else
638b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar# define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
648b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar#endif
658b3db9c542e18b71d4820da4dd9401ee030feacbIngo Molnar
6665eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs/**
67ee2f154a598e96df2ebb01648a7699373bc085c7Randy Dunlap * init_completion - Initialize a dynamically allocated completion
6865eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * @x:  completion structure that is to be initialized
6965eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs *
7065eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * This inline function will initialize a dynamically created completion
7165eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * structure.
7265eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs */
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline void init_completion(struct completion *x)
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	x->done = 0;
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_waitqueue_head(&x->wait);
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
79b15136e9497ef5d6e08cf665e0d0acf7a229f6dcIngo Molnarextern void wait_for_completion(struct completion *);
80b15136e9497ef5d6e08cf665e0d0acf7a229f6dcIngo Molnarextern int wait_for_completion_interruptible(struct completion *x);
81009e577e079656d51d0fe9b15e61e41b00816c29Matthew Wilcoxextern int wait_for_completion_killable(struct completion *x);
82b15136e9497ef5d6e08cf665e0d0acf7a229f6dcIngo Molnarextern unsigned long wait_for_completion_timeout(struct completion *x,
83b15136e9497ef5d6e08cf665e0d0acf7a229f6dcIngo Molnar						   unsigned long timeout);
846bf4123760a5aece6e4829ce90b70b6ffd751d65NeilBrownextern long wait_for_completion_interruptible_timeout(
856bf4123760a5aece6e4829ce90b70b6ffd751d65NeilBrown	struct completion *x, unsigned long timeout);
866bf4123760a5aece6e4829ce90b70b6ffd751d65NeilBrownextern long wait_for_completion_killable_timeout(
876bf4123760a5aece6e4829ce90b70b6ffd751d65NeilBrown	struct completion *x, unsigned long timeout);
88be4de35263f59ca1f4740edfffbfb02cc3f2189eDave Chinnerextern bool try_wait_for_completion(struct completion *x);
89be4de35263f59ca1f4740edfffbfb02cc3f2189eDave Chinnerextern bool completion_done(struct completion *x);
90b15136e9497ef5d6e08cf665e0d0acf7a229f6dcIngo Molnar
91b15136e9497ef5d6e08cf665e0d0acf7a229f6dcIngo Molnarextern void complete(struct completion *);
92b15136e9497ef5d6e08cf665e0d0acf7a229f6dcIngo Molnarextern void complete_all(struct completion *);
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9465eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs/**
95ee2f154a598e96df2ebb01648a7699373bc085c7Randy Dunlap * INIT_COMPLETION - reinitialize a completion structure
9665eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * @x:  completion structure to be reinitialized
9765eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs *
9865eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * This macro should be used to reinitialize a completion structure so it can
9965eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs * be reused. This is especially important after complete_all() is used.
10065eb3dc609dec17deea48dcd4de2e549d29a9824Kevin Diggs */
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define INIT_COMPLETION(x)	((x).done = 0)
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10339d2f1ab2a36ac527a6c41cfe689f50c239eaca3David Chinner
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
105