xref: /dflybsd-src/sys/dev/drm/include/linux/completion.h (revision a62226e46c982d037de05e1bb0894805c0b7a32f)
1 /*
2  * Copyright (c) 2014 François Tigeot
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 
31 struct completion {
32 	unsigned int done;
33 	wait_queue_head_t wait;
34 };
35 
36 static inline void
37 init_completion(struct completion *c)
38 {
39 	c->done = 0;
40 	init_waitqueue_head(&c->wait);
41 }
42 
43 #define	INIT_COMPLETION(c)	(c.done = 0)
44 
45 static inline void
46 complete(struct completion *c)
47 {
48 	spin_lock(&c->wait.lock);
49 	c->done++;
50 	wakeup_one(&c->wait);
51 	spin_unlock(&c->wait.lock);
52 }
53 
54 static inline void
55 complete_all(struct completion *c)
56 {
57 	spin_lock(&c->wait.lock);
58 	c->done++;
59 	wakeup(&c->wait);
60 	spin_unlock(&c->wait.lock);
61 }
62 
63 static inline long
64 wait_for_completion_interruptible_timeout(struct completion *x,
65 		unsigned long timeout)
66 {
67 	int start_jiffies, elapsed_jiffies, remaining_jiffies;
68 	bool timeout_expired = false, awakened = false;
69 	long ret = 1;
70 
71 	start_jiffies = ticks;
72 
73 	spin_lock(&x->wait.lock);
74 	while (x->done == 0 && !timeout_expired) {
75 		ret = ssleep(&x->wait, &x->wait.lock, PCATCH, "wfcit", timeout);
76 		switch(ret) {
77 		case EWOULDBLOCK:
78 			timeout_expired = true;
79 			ret = 0;
80 			break;
81 		case ERESTART:
82 			ret = -ERESTART;	/* -ERESTARTSYS on Linux */
83 			break;
84 		case 0:
85 			awakened = true;
86 			break;
87 		}
88 	}
89 	spin_unlock(&x->wait.lock);
90 
91 	if (awakened) {
92 		elapsed_jiffies = ticks - start_jiffies;
93 		remaining_jiffies = timeout - elapsed_jiffies;
94 		if (remaining_jiffies > 0)
95 			ret = remaining_jiffies;
96 	}
97 
98 	return ret;
99 }
100 
101 #endif	/* _LINUX_COMPLETION_H_ */
102