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

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