1 | /*
|
---|
2 | * Copyright (c) 2009 Martin Decky
|
---|
3 | * Copyright (c) 2009 Jiri Svoboda
|
---|
4 | * Copyright (c) 2015 Michal Koutny
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
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.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include <assert.h>
|
---|
32 | #include <async.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include <macros.h>
|
---|
35 | #include <malloc.h>
|
---|
36 | #include <stdbool.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <task.h>
|
---|
39 | #include <types/task.h>
|
---|
40 |
|
---|
41 | #include "task.h"
|
---|
42 | #include "taskman.h"
|
---|
43 |
|
---|
44 | static size_t task_key_hash(void *key)
|
---|
45 | {
|
---|
46 | return *(task_id_t*)key;
|
---|
47 | }
|
---|
48 |
|
---|
49 | static size_t task_hash(const ht_link_t *item)
|
---|
50 | {
|
---|
51 | task_t *ht = hash_table_get_inst(item, task_t, link);
|
---|
52 | return ht->id;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static bool task_key_equal(void *key, const ht_link_t *item)
|
---|
56 | {
|
---|
57 | task_t *ht = hash_table_get_inst(item, task_t, link);
|
---|
58 | return ht->id == *(task_id_t*)key;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** Perform actions after removal of item from the hash table. */
|
---|
62 | static void task_remove(ht_link_t *item)
|
---|
63 | {
|
---|
64 | free(hash_table_get_inst(item, task_t, link));
|
---|
65 | }
|
---|
66 |
|
---|
67 | /** Operations for task hash table. */
|
---|
68 | static hash_table_ops_t task_hash_table_ops = {
|
---|
69 | .hash = task_hash,
|
---|
70 | .key_hash = task_key_hash,
|
---|
71 | .key_equal = task_key_equal,
|
---|
72 | .equal = NULL,
|
---|
73 | .remove_callback = task_remove
|
---|
74 | };
|
---|
75 |
|
---|
76 | /** Task hash table structure. */
|
---|
77 | hash_table_t task_hash_table;
|
---|
78 | fibril_rwlock_t task_hash_table_lock;
|
---|
79 |
|
---|
80 | int task_init(void)
|
---|
81 | {
|
---|
82 | if (!hash_table_create(&task_hash_table, 0, 0, &task_hash_table_ops)) {
|
---|
83 | printf(NAME ": No memory available for tasks\n");
|
---|
84 | return ENOMEM;
|
---|
85 | }
|
---|
86 |
|
---|
87 | fibril_rwlock_initialize(&task_hash_table_lock);
|
---|
88 |
|
---|
89 | return EOK;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /** Find task by its ID
|
---|
93 | *
|
---|
94 | * Assumes held lock of task_hash_table.
|
---|
95 | *
|
---|
96 | * @param[in] id
|
---|
97 | * @return task structure
|
---|
98 | * @return NULL when no task with given ID exists
|
---|
99 | */
|
---|
100 | task_t *task_get_by_id(task_id_t id)
|
---|
101 | {
|
---|
102 | ht_link_t *link = hash_table_find(&task_hash_table, &id);
|
---|
103 | if (!link) {
|
---|
104 | return NULL;
|
---|
105 | }
|
---|
106 |
|
---|
107 | task_t *t = hash_table_get_inst(link, task_t, link);
|
---|
108 | return t;
|
---|
109 | }
|
---|
110 |
|
---|
111 | int task_intro(task_id_t id)
|
---|
112 | {
|
---|
113 | int rc = EOK;
|
---|
114 |
|
---|
115 | fibril_rwlock_write_lock(&task_hash_table_lock);
|
---|
116 |
|
---|
117 | task_t *t = task_get_by_id(id);
|
---|
118 | if (t != NULL) {
|
---|
119 | rc = EEXISTS;
|
---|
120 | goto finish;
|
---|
121 | }
|
---|
122 |
|
---|
123 | t = malloc(sizeof(task_t));
|
---|
124 | if (t == NULL) {
|
---|
125 | rc = ENOMEM;
|
---|
126 | goto finish;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Insert into the main table.
|
---|
131 | */
|
---|
132 | t->id = id;
|
---|
133 | t->exit = TASK_EXIT_RUNNING;
|
---|
134 | t->failed = false;
|
---|
135 | t->retval_type = RVAL_UNSET;
|
---|
136 | t->retval = -1;
|
---|
137 | link_initialize(&t->listeners);
|
---|
138 | t->sess = NULL;
|
---|
139 |
|
---|
140 | hash_table_insert(&task_hash_table, &t->link);
|
---|
141 | printf("%s: %llu\n", __func__, t->id);
|
---|
142 |
|
---|
143 | finish:
|
---|
144 | fibril_rwlock_write_unlock(&task_hash_table_lock);
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * @}
|
---|
151 | */
|
---|