| @@ -1,165 +1,170 @@ | | | @@ -1,165 +1,170 @@ |
1 | /* $NetBSD: completion.h,v 1.1.2.2 2013/07/24 03:15:59 riastradh Exp $ */ | | 1 | /* $NetBSD: completion.h,v 1.1.2.3 2013/07/24 03:29:43 riastradh Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2013 The NetBSD Foundation, Inc. |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * This code is derived from software contributed to The NetBSD Foundation | | 7 | * This code is derived from software contributed to The NetBSD Foundation |
8 | * by Taylor R. Campbell. | | 8 | * by Taylor R. Campbell. |
9 | * | | 9 | * |
10 | * Redistribution and use in source and binary forms, with or without | | 10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions | | 11 | * modification, are permitted provided that the following conditions |
12 | * are met: | | 12 | * are met: |
13 | * 1. Redistributions of source code must retain the above copyright | | 13 | * 1. Redistributions of source code must retain the above copyright |
14 | * notice, this list of conditions and the following disclaimer. | | 14 | * notice, this list of conditions and the following disclaimer. |
15 | * 2. Redistributions in binary form must reproduce the above copyright | | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * notice, this list of conditions and the following disclaimer in the | | 16 | * notice, this list of conditions and the following disclaimer in the |
17 | * documentation and/or other materials provided with the distribution. | | 17 | * documentation and/or other materials provided with the distribution. |
18 | * | | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | | 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | | 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | | 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | | 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. | | 29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ | | 30 | */ |
31 | | | 31 | |
32 | #ifndef _LINUX_COMPLETION_H_ | | 32 | #ifndef _LINUX_COMPLETION_H_ |
33 | #define _LINUX_COMPLETION_H_ | | 33 | #define _LINUX_COMPLETION_H_ |
34 | | | 34 | |
35 | #include <sys/types.h> | | 35 | #include <sys/types.h> |
36 | #include <sys/condvar.h> | | 36 | #include <sys/condvar.h> |
37 | #include <sys/mutex.h> | | 37 | #include <sys/mutex.h> |
38 | | | 38 | |
39 | #include <machine/limits.h> | | 39 | #include <machine/limits.h> |
40 | | | 40 | |
41 | struct completion { | | 41 | struct completion { |
42 | kmutex_t c_lock; | | 42 | kmutex_t c_lock; |
43 | kcondvar_t c_cv; | | 43 | kcondvar_t c_cv; |
44 | | | 44 | |
45 | /* | | 45 | /* |
46 | * c_done is either | | 46 | * c_done is either |
47 | * | | 47 | * |
48 | * . -1, meaning it's open season and we're done for good and | | 48 | * . -1, meaning it's open season and we're done for good and |
49 | * nobody need wait any more; | | 49 | * nobody need wait any more; |
50 | * | | 50 | * |
51 | * . 0, meaning nothing is done, so waiters must block; or | | 51 | * . 0, meaning nothing is done, so waiters must block; or |
52 | * | | 52 | * |
53 | * . a positive integer, meaning that many waiters can | | 53 | * . a positive integer, meaning that many waiters can |
54 | * proceed before further waiters must block. | | 54 | * proceed before further waiters must block. |
55 | * | | 55 | * |
56 | * Negative values other than -1 are not allowed. | | 56 | * Negative values other than -1 are not allowed. |
57 | */ | | 57 | */ |
58 | int c_done; | | 58 | int c_done; |
59 | }; | | 59 | }; |
60 | | | 60 | |
61 | /* | | 61 | /* |
62 | * Initialize a new completion object. | | 62 | * Initialize a new completion object. |
63 | */ | | 63 | */ |
64 | static inline void | | 64 | static inline void |
65 | init_completion(struct completion *completion) | | 65 | init_completion(struct completion *completion) |
66 | { | | 66 | { |
67 | | | 67 | |
68 | mutex_init(&completion->c_lock, MUTEX_DEFAULT, IPL_NONE); | | 68 | mutex_init(&completion->c_lock, MUTEX_DEFAULT, IPL_NONE); |
69 | cv_init(&completion->c_cv, "lnxcmplt"); | | 69 | cv_init(&completion->c_cv, "lnxcmplt"); |
70 | completion->c_done = 0; | | 70 | completion->c_done = 0; |
71 | } | | 71 | } |
72 | | | 72 | |
73 | /* | | 73 | /* |
74 | * Notify one waiter of completion, but not any future ones. | | 74 | * Notify one waiter of completion, but not any future ones. |
75 | */ | | 75 | */ |
76 | static inline void | | 76 | static inline void |
77 | complete(struct completion *completion) | | 77 | complete(struct completion *completion) |
78 | { | | 78 | { |
79 | | | 79 | |
80 | mutex_enter(&completion->c_lock); | | 80 | mutex_enter(&completion->c_lock); |
81 | | | 81 | |
82 | /* If it's not open season, wake one waiter. */ | | 82 | /* If it's not open season, wake one waiter. */ |
83 | if (completion->c_done >= 0) { | | 83 | if (completion->c_done >= 0) { |
84 | KASSERT(completion->c_done < INT_MAX); /* XXX check */ | | 84 | KASSERT(completion->c_done < INT_MAX); /* XXX check */ |
85 | completion->c_done++; | | 85 | completion->c_done++; |
86 | cv_signal(&completion->c_cv); | | 86 | cv_signal(&completion->c_cv); |
87 | } else { | | 87 | } else { |
88 | KASSERT(completion->c_done == -1); | | 88 | KASSERT(completion->c_done == -1); |
89 | } | | 89 | } |
90 | | | 90 | |
91 | mutex_exit(&completion->c_lock); | | 91 | mutex_exit(&completion->c_lock); |
92 | } | | 92 | } |
93 | | | 93 | |
94 | /* | | 94 | /* |
95 | * Notify all waiters, present and future (until INIT_COMPLETION), of | | 95 | * Notify all waiters, present and future (until INIT_COMPLETION), of |
96 | * completion. | | 96 | * completion. |
97 | */ | | 97 | */ |
98 | static inline void | | 98 | static inline void |
99 | complete_all(struct completion *completion) | | 99 | complete_all(struct completion *completion) |
100 | { | | 100 | { |
101 | | | 101 | |
102 | mutex_enter(&completion->c_lock); | | 102 | mutex_enter(&completion->c_lock); |
103 | | | 103 | |
104 | /* If it's not open season, make it open season and wake everyone. */ | | 104 | /* If it's not open season, make it open season and wake everyone. */ |
105 | if (completion->c_done >= 0) { | | 105 | if (completion->c_done >= 0) { |
106 | completion->c_done = -1; | | 106 | completion->c_done = -1; |
107 | cv_broadcast(&completion->c_cv); | | 107 | cv_broadcast(&completion->c_cv); |
108 | } else { | | 108 | } else { |
109 | KASSERT(completion->c_done == -1); | | 109 | KASSERT(completion->c_done == -1); |
110 | } | | 110 | } |
111 | | | 111 | |
112 | mutex_exit(&completion->c_lock); | | 112 | mutex_exit(&completion->c_lock); |
113 | } | | 113 | } |
114 | | | 114 | |
115 | /* | | 115 | /* |
116 | * Reverse the effect of complete_all so that subsequent waiters block | | 116 | * Reverse the effect of complete_all so that subsequent waiters block |
117 | * until someone calls complete or complete_all. | | 117 | * until someone calls complete or complete_all. |
118 | * | | 118 | * |
119 | * This operation is very different from its lowercase counterpart. | | 119 | * This operation is very different from its lowercase counterpart. |
| | | 120 | * |
| | | 121 | * For some reason this works on the completion object itself, not on a |
| | | 122 | * pointer thereto, so it must be a macro. |
120 | */ | | 123 | */ |
| | | 124 | #define INIT_COMPLETION(COMPLETION) INIT_COMPLETION_blorp(&(COMPLETION)) |
| | | 125 | |
121 | static inline void | | 126 | static inline void |
122 | INIT_COMPLETION(struct completion *completion) | | 127 | INIT_COMPLETION_blorp(struct completion *completion) |
123 | { | | 128 | { |
124 | | | 129 | |
125 | mutex_enter(&completion->c_lock); | | 130 | mutex_enter(&completion->c_lock); |
126 | completion->c_done = 0; | | 131 | completion->c_done = 0; |
127 | /* No notify -- waiters are interested only in nonzero values. */ | | 132 | /* No notify -- waiters are interested only in nonzero values. */ |
128 | mutex_exit(&completion->c_lock); | | 133 | mutex_exit(&completion->c_lock); |
129 | } | | 134 | } |
130 | | | 135 | |
131 | /* | | 136 | /* |
132 | * Wait interruptibly with a timeout for someone to call complete or | | 137 | * Wait interruptibly with a timeout for someone to call complete or |
133 | * complete_all. | | 138 | * complete_all. |
134 | */ | | 139 | */ |
135 | static inline int | | 140 | static inline int |
136 | wait_for_completion_interruptible_timeout(struct completion *completion, | | 141 | wait_for_completion_interruptible_timeout(struct completion *completion, |
137 | unsigned long ticks) | | 142 | unsigned long ticks) |
138 | { | | 143 | { |
139 | int error; | | 144 | int error; |
140 | | | 145 | |
141 | mutex_enter(&completion->c_lock); | | 146 | mutex_enter(&completion->c_lock); |
142 | | | 147 | |
143 | /* Wait until c_done is nonzero. */ | | 148 | /* Wait until c_done is nonzero. */ |
144 | while (completion->c_done == 0) { | | 149 | while (completion->c_done == 0) { |
145 | /* XXX errno NetBSD->Linux */ | | 150 | /* XXX errno NetBSD->Linux */ |
146 | error = -cv_timedwait_sig(&completion->c_cv, | | 151 | error = -cv_timedwait_sig(&completion->c_cv, |
147 | &completion->c_lock, ticks); | | 152 | &completion->c_lock, ticks); |
148 | if (error) | | 153 | if (error) |
149 | goto out; | | 154 | goto out; |
150 | } | | 155 | } |
151 | | | 156 | |
152 | /* Claim a completion if it's not open season. */ | | 157 | /* Claim a completion if it's not open season. */ |
153 | if (completion->c_done > 0) | | 158 | if (completion->c_done > 0) |
154 | completion->c_done--; | | 159 | completion->c_done--; |
155 | else | | 160 | else |
156 | KASSERT(completion->c_done == -1); | | 161 | KASSERT(completion->c_done == -1); |
157 | | | 162 | |
158 | /* Success! */ | | 163 | /* Success! */ |
159 | error = 0; | | 164 | error = 0; |
160 | | | 165 | |
161 | out: mutex_exit(&completion->c_lock); | | 166 | out: mutex_exit(&completion->c_lock); |
162 | return error; | | 167 | return error; |
163 | } | | 168 | } |
164 | | | 169 | |
165 | #endif /* _LINUX_COMPLETION_H_ */ | | 170 | #endif /* _LINUX_COMPLETION_H_ */ |