xref: /dragonfly/sys/dev/drm/include/linux/completion.h (revision 5503793e)
1 /*
2  * Copyright (c) 2014-2017 François Tigeot <ftigeot@wolfpond.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  */
26 #ifndef	_LINUX_COMPLETION_H_
27 #define	_LINUX_COMPLETION_H_
28 
29 #include <linux/wait.h>
30 #include <linux/errno.h>
31 
32 struct completion {
33 	unsigned int done;
34 	wait_queue_head_t wait;
35 };
36 
37 static inline void
38 init_completion(struct completion *c)
39 {
40 	c->done = 0;
41 	init_waitqueue_head(&c->wait);
42 }
43 
44 static inline void
45 reinit_completion(struct completion *c)
46 {
47 	c->done = 0;
48 }
49 
50 #define	INIT_COMPLETION(c)	(c.done = 0)
51 
52 /*
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).
56  */
57 static inline void
58 complete(struct completion *c)
59 {
60 	lockmgr(&c->wait.lock, LK_EXCLUSIVE);
61 	c->done++;
62 	lockmgr(&c->wait.lock, LK_RELEASE);
63 	wakeup_one(&c->wait);
64 }
65 
66 static inline void
67 complete_all(struct completion *c)
68 {
69 	lockmgr(&c->wait.lock, LK_EXCLUSIVE);
70 	c->done++;
71 	lockmgr(&c->wait.lock, LK_RELEASE);
72 	wakeup(&c->wait);
73 }
74 
75 static inline long
76 __wait_for_completion_generic(struct completion *c,
77 			      unsigned long timeout, int flags)
78 {
79 	int start_jiffies, elapsed_jiffies, remaining_jiffies;
80 	bool timeout_expired = false, awakened = false;
81 	long ret = 1;
82 
83 	start_jiffies = ticks;
84 
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);
88 		switch(ret) {
89 		case EWOULDBLOCK:
90 			timeout_expired = true;
91 			ret = 0;
92 			break;
93 		case ERESTART:
94 			ret = -ERESTARTSYS;
95 			break;
96 		case 0:
97 			awakened = true;
98 			break;
99 		}
100 	}
101 	lockmgr(&c->wait.lock, LK_RELEASE);
102 
103 	if (awakened) {
104 		elapsed_jiffies = ticks - start_jiffies;
105 		remaining_jiffies = timeout - elapsed_jiffies;
106 		if (remaining_jiffies > 0)
107 			ret = remaining_jiffies;
108 	}
109 
110 	return ret;
111 }
112 
113 static inline long
114 wait_for_completion_interruptible_timeout(struct completion *c,
115 		unsigned long timeout)
116 {
117 	return __wait_for_completion_generic(c, timeout, PCATCH);
118 }
119 
120 static inline unsigned long
121 wait_for_completion_timeout(struct completion *c, unsigned long timeout)
122 {
123 	return __wait_for_completion_generic(c, timeout, 0);
124 }
125 
126 /*
127  * try_wait_for_completion: try to decrement a completion without blocking
128  * 			    its thread
129  * return: false if the completion thread would need to be blocked/queued
130  * 	   true if a non-blocking decrement was successful
131  */
132 static inline bool
133 try_wait_for_completion(struct completion *c)
134 {
135 	bool ret = false;
136 
137 	/* we can't decrement c->done below 0 */
138 	if (READ_ONCE(c->done) == 0)
139 		return false;
140 
141 	lockmgr(&c->wait.lock, LK_EXCLUSIVE);
142 	if (c->done > 0) {
143 		c->done--;
144 		ret = true;
145 	}
146 	lockmgr(&c->wait.lock, LK_RELEASE);
147 
148 	return ret;
149 }
150 
151 #endif	/* _LINUX_COMPLETION_H_ */
152