[1be7bee] | 1 | /*
|
---|
[c675ab1] | 2 | * Copyright (c) 2009 Martin Decky
|
---|
| 3 | * Copyright (c) 2009 Jiri Svoboda
|
---|
| 4 | * Copyright (c) 2015 Michal Koutny
|
---|
| 5 | * All rights reserved.
|
---|
[1be7bee] | 6 | *
|
---|
[c675ab1] | 7 | * Redistribution and use in source and binary forms, with or without
|
---|
[1be7bee] | 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
[c675ab1] | 11 | * - Redistributions of source code must retain the above copyright
|
---|
[1be7bee] | 12 | * notice, this list of conditions and the following disclaimer.
|
---|
[c675ab1] | 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
[1be7bee] | 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
[c675ab1] | 16 | * - The name of the author may not be used to endorse or promote products
|
---|
[1be7bee] | 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
[c675ab1] | 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[1be7bee] | 29 | */
|
---|
| 30 |
|
---|
[2f44fafd] | 31 | /**
|
---|
[035d7d8] | 32 | * locking order: (TODO move to main?)
|
---|
[2f44fafd] | 33 | * - task_hash_table_lock,
|
---|
| 34 | * - pending_wait_lock.
|
---|
[035d7d8] | 35 | * - listeners_lock
|
---|
[2f44fafd] | 36 | *
|
---|
| 37 | * @addtogroup taskman
|
---|
[1be7bee] | 38 | * @{
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | #include <assert.h>
|
---|
| 42 | #include <async.h>
|
---|
| 43 | #include <errno.h>
|
---|
| 44 | #include <macros.h>
|
---|
| 45 | #include <malloc.h>
|
---|
| 46 | #include <stdbool.h>
|
---|
| 47 | #include <stdio.h>
|
---|
[2f44fafd] | 48 | #include <task.h>
|
---|
[1be7bee] | 49 | #include <types/task.h>
|
---|
| 50 |
|
---|
| 51 | #include "task.h"
|
---|
| 52 | #include "taskman.h"
|
---|
| 53 |
|
---|
| 54 | static size_t task_key_hash(void *key)
|
---|
| 55 | {
|
---|
| 56 | return *(task_id_t*)key;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static size_t task_hash(const ht_link_t *item)
|
---|
| 60 | {
|
---|
[035d7d8] | 61 | task_t *ht = hash_table_get_inst(item, task_t, link);
|
---|
[1be7bee] | 62 | return ht->id;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | static bool task_key_equal(void *key, const ht_link_t *item)
|
---|
| 66 | {
|
---|
[035d7d8] | 67 | task_t *ht = hash_table_get_inst(item, task_t, link);
|
---|
[1be7bee] | 68 | return ht->id == *(task_id_t*)key;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[c675ab1] | 71 | /** Perform actions after removal of item from the hash table. */
|
---|
[1be7bee] | 72 | static void task_remove(ht_link_t *item)
|
---|
| 73 | {
|
---|
[035d7d8] | 74 | free(hash_table_get_inst(item, task_t, link));
|
---|
[1be7bee] | 75 | }
|
---|
| 76 |
|
---|
[c675ab1] | 77 | /** Operations for task hash table. */
|
---|
[1be7bee] | 78 | static hash_table_ops_t task_hash_table_ops = {
|
---|
| 79 | .hash = task_hash,
|
---|
| 80 | .key_hash = task_key_hash,
|
---|
| 81 | .key_equal = task_key_equal,
|
---|
| 82 | .equal = NULL,
|
---|
| 83 | .remove_callback = task_remove
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | /** Task hash table structure. */
|
---|
[035d7d8] | 87 | hash_table_t task_hash_table;
|
---|
| 88 | fibril_rwlock_t task_hash_table_lock;
|
---|
[1be7bee] | 89 |
|
---|
| 90 | int task_init(void)
|
---|
| 91 | {
|
---|
| 92 | if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) {
|
---|
| 93 | printf(NAME ": No memory available for tasks\n");
|
---|
| 94 | return ENOMEM;
|
---|
| 95 | }
|
---|
[035d7d8] | 96 |
|
---|
| 97 | fibril_rwlock_initialize(&task_hash_table_lock);
|
---|
[1be7bee] | 98 |
|
---|
| 99 | return EOK;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[035d7d8] | 102 | /** Find task by its ID
|
---|
| 103 | *
|
---|
| 104 | * Assumes held lock of task_hash_table.
|
---|
[2f44fafd] | 105 | *
|
---|
[035d7d8] | 106 | * @param[in] id
|
---|
| 107 | * @return task structure
|
---|
| 108 | * @return NULL when no task with given ID exists
|
---|
[2f44fafd] | 109 | */
|
---|
[035d7d8] | 110 | task_t *task_get_by_id(task_id_t id)
|
---|
[1be7bee] | 111 | {
|
---|
| 112 | ht_link_t *link = hash_table_find(&task_hash_table, &id);
|
---|
[035d7d8] | 113 | if (!link) {
|
---|
| 114 | return NULL;
|
---|
[1be7bee] | 115 | }
|
---|
| 116 |
|
---|
[035d7d8] | 117 | task_t *t = hash_table_get_inst(link, task_t, link);
|
---|
| 118 | return t;
|
---|
[1be7bee] | 119 | }
|
---|
| 120 |
|
---|
[2f44fafd] | 121 | int task_intro(ipc_call_t *call, bool check_unique)
|
---|
[1be7bee] | 122 | {
|
---|
[2f44fafd] | 123 | int rc = EOK;
|
---|
| 124 |
|
---|
| 125 | fibril_rwlock_write_lock(&task_hash_table_lock);
|
---|
| 126 |
|
---|
[035d7d8] | 127 | task_t *t = task_get_by_id(call->in_task_id);
|
---|
| 128 | if (t != NULL) {
|
---|
[2f44fafd] | 129 | rc = EEXISTS;
|
---|
| 130 | goto finish;
|
---|
| 131 | }
|
---|
[1be7bee] | 132 |
|
---|
[035d7d8] | 133 | t = malloc(sizeof(task_t));
|
---|
| 134 | if (t == NULL) {
|
---|
[2f44fafd] | 135 | rc = ENOMEM;
|
---|
| 136 | goto finish;
|
---|
| 137 | }
|
---|
[62273d1] | 138 |
|
---|
[1be7bee] | 139 | /*
|
---|
| 140 | * Insert into the main table.
|
---|
| 141 | */
|
---|
[035d7d8] | 142 | t->id = call->in_task_id;
|
---|
| 143 | t->exit = TASK_EXIT_RUNNING;
|
---|
| 144 | t->failed = false;
|
---|
| 145 | t->retval_type = RVAL_UNSET;
|
---|
| 146 | t->retval = -1;
|
---|
| 147 | link_initialize(&t->listeners);
|
---|
| 148 | t->sess = NULL;
|
---|
| 149 |
|
---|
| 150 | hash_table_insert(&task_hash_table, &t->link);
|
---|
| 151 | printf("%s: %llu\n", __func__, t->id);
|
---|
[1be7bee] | 152 |
|
---|
[2f44fafd] | 153 | finish:
|
---|
| 154 | fibril_rwlock_write_unlock(&task_hash_table_lock);
|
---|
| 155 | return rc;
|
---|
[1be7bee] | 156 | }
|
---|
| 157 |
|
---|
[5cd2290] | 158 |
|
---|
[1be7bee] | 159 | /**
|
---|
| 160 | * @}
|
---|
| 161 | */
|
---|