blob: 57faa60de9bd48bef32a6acf9eeff9785b09e48f [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.
8 * See kernel/sched.c for details.
9 */
10
11#include <linux/wait.h>
12
13struct completion {
14 unsigned int done;
15 wait_queue_head_t wait;
16};
17
18#define COMPLETION_INITIALIZER(work) \
19 { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }
20
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070021#define COMPLETION_INITIALIZER_ONSTACK(work) \
22 ({ init_completion(&work); work; })
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define DECLARE_COMPLETION(work) \
25 struct completion work = COMPLETION_INITIALIZER(work)
26
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070027/*
28 * Lockdep needs to run a non-constant initializer for on-stack
29 * completions - so we use the _ONSTACK() variant for those that
30 * are on the kernel stack:
31 */
32#ifdef CONFIG_LOCKDEP
33# define DECLARE_COMPLETION_ONSTACK(work) \
Ingo Molnarf86bf9b2006-07-10 04:44:05 -070034 struct completion work = COMPLETION_INITIALIZER_ONSTACK(work)
Ingo Molnar8b3db9c2006-07-03 00:24:28 -070035#else
36# define DECLARE_COMPLETION_ONSTACK(work) DECLARE_COMPLETION(work)
37#endif
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static inline void init_completion(struct completion *x)
40{
41 x->done = 0;
42 init_waitqueue_head(&x->wait);
43}
44
Ingo Molnarb15136e2007-10-24 18:23:48 +020045extern void wait_for_completion(struct completion *);
46extern int wait_for_completion_interruptible(struct completion *x);
Matthew Wilcox009e5772007-12-06 12:29:54 -050047extern int wait_for_completion_killable(struct completion *x);
Ingo Molnarb15136e2007-10-24 18:23:48 +020048extern unsigned long wait_for_completion_timeout(struct completion *x,
49 unsigned long timeout);
50extern unsigned long wait_for_completion_interruptible_timeout(
51 struct completion *x, unsigned long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Ingo Molnarb15136e2007-10-24 18:23:48 +020053extern void complete(struct completion *);
54extern void complete_all(struct completion *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define INIT_COMPLETION(x) ((x).done = 0)
57
David Chinner39d2f1a2008-08-13 16:40:43 +100058
59/**
60 * try_wait_for_completion - try to decrement a completion without blocking
61 * @x: completion structure
62 *
63 * Returns: 0 if a decrement cannot be done without blocking
64 * 1 if a decrement succeeded.
65 *
66 * If a completion is being used as a counting completion,
67 * attempt to decrement the counter without blocking. This
68 * enables us to avoid waiting if the resource the completion
69 * is protecting is not available.
70 */
71static inline bool try_wait_for_completion(struct completion *x)
72{
73 int ret = 1;
74
75 spin_lock_irq(&x->wait.lock);
76 if (!x->done)
77 ret = 0;
78 else
79 x->done--;
80 spin_unlock_irq(&x->wait.lock);
81 return ret;
82}
83
84/**
85 * completion_done - Test to see if a completion has any waiters
86 * @x: completion structure
87 *
88 * Returns: 0 if there are waiters (wait_for_completion() in progress)
89 * 1 if there are no waiters.
90 *
91 */
92static inline bool completion_done(struct completion *x)
93{
94 int ret = 1;
95
96 spin_lock_irq(&x->wait.lock);
97 if (!x->done)
98 ret = 0;
99 spin_unlock_irq(&x->wait.lock);
100 return ret;
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif