| 1 | /* |
|---|
| 2 | * kern/sys_thread_exit.c - terminates the execution of current thread |
|---|
| 3 | * |
|---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless |
|---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
|---|
| 6 | * |
|---|
| 7 | * This file is part of ALMOS-kernel. |
|---|
| 8 | * |
|---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it |
|---|
| 10 | * under the terms of the GNU General Public License as published by |
|---|
| 11 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 12 | * |
|---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but |
|---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 16 | * General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, |
|---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | #include <list.h> |
|---|
| 24 | #include <thread.h> |
|---|
| 25 | #include <scheduler.h> |
|---|
| 26 | #include <wait_queue.h> |
|---|
| 27 | #include <cluster.h> |
|---|
| 28 | #include <dqdt.h> |
|---|
| 29 | #include <process.h> |
|---|
| 30 | |
|---|
| 31 | /////////////////////////////////////// |
|---|
| 32 | int sys_thread_exit ( void * exit_val ) |
|---|
| 33 | { |
|---|
| 34 | thread_t * this = CURRENT_THREAD; |
|---|
| 35 | core_t * core = this->core; |
|---|
| 36 | bool_t isEmpty; |
|---|
| 37 | bool_t isReleased; |
|---|
| 38 | uint32_t irq_state; |
|---|
| 39 | |
|---|
| 40 | // update DQDT TODO must be done by thread destroy |
|---|
| 41 | if(this->process->pid != 1) dqdt_local_update_threads( -1 ); |
|---|
| 42 | |
|---|
| 43 | spinlock_lock( &this->lock ); |
|---|
| 44 | |
|---|
| 45 | if(!(thread_isJoinable(this))) |
|---|
| 46 | { |
|---|
| 47 | goto exit_dead; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | // Check if there's a thread waiting the end of calling thread |
|---|
| 51 | isEmpty = wait_queue_isEmpty(&this->info.wait_queue); |
|---|
| 52 | |
|---|
| 53 | if(isEmpty) |
|---|
| 54 | { |
|---|
| 55 | this->info.exit_value = exit_val; |
|---|
| 56 | wait_on(&this->info.wait_queue, WAIT_ANY); |
|---|
| 57 | spinlock_unlock_nosched(&this->lock); |
|---|
| 58 | sched_sleep(this); |
|---|
| 59 | } |
|---|
| 60 | else |
|---|
| 61 | { |
|---|
| 62 | this->info.join->info.exit_value = exit_val; |
|---|
| 63 | wakeup_one(&this->info.wait_queue, WAIT_ANY); |
|---|
| 64 | spinlock_unlock_nosched(&this->lock); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | exit_dead: |
|---|
| 68 | |
|---|
| 69 | isReleased = false; |
|---|
| 70 | |
|---|
| 71 | // Release FPU if required |
|---|
| 72 | hal_disable_irq( &irq_state ); |
|---|
| 73 | if(core->fpu_owner == this) |
|---|
| 74 | { |
|---|
| 75 | core->fpu_owner = NULL; |
|---|
| 76 | isReleased = true; |
|---|
| 77 | } |
|---|
| 78 | hal_restore_irq( irq_state ); |
|---|
| 79 | |
|---|
| 80 | if(isReleased) |
|---|
| 81 | { |
|---|
| 82 | thread_dmsg(1, "INFO: Thread %x has released FPU on CPU %d\n", |
|---|
| 83 | this, |
|---|
| 84 | current_cpu->gid); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | sched_exit(this); |
|---|
| 88 | |
|---|
| 89 | PANIC ("Thread %x, CPU %d must never return", this, current_cpu->gid); |
|---|
| 90 | return -1; /* Fake return ! */ |
|---|
| 91 | } |
|---|