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
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 <stdbool.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 ht_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
64static size_t task_key_hash(void *key)
65{
66 return *(task_id_t*)key;
67}
68
69static size_t task_hash(const ht_link_t *item)
70{
71 hashed_task_t *ht = hash_table_get_inst(item, hashed_task_t, link);
72 return ht->id;
73}
74
75static bool task_key_equal(void *key, const ht_link_t *item)
76{
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));
85}
86
87/** Operations for task hash table. */
88static hash_table_ops_t task_hash_table_ops = {
89 .hash = task_hash,
90 .key_hash = task_key_hash,
91 .key_equal = task_key_equal,
92 .equal = NULL,
93 .remove_callback = task_remove
94};
95
96/** Task hash table structure. */
97static hash_table_t task_hash_table;
98
99typedef struct {
100 ht_link_t link;
101 sysarg_t in_phone_hash; /**< Incoming phone hash. */
102 task_id_t id; /**< Task ID. */
103} p2i_entry_t;
104
105/* phone-to-id hash table operations */
106
107static size_t p2i_key_hash(void *key)
108{
109 sysarg_t in_phone_hash = *(sysarg_t*)key;
110 return in_phone_hash;
111}
112
113static size_t p2i_hash(const ht_link_t *item)
114{
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);
123
124 return (in_phone_hash == entry->in_phone_hash);
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 */
132static void p2i_remove(ht_link_t *item)
133{
134 assert(item);
135 free(hash_table_get_inst(item, p2i_entry_t, link));
136}
137
138/** Operations for task hash table. */
139static hash_table_ops_t p2i_ops = {
140 .hash = p2i_hash,
141 .key_hash = p2i_key_hash,
142 .key_equal = p2i_key_equal,
143 .equal = NULL,
144 .remove_callback = p2i_remove
145};
146
147/** Map phone hash to task ID */
148static hash_table_t phone_to_id;
149
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
157static list_t pending_wait;
158
159int task_init(void)
160{
161 if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) {
162 printf(NAME ": No memory available for tasks\n");
163 return ENOMEM;
164 }
165
166 if (!hash_table_create(&phone_to_id, 0, 0, &p2i_ops)) {
167 printf(NAME ": No memory available for tasks\n");
168 return ENOMEM;
169 }
170
171 list_initialize(&pending_wait);
172 return EOK;
173}
174
175/** Process pending wait requests */
176void process_pending_wait(void)
177{
178 task_exit_t texit;
179
180loop:
181 list_foreach(pending_wait, link, pending_wait_t, pr) {
182 ht_link_t *link = hash_table_find(&task_hash_table, &pr->id);
183 if (!link)
184 continue;
185
186 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
187 if (!ht->finished)
188 continue;
189
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 }
196
197 hash_table_remove(&task_hash_table, &pr->id);
198 list_remove(&pr->link);
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{
206 sysarg_t retval;
207 task_exit_t texit;
208 bool remove = false;
209
210 ht_link_t *link = hash_table_find(&task_hash_table, &id);
211 hashed_task_t *ht = (link != NULL) ?
212 hash_table_get_inst(link, hashed_task_t, link) : NULL;
213
214 if (ht == NULL) {
215 /* No such task exists. */
216 ipc_answer_0(callid, ENOENT);
217 return;
218 }
219
220 if (!ht->finished) {
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
229 link_initialize(&pr->link);
230 pr->id = id;
231 pr->callid = callid;
232 list_append(&pr->link, &pending_wait);
233 return;
234 }
235
236 remove = true;
237 retval = EOK;
238
239out:
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 }
244 if (remove)
245 hash_table_remove_item(&task_hash_table, link);
246}
247
248int ns_task_id_intro(ipc_call_t *call)
249{
250
251 task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
252
253 ht_link_t *link = hash_table_find(&phone_to_id, &call->in_phone_hash);
254 if (link != NULL)
255 return EEXISTS;
256
257 p2i_entry_t *entry = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
258 if (entry == NULL)
259 return ENOMEM;
260
261 hashed_task_t *ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
262 if (ht == NULL)
263 return ENOMEM;
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;
271 hash_table_insert(&phone_to_id, &entry->link);
272
273 /*
274 * Insert into the main table.
275 */
276
277 ht->id = id;
278 ht->finished = false;
279 ht->have_rval = false;
280 ht->retval = -1;
281 hash_table_insert(&task_hash_table, &ht->link);
282
283 return EOK;
284}
285
286static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
287{
288 ht_link_t *link = hash_table_find(&phone_to_id, &phone_hash);
289 if (link == NULL)
290 return ENOENT;
291
292 p2i_entry_t *entry = hash_table_get_inst(link, p2i_entry_t, link);
293 *id = entry->id;
294
295 return EOK;
296}
297
298int ns_task_retval(ipc_call_t *call)
299{
300 task_id_t id;
301 int rc = get_id_by_phone(call->in_phone_hash, &id);
302 if (rc != EOK)
303 return rc;
304
305 ht_link_t *link = hash_table_find(&task_hash_table, &id);
306 hashed_task_t *ht = (link != NULL) ?
307 hash_table_get_inst(link, hashed_task_t, link) : NULL;
308
309 if ((ht == NULL) || (ht->finished))
310 return EINVAL;
311
312 ht->finished = true;
313 ht->have_rval = true;
314 ht->retval = IPC_GET_ARG1(*call);
315
316 return EOK;
317}
318
319int ns_task_disconnect(ipc_call_t *call)
320{
321 task_id_t id;
322 int rc = get_id_by_phone(call->in_phone_hash, &id);
323 if (rc != EOK)
324 return rc;
325
326 /* Delete from phone-to-id map. */
327 hash_table_remove(&phone_to_id, &call->in_phone_hash);
328
329 /* Mark task as finished. */
330 ht_link_t *link = hash_table_find(&task_hash_table, &id);
331 if (link == NULL)
332 return EOK;
333
334 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
335
336 ht->finished = true;
337
338 return EOK;
339}
340
341/**
342 * @}
343 */
Note: See TracBrowser for help on using the repository browser.