[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>
|
---|
[40313e4] | 36 | #include <bool.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <assert.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <macros.h>
|
---|
| 41 | #include "task.h"
|
---|
| 42 | #include "ns.h"
|
---|
| 43 |
|
---|
| 44 | #define TASK_HASH_TABLE_CHAINS 256
|
---|
[007e6efa] | 45 | #define P2I_HASH_TABLE_CHAINS 256
|
---|
[40313e4] | 46 |
|
---|
| 47 | /* TODO:
|
---|
| 48 | *
|
---|
[0315679] | 49 | * As there is currently no convention that each task has to be waited
|
---|
[40313e4] | 50 | * for, the NS can leak memory because of the zombie tasks.
|
---|
| 51 | *
|
---|
| 52 | */
|
---|
| 53 |
|
---|
| 54 | /** Task hash table item. */
|
---|
| 55 | typedef struct {
|
---|
| 56 | link_t link;
|
---|
[007e6efa] | 57 |
|
---|
| 58 | task_id_t id; /**< Task ID. */
|
---|
| 59 | bool finished; /**< Task is done. */
|
---|
| 60 | bool have_rval; /**< Task returned a value. */
|
---|
| 61 | int retval; /**< The return value. */
|
---|
[40313e4] | 62 | } hashed_task_t;
|
---|
| 63 |
|
---|
| 64 | /** Compute hash index into task hash table.
|
---|
| 65 | *
|
---|
| 66 | * @param key Pointer keys. However, only the first key (i.e. truncated task
|
---|
| 67 | * number) is used to compute the hash index.
|
---|
| 68 | *
|
---|
| 69 | * @return Hash index corresponding to key[0].
|
---|
| 70 | *
|
---|
| 71 | */
|
---|
[007e6efa] | 72 | static hash_index_t task_hash(unsigned long key[])
|
---|
[40313e4] | 73 | {
|
---|
| 74 | assert(key);
|
---|
[007e6efa] | 75 | return (LOWER32(key[0]) % TASK_HASH_TABLE_CHAINS);
|
---|
[40313e4] | 76 | }
|
---|
| 77 |
|
---|
| 78 | /** Compare a key with hashed item.
|
---|
| 79 | *
|
---|
| 80 | * @param key Array of keys.
|
---|
[5d96851b] | 81 | * @param keys Must be less than or equal to 2.
|
---|
[40313e4] | 82 | * @param item Pointer to a hash table item.
|
---|
| 83 | *
|
---|
| 84 | * @return Non-zero if the key matches the item, zero otherwise.
|
---|
| 85 | *
|
---|
| 86 | */
|
---|
| 87 | static int task_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
| 88 | {
|
---|
| 89 | assert(key);
|
---|
| 90 | assert(keys <= 2);
|
---|
| 91 | assert(item);
|
---|
| 92 |
|
---|
| 93 | hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link);
|
---|
| 94 |
|
---|
| 95 | if (keys == 2)
|
---|
| 96 | return ((LOWER32(key[1]) == UPPER32(ht->id))
|
---|
| 97 | && (LOWER32(key[0]) == LOWER32(ht->id)));
|
---|
| 98 | else
|
---|
| 99 | return (LOWER32(key[0]) == LOWER32(ht->id));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Perform actions after removal of item from the hash table.
|
---|
| 103 | *
|
---|
| 104 | * @param item Item that was removed from the hash table.
|
---|
| 105 | *
|
---|
| 106 | */
|
---|
| 107 | static void task_remove(link_t *item)
|
---|
| 108 | {
|
---|
| 109 | assert(item);
|
---|
| 110 | free(hash_table_get_instance(item, hashed_task_t, link));
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /** Operations for task hash table. */
|
---|
| 114 | static hash_table_operations_t task_hash_table_ops = {
|
---|
| 115 | .hash = task_hash,
|
---|
| 116 | .compare = task_compare,
|
---|
| 117 | .remove_callback = task_remove
|
---|
| 118 | };
|
---|
| 119 |
|
---|
| 120 | /** Task hash table structure. */
|
---|
| 121 | static hash_table_t task_hash_table;
|
---|
| 122 |
|
---|
[5d96851b] | 123 | typedef struct {
|
---|
| 124 | link_t link;
|
---|
[007e6efa] | 125 | sysarg_t in_phone_hash; /**< Incoming phone hash. */
|
---|
| 126 | task_id_t id; /**< Task ID. */
|
---|
[5d96851b] | 127 | } p2i_entry_t;
|
---|
| 128 |
|
---|
| 129 | /** Compute hash index into task hash table.
|
---|
| 130 | *
|
---|
| 131 | * @param key Array of keys.
|
---|
[007e6efa] | 132 | *
|
---|
[5d96851b] | 133 | * @return Hash index corresponding to key[0].
|
---|
| 134 | *
|
---|
| 135 | */
|
---|
[007e6efa] | 136 | static hash_index_t p2i_hash(unsigned long key[])
|
---|
[5d96851b] | 137 | {
|
---|
| 138 | assert(key);
|
---|
[007e6efa] | 139 | return (key[0] % TASK_HASH_TABLE_CHAINS);
|
---|
[5d96851b] | 140 | }
|
---|
| 141 |
|
---|
| 142 | /** Compare a key with hashed item.
|
---|
| 143 | *
|
---|
| 144 | * @param key Array of keys.
|
---|
| 145 | * @param keys Must be less than or equal to 1.
|
---|
| 146 | * @param item Pointer to a hash table item.
|
---|
| 147 | *
|
---|
| 148 | * @return Non-zero if the key matches the item, zero otherwise.
|
---|
| 149 | *
|
---|
| 150 | */
|
---|
| 151 | static int p2i_compare(unsigned long key[], hash_count_t keys, link_t *item)
|
---|
| 152 | {
|
---|
| 153 | assert(key);
|
---|
| 154 | assert(keys == 1);
|
---|
| 155 | assert(item);
|
---|
[007e6efa] | 156 |
|
---|
| 157 | p2i_entry_t *entry = hash_table_get_instance(item, p2i_entry_t, link);
|
---|
| 158 |
|
---|
| 159 | return (key[0] == entry->in_phone_hash);
|
---|
[5d96851b] | 160 | }
|
---|
| 161 |
|
---|
| 162 | /** Perform actions after removal of item from the hash table.
|
---|
| 163 | *
|
---|
| 164 | * @param item Item that was removed from the hash table.
|
---|
| 165 | *
|
---|
| 166 | */
|
---|
| 167 | static void p2i_remove(link_t *item)
|
---|
| 168 | {
|
---|
| 169 | assert(item);
|
---|
| 170 | free(hash_table_get_instance(item, p2i_entry_t, link));
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /** Operations for task hash table. */
|
---|
| 174 | static hash_table_operations_t p2i_ops = {
|
---|
| 175 | .hash = p2i_hash,
|
---|
| 176 | .compare = p2i_compare,
|
---|
| 177 | .remove_callback = p2i_remove
|
---|
| 178 | };
|
---|
| 179 |
|
---|
| 180 | /** Map phone hash to task ID */
|
---|
| 181 | static hash_table_t phone_to_id;
|
---|
| 182 |
|
---|
[40313e4] | 183 | /** Pending task wait structure. */
|
---|
| 184 | typedef struct {
|
---|
| 185 | link_t link;
|
---|
| 186 | task_id_t id; /**< Task ID. */
|
---|
| 187 | ipc_callid_t callid; /**< Call ID waiting for the connection */
|
---|
| 188 | } pending_wait_t;
|
---|
| 189 |
|
---|
| 190 | static link_t pending_wait;
|
---|
| 191 |
|
---|
| 192 | int task_init(void)
|
---|
| 193 | {
|
---|
| 194 | if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS,
|
---|
| 195 | 2, &task_hash_table_ops)) {
|
---|
| 196 | printf(NAME ": No memory available for tasks\n");
|
---|
| 197 | return ENOMEM;
|
---|
| 198 | }
|
---|
[007e6efa] | 199 |
|
---|
[5d96851b] | 200 | if (!hash_table_create(&phone_to_id, P2I_HASH_TABLE_CHAINS,
|
---|
| 201 | 1, &p2i_ops)) {
|
---|
| 202 | printf(NAME ": No memory available for tasks\n");
|
---|
| 203 | return ENOMEM;
|
---|
| 204 | }
|
---|
[40313e4] | 205 |
|
---|
| 206 | list_initialize(&pending_wait);
|
---|
| 207 | return EOK;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /** Process pending wait requests */
|
---|
| 211 | void process_pending_wait(void)
|
---|
| 212 | {
|
---|
| 213 | link_t *cur;
|
---|
[adb49f58] | 214 | task_exit_t texit;
|
---|
[40313e4] | 215 |
|
---|
| 216 | loop:
|
---|
| 217 | for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) {
|
---|
| 218 | pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link);
|
---|
| 219 |
|
---|
| 220 | unsigned long keys[2] = {
|
---|
| 221 | LOWER32(pr->id),
|
---|
| 222 | UPPER32(pr->id)
|
---|
| 223 | };
|
---|
| 224 |
|
---|
| 225 | link_t *link = hash_table_find(&task_hash_table, keys);
|
---|
| 226 | if (!link)
|
---|
| 227 | continue;
|
---|
| 228 |
|
---|
| 229 | hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link);
|
---|
[adb49f58] | 230 | if (!ht->finished)
|
---|
[40313e4] | 231 | continue;
|
---|
| 232 |
|
---|
[adb49f58] | 233 | if (!(pr->callid & IPC_CALLID_NOTIFICATION)) {
|
---|
| 234 | texit = ht->have_rval ? TASK_EXIT_NORMAL :
|
---|
| 235 | TASK_EXIT_UNEXPECTED;
|
---|
| 236 | ipc_answer_2(pr->callid, EOK, texit,
|
---|
| 237 | ht->retval);
|
---|
| 238 | }
|
---|
[007e6efa] | 239 |
|
---|
[40313e4] | 240 | hash_table_remove(&task_hash_table, keys, 2);
|
---|
| 241 | list_remove(cur);
|
---|
| 242 | free(pr);
|
---|
| 243 | goto loop;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid)
|
---|
| 248 | {
|
---|
[96b02eb9] | 249 | sysarg_t retval;
|
---|
[adb49f58] | 250 | task_exit_t texit;
|
---|
[007e6efa] | 251 |
|
---|
[40313e4] | 252 | unsigned long keys[2] = {
|
---|
| 253 | LOWER32(id),
|
---|
| 254 | UPPER32(id)
|
---|
| 255 | };
|
---|
[007e6efa] | 256 |
|
---|
[40313e4] | 257 | link_t *link = hash_table_find(&task_hash_table, keys);
|
---|
| 258 | hashed_task_t *ht = (link != NULL) ?
|
---|
| 259 | hash_table_get_instance(link, hashed_task_t, link) : NULL;
|
---|
[007e6efa] | 260 |
|
---|
[5d96851b] | 261 | if (ht == NULL) {
|
---|
[0315679] | 262 | /* No such task exists. */
|
---|
[475fe35] | 263 | ipc_answer_0(callid, ENOENT);
|
---|
| 264 | return;
|
---|
[5d96851b] | 265 | }
|
---|
[007e6efa] | 266 |
|
---|
[adb49f58] | 267 | if (!ht->finished) {
|
---|
[40313e4] | 268 | /* Add to pending list */
|
---|
| 269 | pending_wait_t *pr =
|
---|
| 270 | (pending_wait_t *) malloc(sizeof(pending_wait_t));
|
---|
| 271 | if (!pr) {
|
---|
| 272 | retval = ENOMEM;
|
---|
| 273 | goto out;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[007e6efa] | 276 | link_initialize(&pr->link);
|
---|
[40313e4] | 277 | pr->id = id;
|
---|
| 278 | pr->callid = callid;
|
---|
| 279 | list_append(&pr->link, &pending_wait);
|
---|
| 280 | return;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | hash_table_remove(&task_hash_table, keys, 2);
|
---|
| 284 | retval = EOK;
|
---|
| 285 |
|
---|
| 286 | out:
|
---|
[adb49f58] | 287 | if (!(callid & IPC_CALLID_NOTIFICATION)) {
|
---|
| 288 | texit = ht->have_rval ? TASK_EXIT_NORMAL : TASK_EXIT_UNEXPECTED;
|
---|
| 289 | ipc_answer_2(callid, retval, texit, ht->retval);
|
---|
| 290 | }
|
---|
[7114d83] | 291 | }
|
---|
| 292 |
|
---|
[5d96851b] | 293 | int ns_task_id_intro(ipc_call_t *call)
|
---|
| 294 | {
|
---|
[0315679] | 295 | unsigned long keys[2];
|
---|
[007e6efa] | 296 |
|
---|
| 297 | task_id_t id = MERGE_LOUP32(IPC_GET_ARG1(*call), IPC_GET_ARG2(*call));
|
---|
[5d96851b] | 298 | keys[0] = call->in_phone_hash;
|
---|
[007e6efa] | 299 |
|
---|
| 300 | link_t *link = hash_table_find(&phone_to_id, keys);
|
---|
[5d96851b] | 301 | if (link != NULL)
|
---|
| 302 | return EEXISTS;
|
---|
[007e6efa] | 303 |
|
---|
| 304 | p2i_entry_t *entry = (p2i_entry_t *) malloc(sizeof(p2i_entry_t));
|
---|
| 305 | if (entry == NULL)
|
---|
[5d96851b] | 306 | return ENOMEM;
|
---|
[007e6efa] | 307 |
|
---|
| 308 | hashed_task_t *ht = (hashed_task_t *) malloc(sizeof(hashed_task_t));
|
---|
[0315679] | 309 | if (ht == NULL)
|
---|
| 310 | return ENOMEM;
|
---|
[007e6efa] | 311 |
|
---|
| 312 | /*
|
---|
| 313 | * Insert into the phone-to-id map.
|
---|
| 314 | */
|
---|
| 315 |
|
---|
| 316 | link_initialize(&entry->link);
|
---|
| 317 | entry->in_phone_hash = call->in_phone_hash;
|
---|
| 318 | entry->id = id;
|
---|
| 319 | hash_table_insert(&phone_to_id, keys, &entry->link);
|
---|
| 320 |
|
---|
| 321 | /*
|
---|
| 322 | * Insert into the main table.
|
---|
| 323 | */
|
---|
| 324 |
|
---|
[0315679] | 325 | keys[0] = LOWER32(id);
|
---|
| 326 | keys[1] = UPPER32(id);
|
---|
[007e6efa] | 327 |
|
---|
[0315679] | 328 | link_initialize(&ht->link);
|
---|
| 329 | ht->id = id;
|
---|
[adb49f58] | 330 | ht->finished = false;
|
---|
| 331 | ht->have_rval = false;
|
---|
[0315679] | 332 | ht->retval = -1;
|
---|
| 333 | hash_table_insert(&task_hash_table, keys, &ht->link);
|
---|
[007e6efa] | 334 |
|
---|
| 335 | return EOK;
|
---|
| 336 | }
|
---|
[0315679] | 337 |
|
---|
[007e6efa] | 338 | static int get_id_by_phone(sysarg_t phone_hash, task_id_t *id)
|
---|
| 339 | {
|
---|
| 340 | unsigned long keys[1] = {phone_hash};
|
---|
| 341 |
|
---|
| 342 | link_t *link = hash_table_find(&phone_to_id, keys);
|
---|
| 343 | if (link == NULL)
|
---|
| 344 | return ENOENT;
|
---|
| 345 |
|
---|
| 346 | p2i_entry_t *entry = hash_table_get_instance(link, p2i_entry_t, link);
|
---|
| 347 | *id = entry->id;
|
---|
| 348 |
|
---|
[5d96851b] | 349 | return EOK;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
[7114d83] | 352 | int ns_task_retval(ipc_call_t *call)
|
---|
| 353 | {
|
---|
| 354 | task_id_t id;
|
---|
[007e6efa] | 355 | int rc = get_id_by_phone(call->in_phone_hash, &id);
|
---|
[5d96851b] | 356 | if (rc != EOK)
|
---|
| 357 | return rc;
|
---|
[007e6efa] | 358 |
|
---|
| 359 | unsigned long keys[2] = {
|
---|
| 360 | LOWER32(id),
|
---|
| 361 | UPPER32(id)
|
---|
| 362 | };
|
---|
[7114d83] | 363 |
|
---|
| 364 | link_t *link = hash_table_find(&task_hash_table, keys);
|
---|
| 365 | hashed_task_t *ht = (link != NULL) ?
|
---|
| 366 | hash_table_get_instance(link, hashed_task_t, link) : NULL;
|
---|
| 367 |
|
---|
[007e6efa] | 368 | if ((ht == NULL) || (ht->finished))
|
---|
[7114d83] | 369 | return EINVAL;
|
---|
[007e6efa] | 370 |
|
---|
[adb49f58] | 371 | ht->finished = true;
|
---|
| 372 | ht->have_rval = true;
|
---|
[5d96851b] | 373 | ht->retval = IPC_GET_ARG1(*call);
|
---|
[007e6efa] | 374 |
|
---|
[5d96851b] | 375 | return EOK;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | int ns_task_disconnect(ipc_call_t *call)
|
---|
| 379 | {
|
---|
[0315679] | 380 | unsigned long keys[2];
|
---|
[007e6efa] | 381 |
|
---|
[0315679] | 382 | task_id_t id;
|
---|
[007e6efa] | 383 | int rc = get_id_by_phone(call->in_phone_hash, &id);
|
---|
[0315679] | 384 | if (rc != EOK)
|
---|
| 385 | return rc;
|
---|
[007e6efa] | 386 |
|
---|
[0315679] | 387 | /* Delete from phone-to-id map. */
|
---|
[5d96851b] | 388 | keys[0] = call->in_phone_hash;
|
---|
| 389 | hash_table_remove(&phone_to_id, keys, 1);
|
---|
[007e6efa] | 390 |
|
---|
[0315679] | 391 | /* Mark task as finished. */
|
---|
| 392 | keys[0] = LOWER32(id);
|
---|
| 393 | keys[1] = UPPER32(id);
|
---|
[007e6efa] | 394 |
|
---|
[0315679] | 395 | link_t *link = hash_table_find(&task_hash_table, keys);
|
---|
| 396 | hashed_task_t *ht =
|
---|
| 397 | hash_table_get_instance(link, hashed_task_t, link);
|
---|
[adb49f58] | 398 | if (ht == NULL)
|
---|
| 399 | return EOK;
|
---|
[007e6efa] | 400 |
|
---|
[adb49f58] | 401 | ht->finished = true;
|
---|
[007e6efa] | 402 |
|
---|
[7114d83] | 403 | return EOK;
|
---|
[40313e4] | 404 | }
|
---|
| 405 |
|
---|
| 406 | /**
|
---|
| 407 | * @}
|
---|
| 408 | */
|
---|