| 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 | cpu_t * cpu = current_cpu;
|
|---|
| 36 | uint32_t state;
|
|---|
| 37 | bool_t isEmpty;
|
|---|
| 38 | bool_t isReleased;
|
|---|
| 39 |
|
|---|
| 40 | /* TODO: the cpu->lid must match the core index in the logical cluster */
|
|---|
| 41 | if(this->process->pid != 1)
|
|---|
| 42 | dqdt_update_threads_number( local_cxy , cpu->lid , -1 );
|
|---|
| 43 |
|
|---|
| 44 | spinlock_lock( &this->lock );
|
|---|
| 45 |
|
|---|
| 46 | if(!(thread_isJoinable(this)))
|
|---|
| 47 | {
|
|---|
| 48 | spinlock_unlock_nosched(&this->lock);
|
|---|
| 49 | goto exit_dead;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | // Check if there's a thread waiting the end of callee thread
|
|---|
| 53 | isEmpty = wait_queue_isEmpty(&this->info.wait_queue);
|
|---|
| 54 |
|
|---|
| 55 | if(isEmpty)
|
|---|
| 56 | {
|
|---|
| 57 | this->info.exit_value = exit_val;
|
|---|
| 58 | wait_on(&this->info.wait_queue, WAIT_ANY);
|
|---|
| 59 | spinlock_unlock_nosched(&this->lock);
|
|---|
| 60 | sched_sleep(this);
|
|---|
| 61 | }
|
|---|
| 62 | else
|
|---|
| 63 | {
|
|---|
| 64 | this->info.join->info.exit_value = exit_val;
|
|---|
| 65 | wakeup_one(&this->info.wait_queue, WAIT_ANY);
|
|---|
| 66 | spinlock_unlock_nosched(&this->lock);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | exit_dead:
|
|---|
| 70 |
|
|---|
| 71 | isReleased = false;
|
|---|
| 72 |
|
|---|
| 73 | // Release FPU if required
|
|---|
| 74 | cpu_disable_all_irq(&state);
|
|---|
| 75 | if(current_cpu->fpu_owner == this)
|
|---|
| 76 | {
|
|---|
| 77 | current_cpu->fpu_owner = NULL;
|
|---|
| 78 | isReleased = true;
|
|---|
| 79 | }
|
|---|
| 80 | cpu_restore_irq(state);
|
|---|
| 81 |
|
|---|
| 82 | if(isReleased)
|
|---|
| 83 | {
|
|---|
| 84 | thread_dmsg(1, "INFO: Thread %x has released FPU on CPU %d\n",
|
|---|
| 85 | this,
|
|---|
| 86 | current_cpu->gid);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | sched_exit(this);
|
|---|
| 90 |
|
|---|
| 91 | PANIC ("Thread %x, CPU %d must never return", this, current_cpu->gid);
|
|---|
| 92 | return -1; /* Fake return ! */
|
|---|
| 93 | }
|
|---|