Rev 2040 | Rev 2042 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | jermar | 1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
||
3 | * All rights reserved. |
||
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
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 |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
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 |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
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 |
||
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 |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1757 | jermar | 29 | /** @addtogroup genericproc |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | |||
1248 | jermar | 33 | /** |
1702 | cejka | 34 | * @file |
1248 | jermar | 35 | * @brief Task management. |
36 | */ |
||
37 | |||
973 | palkovsky | 38 | #include <main/uinit.h> |
1 | jermar | 39 | #include <proc/thread.h> |
40 | #include <proc/task.h> |
||
1078 | jermar | 41 | #include <proc/uarg.h> |
703 | jermar | 42 | #include <mm/as.h> |
814 | palkovsky | 43 | #include <mm/slab.h> |
1 | jermar | 44 | #include <synch/spinlock.h> |
45 | #include <arch.h> |
||
46 | #include <panic.h> |
||
1159 | jermar | 47 | #include <adt/btree.h> |
788 | jermar | 48 | #include <adt/list.h> |
955 | palkovsky | 49 | #include <ipc/ipc.h> |
1174 | jermar | 50 | #include <security/cap.h> |
955 | palkovsky | 51 | #include <memstr.h> |
1060 | palkovsky | 52 | #include <print.h> |
2000 | decky | 53 | #include <lib/elf.h> |
1579 | jermar | 54 | #include <errno.h> |
1288 | jermar | 55 | #include <syscall/copy.h> |
1597 | palkovsky | 56 | #include <console/klog.h> |
973 | palkovsky | 57 | |
1170 | vana | 58 | #ifndef LOADED_PROG_STACK_PAGES_NO |
59 | #define LOADED_PROG_STACK_PAGES_NO 1 |
||
60 | #endif |
||
1168 | vana | 61 | |
1636 | jermar | 62 | /** Spinlock protecting the tasks_btree B+tree. */ |
623 | jermar | 63 | SPINLOCK_INITIALIZE(tasks_lock); |
1636 | jermar | 64 | |
65 | /** B+tree of active tasks. |
||
66 | * |
||
67 | * The task is guaranteed to exist after it was found in the tasks_btree as long as: |
||
68 | * @li the tasks_lock is held, |
||
69 | * @li the task's lock is held when task's lock is acquired before releasing tasks_lock or |
||
1880 | jermar | 70 | * @li the task's refcount is greater than 0 |
1636 | jermar | 71 | * |
72 | */ |
||
1159 | jermar | 73 | btree_t tasks_btree; |
1636 | jermar | 74 | |
1005 | palkovsky | 75 | static task_id_t task_counter = 0; |
1 | jermar | 76 | |
1585 | jermar | 77 | static void ktaskclnp(void *arg); |
1661 | jermar | 78 | static void ktaskgc(void *arg); |
1579 | jermar | 79 | |
107 | decky | 80 | /** Initialize tasks |
81 | * |
||
82 | * Initialize kernel tasks support. |
||
83 | * |
||
84 | */ |
||
1 | jermar | 85 | void task_init(void) |
86 | { |
||
15 | jermar | 87 | TASK = NULL; |
1159 | jermar | 88 | btree_create(&tasks_btree); |
1 | jermar | 89 | } |
90 | |||
107 | decky | 91 | |
92 | /** Create new task |
||
93 | * |
||
94 | * Create new task with no threads. |
||
95 | * |
||
703 | jermar | 96 | * @param as Task's address space. |
1062 | jermar | 97 | * @param name Symbolic name. |
107 | decky | 98 | * |
973 | palkovsky | 99 | * @return New task's structure |
107 | decky | 100 | * |
101 | */ |
||
1062 | jermar | 102 | task_t *task_create(as_t *as, char *name) |
1 | jermar | 103 | { |
413 | jermar | 104 | ipl_t ipl; |
1 | jermar | 105 | task_t *ta; |
1040 | palkovsky | 106 | int i; |
1 | jermar | 107 | |
822 | palkovsky | 108 | ta = (task_t *) malloc(sizeof(task_t), 0); |
109 | |||
1185 | jermar | 110 | task_create_arch(ta); |
111 | |||
822 | palkovsky | 112 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
113 | list_initialize(&ta->th_head); |
||
114 | ta->as = as; |
||
1062 | jermar | 115 | ta->name = name; |
1585 | jermar | 116 | ta->main_thread = NULL; |
1579 | jermar | 117 | ta->refcount = 0; |
1839 | decky | 118 | ta->context = CONTEXT; |
1579 | jermar | 119 | |
1174 | jermar | 120 | ta->capabilities = 0; |
1579 | jermar | 121 | ta->accept_new_threads = true; |
2039 | decky | 122 | ta->cycles = 0; |
1040 | palkovsky | 123 | |
955 | palkovsky | 124 | ipc_answerbox_init(&ta->answerbox); |
1839 | decky | 125 | for (i = 0; i < IPC_MAX_PHONES; i++) |
1040 | palkovsky | 126 | ipc_phone_init(&ta->phones[i]); |
1839 | decky | 127 | if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context, ta->context))) |
1040 | palkovsky | 128 | ipc_phone_connect(&ta->phones[0], ipc_phone_0); |
998 | palkovsky | 129 | atomic_set(&ta->active_calls, 0); |
1460 | jermar | 130 | |
131 | mutex_initialize(&ta->futexes_lock); |
||
132 | btree_create(&ta->futexes); |
||
822 | palkovsky | 133 | |
134 | ipl = interrupts_disable(); |
||
1468 | jermar | 135 | |
136 | /* |
||
137 | * Increment address space reference count. |
||
138 | * TODO: Reconsider the locking scheme. |
||
139 | */ |
||
140 | mutex_lock(&as->lock); |
||
141 | as->refcount++; |
||
142 | mutex_unlock(&as->lock); |
||
143 | |||
822 | palkovsky | 144 | spinlock_lock(&tasks_lock); |
1005 | palkovsky | 145 | |
146 | ta->taskid = ++task_counter; |
||
1177 | jermar | 147 | btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL); |
1005 | palkovsky | 148 | |
822 | palkovsky | 149 | spinlock_unlock(&tasks_lock); |
150 | interrupts_restore(ipl); |
||
151 | |||
1 | jermar | 152 | return ta; |
153 | } |
||
154 | |||
1579 | jermar | 155 | /** Destroy task. |
156 | * |
||
157 | * @param t Task to be destroyed. |
||
158 | */ |
||
159 | void task_destroy(task_t *t) |
||
160 | { |
||
1587 | jermar | 161 | task_destroy_arch(t); |
162 | btree_destroy(&t->futexes); |
||
163 | |||
164 | mutex_lock_active(&t->as->lock); |
||
165 | if (--t->as->refcount == 0) { |
||
166 | mutex_unlock(&t->as->lock); |
||
167 | as_destroy(t->as); |
||
168 | /* |
||
169 | * t->as is destroyed. |
||
170 | */ |
||
2040 | decky | 171 | } else |
1587 | jermar | 172 | mutex_unlock(&t->as->lock); |
173 | |||
174 | free(t); |
||
175 | TASK = NULL; |
||
1579 | jermar | 176 | } |
177 | |||
973 | palkovsky | 178 | /** Create new task with 1 thread and run it |
179 | * |
||
1248 | jermar | 180 | * @param program_addr Address of program executable image. |
1062 | jermar | 181 | * @param name Program name. |
182 | * |
||
1229 | jermar | 183 | * @return Task of the running program or NULL on error. |
973 | palkovsky | 184 | */ |
1062 | jermar | 185 | task_t * task_run_program(void *program_addr, char *name) |
973 | palkovsky | 186 | { |
187 | as_t *as; |
||
188 | as_area_t *a; |
||
189 | int rc; |
||
1585 | jermar | 190 | thread_t *t1, *t2; |
973 | palkovsky | 191 | task_t *task; |
1078 | jermar | 192 | uspace_arg_t *kernel_uarg; |
973 | palkovsky | 193 | |
194 | as = as_create(0); |
||
1115 | jermar | 195 | ASSERT(as); |
973 | palkovsky | 196 | |
1025 | palkovsky | 197 | rc = elf_load((elf_header_t *) program_addr, as); |
973 | palkovsky | 198 | if (rc != EE_OK) { |
1468 | jermar | 199 | as_destroy(as); |
973 | palkovsky | 200 | return NULL; |
201 | } |
||
202 | |||
1078 | jermar | 203 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
204 | kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry; |
||
205 | kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS; |
||
206 | kernel_uarg->uspace_thread_function = NULL; |
||
207 | kernel_uarg->uspace_thread_arg = NULL; |
||
208 | kernel_uarg->uspace_uarg = NULL; |
||
1066 | jermar | 209 | |
1062 | jermar | 210 | task = task_create(as, name); |
1115 | jermar | 211 | ASSERT(task); |
212 | |||
973 | palkovsky | 213 | /* |
214 | * Create the data as_area. |
||
215 | */ |
||
1424 | jermar | 216 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
217 | LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, |
||
1409 | jermar | 218 | USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL); |
1115 | jermar | 219 | |
1585 | jermar | 220 | /* |
221 | * Create the main thread. |
||
222 | */ |
||
1864 | jermar | 223 | t1 = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE, "uinit"); |
1585 | jermar | 224 | ASSERT(t1); |
973 | palkovsky | 225 | |
1585 | jermar | 226 | /* |
227 | * Create killer thread for the new task. |
||
228 | */ |
||
1661 | jermar | 229 | t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc"); |
1585 | jermar | 230 | ASSERT(t2); |
231 | thread_ready(t2); |
||
232 | |||
233 | thread_ready(t1); |
||
234 | |||
973 | palkovsky | 235 | return task; |
236 | } |
||
1060 | palkovsky | 237 | |
1176 | jermar | 238 | /** Syscall for reading task ID from userspace. |
239 | * |
||
1248 | jermar | 240 | * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID. |
1176 | jermar | 241 | * |
1288 | jermar | 242 | * @return 0 on success or an error code from @ref errno.h. |
1176 | jermar | 243 | */ |
1780 | jermar | 244 | unative_t sys_task_get_id(task_id_t *uspace_task_id) |
1176 | jermar | 245 | { |
246 | /* |
||
247 | * No need to acquire lock on TASK because taskid |
||
248 | * remains constant for the lifespan of the task. |
||
249 | */ |
||
1780 | jermar | 250 | return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid)); |
1176 | jermar | 251 | } |
252 | |||
1178 | jermar | 253 | /** Find task structure corresponding to task ID. |
254 | * |
||
255 | * The tasks_lock must be already held by the caller of this function |
||
256 | * and interrupts must be disabled. |
||
257 | * |
||
258 | * @param id Task ID. |
||
259 | * |
||
260 | * @return Task structure address or NULL if there is no such task ID. |
||
261 | */ |
||
262 | task_t *task_find_by_id(task_id_t id) |
||
263 | { |
||
264 | btree_node_t *leaf; |
||
265 | |||
266 | return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf); |
||
267 | } |
||
268 | |||
2039 | decky | 269 | /** Get accounting data of given task. |
270 | * |
||
271 | * Note that task_lock on @t must be already held and |
||
272 | * interrupts must be already disabled. |
||
273 | * |
||
274 | * @param t Pointer to thread. |
||
275 | * |
||
276 | */ |
||
277 | uint64_t task_get_accounting(task_t *t) |
||
278 | { |
||
279 | /* Accumulated value of task */ |
||
280 | uint64_t ret = t->cycles; |
||
281 | |||
282 | /* Current values of threads */ |
||
283 | link_t *cur; |
||
284 | for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) { |
||
285 | thread_t *thr = list_get_instance(cur, thread_t, th_link); |
||
286 | |||
287 | spinlock_lock(&thr->lock); |
||
288 | |||
289 | if (thr == THREAD) /* Update accounting of current thread */ |
||
290 | thread_update_accounting(); |
||
291 | ret += thr->cycles; |
||
292 | |||
293 | spinlock_unlock(&thr->lock); |
||
294 | } |
||
295 | |||
296 | return ret; |
||
297 | } |
||
298 | |||
1579 | jermar | 299 | /** Kill task. |
300 | * |
||
301 | * @param id ID of the task to be killed. |
||
302 | * |
||
303 | * @return 0 on success or an error code from errno.h |
||
304 | */ |
||
305 | int task_kill(task_id_t id) |
||
306 | { |
||
307 | ipl_t ipl; |
||
308 | task_t *ta; |
||
309 | thread_t *t; |
||
310 | link_t *cur; |
||
1600 | jermar | 311 | |
312 | if (id == 1) |
||
313 | return EPERM; |
||
1579 | jermar | 314 | |
315 | ipl = interrupts_disable(); |
||
316 | spinlock_lock(&tasks_lock); |
||
317 | |||
318 | if (!(ta = task_find_by_id(id))) { |
||
319 | spinlock_unlock(&tasks_lock); |
||
320 | interrupts_restore(ipl); |
||
321 | return ENOENT; |
||
322 | } |
||
1588 | jermar | 323 | |
1579 | jermar | 324 | spinlock_lock(&ta->lock); |
325 | ta->refcount++; |
||
326 | spinlock_unlock(&ta->lock); |
||
1587 | jermar | 327 | |
1588 | jermar | 328 | btree_remove(&tasks_btree, ta->taskid, NULL); |
1587 | jermar | 329 | spinlock_unlock(&tasks_lock); |
1579 | jermar | 330 | |
1580 | jermar | 331 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp"); |
1579 | jermar | 332 | |
333 | spinlock_lock(&ta->lock); |
||
1580 | jermar | 334 | ta->accept_new_threads = false; |
1579 | jermar | 335 | ta->refcount--; |
1585 | jermar | 336 | |
337 | /* |
||
1687 | jermar | 338 | * Interrupt all threads except ktaskclnp. |
1585 | jermar | 339 | */ |
1579 | jermar | 340 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
341 | thread_t *thr; |
||
342 | bool sleeping = false; |
||
343 | |||
344 | thr = list_get_instance(cur, thread_t, th_link); |
||
345 | if (thr == t) |
||
346 | continue; |
||
347 | |||
348 | spinlock_lock(&thr->lock); |
||
349 | thr->interrupted = true; |
||
350 | if (thr->state == Sleeping) |
||
351 | sleeping = true; |
||
352 | spinlock_unlock(&thr->lock); |
||
353 | |||
354 | if (sleeping) |
||
355 | waitq_interrupt_sleep(thr); |
||
356 | } |
||
357 | |||
1580 | jermar | 358 | spinlock_unlock(&ta->lock); |
359 | interrupts_restore(ipl); |
||
1579 | jermar | 360 | |
1580 | jermar | 361 | if (t) |
362 | thread_ready(t); |
||
363 | |||
1579 | jermar | 364 | return 0; |
365 | } |
||
366 | |||
1060 | palkovsky | 367 | /** Print task list */ |
368 | void task_print_list(void) |
||
369 | { |
||
370 | link_t *cur; |
||
371 | ipl_t ipl; |
||
372 | |||
373 | /* Messing with thread structures, avoid deadlock */ |
||
374 | ipl = interrupts_disable(); |
||
375 | spinlock_lock(&tasks_lock); |
||
2035 | decky | 376 | |
2039 | decky | 377 | printf("taskid name ctx address as cycles threads calls callee\n"); |
378 | printf("------ ---------- --- ---------- ---------- ---------- ------- ------ ------>\n"); |
||
1060 | palkovsky | 379 | |
1159 | jermar | 380 | for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) { |
381 | btree_node_t *node; |
||
382 | int i; |
||
383 | |||
384 | node = list_get_instance(cur, btree_node_t, leaf_link); |
||
385 | for (i = 0; i < node->keys; i++) { |
||
386 | task_t *t; |
||
387 | int j; |
||
388 | |||
389 | t = (task_t *) node->value[i]; |
||
390 | |||
391 | spinlock_lock(&t->lock); |
||
2039 | decky | 392 | |
393 | uint64_t cycles = task_get_accounting(t); |
||
394 | char suffix; |
||
395 | |||
396 | if (cycles > 1000000000000000000LL) { |
||
397 | cycles = cycles / 1000000000000000000LL; |
||
398 | suffix = 'E'; |
||
399 | } else if (cycles > 1000000000000LL) { |
||
400 | cycles = cycles / 1000000000000LL; |
||
401 | suffix = 'T'; |
||
402 | } else if (cycles > 1000000LL) { |
||
403 | cycles = cycles / 1000000LL; |
||
404 | suffix = 'M'; |
||
405 | } else |
||
406 | suffix = ' '; |
||
407 | |||
408 | printf("%-6lld %-10s %-3ld %#10zx %#10zx %9llu%c %7zd %6zd", t->taskid, t->name, t->context, t, t->as, cycles, suffix, t->refcount, atomic_get(&t->active_calls)); |
||
2035 | decky | 409 | for (j = 0; j < IPC_MAX_PHONES; j++) { |
1159 | jermar | 410 | if (t->phones[j].callee) |
2035 | decky | 411 | printf(" %zd:%#zx", j, t->phones[j].callee); |
1159 | jermar | 412 | } |
413 | printf("\n"); |
||
2039 | decky | 414 | |
1159 | jermar | 415 | spinlock_unlock(&t->lock); |
1060 | palkovsky | 416 | } |
417 | } |
||
418 | |||
419 | spinlock_unlock(&tasks_lock); |
||
420 | interrupts_restore(ipl); |
||
421 | } |
||
1579 | jermar | 422 | |
1585 | jermar | 423 | /** Kernel thread used to cleanup the task after it is killed. */ |
1580 | jermar | 424 | void ktaskclnp(void *arg) |
1579 | jermar | 425 | { |
1580 | jermar | 426 | ipl_t ipl; |
1585 | jermar | 427 | thread_t *t = NULL, *main_thread; |
1580 | jermar | 428 | link_t *cur; |
1661 | jermar | 429 | bool again; |
1580 | jermar | 430 | |
431 | thread_detach(THREAD); |
||
432 | |||
433 | loop: |
||
434 | ipl = interrupts_disable(); |
||
435 | spinlock_lock(&TASK->lock); |
||
436 | |||
1585 | jermar | 437 | main_thread = TASK->main_thread; |
438 | |||
1579 | jermar | 439 | /* |
1580 | jermar | 440 | * Find a thread to join. |
441 | */ |
||
1661 | jermar | 442 | again = false; |
1580 | jermar | 443 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
444 | t = list_get_instance(cur, thread_t, th_link); |
||
1661 | jermar | 445 | |
446 | spinlock_lock(&t->lock); |
||
447 | if (t == THREAD) { |
||
448 | spinlock_unlock(&t->lock); |
||
1580 | jermar | 449 | continue; |
1661 | jermar | 450 | } else if (t == main_thread) { |
451 | spinlock_unlock(&t->lock); |
||
1585 | jermar | 452 | continue; |
1661 | jermar | 453 | } else if (t->join_type != None) { |
454 | spinlock_unlock(&t->lock); |
||
455 | again = true; |
||
456 | continue; |
||
457 | } else { |
||
458 | t->join_type = TaskClnp; |
||
459 | spinlock_unlock(&t->lock); |
||
460 | again = false; |
||
1580 | jermar | 461 | break; |
1661 | jermar | 462 | } |
1580 | jermar | 463 | } |
464 | |||
465 | spinlock_unlock(&TASK->lock); |
||
466 | interrupts_restore(ipl); |
||
467 | |||
1661 | jermar | 468 | if (again) { |
469 | /* |
||
470 | * Other cleanup (e.g. ktaskgc) is in progress. |
||
471 | */ |
||
472 | scheduler(); |
||
473 | goto loop; |
||
474 | } |
||
475 | |||
1580 | jermar | 476 | if (t != THREAD) { |
1661 | jermar | 477 | ASSERT(t != main_thread); /* uninit is joined and detached in ktaskgc */ |
1580 | jermar | 478 | thread_join(t); |
479 | thread_detach(t); |
||
1585 | jermar | 480 | goto loop; /* go for another thread */ |
1580 | jermar | 481 | } |
482 | |||
483 | /* |
||
484 | * Now there are no other threads in this task |
||
485 | * and no new threads can be created. |
||
486 | */ |
||
1880 | jermar | 487 | |
1583 | jermar | 488 | ipc_cleanup(); |
489 | futex_cleanup(); |
||
1597 | palkovsky | 490 | klog_printf("Cleanup of task %lld completed.", TASK->taskid); |
1579 | jermar | 491 | } |
1585 | jermar | 492 | |
1684 | jermar | 493 | /** Kernel thread used to kill the userspace task when its main thread exits. |
1585 | jermar | 494 | * |
495 | * This thread waits until the main userspace thread (i.e. uninit) exits. |
||
1661 | jermar | 496 | * When this happens, the task is killed. In the meantime, exited threads |
497 | * are garbage collected. |
||
1585 | jermar | 498 | * |
499 | * @param arg Pointer to the thread structure of the task's main thread. |
||
500 | */ |
||
1661 | jermar | 501 | void ktaskgc(void *arg) |
1585 | jermar | 502 | { |
503 | thread_t *t = (thread_t *) arg; |
||
1661 | jermar | 504 | loop: |
1585 | jermar | 505 | /* |
506 | * Userspace threads cannot detach themselves, |
||
507 | * therefore the thread pointer is guaranteed to be valid. |
||
508 | */ |
||
1661 | jermar | 509 | if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) { /* sleep uninterruptibly here! */ |
510 | ipl_t ipl; |
||
511 | link_t *cur; |
||
512 | thread_t *thr = NULL; |
||
513 | |||
514 | /* |
||
515 | * The join timed out. Try to do some garbage collection of Undead threads. |
||
516 | */ |
||
517 | more_gc: |
||
518 | ipl = interrupts_disable(); |
||
519 | spinlock_lock(&TASK->lock); |
||
520 | |||
521 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
||
522 | thr = list_get_instance(cur, thread_t, th_link); |
||
523 | spinlock_lock(&thr->lock); |
||
1687 | jermar | 524 | if (thr != t && thr->state == Undead && thr->join_type == None) { |
1661 | jermar | 525 | thr->join_type = TaskGC; |
526 | spinlock_unlock(&thr->lock); |
||
527 | break; |
||
528 | } |
||
529 | spinlock_unlock(&thr->lock); |
||
530 | thr = NULL; |
||
531 | } |
||
532 | spinlock_unlock(&TASK->lock); |
||
1676 | jermar | 533 | interrupts_restore(ipl); |
1661 | jermar | 534 | |
535 | if (thr) { |
||
536 | thread_join(thr); |
||
537 | thread_detach(thr); |
||
538 | scheduler(); |
||
539 | goto more_gc; |
||
540 | } |
||
541 | |||
542 | goto loop; |
||
543 | } |
||
1585 | jermar | 544 | thread_detach(t); |
545 | task_kill(TASK->taskid); |
||
546 | } |
||
1702 | cejka | 547 | |
1757 | jermar | 548 | /** @} |
1702 | cejka | 549 | */ |