2 * Copyright (c) 2014-2017 François Tigeot <ftigeot@wolfpond.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef _LINUX_COMPLETION_H_
27 #define _LINUX_COMPLETION_H_
29 #include <linux/wait.h>
30 #include <linux/errno.h>
34 wait_queue_head_t wait;
38 init_completion(struct completion *c)
41 init_waitqueue_head(&c->wait);
45 reinit_completion(struct completion *c)
50 #define INIT_COMPLETION(c) (c.done = 0)
53 * Completion interlock and wakeup. Be careful not to execute the wakeup
54 * from inside the spinlock as this can deadlock if the IPIQ fifo is full.
55 * (also note that wakeup() is asynchronous anyway, so no point doing that).
58 complete(struct completion *c)
60 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
62 lockmgr(&c->wait.lock, LK_RELEASE);
67 complete_all(struct completion *c)
69 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
71 lockmgr(&c->wait.lock, LK_RELEASE);
76 __wait_for_completion_generic(struct completion *c,
77 unsigned long timeout, int flags)
79 int start_jiffies, elapsed_jiffies, remaining_jiffies;
80 bool timeout_expired = false, awakened = false;
83 start_jiffies = ticks;
85 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
86 while (c->done == 0 && !timeout_expired) {
87 ret = lksleep(&c->wait, &c->wait.lock, flags, "lwfcg", timeout);
90 timeout_expired = true;
101 lockmgr(&c->wait.lock, LK_RELEASE);
104 elapsed_jiffies = ticks - start_jiffies;
105 remaining_jiffies = timeout - elapsed_jiffies;
106 if (remaining_jiffies > 0)
107 ret = remaining_jiffies;
114 wait_for_completion_interruptible_timeout(struct completion *c,
115 unsigned long timeout)
117 return __wait_for_completion_generic(c, timeout, PCATCH);
120 static inline unsigned long
121 wait_for_completion_timeout(struct completion *c, unsigned long timeout)
123 return __wait_for_completion_generic(c, timeout, 0);
127 * try_wait_for_completion: try to decrement a completion without blocking
129 * return: false if the completion thread would need to be blocked/queued
130 * true if a non-blocking decrement was successful
133 try_wait_for_completion(struct completion *c)
137 /* we can't decrement c->done below 0 */
138 if (READ_ONCE(c->done) == 0)
141 lockmgr(&c->wait.lock, LK_EXCLUSIVE);
146 lockmgr(&c->wait.lock, LK_RELEASE);
151 #endif /* _LINUX_COMPLETION_H_ */