source: mainline/uspace/srv/ns/task.c@ 2696a76

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2696a76 was 2696a76, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

ns: Fixed checking if a task is present in the hash table in ns_task_disconnect().

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
2 * Copyright (c) 2009 Martin Decky
3 * Copyright (c) 2009 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup ns
31 * @{
32 */
33
34#include <ipc/ipc.h>
35#include <adt/hash_table.h>
36#include <bool.h>
37#include <errno.h>
38#include <assert.h>
39#include <stdio.h>
40#include <macros.h>
41#include <malloc.h>
42#include "task.h"
43#include "ns.h"
44
45
46/* TODO:
47 *
48 * As there is currently no convention that each task has to be waited
49 * for, the NS can leak memory because of the zombie tasks.
50 *
51 */
52
53/** Task hash table item. */
54typedef struct {
55 link_t link;
56
57 task_id_t id; /**< Task ID. */
58 bool finished; /**< Task is done. */
59 bool have_rval; /**< Task returned a value. */
60 int retval; /**< The return value. */
61} hashed_task_t;
62
63/** Compute hash index into task hash table.
64 *
65 * @param key Pointer keys. However, only the first key (i.e. truncated task
66 * number) is used to compute the hash index.
67 *
68 * @return Hash index corresponding to key[0].
69 *
70 */
71static size_t task_key_hash(unsigned long key[])
72{
73 size_t hash = 17;
74 hash = 37 * hash + key[1];
75 hash = 37 * hash + key[0];
76 return hash;
77}
78
79static size_t task_hash(const link_t *item)
80{
81 hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
82
83 unsigned long key[] = {
84 LOWER32(ht->id),
85 UPPER32(ht->id)
86 };
87
88 return task_key_hash(key);
89}
90
91/** Compare a key with hashed item.
92 *
93 * @param key Array of keys.
94 * @param keys Must be less than or equal to 2.
95 * @param item Pointer to a hash table item.
96 *
97 * @return Non-zero if the key matches the item, zero otherwise.
98 *
99 */
100static bool task_match(unsigned long key[], size_t keys, const link_t *item)
101{
102 assert(key);
103 assert(keys == 2);
104 assert(item);
105
106 hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
107
108 return (key[0] == LOWER32(ht->id))
109 && (key[1] == UPPER32(ht->id));
110}
111
112/** Perform actions after removal of item from the hash table.
113 *
114 * @param item Item that was removed from the hash table.
115 *
116 */
117static void task_remove(link_t *item)
118{
119 assert(item);
120 free(hash_table_get_instance(item, hashed_task_t, link));
121}
122
123/** Operations for task hash table. */
124static hash_table_ops_t task_hash_table_ops = {
125 .hash = task_hash,
126 .key_hash = task_key_hash,
127 .match = task_match,
128 .equal = 0,
129 .remove_callback = task_remove
130};
131
132/** Task hash table structure. */
133static hash_table_t task_hash_table;
134
135typedef struct {
136 link_t link;
137 sysarg_t in_phone_hash; /**< Incoming phone hash. */
138 task_id_t id; /**< Task ID. */
139} p2i_entry_t;
140
141/** Compute hash index into task hash table.
142 *
143 * @param key Array of keys.
144 *
145 * @return Hash index corresponding to key[0].
146 *
147 */
148static size_t p2i_key_hash(unsigned long key[])
149{
150 assert(key);
151 return key[0];
152}
153
154static size_t p2i_hash(const link_t *item)
155{
156 p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
157 unsigned long key = entry->in_phone_hash;
158 return p2i_key_hash(&key);
159}
160
161/** Compare a key with hashed item.
162 *
163 * @param key Array of keys.
164 * @param keys Must be less than or equal to 1.
165 * @param item Pointer to a hash table item.
166 *
167 * @return Non-zero if the key matches the item, zero otherwise.
168 *
169 */
170static bool p2i_match(unsigned long key[], size_t keys, const link_t *item)
171{
172 assert(key);
173 assert(keys == 1);
174 assert(item);
175
176 p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
177
178 return (key[0] == entry->in_phone_hash);
179}
180
181/** Perform actions after removal of item from the hash table.
182 *
183 * @param item Item that was removed from the hash table.
184 *
185 */
186static void p2i_remove(link_t *item)
187{
188 assert(item);
189 free(hash_table_get_instance(item, p2i_entry_t, link));
190}
191
192/** Operations for task hash table. */
193static hash_table_ops_t p2i_ops = {
194 .hash = p2i_hash,
195 .key_hash = p2i_key_hash,
196 .match = p2i_match,
197 .equal = 0,
198 .remove_callback = p2i_remove
199};
200
201/** Map phone hash to task ID */
202static hash_table_t phone_to_id;
203
204/** Pending task wait structure. */
205typedef struct {
206 link_t link;
207 task_id_t id; /**< Task ID. */
208 ipc_callid_t callid; /**< Call ID waiting for the connection */
209} pending_wait_t;
210
211static list_t pending_wait;
212
213int task_init(void)
214{
215 if (!hash_table_create(&task_hash_table, 0, 2, &task_hash_table_ops)) {
216 printf(NAME ": No memory available for tasks\n");
217 return ENOMEM;
218 }
219
220 if (!hash_table_create(&phone_to_id, 0, 1, &p2i_ops)) {
221 printf(NAME ": No memory available for tasks\n");
222 return ENOMEM;
223 }
224
225 list_initialize(&pending_wait);
226 return EOK;
227}
228
229/** Process pending wait requests */
230void process_pending_wait(void)
231{
232 task_exit_t texit;
233
234loop:
235 list_foreach(pending_wait, cur) {
236 pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
237
238 unsigned long keys[2] = {
239 LOWER32(pr->id),
240 UPPER32(pr->id)
241 };
242
243 link_t *link = hash_table_find(&task_hash_table, keys);
244 if (!link)
245 continue;
246
247 hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
248 if (!ht->finished)
249 continue;
250
251 if (!(pr->callid & IPC_CALLID_NOTIFICATION)) {
252 texit = ht->have_rval ? TASK_EXIT_NORMAL :
253 TASK_EXIT_UNEXPECTED;
254 ipc_answer_2(pr->callid, EOK, texit,
255 ht->retval);
256 }
257
258 hash_table_remove(&task_hash_table, keys, 2);
259 list_remove(cur);
260 free(pr);
261 goto loop;
262 }
263}
264
265void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
266{
267 sysarg_t retval;
268 task_exit_t texit;
269
270 unsigned long keys[2] = {
271 LOWER32(id),
272 UPPER32(id)
273 };
274
275 link_t *link = hash_table_find(&task_hash_table, keys);
276 hashed_task_t *ht = (link != NULL) ?
277 hash_table_get_instance(link, hashed_task_t, link) : NULL;
278
279 if (ht == NULL) {
280 /* No such task exists. */
281 ipc_answer_0(callid, ENOENT);
282 return;
283 }
284
285 if (!ht->finished) {
286 /* Add to pending list */
287 pending_wait_t *pr =
288 (pending_wait_t *) malloc(sizeof(pending_wait_t));
289 if (!pr) {
290 retval = ENOMEM;
291 goto out;
292 }
293
294 link_initialize(&pr->link);
295 pr->id = id;
296 pr->callid = callid;
297 list_append(&pr->link, &pending_wait);
298 return;
299 }
300
301 hash_table_remove(&task_hash_table, keys, 2);
302 retval = EOK;
303
304out:
305 if (!(callid & IPC_CALLID_NOTIFICATION)) {
306 texit = ht->have_rval ? TASK_EXIT_NORMAL : TASK_EXIT_UNEXPECTED;
307 ipc_answer_2(callid, retval, texit, ht->retval);
308 }
309}
310
311int ns_task_id_intro(ipc_call_t *call)
312{
313
314 task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
315
316 unsigned long keys[] = { call->in_phone_hash };
317
318 link_t *link = hash_table_find(&phone_to_id, keys);
319 if (link != NULL)
320 return EEXISTS;
321
322 p2i_entry_t *entry = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
323 if (entry == NULL)
324 return ENOMEM;
325
326 hashed_task_t *ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
327 if (ht == NULL)
328 return ENOMEM;
329
330 /*
331 * Insert into the phone-to-id map.
332 */
333
334 link_initialize(&entry->link);
335 entry->in_phone_hash = call->in_phone_hash;
336 entry->id = id;
337 hash_table_insert(&phone_to_id, &entry->link);
338
339 /*
340 * Insert into the main table.
341 */
342
343 link_initialize(&ht->link);
344 ht->id = id;
345 ht->finished = false;
346 ht->have_rval = false;
347 ht->retval = -1;
348 hash_table_insert(&task_hash_table, &ht->link);
349
350 return EOK;
351}
352
353static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
354{
355 unsigned long keys[1] = {phone_hash};
356
357 link_t *link = hash_table_find(&phone_to_id, keys);
358 if (link == NULL)
359 return ENOENT;
360
361 p2i_entry_t *entry = hash_table_get_instance(link, p2i_entry_t, link);
362 *id = entry->id;
363
364 return EOK;
365}
366
367int ns_task_retval(ipc_call_t *call)
368{
369 task_id_t id;
370 int rc = get_id_by_phone(call->in_phone_hash, &id);
371 if (rc != EOK)
372 return rc;
373
374 unsigned long keys[2] = {
375 LOWER32(id),
376 UPPER32(id)
377 };
378
379 link_t *link = hash_table_find(&task_hash_table, keys);
380 hashed_task_t *ht = (link != NULL) ?
381 hash_table_get_instance(link, hashed_task_t, link) : NULL;
382
383 if ((ht == NULL) || (ht->finished))
384 return EINVAL;
385
386 ht->finished = true;
387 ht->have_rval = true;
388 ht->retval = IPC_GET_ARG1(*call);
389
390 return EOK;
391}
392
393int ns_task_disconnect(ipc_call_t *call)
394{
395 unsigned long keys[2];
396
397 task_id_t id;
398 int rc = get_id_by_phone(call->in_phone_hash, &id);
399 if (rc != EOK)
400 return rc;
401
402 /* Delete from phone-to-id map. */
403 keys[0] = call->in_phone_hash;
404 hash_table_remove(&phone_to_id, keys, 1);
405
406 /* Mark task as finished. */
407 keys[0] = LOWER32(id);
408 keys[1] = UPPER32(id);
409
410 link_t *link = hash_table_find(&task_hash_table, keys);
411 if (link == NULL)
412 return EOK;
413
414 hashed_task_t *ht =
415 hash_table_get_instance(link, hashed_task_t, link);
416
417 ht->finished = true;
418
419 return EOK;
420}
421
422/**
423 * @}
424 */
Note: See TracBrowser for help on using the repository browser.