blob: 7828451e161aed19181a3b6cd6e210e6468cb190 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __LINUX_COMPLETION_H
3#define __LINUX_COMPLETION_H
4
5/*
6 * (C) Copyright 2001 Linus Torvalds
7 *
8 * Atomic wait-for-completion handler data structures.
Peter Zijlstrab8a21622013-10-04 22:06:53 +02009 * See kernel/sched/completion.c for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
12#include <linux/wait.h>
Byungchul Parkea3f2c02017-08-17 17:57:41 +090013#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090014#include <linux/lockdep.h>
15#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Randy Dunlapee2f1542010-10-26 14:17:25 -070017/*
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020018 * struct completion - structure used to maintain state for a "completion"
19 *
20 * This is the opaque structure used to maintain the state for a "completion".
21 * Completions currently use a FIFO to queue threads that have to wait for
22 * the "completion" event.
23 *
24 * See also: complete(), wait_for_completion() (and friends _timeout,
25 * _interruptible, _interruptible_timeout, and _killable), init_completion(),
Wolfram Sangc32f74a2013-11-14 14:32:01 -080026 * reinit_completion(), and macros DECLARE_COMPLETION(),
27 * DECLARE_COMPLETION_ONSTACK().
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070029struct completion {
30 unsigned int done;
31 wait_queue_head_t wait;
Byungchul Parkea3f2c02017-08-17 17:57:41 +090032#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090033 struct lockdep_map_cross map;
34#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Byungchul Parkea3f2c02017-08-17 17:57:41 +090037#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090038static inline void complete_acquire(struct completion *x)
39{
40 lock_acquire_exclusive((struct lockdep_map *)&x->map, 0, 0, NULL, _RET_IP_);
41}
42
43static inline void complete_release(struct completion *x)
44{
45 lock_release((struct lockdep_map *)&x->map, 0, _RET_IP_);
46}
47
48static inline void complete_release_commit(struct completion *x)
49{
50 lock_commit_crosslock((struct lockdep_map *)&x->map);
51}
52
53#define init_completion(x) \
54do { \
55 static struct lock_class_key __key; \
56 lockdep_init_map_crosslock((struct lockdep_map *)&(x)->map, \
57 "(complete)" #x, \
58 &__key, 0); \
59 __init_completion(x); \
60} while (0)
61#else
62#define init_completion(x) __init_completion(x)
63static inline void complete_acquire(struct completion *x) {}
64static inline void complete_release(struct completion *x) {}
65static inline void complete_release_commit(struct completion *x) {}
66#endif
67
Byungchul Parkea3f2c02017-08-17 17:57:41 +090068#ifdef CONFIG_LOCKDEP_COMPLETIONS
Byungchul Parkcd8084f2017-08-07 16:12:56 +090069#define COMPLETION_INITIALIZER(work) \
70 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait), \
71 STATIC_CROSS_LOCKDEP_MAP_INIT("(complete)" #work, &(work)) }
72#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define COMPLETION_INITIALIZER(work) \
74 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
Byungchul Parkcd8084f2017-08-07 16:12:56 +090075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070077#define COMPLETION_INITIALIZER_ONSTACK(work) \
Boqun Fengec810482017-08-23 23:25:38 +080078 (*({ init_completion(&work); &work; }))
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070079
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020080/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070081 * DECLARE_COMPLETION - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020082 * @work: identifier for the completion structure
83 *
84 * This macro declares and initializes a completion structure. Generally used
85 * for static declarations. You should use the _ONSTACK variant for automatic
86 * variables.
87 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#define DECLARE_COMPLETION(work) \
89 struct completion work = COMPLETION_INITIALIZER(work)
90
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070091/*
92 * Lockdep needs to run a non-constant initializer for on-stack
93 * completions - so we use the _ONSTACK() variant for those that
94 * are on the kernel stack:
95 */
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020096/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070097 * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020098 * @work: identifier for the completion structure
99 *
100 * This macro declares and initializes a completion structure on the kernel
101 * stack.
102 */
Ingo Molnar8b3db9c2006-07-03 00:24:28 -0700103#ifdef CONFIG_LOCKDEP
104# define DECLARE_COMPLETION_ONSTACK(work) \
Ingo Molnarf86bf9b2006-07-10 04:44:05 -0700105 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
Ingo Molnar8b3db9c2006-07-03 00:24:28 -0700106#else
107# define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
108#endif
109
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200110/**
Randy Dunlapee2f1542010-10-26 14:17:25 -0700111 * init_completion - Initialize a dynamically allocated completion
Wolfram Sangc32f74a2013-11-14 14:32:01 -0800112 * @x: pointer to completion structure that is to be initialized
Kevin Diggs65eb3dc2008-08-26 10:26:54 +0200113 *
114 * This inline function will initialize a dynamically created completion
115 * structure.
116 */
Byungchul Parkcd8084f2017-08-07 16:12:56 +0900117static inline void __init_completion(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 x->done = 0;
120 init_waitqueue_head(&x->wait);
121}
122
Wolfram Sangc32f74a2013-11-14 14:32:01 -0800123/**
124 * reinit_completion - reinitialize a completion structure
125 * @x: pointer to completion structure that is to be reinitialized
126 *
127 * This inline function should be used to reinitialize a completion structure so it can
128 * be reused. This is especially important after complete_all() is used.
129 */
130static inline void reinit_completion(struct completion *x)
131{
132 x->done = 0;
133}
134
Ingo Molnarb15136e2007-10-24 18:23:48 +0200135extern void wait_for_completion(struct completion *);
Vladimir Davydov686855f2013-02-14 18:19:58 +0400136extern void wait_for_completion_io(struct completion *);
Ingo Molnarb15136e2007-10-24 18:23:48 +0200137extern int wait_for_completion_interruptible(struct completion *x);
Matthew Wilcox009e5772007-12-06 12:29:54 -0500138extern int wait_for_completion_killable(struct completion *x);
Ingo Molnarb15136e2007-10-24 18:23:48 +0200139extern unsigned long wait_for_completion_timeout(struct completion *x,
140 unsigned long timeout);
Vladimir Davydov686855f2013-02-14 18:19:58 +0400141extern unsigned long wait_for_completion_io_timeout(struct completion *x,
142 unsigned long timeout);
NeilBrown6bf41232011-01-05 12:50:16 +1100143extern long wait_for_completion_interruptible_timeout(
144 struct completion *x, unsigned long timeout);
145extern long wait_for_completion_killable_timeout(
146 struct completion *x, unsigned long timeout);
Dave Chinnerbe4de352008-08-15 00:40:44 -0700147extern bool try_wait_for_completion(struct completion *x);
148extern bool completion_done(struct completion *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ingo Molnarb15136e2007-10-24 18:23:48 +0200150extern void complete(struct completion *);
151extern void complete_all(struct completion *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#endif