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. |
Peter Zijlstra | b8a2162 | 2013-10-04 22:06:53 +0200 | [diff] [blame] | 8 | * See kernel/sched/completion.c for details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/wait.h> |
Byungchul Park | ea3f2c0 | 2017-08-17 17:57:41 +0900 | [diff] [blame] | 12 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
Byungchul Park | cd8084f | 2017-08-07 16:12:56 +0900 | [diff] [blame] | 13 | #include <linux/lockdep.h> |
| 14 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 16 | /* |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 17 | * struct completion - structure used to maintain state for a "completion" |
| 18 | * |
| 19 | * This is the opaque structure used to maintain the state for a "completion". |
| 20 | * Completions currently use a FIFO to queue threads that have to wait for |
| 21 | * the "completion" event. |
| 22 | * |
| 23 | * See also: complete(), wait_for_completion() (and friends _timeout, |
| 24 | * _interruptible, _interruptible_timeout, and _killable), init_completion(), |
Wolfram Sang | c32f74a | 2013-11-14 14:32:01 -0800 | [diff] [blame] | 25 | * reinit_completion(), and macros DECLARE_COMPLETION(), |
| 26 | * DECLARE_COMPLETION_ONSTACK(). |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 27 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct completion { |
| 29 | unsigned int done; |
| 30 | wait_queue_head_t wait; |
Byungchul Park | ea3f2c0 | 2017-08-17 17:57:41 +0900 | [diff] [blame] | 31 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
Byungchul Park | cd8084f | 2017-08-07 16:12:56 +0900 | [diff] [blame] | 32 | struct lockdep_map_cross map; |
| 33 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Byungchul Park | ea3f2c0 | 2017-08-17 17:57:41 +0900 | [diff] [blame] | 36 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
Byungchul Park | cd8084f | 2017-08-07 16:12:56 +0900 | [diff] [blame] | 37 | static inline void complete_acquire(struct completion *x) |
| 38 | { |
| 39 | lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_); |
| 40 | } |
| 41 | |
| 42 | static inline void complete_release(struct completion *x) |
| 43 | { |
| 44 | lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_); |
| 45 | } |
| 46 | |
| 47 | static inline void complete_release_commit(struct completion *x) |
| 48 | { |
| 49 | lock_commit_crosslock((struct lockdep_map *)&x->map); |
| 50 | } |
| 51 | |
| 52 | #define init_completion(x) \ |
| 53 | do { \ |
| 54 | static struct lock_class_key __key; \ |
| 55 | lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \ |
| 56 | "(complete)" #x, \ |
| 57 | &__key, 0); \ |
| 58 | __init_completion(x); \ |
| 59 | } while (0) |
| 60 | #else |
| 61 | #define init_completion(x) __init_completion(x) |
| 62 | static inline void complete_acquire(struct completion *x) {} |
| 63 | static inline void complete_release(struct completion *x) {} |
| 64 | static inline void complete_release_commit(struct completion *x) {} |
| 65 | #endif |
| 66 | |
Byungchul Park | ea3f2c0 | 2017-08-17 17:57:41 +0900 | [diff] [blame] | 67 | #ifdef CONFIG_LOCKDEP_COMPLETIONS |
Byungchul Park | cd8084f | 2017-08-07 16:12:56 +0900 | [diff] [blame] | 68 | #define COMPLETION_INITIALIZER(work) \ |
| 69 | { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \ |
| 70 | STATIC_CROSS_LOCKDEP_MAP_INIT("(complete)" #work, &(work)) } |
| 71 | #else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #define COMPLETION_INITIALIZER(work) \ |
| 73 | { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) } |
Byungchul Park | cd8084f | 2017-08-07 16:12:56 +0900 | [diff] [blame] | 74 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Ingo Molnar | f86bf9b | 2006-07-10 04:44:05 -0700 | [diff] [blame] | 76 | #define COMPLETION_INITIALIZER_ONSTACK(work) \ |
Boqun Feng | ec81048 | 2017-08-23 23:25:38 +0800 | [diff] [blame] | 77 | (*({ init_completion(&work); &work; })) |
Ingo Molnar | f86bf9b | 2006-07-10 04:44:05 -0700 | [diff] [blame] | 78 | |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 79 | /** |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 80 | * DECLARE_COMPLETION - declare and initialize a completion structure |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 81 | * @work: identifier for the completion structure |
| 82 | * |
| 83 | * This macro declares and initializes a completion structure. Generally used |
| 84 | * for static declarations. You should use the _ONSTACK variant for automatic |
| 85 | * variables. |
| 86 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | #define DECLARE_COMPLETION(work) \ |
| 88 | struct completion work = COMPLETION_INITIALIZER(work) |
| 89 | |
Ingo Molnar | 8b3db9c | 2006-07-03 00:24:28 -0700 | [diff] [blame] | 90 | /* |
| 91 | * Lockdep needs to run a non-constant initializer for on-stack |
| 92 | * completions - so we use the _ONSTACK() variant for those that |
| 93 | * are on the kernel stack: |
| 94 | */ |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 95 | /** |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 96 | * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 97 | * @work: identifier for the completion structure |
| 98 | * |
| 99 | * This macro declares and initializes a completion structure on the kernel |
| 100 | * stack. |
| 101 | */ |
Ingo Molnar | 8b3db9c | 2006-07-03 00:24:28 -0700 | [diff] [blame] | 102 | #ifdef CONFIG_LOCKDEP |
| 103 | # define DECLARE_COMPLETION_ONSTACK(work) \ |
Ingo Molnar | f86bf9b | 2006-07-10 04:44:05 -0700 | [diff] [blame] | 104 | struct completion work = COMPLETION_INITIALIZER_ONSTACK(work) |
Ingo Molnar | 8b3db9c | 2006-07-03 00:24:28 -0700 | [diff] [blame] | 105 | #else |
| 106 | # define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work) |
| 107 | #endif |
| 108 | |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 109 | /** |
Randy Dunlap | ee2f154 | 2010-10-26 14:17:25 -0700 | [diff] [blame] | 110 | * init_completion - Initialize a dynamically allocated completion |
Wolfram Sang | c32f74a | 2013-11-14 14:32:01 -0800 | [diff] [blame] | 111 | * @x: pointer to completion structure that is to be initialized |
Kevin Diggs | 65eb3dc | 2008-08-26 10:26:54 +0200 | [diff] [blame] | 112 | * |
| 113 | * This inline function will initialize a dynamically created completion |
| 114 | * structure. |
| 115 | */ |
Byungchul Park | cd8084f | 2017-08-07 16:12:56 +0900 | [diff] [blame] | 116 | static inline void __init_completion(struct completion *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | x->done = 0; |
| 119 | init_waitqueue_head(&x->wait); |
| 120 | } |
| 121 | |
Wolfram Sang | c32f74a | 2013-11-14 14:32:01 -0800 | [diff] [blame] | 122 | /** |
| 123 | * reinit_completion - reinitialize a completion structure |
| 124 | * @x: pointer to completion structure that is to be reinitialized |
| 125 | * |
| 126 | * This inline function should be used to reinitialize a completion structure so it can |
| 127 | * be reused. This is especially important after complete_all() is used. |
| 128 | */ |
| 129 | static inline void reinit_completion(struct completion *x) |
| 130 | { |
| 131 | x->done = 0; |
| 132 | } |
| 133 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 134 | extern void wait_for_completion(struct completion *); |
Vladimir Davydov | 686855f | 2013-02-14 18:19:58 +0400 | [diff] [blame] | 135 | extern void wait_for_completion_io(struct completion *); |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 136 | extern int wait_for_completion_interruptible(struct completion *x); |
Matthew Wilcox | 009e577 | 2007-12-06 12:29:54 -0500 | [diff] [blame] | 137 | extern int wait_for_completion_killable(struct completion *x); |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 138 | extern unsigned long wait_for_completion_timeout(struct completion *x, |
| 139 | unsigned long timeout); |
Vladimir Davydov | 686855f | 2013-02-14 18:19:58 +0400 | [diff] [blame] | 140 | extern unsigned long wait_for_completion_io_timeout(struct completion *x, |
| 141 | unsigned long timeout); |
NeilBrown | 6bf4123 | 2011-01-05 12:50:16 +1100 | [diff] [blame] | 142 | extern long wait_for_completion_interruptible_timeout( |
| 143 | struct completion *x, unsigned long timeout); |
| 144 | extern long wait_for_completion_killable_timeout( |
| 145 | struct completion *x, unsigned long timeout); |
Dave Chinner | be4de35 | 2008-08-15 00:40:44 -0700 | [diff] [blame] | 146 | extern bool try_wait_for_completion(struct completion *x); |
| 147 | extern bool completion_done(struct completion *x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Ingo Molnar | b15136e | 2007-10-24 18:23:48 +0200 | [diff] [blame] | 149 | extern void complete(struct completion *); |
| 150 | extern void complete_all(struct completion *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | #endif |