source: mainline/uspace/srv/ns/task.c@ 1d1bb0f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d1bb0f was c7bbf029, checked in by Martin Decky <martin@…>, 14 years ago

improve stack traces and assertions
reduce header files pollution

  • Property mode set to 100644
File size: 9.6 KB
RevLine 
[40313e4]1/*
2 * Copyright (c) 2009 Martin Decky
[5d96851b]3 * Copyright (c) 2009 Jiri Svoboda
[40313e4]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>
[d9c8c81]35#include <adt/hash_table.h>
[40313e4]36#include <bool.h>
37#include <errno.h>
38#include <assert.h>
39#include <stdio.h>
40#include <macros.h>
[c7bbf029]41#include <malloc.h>
[40313e4]42#include "task.h"
43#include "ns.h"
44
45#define TASK_HASH_TABLE_CHAINS 256
[007e6efa]46#define P2I_HASH_TABLE_CHAINS 256
[40313e4]47
48/* TODO:
49 *
[0315679]50 * As there is currently no convention that each task has to be waited
[40313e4]51 * for, the NS can leak memory because of the zombie tasks.
52 *
53 */
54
55/** Task hash table item. */
56typedef struct {
57 link_t link;
[007e6efa]58
59 task_id_t id; /**< Task ID. */
60 bool finished; /**< Task is done. */
61 bool have_rval; /**< Task returned a value. */
62 int retval; /**< The return value. */
[40313e4]63} hashed_task_t;
64
65/** Compute hash index into task hash table.
66 *
67 * @param key Pointer keys. However, only the first key (i.e. truncated task
68 * number) is used to compute the hash index.
69 *
70 * @return Hash index corresponding to key[0].
71 *
72 */
[007e6efa]73static hash_index_t task_hash(unsigned long key[])
[40313e4]74{
75 assert(key);
[007e6efa]76 return (LOWER32(key[0]) % TASK_HASH_TABLE_CHAINS);
[40313e4]77}
78
79/** Compare a key with hashed item.
80 *
81 * @param key Array of keys.
[5d96851b]82 * @param keys Must be less than or equal to 2.
[40313e4]83 * @param item Pointer to a hash table item.
84 *
85 * @return Non-zero if the key matches the item, zero otherwise.
86 *
87 */
88static int task_compare(unsigned long key[], hash_count_t keys, link_t *item)
89{
90 assert(key);
91 assert(keys <= 2);
92 assert(item);
93
94 hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
95
96 if (keys == 2)
97 return ((LOWER32(key[1]) == UPPER32(ht->id))
98 && (LOWER32(key[0]) == LOWER32(ht->id)));
99 else
100 return (LOWER32(key[0]) == LOWER32(ht->id));
101}
102
103/** Perform actions after removal of item from the hash table.
104 *
105 * @param item Item that was removed from the hash table.
106 *
107 */
108static void task_remove(link_t *item)
109{
110 assert(item);
111 free(hash_table_get_instance(item, hashed_task_t, link));
112}
113
114/** Operations for task hash table. */
115static hash_table_operations_t task_hash_table_ops = {
116 .hash = task_hash,
117 .compare = task_compare,
118 .remove_callback = task_remove
119};
120
121/** Task hash table structure. */
122static hash_table_t task_hash_table;
123
[5d96851b]124typedef struct {
125 link_t link;
[007e6efa]126 sysarg_t in_phone_hash; /**< Incoming phone hash. */
127 task_id_t id; /**< Task ID. */
[5d96851b]128} p2i_entry_t;
129
130/** Compute hash index into task hash table.
131 *
132 * @param key Array of keys.
[007e6efa]133 *
[5d96851b]134 * @return Hash index corresponding to key[0].
135 *
136 */
[007e6efa]137static hash_index_t p2i_hash(unsigned long key[])
[5d96851b]138{
139 assert(key);
[007e6efa]140 return (key[0] % TASK_HASH_TABLE_CHAINS);
[5d96851b]141}
142
143/** Compare a key with hashed item.
144 *
145 * @param key Array of keys.
146 * @param keys Must be less than or equal to 1.
147 * @param item Pointer to a hash table item.
148 *
149 * @return Non-zero if the key matches the item, zero otherwise.
150 *
151 */
152static int p2i_compare(unsigned long key[], hash_count_t keys, link_t *item)
153{
154 assert(key);
155 assert(keys == 1);
156 assert(item);
[007e6efa]157
158 p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
159
160 return (key[0] == entry->in_phone_hash);
[5d96851b]161}
162
163/** Perform actions after removal of item from the hash table.
164 *
165 * @param item Item that was removed from the hash table.
166 *
167 */
168static void p2i_remove(link_t *item)
169{
170 assert(item);
171 free(hash_table_get_instance(item, p2i_entry_t, link));
172}
173
174/** Operations for task hash table. */
175static hash_table_operations_t p2i_ops = {
176 .hash = p2i_hash,
177 .compare = p2i_compare,
178 .remove_callback = p2i_remove
179};
180
181/** Map phone hash to task ID */
182static hash_table_t phone_to_id;
183
[40313e4]184/** Pending task wait structure. */
185typedef struct {
186 link_t link;
187 task_id_t id; /**< Task ID. */
188 ipc_callid_t callid; /**< Call ID waiting for the connection */
189} pending_wait_t;
190
191static link_t pending_wait;
192
193int task_init(void)
194{
195 if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS,
196 2, &task_hash_table_ops)) {
197 printf(NAME ": No memory available for tasks\n");
198 return ENOMEM;
199 }
[007e6efa]200
[5d96851b]201 if (!hash_table_create(&phone_to_id, P2I_HASH_TABLE_CHAINS,
202 1, &p2i_ops)) {
203 printf(NAME ": No memory available for tasks\n");
204 return ENOMEM;
205 }
[40313e4]206
207 list_initialize(&pending_wait);
208 return EOK;
209}
210
211/** Process pending wait requests */
212void process_pending_wait(void)
213{
214 link_t *cur;
[adb49f58]215 task_exit_t texit;
[40313e4]216
217loop:
218 for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) {
219 pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
220
221 unsigned long keys[2] = {
222 LOWER32(pr->id),
223 UPPER32(pr->id)
224 };
225
226 link_t *link = hash_table_find(&task_hash_table, keys);
227 if (!link)
228 continue;
229
230 hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
[adb49f58]231 if (!ht->finished)
[40313e4]232 continue;
233
[adb49f58]234 if (!(pr->callid & IPC_CALLID_NOTIFICATION)) {
235 texit = ht->have_rval ? TASK_EXIT_NORMAL :
236 TASK_EXIT_UNEXPECTED;
237 ipc_answer_2(pr->callid, EOK, texit,
238 ht->retval);
239 }
[007e6efa]240
[40313e4]241 hash_table_remove(&task_hash_table, keys, 2);
242 list_remove(cur);
243 free(pr);
244 goto loop;
245 }
246}
247
248void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
249{
[96b02eb9]250 sysarg_t retval;
[adb49f58]251 task_exit_t texit;
[007e6efa]252
[40313e4]253 unsigned long keys[2] = {
254 LOWER32(id),
255 UPPER32(id)
256 };
[007e6efa]257
[40313e4]258 link_t *link = hash_table_find(&task_hash_table, keys);
259 hashed_task_t *ht = (link != NULL) ?
260 hash_table_get_instance(link, hashed_task_t, link) : NULL;
[007e6efa]261
[5d96851b]262 if (ht == NULL) {
[0315679]263 /* No such task exists. */
[475fe35]264 ipc_answer_0(callid, ENOENT);
265 return;
[5d96851b]266 }
[007e6efa]267
[adb49f58]268 if (!ht->finished) {
[40313e4]269 /* Add to pending list */
270 pending_wait_t *pr =
271 (pending_wait_t *) malloc(sizeof(pending_wait_t));
272 if (!pr) {
273 retval = ENOMEM;
274 goto out;
275 }
276
[007e6efa]277 link_initialize(&pr->link);
[40313e4]278 pr->id = id;
279 pr->callid = callid;
280 list_append(&pr->link, &pending_wait);
281 return;
282 }
283
284 hash_table_remove(&task_hash_table, keys, 2);
285 retval = EOK;
286
287out:
[adb49f58]288 if (!(callid & IPC_CALLID_NOTIFICATION)) {
289 texit = ht->have_rval ? TASK_EXIT_NORMAL : TASK_EXIT_UNEXPECTED;
290 ipc_answer_2(callid, retval, texit, ht->retval);
291 }
[7114d83]292}
293
[5d96851b]294int ns_task_id_intro(ipc_call_t *call)
295{
[0315679]296 unsigned long keys[2];
[007e6efa]297
298 task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
[5d96851b]299 keys[0] = call->in_phone_hash;
[007e6efa]300
301 link_t *link = hash_table_find(&phone_to_id, keys);
[5d96851b]302 if (link != NULL)
303 return EEXISTS;
[007e6efa]304
305 p2i_entry_t *entry = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
306 if (entry == NULL)
[5d96851b]307 return ENOMEM;
[007e6efa]308
309 hashed_task_t *ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
[0315679]310 if (ht == NULL)
311 return ENOMEM;
[007e6efa]312
313 /*
314 * Insert into the phone-to-id map.
315 */
316
317 link_initialize(&entry->link);
318 entry->in_phone_hash = call->in_phone_hash;
319 entry->id = id;
320 hash_table_insert(&phone_to_id, keys, &entry->link);
321
322 /*
323 * Insert into the main table.
324 */
325
[0315679]326 keys[0] = LOWER32(id);
327 keys[1] = UPPER32(id);
[007e6efa]328
[0315679]329 link_initialize(&ht->link);
330 ht->id = id;
[adb49f58]331 ht->finished = false;
332 ht->have_rval = false;
[0315679]333 ht->retval = -1;
334 hash_table_insert(&task_hash_table, keys, &ht->link);
[007e6efa]335
336 return EOK;
337}
[0315679]338
[007e6efa]339static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
340{
341 unsigned long keys[1] = {phone_hash};
342
343 link_t *link = hash_table_find(&phone_to_id, keys);
344 if (link == NULL)
345 return ENOENT;
346
347 p2i_entry_t *entry = hash_table_get_instance(link, p2i_entry_t, link);
348 *id = entry->id;
349
[5d96851b]350 return EOK;
351}
352
[7114d83]353int ns_task_retval(ipc_call_t *call)
354{
355 task_id_t id;
[007e6efa]356 int rc = get_id_by_phone(call->in_phone_hash, &id);
[5d96851b]357 if (rc != EOK)
358 return rc;
[007e6efa]359
360 unsigned long keys[2] = {
361 LOWER32(id),
362 UPPER32(id)
363 };
[7114d83]364
365 link_t *link = hash_table_find(&task_hash_table, keys);
366 hashed_task_t *ht = (link != NULL) ?
367 hash_table_get_instance(link, hashed_task_t, link) : NULL;
368
[007e6efa]369 if ((ht == NULL) || (ht->finished))
[7114d83]370 return EINVAL;
[007e6efa]371
[adb49f58]372 ht->finished = true;
373 ht->have_rval = true;
[5d96851b]374 ht->retval = IPC_GET_ARG1(*call);
[007e6efa]375
[5d96851b]376 return EOK;
377}
378
379int ns_task_disconnect(ipc_call_t *call)
380{
[0315679]381 unsigned long keys[2];
[007e6efa]382
[0315679]383 task_id_t id;
[007e6efa]384 int rc = get_id_by_phone(call->in_phone_hash, &id);
[0315679]385 if (rc != EOK)
386 return rc;
[007e6efa]387
[0315679]388 /* Delete from phone-to-id map. */
[5d96851b]389 keys[0] = call->in_phone_hash;
390 hash_table_remove(&phone_to_id, keys, 1);
[007e6efa]391
[0315679]392 /* Mark task as finished. */
393 keys[0] = LOWER32(id);
394 keys[1] = UPPER32(id);
[007e6efa]395
[0315679]396 link_t *link = hash_table_find(&task_hash_table, keys);
397 hashed_task_t *ht =
398 hash_table_get_instance(link, hashed_task_t, link);
[adb49f58]399 if (ht == NULL)
400 return EOK;
[007e6efa]401
[adb49f58]402 ht->finished = true;
[007e6efa]403
[7114d83]404 return EOK;
[40313e4]405}
406
407/**
408 * @}
409 */
Note: See TracBrowser for help on using the repository browser.