Rev 2745 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2745 | Rev 3603 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Jakub Jermar |
2 | * Copyright (c) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup sync |
29 | /** @addtogroup sync |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief Kernel backend for futexes. |
35 | * @brief Kernel backend for futexes. |
36 | */ |
36 | */ |
37 | 37 | ||
38 | #include <synch/futex.h> |
38 | #include <synch/futex.h> |
39 | #include <synch/rwlock.h> |
39 | #include <synch/rwlock.h> |
40 | #include <synch/spinlock.h> |
40 | #include <synch/spinlock.h> |
41 | #include <synch/synch.h> |
41 | #include <synch/synch.h> |
42 | #include <mm/frame.h> |
42 | #include <mm/frame.h> |
43 | #include <mm/page.h> |
43 | #include <mm/page.h> |
44 | #include <mm/slab.h> |
44 | #include <mm/slab.h> |
45 | #include <proc/thread.h> |
45 | #include <proc/thread.h> |
46 | #include <proc/task.h> |
46 | #include <proc/task.h> |
47 | #include <genarch/mm/page_pt.h> |
47 | #include <genarch/mm/page_pt.h> |
48 | #include <genarch/mm/page_ht.h> |
48 | #include <genarch/mm/page_ht.h> |
49 | #include <adt/hash_table.h> |
49 | #include <adt/hash_table.h> |
50 | #include <adt/list.h> |
50 | #include <adt/list.h> |
51 | #include <arch.h> |
51 | #include <arch.h> |
52 | #include <align.h> |
52 | #include <align.h> |
53 | #include <panic.h> |
53 | #include <panic.h> |
54 | #include <errno.h> |
54 | #include <errno.h> |
55 | #include <print.h> |
55 | #include <print.h> |
56 | 56 | ||
57 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */ |
57 | #define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */ |
58 | 58 | ||
59 | static void futex_initialize(futex_t *futex); |
59 | static void futex_initialize(futex_t *futex); |
60 | 60 | ||
61 | static futex_t *futex_find(uintptr_t paddr); |
61 | static futex_t *futex_find(uintptr_t paddr); |
62 | static index_t futex_ht_hash(unative_t *key); |
62 | static index_t futex_ht_hash(unative_t *key); |
63 | static bool futex_ht_compare(unative_t *key, count_t keys, link_t *item); |
63 | static bool futex_ht_compare(unative_t *key, count_t keys, link_t *item); |
64 | static void futex_ht_remove_callback(link_t *item); |
64 | static void futex_ht_remove_callback(link_t *item); |
65 | 65 | ||
66 | /** |
66 | /** |
67 | * Read-write lock protecting global futex hash table. |
67 | * Read-write lock protecting global futex hash table. |
68 | * It is also used to serialize access to all futex_t structures. |
68 | * It is also used to serialize access to all futex_t structures. |
69 | * Must be acquired before the task futex B+tree lock. |
69 | * Must be acquired before the task futex B+tree lock. |
70 | */ |
70 | */ |
71 | static rwlock_t futex_ht_lock; |
71 | static rwlock_t futex_ht_lock; |
72 | 72 | ||
73 | /** Futex hash table. */ |
73 | /** Futex hash table. */ |
74 | static hash_table_t futex_ht; |
74 | static hash_table_t futex_ht; |
75 | 75 | ||
76 | /** Futex hash table operations. */ |
76 | /** Futex hash table operations. */ |
77 | static hash_table_operations_t futex_ht_ops = { |
77 | static hash_table_operations_t futex_ht_ops = { |
78 | .hash = futex_ht_hash, |
78 | .hash = futex_ht_hash, |
79 | .compare = futex_ht_compare, |
79 | .compare = futex_ht_compare, |
80 | .remove_callback = futex_ht_remove_callback |
80 | .remove_callback = futex_ht_remove_callback |
81 | }; |
81 | }; |
82 | 82 | ||
83 | /** Initialize futex subsystem. */ |
83 | /** Initialize futex subsystem. */ |
84 | void futex_init(void) |
84 | void futex_init(void) |
85 | { |
85 | { |
86 | rwlock_initialize(&futex_ht_lock); |
86 | rwlock_initialize(&futex_ht_lock); |
87 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops); |
87 | hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops); |
88 | } |
88 | } |
89 | 89 | ||
90 | /** Initialize kernel futex structure. |
90 | /** Initialize kernel futex structure. |
91 | * |
91 | * |
92 | * @param futex Kernel futex structure. |
92 | * @param futex Kernel futex structure. |
93 | */ |
93 | */ |
94 | void futex_initialize(futex_t *futex) |
94 | void futex_initialize(futex_t *futex) |
95 | { |
95 | { |
96 | waitq_initialize(&futex->wq); |
96 | waitq_initialize(&futex->wq); |
97 | link_initialize(&futex->ht_link); |
97 | link_initialize(&futex->ht_link); |
98 | futex->paddr = 0; |
98 | futex->paddr = 0; |
99 | futex->refcount = 1; |
99 | futex->refcount = 1; |
100 | } |
100 | } |
101 | 101 | ||
102 | /** Sleep in futex wait queue. |
102 | /** Sleep in futex wait queue. |
103 | * |
103 | * |
104 | * @param uaddr Userspace address of the futex counter. |
104 | * @param uaddr Userspace address of the futex counter. |
105 | * @param usec If non-zero, number of microseconds this thread is willing to |
105 | * @param usec If non-zero, number of microseconds this thread is willing to |
106 | * sleep. |
106 | * sleep. |
107 | * @param flags Select mode of operation. |
107 | * @param flags Select mode of operation. |
108 | * |
108 | * |
109 | * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See |
109 | * @return One of ESYNCH_TIMEOUT, ESYNCH_OK_ATOMIC and ESYNCH_OK_BLOCKED. See |
110 | * synch.h. If there is no physical mapping for uaddr ENOENT is returned. |
110 | * synch.h. If there is no physical mapping for uaddr ENOENT is returned. |
111 | */ |
111 | */ |
112 | unative_t sys_futex_sleep_timeout(uintptr_t uaddr, uint32_t usec, int flags) |
112 | unative_t sys_futex_sleep_timeout(uintptr_t uaddr, uint32_t usec, int flags) |
113 | { |
113 | { |
114 | futex_t *futex; |
114 | futex_t *futex; |
115 | uintptr_t paddr; |
115 | uintptr_t paddr; |
116 | pte_t *t; |
116 | pte_t *t; |
117 | ipl_t ipl; |
117 | ipl_t ipl; |
- | 118 | int rc; |
|
118 | 119 | ||
119 | ipl = interrupts_disable(); |
120 | ipl = interrupts_disable(); |
120 | 121 | ||
121 | /* |
122 | /* |
122 | * Find physical address of futex counter. |
123 | * Find physical address of futex counter. |
123 | */ |
124 | */ |
124 | page_table_lock(AS, true); |
125 | page_table_lock(AS, true); |
125 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
126 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
126 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
127 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
127 | page_table_unlock(AS, true); |
128 | page_table_unlock(AS, true); |
128 | interrupts_restore(ipl); |
129 | interrupts_restore(ipl); |
129 | return (unative_t) ENOENT; |
130 | return (unative_t) ENOENT; |
130 | } |
131 | } |
131 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
132 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
132 | page_table_unlock(AS, true); |
133 | page_table_unlock(AS, true); |
133 | 134 | ||
134 | interrupts_restore(ipl); |
135 | interrupts_restore(ipl); |
135 | 136 | ||
136 | futex = futex_find(paddr); |
137 | futex = futex_find(paddr); |
137 | 138 | ||
- | 139 | #ifdef CONFIG_UDEBUG |
|
- | 140 | udebug_stoppable_begin(); |
|
- | 141 | #endif |
|
138 | return (unative_t) waitq_sleep_timeout(&futex->wq, usec, flags | |
142 | rc = waitq_sleep_timeout(&futex->wq, usec, flags | |
139 | SYNCH_FLAGS_INTERRUPTIBLE); |
143 | SYNCH_FLAGS_INTERRUPTIBLE); |
- | 144 | ||
- | 145 | #ifdef CONFIG_UDEBUG |
|
- | 146 | udebug_stoppable_end(); |
|
- | 147 | #endif |
|
- | 148 | return (unative_t) rc; |
|
140 | } |
149 | } |
141 | 150 | ||
142 | /** Wakeup one thread waiting in futex wait queue. |
151 | /** Wakeup one thread waiting in futex wait queue. |
143 | * |
152 | * |
144 | * @param uaddr Userspace address of the futex counter. |
153 | * @param uaddr Userspace address of the futex counter. |
145 | * |
154 | * |
146 | * @return ENOENT if there is no physical mapping for uaddr. |
155 | * @return ENOENT if there is no physical mapping for uaddr. |
147 | */ |
156 | */ |
148 | unative_t sys_futex_wakeup(uintptr_t uaddr) |
157 | unative_t sys_futex_wakeup(uintptr_t uaddr) |
149 | { |
158 | { |
150 | futex_t *futex; |
159 | futex_t *futex; |
151 | uintptr_t paddr; |
160 | uintptr_t paddr; |
152 | pte_t *t; |
161 | pte_t *t; |
153 | ipl_t ipl; |
162 | ipl_t ipl; |
154 | 163 | ||
155 | ipl = interrupts_disable(); |
164 | ipl = interrupts_disable(); |
156 | 165 | ||
157 | /* |
166 | /* |
158 | * Find physical address of futex counter. |
167 | * Find physical address of futex counter. |
159 | */ |
168 | */ |
160 | page_table_lock(AS, true); |
169 | page_table_lock(AS, true); |
161 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
170 | t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE)); |
162 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
171 | if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) { |
163 | page_table_unlock(AS, true); |
172 | page_table_unlock(AS, true); |
164 | interrupts_restore(ipl); |
173 | interrupts_restore(ipl); |
165 | return (unative_t) ENOENT; |
174 | return (unative_t) ENOENT; |
166 | } |
175 | } |
167 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
176 | paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE)); |
168 | page_table_unlock(AS, true); |
177 | page_table_unlock(AS, true); |
169 | 178 | ||
170 | interrupts_restore(ipl); |
179 | interrupts_restore(ipl); |
171 | 180 | ||
172 | futex = futex_find(paddr); |
181 | futex = futex_find(paddr); |
173 | 182 | ||
174 | waitq_wakeup(&futex->wq, WAKEUP_FIRST); |
183 | waitq_wakeup(&futex->wq, WAKEUP_FIRST); |
175 | 184 | ||
176 | return 0; |
185 | return 0; |
177 | } |
186 | } |
178 | 187 | ||
179 | /** Find kernel address of the futex structure corresponding to paddr. |
188 | /** Find kernel address of the futex structure corresponding to paddr. |
180 | * |
189 | * |
181 | * If the structure does not exist already, a new one is created. |
190 | * If the structure does not exist already, a new one is created. |
182 | * |
191 | * |
183 | * @param paddr Physical address of the userspace futex counter. |
192 | * @param paddr Physical address of the userspace futex counter. |
184 | * |
193 | * |
185 | * @return Address of the kernel futex structure. |
194 | * @return Address of the kernel futex structure. |
186 | */ |
195 | */ |
187 | futex_t *futex_find(uintptr_t paddr) |
196 | futex_t *futex_find(uintptr_t paddr) |
188 | { |
197 | { |
189 | link_t *item; |
198 | link_t *item; |
190 | futex_t *futex; |
199 | futex_t *futex; |
191 | btree_node_t *leaf; |
200 | btree_node_t *leaf; |
192 | 201 | ||
193 | /* |
202 | /* |
194 | * Find the respective futex structure |
203 | * Find the respective futex structure |
195 | * or allocate new one if it does not exist already. |
204 | * or allocate new one if it does not exist already. |
196 | */ |
205 | */ |
197 | rwlock_read_lock(&futex_ht_lock); |
206 | rwlock_read_lock(&futex_ht_lock); |
198 | item = hash_table_find(&futex_ht, &paddr); |
207 | item = hash_table_find(&futex_ht, &paddr); |
199 | if (item) { |
208 | if (item) { |
200 | futex = hash_table_get_instance(item, futex_t, ht_link); |
209 | futex = hash_table_get_instance(item, futex_t, ht_link); |
201 | 210 | ||
202 | /* |
211 | /* |
203 | * See if the current task knows this futex. |
212 | * See if the current task knows this futex. |
204 | */ |
213 | */ |
205 | mutex_lock(&TASK->futexes_lock); |
214 | mutex_lock(&TASK->futexes_lock); |
206 | if (!btree_search(&TASK->futexes, paddr, &leaf)) { |
215 | if (!btree_search(&TASK->futexes, paddr, &leaf)) { |
207 | /* |
216 | /* |
208 | * The futex is new to the current task. |
217 | * The futex is new to the current task. |
209 | * However, we only have read access. |
218 | * However, we only have read access. |
210 | * Gain write access and try again. |
219 | * Gain write access and try again. |
211 | */ |
220 | */ |
212 | mutex_unlock(&TASK->futexes_lock); |
221 | mutex_unlock(&TASK->futexes_lock); |
213 | goto gain_write_access; |
222 | goto gain_write_access; |
214 | } |
223 | } |
215 | mutex_unlock(&TASK->futexes_lock); |
224 | mutex_unlock(&TASK->futexes_lock); |
216 | 225 | ||
217 | rwlock_read_unlock(&futex_ht_lock); |
226 | rwlock_read_unlock(&futex_ht_lock); |
218 | } else { |
227 | } else { |
219 | gain_write_access: |
228 | gain_write_access: |
220 | /* |
229 | /* |
221 | * Upgrade to writer is not currently supported, |
230 | * Upgrade to writer is not currently supported, |
222 | * therefore, it is necessary to release the read lock |
231 | * therefore, it is necessary to release the read lock |
223 | * and reacquire it as a writer. |
232 | * and reacquire it as a writer. |
224 | */ |
233 | */ |
225 | rwlock_read_unlock(&futex_ht_lock); |
234 | rwlock_read_unlock(&futex_ht_lock); |
226 | 235 | ||
227 | rwlock_write_lock(&futex_ht_lock); |
236 | rwlock_write_lock(&futex_ht_lock); |
228 | /* |
237 | /* |
229 | * Avoid possible race condition by searching |
238 | * Avoid possible race condition by searching |
230 | * the hash table once again with write access. |
239 | * the hash table once again with write access. |
231 | */ |
240 | */ |
232 | item = hash_table_find(&futex_ht, &paddr); |
241 | item = hash_table_find(&futex_ht, &paddr); |
233 | if (item) { |
242 | if (item) { |
234 | futex = hash_table_get_instance(item, futex_t, ht_link); |
243 | futex = hash_table_get_instance(item, futex_t, ht_link); |
235 | 244 | ||
236 | /* |
245 | /* |
237 | * See if this futex is known to the current task. |
246 | * See if this futex is known to the current task. |
238 | */ |
247 | */ |
239 | mutex_lock(&TASK->futexes_lock); |
248 | mutex_lock(&TASK->futexes_lock); |
240 | if (!btree_search(&TASK->futexes, paddr, &leaf)) { |
249 | if (!btree_search(&TASK->futexes, paddr, &leaf)) { |
241 | /* |
250 | /* |
242 | * The futex is new to the current task. |
251 | * The futex is new to the current task. |
243 | * Upgrade its reference count and put it to the |
252 | * Upgrade its reference count and put it to the |
244 | * current task's B+tree of known futexes. |
253 | * current task's B+tree of known futexes. |
245 | */ |
254 | */ |
246 | futex->refcount++; |
255 | futex->refcount++; |
247 | btree_insert(&TASK->futexes, paddr, futex, |
256 | btree_insert(&TASK->futexes, paddr, futex, |
248 | leaf); |
257 | leaf); |
249 | } |
258 | } |
250 | mutex_unlock(&TASK->futexes_lock); |
259 | mutex_unlock(&TASK->futexes_lock); |
251 | 260 | ||
252 | rwlock_write_unlock(&futex_ht_lock); |
261 | rwlock_write_unlock(&futex_ht_lock); |
253 | } else { |
262 | } else { |
254 | futex = (futex_t *) malloc(sizeof(futex_t), 0); |
263 | futex = (futex_t *) malloc(sizeof(futex_t), 0); |
255 | futex_initialize(futex); |
264 | futex_initialize(futex); |
256 | futex->paddr = paddr; |
265 | futex->paddr = paddr; |
257 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link); |
266 | hash_table_insert(&futex_ht, &paddr, &futex->ht_link); |
258 | 267 | ||
259 | /* |
268 | /* |
260 | * This is the first task referencing the futex. |
269 | * This is the first task referencing the futex. |
261 | * It can be directly inserted into its |
270 | * It can be directly inserted into its |
262 | * B+tree of known futexes. |
271 | * B+tree of known futexes. |
263 | */ |
272 | */ |
264 | mutex_lock(&TASK->futexes_lock); |
273 | mutex_lock(&TASK->futexes_lock); |
265 | btree_insert(&TASK->futexes, paddr, futex, NULL); |
274 | btree_insert(&TASK->futexes, paddr, futex, NULL); |
266 | mutex_unlock(&TASK->futexes_lock); |
275 | mutex_unlock(&TASK->futexes_lock); |
267 | 276 | ||
268 | rwlock_write_unlock(&futex_ht_lock); |
277 | rwlock_write_unlock(&futex_ht_lock); |
269 | } |
278 | } |
270 | } |
279 | } |
271 | 280 | ||
272 | return futex; |
281 | return futex; |
273 | } |
282 | } |
274 | 283 | ||
275 | /** Compute hash index into futex hash table. |
284 | /** Compute hash index into futex hash table. |
276 | * |
285 | * |
277 | * @param key Address where the key (i.e. physical address of futex counter) is |
286 | * @param key Address where the key (i.e. physical address of futex counter) is |
278 | * stored. |
287 | * stored. |
279 | * |
288 | * |
280 | * @return Index into futex hash table. |
289 | * @return Index into futex hash table. |
281 | */ |
290 | */ |
282 | index_t futex_ht_hash(unative_t *key) |
291 | index_t futex_ht_hash(unative_t *key) |
283 | { |
292 | { |
284 | return *key & (FUTEX_HT_SIZE-1); |
293 | return *key & (FUTEX_HT_SIZE-1); |
285 | } |
294 | } |
286 | 295 | ||
287 | /** Compare futex hash table item with a key. |
296 | /** Compare futex hash table item with a key. |
288 | * |
297 | * |
289 | * @param key Address where the key (i.e. physical address of futex counter) is |
298 | * @param key Address where the key (i.e. physical address of futex counter) is |
290 | * stored. |
299 | * stored. |
291 | * |
300 | * |
292 | * @return True if the item matches the key. False otherwise. |
301 | * @return True if the item matches the key. False otherwise. |
293 | */ |
302 | */ |
294 | bool futex_ht_compare(unative_t *key, count_t keys, link_t *item) |
303 | bool futex_ht_compare(unative_t *key, count_t keys, link_t *item) |
295 | { |
304 | { |
296 | futex_t *futex; |
305 | futex_t *futex; |
297 | 306 | ||
298 | ASSERT(keys == 1); |
307 | ASSERT(keys == 1); |
299 | 308 | ||
300 | futex = hash_table_get_instance(item, futex_t, ht_link); |
309 | futex = hash_table_get_instance(item, futex_t, ht_link); |
301 | return *key == futex->paddr; |
310 | return *key == futex->paddr; |
302 | } |
311 | } |
303 | 312 | ||
304 | /** Callback for removal items from futex hash table. |
313 | /** Callback for removal items from futex hash table. |
305 | * |
314 | * |
306 | * @param item Item removed from the hash table. |
315 | * @param item Item removed from the hash table. |
307 | */ |
316 | */ |
308 | void futex_ht_remove_callback(link_t *item) |
317 | void futex_ht_remove_callback(link_t *item) |
309 | { |
318 | { |
310 | futex_t *futex; |
319 | futex_t *futex; |
311 | 320 | ||
312 | futex = hash_table_get_instance(item, futex_t, ht_link); |
321 | futex = hash_table_get_instance(item, futex_t, ht_link); |
313 | free(futex); |
322 | free(futex); |
314 | } |
323 | } |
315 | 324 | ||
316 | /** Remove references from futexes known to the current task. */ |
325 | /** Remove references from futexes known to the current task. */ |
317 | void futex_cleanup(void) |
326 | void futex_cleanup(void) |
318 | { |
327 | { |
319 | link_t *cur; |
328 | link_t *cur; |
320 | 329 | ||
321 | rwlock_write_lock(&futex_ht_lock); |
330 | rwlock_write_lock(&futex_ht_lock); |
322 | mutex_lock(&TASK->futexes_lock); |
331 | mutex_lock(&TASK->futexes_lock); |
323 | 332 | ||
324 | for (cur = TASK->futexes.leaf_head.next; |
333 | for (cur = TASK->futexes.leaf_head.next; |
325 | cur != &TASK->futexes.leaf_head; cur = cur->next) { |
334 | cur != &TASK->futexes.leaf_head; cur = cur->next) { |
326 | btree_node_t *node; |
335 | btree_node_t *node; |
327 | unsigned int i; |
336 | unsigned int i; |
328 | 337 | ||
329 | node = list_get_instance(cur, btree_node_t, leaf_link); |
338 | node = list_get_instance(cur, btree_node_t, leaf_link); |
330 | for (i = 0; i < node->keys; i++) { |
339 | for (i = 0; i < node->keys; i++) { |
331 | futex_t *ftx; |
340 | futex_t *ftx; |
332 | uintptr_t paddr = node->key[i]; |
341 | uintptr_t paddr = node->key[i]; |
333 | 342 | ||
334 | ftx = (futex_t *) node->value[i]; |
343 | ftx = (futex_t *) node->value[i]; |
335 | if (--ftx->refcount == 0) |
344 | if (--ftx->refcount == 0) |
336 | hash_table_remove(&futex_ht, &paddr, 1); |
345 | hash_table_remove(&futex_ht, &paddr, 1); |
337 | } |
346 | } |
338 | } |
347 | } |
339 | 348 | ||
340 | mutex_unlock(&TASK->futexes_lock); |
349 | mutex_unlock(&TASK->futexes_lock); |
341 | rwlock_write_unlock(&futex_ht_lock); |
350 | rwlock_write_unlock(&futex_ht_lock); |
342 | } |
351 | } |
343 | 352 | ||
344 | /** @} |
353 | /** @} |
345 | */ |
354 | */ |
346 | 355 |