source: mainline/uspace/srv/ns/task.c@ 8c193d83

Last change on this file since 8c193d83 was fafb8e5, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Mechanically lowercase IPC_SET_*/IPC_GET_*

  • 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;
[6769005]93 sysarg_t label; /**< Incoming phone label. */
94 task_id_t id; /**< Task ID. */
[5d96851b]95} p2i_entry_t;
96
[6769005]97/* label-to-id hash table operations */
[062d900]98
99static size_t p2i_key_hash(void *key)
[5d96851b]100{
[6769005]101 sysarg_t label = *(sysarg_t *)key;
102 return label;
[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);
[6769005]108 return entry->label;
[062d900]109}
110
111static bool p2i_key_equal(void *key, const ht_link_t *item)
112{
[6769005]113 sysarg_t label = *(sysarg_t *)key;
[062d900]114 p2i_entry_t *entry = hash_table_get_inst(item, p2i_entry_t, link);
[a35b458]115
[6769005]116 return (label == entry->label);
[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
[6769005]139/** Map phone label to task ID */
[5d96851b]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{
[fafb8e5]227 task_id_t id = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
[a35b458]228
[6769005]229 ht_link_t *link = hash_table_find(&phone_to_id, &call->request_label);
[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
[6769005]247 assert(call->request_label);
248 entry->label = call->request_label;
[007e6efa]249 entry->id = id;
[062d900]250 hash_table_insert(&phone_to_id, &entry->link);
[a35b458]251
[007e6efa]252 /*
253 * Insert into the main table.
254 */
[a35b458]255
[0315679]256 ht->id = id;
[adb49f58]257 ht->finished = false;
258 ht->have_rval = false;
[0315679]259 ht->retval = -1;
[062d900]260 hash_table_insert(&task_hash_table, &ht->link);
[a35b458]261
[007e6efa]262 return EOK;
263}
[0315679]264
[6769005]265static errno_t get_id_by_phone(sysarg_t label, task_id_t *id)
[007e6efa]266{
[6769005]267 assert(label);
268 ht_link_t *link = hash_table_find(&phone_to_id, &label);
[007e6efa]269 if (link == NULL)
270 return ENOENT;
[a35b458]271
[062d900]272 p2i_entry_t *entry = hash_table_get_inst(link, p2i_entry_t, link);
[007e6efa]273 *id = entry->id;
[a35b458]274
[5d96851b]275 return EOK;
276}
277
[b7fd2a0]278errno_t ns_task_retval(ipc_call_t *call)
[7114d83]279{
[6769005]280 task_id_t id = call->task_id;
[a35b458]281
[062d900]282 ht_link_t *link = hash_table_find(&task_hash_table, &id);
[7114d83]283 hashed_task_t *ht = (link != NULL) ?
[062d900]284 hash_table_get_inst(link, hashed_task_t, link) : NULL;
[a35b458]285
[007e6efa]286 if ((ht == NULL) || (ht->finished))
[7114d83]287 return EINVAL;
[a35b458]288
[adb49f58]289 ht->finished = true;
290 ht->have_rval = true;
[fafb8e5]291 ht->retval = ipc_get_arg1(call);
[a35b458]292
[1c635d6]293 process_pending_wait();
[a35b458]294
[5d96851b]295 return EOK;
296}
297
[b7fd2a0]298errno_t ns_task_disconnect(ipc_call_t *call)
[5d96851b]299{
[0315679]300 task_id_t id;
[6769005]301 errno_t rc = get_id_by_phone(call->request_label, &id);
[0315679]302 if (rc != EOK)
303 return rc;
[a35b458]304
[0315679]305 /* Delete from phone-to-id map. */
[6769005]306 hash_table_remove(&phone_to_id, &call->request_label);
[a35b458]307
[0315679]308 /* Mark task as finished. */
[062d900]309 ht_link_t *link = hash_table_find(&task_hash_table, &id);
310 if (link == NULL)
[adb49f58]311 return EOK;
[062d900]312
313 hashed_task_t *ht = hash_table_get_inst(link, hashed_task_t, link);
[a35b458]314
[adb49f58]315 ht->finished = true;
[a35b458]316
[1c635d6]317 process_pending_wait();
318 hash_table_remove(&task_hash_table, &id);
[a35b458]319
[7114d83]320 return EOK;
[40313e4]321}
322
323/**
324 * @}
325 */
Note: See TracBrowser for help on using the repository browser.