source: mainline/uspace/srv/ns/task.c@ b51cf2c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b51cf2c was feeac0d, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Simplify use of list_foreach.

  • Property mode set to 100644
File size: 8.1 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>
[3e6a98c5]36#include <stdbool.h>
[40313e4]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
46/* TODO:
47 *
[0315679]48 * As there is currently no convention that each task has to be waited
[40313e4]49 * for, the NS can leak memory because of the zombie tasks.
50 *
51 */
52
53/** Task hash table item. */
54typedef struct {
[062d900]55 ht_link_t link;
[007e6efa]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. */
[40313e4]61} hashed_task_t;
62
[062d900]63
64static size_t task_key_hash(void *key)
[40313e4]65{
[062d900]66 return *(task_id_t*)key;
[40313e4]67}
68
[062d900]69static size_t task_hash(const ht_link_t *item)
[40313e4]70{
[062d900]71 hashed_task_t *ht = hash_table_get_inst(item, hashed_task_t, link);
72 return ht->id;
[40313e4]73}
74
[062d900]75static bool task_key_equal(void *key, const ht_link_t *item)
[40313e4]76{
[062d900]77 hashed_task_t *ht = hash_table_get_inst(item, hashed_task_t, link);
78 return ht->id == *(task_id_t*)key;
79}
80
81/** Perform actions after removal of item from the hash table. */
82static void task_remove(ht_link_t *item)
83{
84 free(hash_table_get_inst(item, hashed_task_t, link));
[40313e4]85}
86
87/** Operations for task hash table. */
[062d900]88static hash_table_ops_t task_hash_table_ops = {
[40313e4]89 .hash = task_hash,
[062d900]90 .key_hash = task_key_hash,
91 .key_equal = task_key_equal,
[4e00f87]92 .equal = NULL,
[40313e4]93 .remove_callback = task_remove
94};
95
96/** Task hash table structure. */
97static hash_table_t task_hash_table;
98
[5d96851b]99typedef struct {
[062d900]100 ht_link_t link;
[007e6efa]101 sysarg_t in_phone_hash; /**< Incoming phone hash. */
102 task_id_t id; /**< Task ID. */
[5d96851b]103} p2i_entry_t;
104
[062d900]105/* phone-to-id hash table operations */
106
107static size_t p2i_key_hash(void *key)
[5d96851b]108{
[062d900]109 sysarg_t in_phone_hash = *(sysarg_t*)key;
110 return in_phone_hash;
[5d96851b]111}
112
[062d900]113static size_t p2i_hash(const ht_link_t *item)
[5d96851b]114{
[062d900]115 p2i_entry_t *entry = hash_table_get_inst(item, p2i_entry_t, link);
116 return entry->in_phone_hash;
117}
118
119static bool p2i_key_equal(void *key, const ht_link_t *item)
120{
121 sysarg_t in_phone_hash = *(sysarg_t*)key;
122 p2i_entry_t *entry = hash_table_get_inst(item, p2i_entry_t, link);
[007e6efa]123
[062d900]124 return (in_phone_hash == entry->in_phone_hash);
[5d96851b]125}
126
127/** Perform actions after removal of item from the hash table.
128 *
129 * @param item Item that was removed from the hash table.
130 *
131 */
[062d900]132static void p2i_remove(ht_link_t *item)
[5d96851b]133{
134 assert(item);
[062d900]135 free(hash_table_get_inst(item, p2i_entry_t, link));
[5d96851b]136}
137
138/** Operations for task hash table. */
[062d900]139static hash_table_ops_t p2i_ops = {
[5d96851b]140 .hash = p2i_hash,
[062d900]141 .key_hash = p2i_key_hash,
142 .key_equal = p2i_key_equal,
[4e00f87]143 .equal = NULL,
[5d96851b]144 .remove_callback = p2i_remove
145};
146
147/** Map phone hash to task ID */
148static hash_table_t phone_to_id;
149
[40313e4]150/** Pending task wait structure. */
151typedef struct {
152 link_t link;
153 task_id_t id; /**< Task ID. */
154 ipc_callid_t callid; /**< Call ID waiting for the connection */
155} pending_wait_t;
156
[b72efe8]157static list_t pending_wait;
[40313e4]158
159int task_init(void)
160{
[062d900]161 if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) {
[40313e4]162 printf(NAME ": No memory available for tasks\n");
163 return ENOMEM;
164 }
[007e6efa]165
[062d900]166 if (!hash_table_create(&phone_to_id, 0, 0, &p2i_ops)) {
[5d96851b]167 printf(NAME ": No memory available for tasks\n");
168 return ENOMEM;
169 }
[40313e4]170
171 list_initialize(&pending_wait);
172 return EOK;
173}
174
175/** Process pending wait requests */
176void process_pending_wait(void)
177{
[adb49f58]178 task_exit_t texit;
[40313e4]179
180loop:
[feeac0d]181 list_foreach(pending_wait, link, pending_wait_t, pr) {
[062d900]182 ht_link_t *link = hash_table_find(&task_hash_table, &pr->id);
[40313e4]183 if (!link)
184 continue;
185
[062d900]186 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
[adb49f58]187 if (!ht->finished)
[40313e4]188 continue;
189
[adb49f58]190 if (!(pr->callid & IPC_CALLID_NOTIFICATION)) {
191 texit = ht->have_rval ? TASK_EXIT_NORMAL :
192 TASK_EXIT_UNEXPECTED;
193 ipc_answer_2(pr->callid, EOK, texit,
194 ht->retval);
195 }
[007e6efa]196
[062d900]197 hash_table_remove(&task_hash_table, &pr->id);
[feeac0d]198 list_remove(&pr->link);
[40313e4]199 free(pr);
200 goto loop;
201 }
202}
203
204void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
205{
[96b02eb9]206 sysarg_t retval;
[adb49f58]207 task_exit_t texit;
[166a1f57]208 bool remove = false;
[007e6efa]209
[062d900]210 ht_link_t *link = hash_table_find(&task_hash_table, &id);
[40313e4]211 hashed_task_t *ht = (link != NULL) ?
[062d900]212 hash_table_get_inst(link, hashed_task_t, link) : NULL;
[007e6efa]213
[5d96851b]214 if (ht == NULL) {
[0315679]215 /* No such task exists. */
[475fe35]216 ipc_answer_0(callid, ENOENT);
217 return;
[5d96851b]218 }
[007e6efa]219
[adb49f58]220 if (!ht->finished) {
[40313e4]221 /* Add to pending list */
222 pending_wait_t *pr =
223 (pending_wait_t *) malloc(sizeof(pending_wait_t));
224 if (!pr) {
225 retval = ENOMEM;
226 goto out;
227 }
228
[007e6efa]229 link_initialize(&pr->link);
[40313e4]230 pr->id = id;
231 pr->callid = callid;
232 list_append(&pr->link, &pending_wait);
233 return;
234 }
235
[166a1f57]236 remove = true;
[40313e4]237 retval = EOK;
238
239out:
[adb49f58]240 if (!(callid & IPC_CALLID_NOTIFICATION)) {
241 texit = ht->have_rval ? TASK_EXIT_NORMAL : TASK_EXIT_UNEXPECTED;
242 ipc_answer_2(callid, retval, texit, ht->retval);
243 }
[166a1f57]244 if (remove)
245 hash_table_remove_item(&task_hash_table, link);
[7114d83]246}
247
[5d96851b]248int ns_task_id_intro(ipc_call_t *call)
249{
[007e6efa]250
251 task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
[062d900]252
253 ht_link_t *link = hash_table_find(&phone_to_id, &call->in_phone_hash);
[5d96851b]254 if (link != NULL)
255 return EEXISTS;
[007e6efa]256
257 p2i_entry_t *entry = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
258 if (entry == NULL)
[5d96851b]259 return ENOMEM;
[007e6efa]260
261 hashed_task_t *ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
[0315679]262 if (ht == NULL)
263 return ENOMEM;
[007e6efa]264
265 /*
266 * Insert into the phone-to-id map.
267 */
268
269 entry->in_phone_hash = call->in_phone_hash;
270 entry->id = id;
[062d900]271 hash_table_insert(&phone_to_id, &entry->link);
[007e6efa]272
273 /*
274 * Insert into the main table.
275 */
276
[0315679]277 ht->id = id;
[adb49f58]278 ht->finished = false;
279 ht->have_rval = false;
[0315679]280 ht->retval = -1;
[062d900]281 hash_table_insert(&task_hash_table, &ht->link);
[007e6efa]282
283 return EOK;
284}
[0315679]285
[007e6efa]286static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
287{
[062d900]288 ht_link_t *link = hash_table_find(&phone_to_id, &phone_hash);
[007e6efa]289 if (link == NULL)
290 return ENOENT;
291
[062d900]292 p2i_entry_t *entry = hash_table_get_inst(link, p2i_entry_t, link);
[007e6efa]293 *id = entry->id;
294
[5d96851b]295 return EOK;
296}
297
[7114d83]298int ns_task_retval(ipc_call_t *call)
299{
300 task_id_t id;
[007e6efa]301 int rc = get_id_by_phone(call->in_phone_hash, &id);
[5d96851b]302 if (rc != EOK)
303 return rc;
[007e6efa]304
[062d900]305 ht_link_t *link = hash_table_find(&task_hash_table, &id);
[7114d83]306 hashed_task_t *ht = (link != NULL) ?
[062d900]307 hash_table_get_inst(link, hashed_task_t, link) : NULL;
[7114d83]308
[007e6efa]309 if ((ht == NULL) || (ht->finished))
[7114d83]310 return EINVAL;
[007e6efa]311
[adb49f58]312 ht->finished = true;
313 ht->have_rval = true;
[5d96851b]314 ht->retval = IPC_GET_ARG1(*call);
[007e6efa]315
[5d96851b]316 return EOK;
317}
318
319int ns_task_disconnect(ipc_call_t *call)
320{
[0315679]321 task_id_t id;
[007e6efa]322 int rc = get_id_by_phone(call->in_phone_hash, &id);
[0315679]323 if (rc != EOK)
324 return rc;
[007e6efa]325
[0315679]326 /* Delete from phone-to-id map. */
[062d900]327 hash_table_remove(&phone_to_id, &call->in_phone_hash);
[007e6efa]328
[0315679]329 /* Mark task as finished. */
[062d900]330 ht_link_t *link = hash_table_find(&task_hash_table, &id);
331 if (link == NULL)
[adb49f58]332 return EOK;
[062d900]333
334 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
[007e6efa]335
[adb49f58]336 ht->finished = true;
[007e6efa]337
[7114d83]338 return EOK;
[40313e4]339}
340
341/**
342 * @}
343 */
Note: See TracBrowser for help on using the repository browser.