blob: 519e94915d1850628ba3a79f7341c29ce3a68f40 [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>
13
Randy Dunlapee2f1542010-10-26 14:17:25 -070014/*
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020015 * struct completion - structure used to maintain state for a "completion"
16 *
17 * This is the opaque structure used to maintain the state for a "completion".
18 * Completions currently use a FIFO to queue threads that have to wait for
19 * the "completion" event.
20 *
21 * See also: complete(), wait_for_completion() (and friends _timeout,
22 * _interruptible, _interruptible_timeout, and _killable), init_completion(),
Wolfram Sangc32f74a2013-11-14 14:32:01 -080023 * reinit_completion(), and macros DECLARE_COMPLETION(),
24 * DECLARE_COMPLETION_ONSTACK().
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020025 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct completion {
27 unsigned int done;
28 wait_queue_head_t wait;
29};
30
Byungchul Parka7967bc2017-10-25 17:56:03 +090031#define init_completion_map(x, m) __init_completion(x)
Byungchul Parkcd8084f2017-08-07 16:12:56 +090032#define init_completion(x) __init_completion(x)
33static inline void complete_acquire(struct completion *x) {}
34static inline void complete_release(struct completion *x) {}
Byungchul Parkcd8084f2017-08-07 16:12:56 +090035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define COMPLETION_INITIALIZER(work) \
37 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
38
Byungchul Parka7967bc2017-10-25 17:56:03 +090039#define COMPLETION_INITIALIZER_ONSTACK_MAP(work, map) \
40 (*({ init_completion_map(&(work), &(map)); &(work); }))
41
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070042#define COMPLETION_INITIALIZER_ONSTACK(work) \
Boqun Fengec810482017-08-23 23:25:38 +080043 (*({ init_completion(&work); &work; }))
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070044
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020045/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070046 * DECLARE_COMPLETION - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020047 * @work: identifier for the completion structure
48 *
49 * This macro declares and initializes a completion structure. Generally used
50 * for static declarations. You should use the _ONSTACK variant for automatic
51 * variables.
52 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define DECLARE_COMPLETION(work) \
54 struct completion work = COMPLETION_INITIALIZER(work)
55
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070056/*
57 * Lockdep needs to run a non-constant initializer for on-stack
58 * completions - so we use the _ONSTACK() variant for those that
59 * are on the kernel stack:
60 */
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020061/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070062 * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020063 * @work: identifier for the completion structure
64 *
65 * This macro declares and initializes a completion structure on the kernel
66 * stack.
67 */
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070068#ifdef CONFIG_LOCKDEP
69# define DECLARE_COMPLETION_ONSTACK(work) \
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070070 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
Byungchul Parka7967bc2017-10-25 17:56:03 +090071# define DECLARE_COMPLETION_ONSTACK_MAP(work, map) \
72 struct completion work = COMPLETION_INITIALIZER_ONSTACK_MAP(work, map)
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070073#else
74# define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
Byungchul Parka7967bc2017-10-25 17:56:03 +090075# define DECLARE_COMPLETION_ONSTACK_MAP(work, map) DECLARE_COMPLETION(work)
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070076#endif
77
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020078/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070079 * init_completion - Initialize a dynamically allocated completion
Wolfram Sangc32f74a2013-11-14 14:32:01 -080080 * @x: pointer to completion structure that is to be initialized
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020081 *
82 * This inline function will initialize a dynamically created completion
83 * structure.
84 */
Byungchul Parkcd8084f2017-08-07 16:12:56 +090085static inline void __init_completion(struct completion *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 x->done = 0;
88 init_waitqueue_head(&x->wait);
89}
90
Wolfram Sangc32f74a2013-11-14 14:32:01 -080091/**
92 * reinit_completion - reinitialize a completion structure
93 * @x: pointer to completion structure that is to be reinitialized
94 *
95 * This inline function should be used to reinitialize a completion structure so it can
96 * be reused. This is especially important after complete_all() is used.
97 */
98static inline void reinit_completion(struct completion *x)
99{
100 x->done = 0;
101}
102
Ingo Molnarb15136e2007-10-24 18:23:48 +0200103extern void wait_for_completion(struct completion *);
Vladimir Davydov686855f2013-02-14 18:19:58 +0400104extern void wait_for_completion_io(struct completion *);
Ingo Molnarb15136e2007-10-24 18:23:48 +0200105extern int wait_for_completion_interruptible(struct completion *x);
Matthew Wilcox009e5772007-12-06 12:29:54 -0500106extern int wait_for_completion_killable(struct completion *x);
Ingo Molnarb15136e2007-10-24 18:23:48 +0200107extern unsigned long wait_for_completion_timeout(struct completion *x,
108 unsigned long timeout);
Vladimir Davydov686855f2013-02-14 18:19:58 +0400109extern unsigned long wait_for_completion_io_timeout(struct completion *x,
110 unsigned long timeout);
NeilBrown6bf41232011-01-05 12:50:16 +1100111extern long wait_for_completion_interruptible_timeout(
112 struct completion *x, unsigned long timeout);
113extern long wait_for_completion_killable_timeout(
114 struct completion *x, unsigned long timeout);
Dave Chinnerbe4de352008-08-15 00:40:44 -0700115extern bool try_wait_for_completion(struct completion *x);
116extern bool completion_done(struct completion *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Ingo Molnarb15136e2007-10-24 18:23:48 +0200118extern void complete(struct completion *);
119extern void complete_all(struct completion *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif