source: mainline/kernel/generic/src/synch/futex.c@ a49a1a1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a49a1a1 was a49a1a1, checked in by Jakub Jermar <jakub@…>, 15 years ago

Do not disable interrupts in sys_futex_wakeup().

  • Property mode set to 100644
File size: 7.7 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
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 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Kernel backend for futexes.
36 */
37
38#include <synch/futex.h>
39#include <synch/mutex.h>
40#include <synch/spinlock.h>
41#include <synch/synch.h>
42#include <mm/frame.h>
43#include <mm/page.h>
44#include <mm/slab.h>
45#include <proc/thread.h>
46#include <proc/task.h>
47#include <genarch/mm/page_pt.h>
48#include <genarch/mm/page_ht.h>
49#include <adt/hash_table.h>
50#include <adt/list.h>
51#include <arch.h>
52#include <align.h>
53#include <panic.h>
54#include <errno.h>
55#include <print.h>
56
57#define FUTEX_HT_SIZE 1024 /* keep it a power of 2 */
58
59static void futex_initialize(futex_t *futex);
60
61static futex_t *futex_find(uintptr_t paddr);
62static size_t futex_ht_hash(unative_t *key);
63static bool futex_ht_compare(unative_t *key, size_t keys, link_t *item);
64static void futex_ht_remove_callback(link_t *item);
65
66/**
67 * Mutex protecting global futex hash table.
68 * It is also used to serialize access to all futex_t structures.
69 * Must be acquired before the task futex B+tree lock.
70 */
71static mutex_t futex_ht_lock;
72
73/** Futex hash table. */
74static hash_table_t futex_ht;
75
76/** Futex hash table operations. */
77static hash_table_operations_t futex_ht_ops = {
78 .hash = futex_ht_hash,
79 .compare = futex_ht_compare,
80 .remove_callback = futex_ht_remove_callback
81};
82
83/** Initialize futex subsystem. */
84void futex_init(void)
85{
86 mutex_initialize(&futex_ht_lock, MUTEX_PASSIVE);
87 hash_table_create(&futex_ht, FUTEX_HT_SIZE, 1, &futex_ht_ops);
88}
89
90/** Initialize kernel futex structure.
91 *
92 * @param futex Kernel futex structure.
93 */
94void futex_initialize(futex_t *futex)
95{
96 waitq_initialize(&futex->wq);
97 link_initialize(&futex->ht_link);
98 futex->paddr = 0;
99 futex->refcount = 1;
100}
101
102/** Sleep in futex wait queue.
103 *
104 * @param uaddr Userspace address of the futex counter.
105 *
106 * @return If there is no physical mapping for uaddr ENOENT is
107 * returned. Otherwise returns a wait result as defined in
108 * synch.h.
109 */
110unative_t sys_futex_sleep(uintptr_t uaddr)
111{
112 futex_t *futex;
113 uintptr_t paddr;
114 pte_t *t;
115 int rc;
116
117 /*
118 * Find physical address of futex counter.
119 */
120 page_table_lock(AS, true);
121 t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
122 if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
123 page_table_unlock(AS, true);
124 return (unative_t) ENOENT;
125 }
126 paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
127 page_table_unlock(AS, true);
128
129 futex = futex_find(paddr);
130
131#ifdef CONFIG_UDEBUG
132 udebug_stoppable_begin();
133#endif
134 rc = waitq_sleep_timeout(&futex->wq, 0, SYNCH_FLAGS_INTERRUPTIBLE);
135#ifdef CONFIG_UDEBUG
136 udebug_stoppable_end();
137#endif
138 return (unative_t) rc;
139}
140
141/** Wakeup one thread waiting in futex wait queue.
142 *
143 * @param uaddr Userspace address of the futex counter.
144 *
145 * @return ENOENT if there is no physical mapping for uaddr.
146 */
147unative_t sys_futex_wakeup(uintptr_t uaddr)
148{
149 futex_t *futex;
150 uintptr_t paddr;
151 pte_t *t;
152
153 /*
154 * Find physical address of futex counter.
155 */
156 page_table_lock(AS, true);
157 t = page_mapping_find(AS, ALIGN_DOWN(uaddr, PAGE_SIZE));
158 if (!t || !PTE_VALID(t) || !PTE_PRESENT(t)) {
159 page_table_unlock(AS, true);
160 return (unative_t) ENOENT;
161 }
162 paddr = PTE_GET_FRAME(t) + (uaddr - ALIGN_DOWN(uaddr, PAGE_SIZE));
163 page_table_unlock(AS, true);
164
165 futex = futex_find(paddr);
166
167 waitq_wakeup(&futex->wq, WAKEUP_FIRST);
168
169 return 0;
170}
171
172/** Find kernel address of the futex structure corresponding to paddr.
173 *
174 * If the structure does not exist already, a new one is created.
175 *
176 * @param paddr Physical address of the userspace futex counter.
177 *
178 * @return Address of the kernel futex structure.
179 */
180futex_t *futex_find(uintptr_t paddr)
181{
182 link_t *item;
183 futex_t *futex;
184 btree_node_t *leaf;
185
186 /*
187 * Find the respective futex structure
188 * or allocate new one if it does not exist already.
189 */
190 mutex_lock(&futex_ht_lock);
191 item = hash_table_find(&futex_ht, &paddr);
192 if (item) {
193 futex = hash_table_get_instance(item, futex_t, ht_link);
194
195 /*
196 * See if the current task knows this futex.
197 */
198 mutex_lock(&TASK->futexes_lock);
199 if (!btree_search(&TASK->futexes, paddr, &leaf)) {
200 /*
201 * The futex is new to the current task.
202 * Upgrade its reference count and put it to the
203 * current task's B+tree of known futexes.
204 */
205 futex->refcount++;
206 btree_insert(&TASK->futexes, paddr, futex, leaf);
207 }
208 mutex_unlock(&TASK->futexes_lock);
209 } else {
210 futex = (futex_t *) malloc(sizeof(futex_t), 0);
211 futex_initialize(futex);
212 futex->paddr = paddr;
213 hash_table_insert(&futex_ht, &paddr, &futex->ht_link);
214
215 /*
216 * This is the first task referencing the futex.
217 * It can be directly inserted into its
218 * B+tree of known futexes.
219 */
220 mutex_lock(&TASK->futexes_lock);
221 btree_insert(&TASK->futexes, paddr, futex, NULL);
222 mutex_unlock(&TASK->futexes_lock);
223
224 }
225 mutex_unlock(&futex_ht_lock);
226
227 return futex;
228}
229
230/** Compute hash index into futex hash table.
231 *
232 * @param key Address where the key (i.e. physical address of futex
233 * counter) is stored.
234 *
235 * @return Index into futex hash table.
236 */
237size_t futex_ht_hash(unative_t *key)
238{
239 return (*key & (FUTEX_HT_SIZE - 1));
240}
241
242/** Compare futex hash table item with a key.
243 *
244 * @param key Address where the key (i.e. physical address of futex
245 * counter) is stored.
246 *
247 * @return True if the item matches the key. False otherwise.
248 */
249bool futex_ht_compare(unative_t *key, size_t keys, link_t *item)
250{
251 futex_t *futex;
252
253 ASSERT(keys == 1);
254
255 futex = hash_table_get_instance(item, futex_t, ht_link);
256 return *key == futex->paddr;
257}
258
259/** Callback for removal items from futex hash table.
260 *
261 * @param item Item removed from the hash table.
262 */
263void futex_ht_remove_callback(link_t *item)
264{
265 futex_t *futex;
266
267 futex = hash_table_get_instance(item, futex_t, ht_link);
268 free(futex);
269}
270
271/** Remove references from futexes known to the current task. */
272void futex_cleanup(void)
273{
274 link_t *cur;
275
276 mutex_lock(&futex_ht_lock);
277 mutex_lock(&TASK->futexes_lock);
278
279 for (cur = TASK->futexes.leaf_head.next;
280 cur != &TASK->futexes.leaf_head; cur = cur->next) {
281 btree_node_t *node;
282 unsigned int i;
283
284 node = list_get_instance(cur, btree_node_t, leaf_link);
285 for (i = 0; i < node->keys; i++) {
286 futex_t *ftx;
287 uintptr_t paddr = node->key[i];
288
289 ftx = (futex_t *) node->value[i];
290 if (--ftx->refcount == 0)
291 hash_table_remove(&futex_ht, &paddr, 1);
292 }
293 }
294
295 mutex_unlock(&TASK->futexes_lock);
296 mutex_unlock(&futex_ht_lock);
297}
298
299/** @}
300 */
Note: See TracBrowser for help on using the repository browser.