Rev 1583 | Rev 1587 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 1583 | Rev 1585 | ||
|---|---|---|---|
| Line 56... | Line 56... | ||
| 56 | 56 | ||
| 57 | SPINLOCK_INITIALIZE(tasks_lock); |
57 | SPINLOCK_INITIALIZE(tasks_lock); |
| 58 | btree_t tasks_btree; |
58 | btree_t tasks_btree; |
| 59 | static task_id_t task_counter = 0; |
59 | static task_id_t task_counter = 0; |
| 60 | 60 | ||
| 61 | static void ktaskclnp(void *); |
61 | static void ktaskclnp(void *arg); |
| - | 62 | static void ktaskkill(void *arg); |
|
| 62 | 63 | ||
| 63 | /** Initialize tasks |
64 | /** Initialize tasks |
| 64 | * |
65 | * |
| 65 | * Initialize kernel tasks support. |
66 | * Initialize kernel tasks support. |
| 66 | * |
67 | * |
| Line 94... | Line 95... | ||
| 94 | 95 | ||
| 95 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
96 | spinlock_initialize(&ta->lock, "task_ta_lock"); |
| 96 | list_initialize(&ta->th_head); |
97 | list_initialize(&ta->th_head); |
| 97 | ta->as = as; |
98 | ta->as = as; |
| 98 | ta->name = name; |
99 | ta->name = name; |
| 99 | - | ||
| - | 100 | ta->main_thread = NULL; |
|
| 100 | ta->refcount = 0; |
101 | ta->refcount = 0; |
| 101 | 102 | ||
| 102 | ta->capabilities = 0; |
103 | ta->capabilities = 0; |
| 103 | ta->accept_new_threads = true; |
104 | ta->accept_new_threads = true; |
| 104 | 105 | ||
| Line 151... | Line 152... | ||
| 151 | task_t * task_run_program(void *program_addr, char *name) |
152 | task_t * task_run_program(void *program_addr, char *name) |
| 152 | { |
153 | { |
| 153 | as_t *as; |
154 | as_t *as; |
| 154 | as_area_t *a; |
155 | as_area_t *a; |
| 155 | int rc; |
156 | int rc; |
| 156 | thread_t *t; |
157 | thread_t *t1, *t2; |
| 157 | task_t *task; |
158 | task_t *task; |
| 158 | uspace_arg_t *kernel_uarg; |
159 | uspace_arg_t *kernel_uarg; |
| 159 | 160 | ||
| 160 | as = as_create(0); |
161 | as = as_create(0); |
| 161 | ASSERT(as); |
162 | ASSERT(as); |
| Line 181... | Line 182... | ||
| 181 | */ |
182 | */ |
| 182 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
183 | a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, |
| 183 | LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, |
184 | LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE, |
| 184 | USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL); |
185 | USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL); |
| 185 | 186 | ||
| - | 187 | /* |
|
| - | 188 | * Create the main thread. |
|
| - | 189 | */ |
|
| 186 | t = thread_create(uinit, kernel_uarg, task, 0, "uinit"); |
190 | t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit"); |
| 187 | ASSERT(t); |
191 | ASSERT(t1); |
| 188 | thread_ready(t); |
- | |
| 189 | 192 | ||
| - | 193 | /* |
|
| - | 194 | * Create killer thread for the new task. |
|
| - | 195 | */ |
|
| - | 196 | t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill"); |
|
| - | 197 | ASSERT(t2); |
|
| - | 198 | thread_ready(t2); |
|
| - | 199 | ||
| - | 200 | thread_ready(t1); |
|
| - | 201 | ||
| 190 | return task; |
202 | return task; |
| 191 | } |
203 | } |
| 192 | 204 | ||
| 193 | /** Syscall for reading task ID from userspace. |
205 | /** Syscall for reading task ID from userspace. |
| 194 | * |
206 | * |
| Line 250... | Line 262... | ||
| 250 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp"); |
262 | t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp"); |
| 251 | 263 | ||
| 252 | spinlock_lock(&ta->lock); |
264 | spinlock_lock(&ta->lock); |
| 253 | ta->accept_new_threads = false; |
265 | ta->accept_new_threads = false; |
| 254 | ta->refcount--; |
266 | ta->refcount--; |
| 255 | 267 | ||
| - | 268 | /* |
|
| - | 269 | * Interrupt all threads except this one. |
|
| - | 270 | */ |
|
| 256 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
271 | for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) { |
| 257 | thread_t *thr; |
272 | thread_t *thr; |
| 258 | bool sleeping = false; |
273 | bool sleeping = false; |
| 259 | 274 | ||
| 260 | thr = list_get_instance(cur, thread_t, th_link); |
275 | thr = list_get_instance(cur, thread_t, th_link); |
| Line 315... | Line 330... | ||
| 315 | 330 | ||
| 316 | spinlock_unlock(&tasks_lock); |
331 | spinlock_unlock(&tasks_lock); |
| 317 | interrupts_restore(ipl); |
332 | interrupts_restore(ipl); |
| 318 | } |
333 | } |
| 319 | 334 | ||
| 320 | /** Kernel thread used to cleanup the task. */ |
335 | /** Kernel thread used to cleanup the task after it is killed. */ |
| 321 | void ktaskclnp(void *arg) |
336 | void ktaskclnp(void *arg) |
| 322 | { |
337 | { |
| 323 | ipl_t ipl; |
338 | ipl_t ipl; |
| 324 | thread_t *t = NULL; |
339 | thread_t *t = NULL, *main_thread; |
| 325 | link_t *cur; |
340 | link_t *cur; |
| 326 | 341 | ||
| 327 | thread_detach(THREAD); |
342 | thread_detach(THREAD); |
| 328 | 343 | ||
| 329 | loop: |
344 | loop: |
| 330 | ipl = interrupts_disable(); |
345 | ipl = interrupts_disable(); |
| 331 | spinlock_lock(&TASK->lock); |
346 | spinlock_lock(&TASK->lock); |
| 332 | 347 | ||
| - | 348 | main_thread = TASK->main_thread; |
|
| - | 349 | ||
| 333 | /* |
350 | /* |
| 334 | * Find a thread to join. |
351 | * Find a thread to join. |
| 335 | */ |
352 | */ |
| 336 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
353 | for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) { |
| 337 | t = list_get_instance(cur, thread_t, th_link); |
354 | t = list_get_instance(cur, thread_t, th_link); |
| 338 | if (t == THREAD) |
355 | if (t == THREAD) |
| 339 | continue; |
356 | continue; |
| - | 357 | else if (t == main_thread) |
|
| - | 358 | continue; |
|
| 340 | else |
359 | else |
| 341 | break; |
360 | break; |
| 342 | } |
361 | } |
| 343 | 362 | ||
| 344 | spinlock_unlock(&TASK->lock); |
363 | spinlock_unlock(&TASK->lock); |
| 345 | interrupts_restore(ipl); |
364 | interrupts_restore(ipl); |
| 346 | 365 | ||
| 347 | if (t != THREAD) { |
366 | if (t != THREAD) { |
| - | 367 | ASSERT(t != main_thread); /* uninit is joined and detached in ktaskkill */ |
|
| 348 | thread_join(t); |
368 | thread_join(t); |
| 349 | thread_detach(t); |
369 | thread_detach(t); |
| 350 | goto loop; |
370 | goto loop; /* go for another thread */ |
| 351 | } |
371 | } |
| 352 | 372 | ||
| 353 | /* |
373 | /* |
| 354 | * Now there are no other threads in this task |
374 | * Now there are no other threads in this task |
| 355 | * and no new threads can be created. |
375 | * and no new threads can be created. |
| 356 | */ |
376 | */ |
| 357 | 377 | ||
| 358 | ipc_cleanup(); |
378 | ipc_cleanup(); |
| 359 | futex_cleanup(); |
379 | futex_cleanup(); |
| 360 | } |
380 | } |
| - | 381 | ||
| - | 382 | /** Kernel task used to kill a userspace task when its main thread exits. |
|
| - | 383 | * |
|
| - | 384 | * This thread waits until the main userspace thread (i.e. uninit) exits. |
|
| - | 385 | * When this happens, the task is killed. |
|
| - | 386 | * |
|
| - | 387 | * @param arg Pointer to the thread structure of the task's main thread. |
|
| - | 388 | */ |
|
| - | 389 | void ktaskkill(void *arg) |
|
| - | 390 | { |
|
| - | 391 | thread_t *t = (thread_t *) arg; |
|
| - | 392 | ||
| - | 393 | /* |
|
| - | 394 | * Userspace threads cannot detach themselves, |
|
| - | 395 | * therefore the thread pointer is guaranteed to be valid. |
|
| - | 396 | */ |
|
| - | 397 | thread_join(t); /* sleep uninterruptibly here! */ |
|
| - | 398 | thread_detach(t); |
|
| - | 399 | task_kill(TASK->taskid); |
|
| - | 400 | } |
|