blob: 5d5aaae3af433ff62b6e03f107c7dfe88a06b522 [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>
12
Randy Dunlapee2f1542010-10-26 14:17:25 -070013/*
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020014 * 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(),
Wolfram Sangc32f74a2013-11-14 14:32:01 -080022 * reinit_completion(), and macros DECLARE_COMPLETION(),
23 * DECLARE_COMPLETION_ONSTACK().
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020024 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025struct 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 Molnarf86bf9b2006-07-10 04:44:05 -070033#define COMPLETION_INITIALIZER_ONSTACK(work) \
34 ({ init_completion(&work); work; })
35
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020036/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070037 * DECLARE_COMPLETION - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020038 * @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 Torvalds1da177e2005-04-16 15:20:36 -070044#define DECLARE_COMPLETION(work) \
45 struct completion work = COMPLETION_INITIALIZER(work)
46
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070047/*
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 Diggs65eb3dc2008-08-26 10:26:54 +020052/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070053 * DECLARE_COMPLETION_ONSTACK - declare and initialize a completion structure
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020054 * @work: identifier for the completion structure
55 *
56 * This macro declares and initializes a completion structure on the kernel
57 * stack.
58 */
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070059#ifdef CONFIG_LOCKDEP
60# define DECLARE_COMPLETION_ONSTACK(work) \
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070061 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070062#else
63# define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
64#endif
65
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020066/**
Randy Dunlapee2f1542010-10-26 14:17:25 -070067 * init_completion - Initialize a dynamically allocated completion
Wolfram Sangc32f74a2013-11-14 14:32:01 -080068 * @x: pointer to completion structure that is to be initialized
Kevin Diggs65eb3dc2008-08-26 10:26:54 +020069 *
70 * This inline function will initialize a dynamically created completion
71 * structure.
72 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static inline void init_completion(struct completion *x)
74{
75 x->done = 0;
76 init_waitqueue_head(&x->wait);
77}
78
Wolfram Sangc32f74a2013-11-14 14:32:01 -080079/**
80 * reinit_completion - reinitialize a completion structure
81 * @x: pointer to completion structure that is to be reinitialized
82 *
83 * This inline function should be used to reinitialize a completion structure so it can
84 * be reused. This is especially important after complete_all() is used.
85 */
86static inline void reinit_completion(struct completion *x)
87{
88 x->done = 0;
89}
90
Ingo Molnarb15136e2007-10-24 18:23:48 +020091extern void wait_for_completion(struct completion *);
Vladimir Davydov686855f2013-02-14 18:19:58 +040092extern void wait_for_completion_io(struct completion *);
Ingo Molnarb15136e2007-10-24 18:23:48 +020093extern int wait_for_completion_interruptible(struct completion *x);
Matthew Wilcox009e5772007-12-06 12:29:54 -050094extern int wait_for_completion_killable(struct completion *x);
Ingo Molnarb15136e2007-10-24 18:23:48 +020095extern unsigned long wait_for_completion_timeout(struct completion *x,
96 unsigned long timeout);
Vladimir Davydov686855f2013-02-14 18:19:58 +040097extern unsigned long wait_for_completion_io_timeout(struct completion *x,
98 unsigned long timeout);
NeilBrown6bf41232011-01-05 12:50:16 +110099extern long wait_for_completion_interruptible_timeout(
100 struct completion *x, unsigned long timeout);
101extern long wait_for_completion_killable_timeout(
102 struct completion *x, unsigned long timeout);
Dave Chinnerbe4de352008-08-15 00:40:44 -0700103extern bool try_wait_for_completion(struct completion *x);
104extern bool completion_done(struct completion *x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ingo Molnarb15136e2007-10-24 18:23:48 +0200106extern void complete(struct completion *);
107extern void complete_all(struct completion *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif