blob: 4da49916ef3f2f4fcb0db44ad5490cd7cba19360 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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 Zijlstrab8a21622013-10-04 22:06:53 +02008 * See kernel/sched/completion.c for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/wait.h>
Byungchul Parkea3f2c02017-08-17 17:57:41 +090012#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090013#include <linux/lockdep.h>
14#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Randy Dunlapee2f1542010-10-26 14:17:25 -070016/*
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020017 * 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 Sangc32f74a2013-11-14 14:32:01 -080025 * reinit_completion(), and macros DECLARE_COMPLETION(),
26 * DECLARE_COMPLETION_ONSTACK().
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct completion {
29 unsigned int done;
30 wait_queue_head_t wait;
Byungchul Parkea3f2c02017-08-17 17:57:41 +090031#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090032 struct lockdep_map_cross map;
33#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034};
35
Byungchul Parkea3f2c02017-08-17 17:57:41 +090036#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090037static inline void complete_acquire(struct completion *x)
38{
39 lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_);
40}
41
42static inline void complete_release(struct completion *x)
43{
44 lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_);
45}
46
47static inline void complete_release_commit(struct completion *x)
48{
49 lock_commit_crosslock((struct lockdep_map *)&x->map);
50}
51
Byungchul Parka7967bc2017-10-25 17:56:03 +090052#define init_completion_map(x, m) \
53do { \
54 lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \
55 (m)->name, (m)->key, 0); \
56 __init_completion(x); \
57} while (0)
58
Byungchul Parkcd8084f2017-08-07 16:12:56 +090059#define init_completion(x) \
60do { \
61 static struct lock_class_key __key; \
62 lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \
Byungchul Park24208432017-10-25 17:55:59 +090063 "(completion)" #x, \
Byungchul Parkcd8084f2017-08-07 16:12:56 +090064 &__key, 0); \
65 __init_completion(x); \
66} while (0)
67#else
Byungchul Parka7967bc2017-10-25 17:56:03 +090068#define init_completion_map(x, m) __init_completion(x)
Byungchul Parkcd8084f2017-08-07 16:12:56 +090069#define init_completion(x) __init_completion(x)
70static inline void complete_acquire(struct completion *x) {}
71static inline void complete_release(struct completion *x) {}
72static inline void complete_release_commit(struct completion *x) {}
73#endif
74
Byungchul Parkea3f2c02017-08-17 17:57:41 +090075#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090076#define COMPLETION_INITIALIZER(work) \
77 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \
Byungchul Park24208432017-10-25 17:55:59 +090078 STATIC_CROSS_LOCKDEP_MAP_INIT("(completion)" #work, &(work)) }
Byungchul Parkcd8084f2017-08-07 16:12:56 +090079#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define COMPLETION_INITIALIZER(work) \
81 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
Byungchul Parkcd8084f2017-08-07 16:12:56 +090082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Byungchul Parka7967bc2017-10-25 17:56:03 +090084#define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \
85 (*({ init_completion_map(&(work), &(map)); &(work); }))
86
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070087#define COMPLETION_INITIALIZER_ONSTACK(work) \
Boqun Fengec810482017-08-23 23:25:38 +080088 (*({ init_completion(&work); &work; }))
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070089
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020090/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070091 * DECLARE_COMPLETION - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020092 * @work: identifier for the completion structure
93 *
94 * This macro declares and initializes a completion structure. Generally used
95 * for static declarations. You should use the _ONSTACK variant for automatic
96 * variables.
97 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#define DECLARE_COMPLETION(work) \
99 struct completion work = COMPLETION_INITIALIZER(work)
100
Ingo Molnar8b3db9c2006-07-03 00:24:28 -0700101/*
102 * Lockdep needs to run a non-constant initializer for on-stack
103 * completions - so we use the _ONSTACK() variant for those that
104 * are on the kernel stack:
105 */
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200106/**
Randy Dunlapee2f1542010-10-26 14:17:25 -0700107 * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200108 * @work: identifier for the completion structure
109 *
110 * This macro declares and initializes a completion structure on the kernel
111 * stack.
112 */
Ingo Molnar8b3db9c2006-07-03 00:24:28 -0700113#ifdef CONFIG_LOCKDEP
114# define DECLARE_COMPLETION_ONSTACK(work) \
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700115 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
Byungchul Parka7967bc2017-10-25 17:56:03 +0900116# define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \
117 struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map)
Ingo Molnar8b3db9c2006-07-03 00:24:28 -0700118#else
119# define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
Byungchul Parka7967bc2017-10-25 17:56:03 +0900120# define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work)
Ingo Molnar8b3db9c2006-07-03 00:24:28 -0700121#endif
122
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200123/**
Randy Dunlapee2f1542010-10-26 14:17:25 -0700124 * init_completion - Initialize a dynamically allocated completion
Wolfram Sangc32f74a2013-11-14 14:32:01 -0800125 * @x: pointer to completion structure that is to be initialized
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200126 *
127 * This inline function will initialize a dynamically created completion
128 * structure.
129 */
Byungchul Parkcd8084f2017-08-07 16:12:56 +0900130static inline void __init_completion(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 x->done = 0;
133 init_waitqueue_head(&x->wait);
134}
135
Wolfram Sangc32f74a2013-11-14 14:32:01 -0800136/**
137 * reinit_completion - reinitialize a completion structure
138 * @x: pointer to completion structure that is to be reinitialized
139 *
140 * This inline function should be used to reinitialize a completion structure so it can
141 * be reused. This is especially important after complete_all() is used.
142 */
143static inline void reinit_completion(struct completion *x)
144{
145 x->done = 0;
146}
147
Ingo Molnarb15136e2007-10-24 18:23:48 +0200148extern void wait_for_completion(struct completion *);
Vladimir Davydov686855f2013-02-14 18:19:58 +0400149extern void wait_for_completion_io(struct completion *);
Ingo Molnarb15136e2007-10-24 18:23:48 +0200150extern int wait_for_completion_interruptible(struct completion *x);
Matthew Wilcox009e5772007-12-06 12:29:54 -0500151extern int wait_for_completion_killable(struct completion *x);
Ingo Molnarb15136e2007-10-24 18:23:48 +0200152extern unsigned long wait_for_completion_timeout(struct completion *x,
153 unsigned long timeout);
Vladimir Davydov686855f2013-02-14 18:19:58 +0400154extern unsigned long wait_for_completion_io_timeout(struct completion *x,
155 unsigned long timeout);
NeilBrown6bf41232011-01-05 12:50:16 +1100156extern long wait_for_completion_interruptible_timeout(
157 struct completion *x, unsigned long timeout);
158extern long wait_for_completion_killable_timeout(
159 struct completion *x, unsigned long timeout);
Dave Chinnerbe4de352008-08-15 00:40:44 -0700160extern bool try_wait_for_completion(struct completion *x);
161extern bool completion_done(struct completion *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Ingo Molnarb15136e2007-10-24 18:23:48 +0200163extern void complete(struct completion *);
164extern void complete_all(struct completion *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif