Rev 4509 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4509 | Rev 4581 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Martin Decky |
2 | * Copyright (c) 2009 Martin Decky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup ns |
29 | /** @addtogroup ns |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | #include <ipc/ipc.h> |
33 | #include <ipc/ipc.h> |
34 | #include <adt/hash_table.h> |
34 | #include <adt/hash_table.h> |
35 | #include <bool.h> |
35 | #include <bool.h> |
36 | #include <errno.h> |
36 | #include <errno.h> |
37 | #include <assert.h> |
37 | #include <assert.h> |
38 | #include <stdio.h> |
38 | #include <stdio.h> |
39 | #include <macros.h> |
39 | #include <macros.h> |
40 | #include "task.h" |
40 | #include "task.h" |
41 | #include "ns.h" |
41 | #include "ns.h" |
42 | 42 | ||
43 | #define TASK_HASH_TABLE_CHAINS 256 |
43 | #define TASK_HASH_TABLE_CHAINS 256 |
44 | 44 | ||
45 | /* TODO: |
45 | /* TODO: |
46 | * |
46 | * |
47 | * The current implementation of waiting on a task is not perfect. If somebody |
47 | * The current implementation of waiting on a task is not perfect. If somebody |
48 | * wants to wait on a task which has already finished before the NS asked |
48 | * wants to wait on a task which has already finished before the NS asked |
49 | * the kernel to receive notifications, it would block indefinitively. |
49 | * the kernel to receive notifications, it would block indefinitively. |
50 | * |
50 | * |
51 | * A solution to this is to fail immediately on a task for which no creation |
51 | * A solution to this is to fail immediately on a task for which no creation |
52 | * notification was received yet. However, there is a danger of a race condition |
52 | * notification was received yet. However, there is a danger of a race condition |
53 | * in this solution -- the caller has to make sure that it is not trying to wait |
53 | * in this solution -- the caller has to make sure that it is not trying to wait |
54 | * before the NS has a change to receive the task creation notification. This |
54 | * before the NS has a change to receive the task creation notification. This |
55 | * can be assured by waiting for this event in task_spawn(). |
55 | * can be assured by waiting for this event in task_spawn(). |
56 | * |
56 | * |
57 | * Finally, as there is currently no convention that each task has to be waited |
57 | * Finally, as there is currently no convention that each task has to be waited |
58 | * for, the NS can leak memory because of the zombie tasks. |
58 | * for, the NS can leak memory because of the zombie tasks. |
59 | * |
59 | * |
60 | */ |
60 | */ |
61 | 61 | ||
62 | /** Task hash table item. */ |
62 | /** Task hash table item. */ |
63 | typedef struct { |
63 | typedef struct { |
64 | link_t link; |
64 | link_t link; |
65 | task_id_t id; /**< Task ID. */ |
65 | task_id_t id; /**< Task ID. */ |
66 | bool destroyed; |
66 | bool destroyed; |
67 | } hashed_task_t; |
67 | } hashed_task_t; |
68 | 68 | ||
69 | /** Compute hash index into task hash table. |
69 | /** Compute hash index into task hash table. |
70 | * |
70 | * |
71 | * @param key Pointer keys. However, only the first key (i.e. truncated task |
71 | * @param key Pointer keys. However, only the first key (i.e. truncated task |
72 | * number) is used to compute the hash index. |
72 | * number) is used to compute the hash index. |
73 | * |
73 | * |
74 | * @return Hash index corresponding to key[0]. |
74 | * @return Hash index corresponding to key[0]. |
75 | * |
75 | * |
76 | */ |
76 | */ |
77 | static hash_index_t task_hash(unsigned long *key) |
77 | static hash_index_t task_hash(unsigned long *key) |
78 | { |
78 | { |
79 | assert(key); |
79 | assert(key); |
80 | return (LOWER32(*key) % TASK_HASH_TABLE_CHAINS); |
80 | return (LOWER32(*key) % TASK_HASH_TABLE_CHAINS); |
81 | } |
81 | } |
82 | 82 | ||
83 | /** Compare a key with hashed item. |
83 | /** Compare a key with hashed item. |
84 | * |
84 | * |
85 | * @param key Array of keys. |
85 | * @param key Array of keys. |
86 | * @param keys Must be lesser or equal to 2. |
86 | * @param keys Must be lesser or equal to 2. |
87 | * @param item Pointer to a hash table item. |
87 | * @param item Pointer to a hash table item. |
88 | * |
88 | * |
89 | * @return Non-zero if the key matches the item, zero otherwise. |
89 | * @return Non-zero if the key matches the item, zero otherwise. |
90 | * |
90 | * |
91 | */ |
91 | */ |
92 | static int task_compare(unsigned long key[], hash_count_t keys, link_t *item) |
92 | static int task_compare(unsigned long key[], hash_count_t keys, link_t *item) |
93 | { |
93 | { |
94 | assert(key); |
94 | assert(key); |
95 | assert(keys <= 2); |
95 | assert(keys <= 2); |
96 | assert(item); |
96 | assert(item); |
97 | 97 | ||
98 | hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link); |
98 | hashed_task_t *ht = hash_table_get_instance(item, hashed_task_t, link); |
99 | 99 | ||
100 | if (keys == 2) |
100 | if (keys == 2) |
101 | return ((LOWER32(key[1]) == UPPER32(ht->id)) |
101 | return ((LOWER32(key[1]) == UPPER32(ht->id)) |
102 | && (LOWER32(key[0]) == LOWER32(ht->id))); |
102 | && (LOWER32(key[0]) == LOWER32(ht->id))); |
103 | else |
103 | else |
104 | return (LOWER32(key[0]) == LOWER32(ht->id)); |
104 | return (LOWER32(key[0]) == LOWER32(ht->id)); |
105 | } |
105 | } |
106 | 106 | ||
107 | /** Perform actions after removal of item from the hash table. |
107 | /** Perform actions after removal of item from the hash table. |
108 | * |
108 | * |
109 | * @param item Item that was removed from the hash table. |
109 | * @param item Item that was removed from the hash table. |
110 | * |
110 | * |
111 | */ |
111 | */ |
112 | static void task_remove(link_t *item) |
112 | static void task_remove(link_t *item) |
113 | { |
113 | { |
114 | assert(item); |
114 | assert(item); |
115 | free(hash_table_get_instance(item, hashed_task_t, link)); |
115 | free(hash_table_get_instance(item, hashed_task_t, link)); |
116 | } |
116 | } |
117 | 117 | ||
118 | /** Operations for task hash table. */ |
118 | /** Operations for task hash table. */ |
119 | static hash_table_operations_t task_hash_table_ops = { |
119 | static hash_table_operations_t task_hash_table_ops = { |
120 | .hash = task_hash, |
120 | .hash = task_hash, |
121 | .compare = task_compare, |
121 | .compare = task_compare, |
122 | .remove_callback = task_remove |
122 | .remove_callback = task_remove |
123 | }; |
123 | }; |
124 | 124 | ||
125 | /** Task hash table structure. */ |
125 | /** Task hash table structure. */ |
126 | static hash_table_t task_hash_table; |
126 | static hash_table_t task_hash_table; |
127 | 127 | ||
128 | /** Pending task wait structure. */ |
128 | /** Pending task wait structure. */ |
129 | typedef struct { |
129 | typedef struct { |
130 | link_t link; |
130 | link_t link; |
131 | task_id_t id; /**< Task ID. */ |
131 | task_id_t id; /**< Task ID. */ |
132 | ipc_callid_t callid; /**< Call ID waiting for the connection */ |
132 | ipc_callid_t callid; /**< Call ID waiting for the connection */ |
133 | } pending_wait_t; |
133 | } pending_wait_t; |
134 | 134 | ||
135 | static link_t pending_wait; |
135 | static link_t pending_wait; |
136 | 136 | ||
137 | int task_init(void) |
137 | int task_init(void) |
138 | { |
138 | { |
139 | if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS, |
139 | if (!hash_table_create(&task_hash_table, TASK_HASH_TABLE_CHAINS, |
140 | 2, &task_hash_table_ops)) { |
140 | 2, &task_hash_table_ops)) { |
141 | printf(NAME ": No memory available for tasks\n"); |
141 | printf(NAME ": No memory available for tasks\n"); |
142 | return ENOMEM; |
142 | return ENOMEM; |
143 | } |
143 | } |
144 | 144 | ||
145 | if (event_subscribe(EVENT_WAIT, 0) != EOK) |
145 | if (event_subscribe(EVENT_WAIT, 0) != EOK) |
146 | printf(NAME ": Error registering wait notifications\n"); |
146 | printf(NAME ": Error registering wait notifications\n"); |
147 | 147 | ||
148 | list_initialize(&pending_wait); |
148 | list_initialize(&pending_wait); |
149 | 149 | ||
150 | return EOK; |
150 | return EOK; |
151 | } |
151 | } |
152 | 152 | ||
153 | /** Process pending wait requests */ |
153 | /** Process pending wait requests */ |
154 | void process_pending_wait(void) |
154 | void process_pending_wait(void) |
155 | { |
155 | { |
156 | link_t *cur; |
156 | link_t *cur; |
157 | 157 | ||
158 | loop: |
158 | loop: |
159 | for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) { |
159 | for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) { |
160 | pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); |
160 | pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); |
161 | 161 | ||
162 | unsigned long keys[2] = { |
162 | unsigned long keys[2] = { |
163 | LOWER32(pr->id), |
163 | LOWER32(pr->id), |
164 | UPPER32(pr->id) |
164 | UPPER32(pr->id) |
165 | }; |
165 | }; |
166 | 166 | ||
167 | link_t *link = hash_table_find(&task_hash_table, keys); |
167 | link_t *link = hash_table_find(&task_hash_table, keys); |
168 | if (!link) |
168 | if (!link) |
169 | continue; |
169 | continue; |
170 | 170 | ||
171 | hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link); |
171 | hashed_task_t *ht = hash_table_get_instance(link, hashed_task_t, link); |
172 | if (!ht->destroyed) |
172 | if (!ht->destroyed) |
173 | continue; |
173 | continue; |
174 | 174 | ||
175 | if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
175 | if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
176 | ipc_answer_0(pr->callid, EOK); |
176 | ipc_answer_0(pr->callid, EOK); |
177 | 177 | ||
178 | hash_table_remove(&task_hash_table, keys, 2); |
178 | hash_table_remove(&task_hash_table, keys, 2); |
179 | list_remove(cur); |
179 | list_remove(cur); |
180 | free(pr); |
180 | free(pr); |
181 | goto loop; |
181 | goto loop; |
182 | } |
182 | } |
183 | } |
183 | } |
184 | 184 | ||
185 | static void fail_pending_wait(task_id_t id, int rc) |
185 | static void fail_pending_wait(task_id_t id, int rc) |
186 | { |
186 | { |
187 | link_t *cur; |
187 | link_t *cur; |
188 | 188 | ||
189 | loop: |
189 | loop: |
190 | for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) { |
190 | for (cur = pending_wait.next; cur != &pending_wait; cur = cur->next) { |
191 | pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); |
191 | pending_wait_t *pr = list_get_instance(cur, pending_wait_t, link); |
192 | 192 | ||
193 | if (pr->id == id) { |
193 | if (pr->id == id) { |
194 | if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
194 | if (!(pr->callid & IPC_CALLID_NOTIFICATION)) |
195 | ipc_answer_0(pr->callid, rc); |
195 | ipc_answer_0(pr->callid, rc); |
196 | 196 | ||
197 | list_remove(cur); |
197 | list_remove(cur); |
198 | free(pr); |
198 | free(pr); |
199 | goto loop; |
199 | goto loop; |
200 | } |
200 | } |
201 | } |
201 | } |
202 | } |
202 | } |
203 | 203 | ||
204 | void wait_notification(wait_type_t et, task_id_t id) |
204 | void wait_notification(wait_type_t et, task_id_t id) |
205 | { |
205 | { |
206 | unsigned long keys[2] = { |
206 | unsigned long keys[2] = { |
207 | LOWER32(id), |
207 | LOWER32(id), |
208 | UPPER32(id) |
208 | UPPER32(id) |
209 | }; |
209 | }; |
210 | 210 | ||
211 | link_t *link = hash_table_find(&task_hash_table, keys); |
211 | link_t *link = hash_table_find(&task_hash_table, keys); |
212 | 212 | ||
213 | if (link == NULL) { |
213 | if (link == NULL) { |
214 | hashed_task_t *ht = |
214 | hashed_task_t *ht = |
215 | (hashed_task_t *) malloc(sizeof(hashed_task_t)); |
215 | (hashed_task_t *) malloc(sizeof(hashed_task_t)); |
216 | if (ht == NULL) { |
216 | if (ht == NULL) { |
217 | fail_pending_wait(id, ENOMEM); |
217 | fail_pending_wait(id, ENOMEM); |
218 | return; |
218 | return; |
219 | } |
219 | } |
220 | 220 | ||
221 | link_initialize(&ht->link); |
221 | link_initialize(&ht->link); |
222 | ht->id = id; |
222 | ht->id = id; |
223 | ht->destroyed = (et == TASK_CREATE) ? false : true; |
223 | ht->destroyed = (et == TASK_CREATE) ? false : true; |
224 | hash_table_insert(&task_hash_table, keys, &ht->link); |
224 | hash_table_insert(&task_hash_table, keys, &ht->link); |
225 | } else { |
225 | } else { |
226 | hashed_task_t *ht = |
226 | hashed_task_t *ht = |
227 | hash_table_get_instance(link, hashed_task_t, link); |
227 | hash_table_get_instance(link, hashed_task_t, link); |
228 | ht->destroyed = (et == TASK_CREATE) ? false : true; |
228 | ht->destroyed = (et == TASK_CREATE) ? false : true; |
229 | } |
229 | } |
230 | } |
230 | } |
231 | 231 | ||
232 | void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid) |
232 | void wait_for_task(task_id_t id, ipc_call_t *call, ipc_callid_t callid) |
233 | { |
233 | { |
234 | ipcarg_t retval; |
234 | ipcarg_t retval; |
235 | unsigned long keys[2] = { |
235 | unsigned long keys[2] = { |
236 | LOWER32(id), |
236 | LOWER32(id), |
237 | UPPER32(id) |
237 | UPPER32(id) |
238 | }; |
238 | }; |
239 | 239 | ||
240 | link_t *link = hash_table_find(&task_hash_table, keys); |
240 | link_t *link = hash_table_find(&task_hash_table, keys); |
241 | hashed_task_t *ht = (link != NULL) ? |
241 | hashed_task_t *ht = (link != NULL) ? |
242 | hash_table_get_instance(link, hashed_task_t, link) : NULL; |
242 | hash_table_get_instance(link, hashed_task_t, link) : NULL; |
243 | 243 | ||
244 | if ((ht == NULL) || (!ht->destroyed)) { |
244 | if ((ht == NULL) || (!ht->destroyed)) { |
245 | /* Add to pending list */ |
245 | /* Add to pending list */ |
246 | pending_wait_t *pr = |
246 | pending_wait_t *pr = |
247 | (pending_wait_t *) malloc(sizeof(pending_wait_t)); |
247 | (pending_wait_t *) malloc(sizeof(pending_wait_t)); |
248 | if (!pr) { |
248 | if (!pr) { |
249 | retval = ENOMEM; |
249 | retval = ENOMEM; |
250 | goto out; |
250 | goto out; |
251 | } |
251 | } |
252 | 252 | ||
253 | pr->id = id; |
253 | pr->id = id; |
254 | pr->callid = callid; |
254 | pr->callid = callid; |
255 | list_append(&pr->link, &pending_wait); |
255 | list_append(&pr->link, &pending_wait); |
256 | return; |
256 | return; |
257 | } |
257 | } |
258 | 258 | ||
259 | hash_table_remove(&task_hash_table, keys, 2); |
259 | hash_table_remove(&task_hash_table, keys, 2); |
260 | retval = EOK; |
260 | retval = EOK; |
261 | 261 | ||
262 | out: |
262 | out: |
263 | if (!(callid & IPC_CALLID_NOTIFICATION)) |
263 | if (!(callid & IPC_CALLID_NOTIFICATION)) |
264 | ipc_answer_0(callid, retval); |
264 | ipc_answer_0(callid, retval); |
265 | } |
265 | } |
266 | 266 | ||
267 | /** |
267 | /** |
268 | * @} |
268 | * @} |
269 | */ |
269 | */ |
270 | 270 |