Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __LINUX_COMPLETION_H |
| 2 | #define __LINUX_COMPLETION_H |
| 3 | |
| 4 | /* |
| 5 | * (C) Copyright 2001 Linus Torvalds |
| 6 | * |
| 7 | * Atomic wait-for-completion handler data structures. |
Viresh Kumar | 0a0fca9 | 2013-06-04 13:10:24 +0530 | [diff] [blame] | 8 | * See kernel/sched/core.c for details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/wait.h> |
| 12 | |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 13 | /* |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 14 | * struct completion - structure used to maintain state for a "completion" |
| 15 | * |
| 16 | * This is the opaque structure used to maintain the state for a "completion". |
| 17 | * Completions currently use a FIFO to queue threads that have to wait for |
| 18 | * the "completion" event. |
| 19 | * |
| 20 | * See also: complete(), wait_for_completion() (and friends _timeout, |
| 21 | * _interruptible, _interruptible_timeout, and _killable), init_completion(), |
| 22 | * and macros DECLARE_COMPLETION(), DECLARE_COMPLETION_ONSTACK(), and |
| 23 | * INIT_COMPLETION(). |
| 24 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | struct completion { |
| 26 | unsigned int done; |
| 27 | wait_queue_head_t wait; |
| 28 | }; |
| 29 | |
| 30 | #define COMPLETION_INITIALIZER(work) \ |
| 31 | { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) } |
| 32 | |
Ingo Molnar | f86bf9b | 2006-07-10 04:44:05 -0700 | [diff] [blame] | 33 | #define COMPLETION_INITIALIZER_ONSTACK(work) \ |
| 34 | ({ init_completion(&work); work; }) |
| 35 | |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 36 | /** |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 37 | * DECLARE_COMPLETION - declare and initialize a completion structure |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 38 | * @work: identifier for the completion structure |
| 39 | * |
| 40 | * This macro declares and initializes a completion structure. Generally used |
| 41 | * for static declarations. You should use the _ONSTACK variant for automatic |
| 42 | * variables. |
| 43 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #define DECLARE_COMPLETION(work) \ |
| 45 | struct completion work = COMPLETION_INITIALIZER(work) |
| 46 | |
Ingo Molnar | 8b3db9c | 2006-07-03 00:24:28 -0700 | [diff] [blame] | 47 | /* |
| 48 | * Lockdep needs to run a non-constant initializer for on-stack |
| 49 | * completions - so we use the _ONSTACK() variant for those that |
| 50 | * are on the kernel stack: |
| 51 | */ |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 52 | /** |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 53 | * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 54 | * @work: identifier for the completion structure |
| 55 | * |
| 56 | * This macro declares and initializes a completion structure on the kernel |
| 57 | * stack. |
| 58 | */ |
Ingo Molnar | 8b3db9c | 2006-07-03 00:24:28 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_LOCKDEP |
| 60 | # define DECLARE_COMPLETION_ONSTACK(work) \ |
Ingo Molnar | f86bf9b | 2006-07-10 04:44:05 -0700 | [diff] [blame] | 61 | struct completion work = COMPLETION_INITIALIZER_ONSTACK(work) |
Ingo Molnar | 8b3db9c | 2006-07-03 00:24:28 -0700 | [diff] [blame] | 62 | #else |
| 63 | # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work) |
| 64 | #endif |
| 65 | |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 66 | /** |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 67 | * init_completion - Initialize a dynamically allocated completion |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 68 | * @x: completion structure that is to be initialized |
| 69 | * |
| 70 | * This inline function will initialize a dynamically created completion |
| 71 | * structure. |
| 72 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | static inline void init_completion(struct completion *x) |
| 74 | { |
| 75 | x->done = 0; |
| 76 | init_waitqueue_head(&x->wait); |
| 77 | } |
| 78 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 79 | extern void wait_for_completion(struct completion *); |
Vladimir Davydov | 686855f | 2013-02-14 18:19:58 +0400 | [diff] [blame] | 80 | extern void wait_for_completion_io(struct completion *); |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 81 | extern int wait_for_completion_interruptible(struct completion *x); |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 82 | extern int wait_for_completion_killable(struct completion *x); |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 83 | extern unsigned long wait_for_completion_timeout(struct completion *x, |
| 84 | unsigned long timeout); |
Vladimir Davydov | 686855f | 2013-02-14 18:19:58 +0400 | [diff] [blame] | 85 | extern unsigned long wait_for_completion_io_timeout(struct completion *x, |
| 86 | unsigned long timeout); |
NeilBrown | 6bf4123 | 2011-01-05 12:50:16 +1100 | [diff] [blame] | 87 | extern long wait_for_completion_interruptible_timeout( |
| 88 | struct completion *x, unsigned long timeout); |
| 89 | extern long wait_for_completion_killable_timeout( |
| 90 | struct completion *x, unsigned long timeout); |
Dave Chinner | be4de35 | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 91 | extern bool try_wait_for_completion(struct completion *x); |
| 92 | extern bool completion_done(struct completion *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 94 | extern void complete(struct completion *); |
| 95 | extern void complete_all(struct completion *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 97 | /** |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 98 | * INIT_COMPLETION - reinitialize a completion structure |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 99 | * @x: completion structure to be reinitialized |
| 100 | * |
| 101 | * This macro should be used to reinitialize a completion structure so it can |
| 102 | * be reused. This is especially important after complete_all() is used. |
| 103 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #define INIT_COMPLETION(x) ((x).done = 0) |
| 105 | |
David Chinner | 39d2f1a | 2008-08-13 16:40:43 +1000 | [diff] [blame] | 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #endif |