source: mainline/uspace/srv/ns/task.c@ 889cdb1

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

Fix vertical spacing with new Ccheck revision.

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