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