[1] | 1 | /* |
---|
[409] | 2 | * sys_thread_exit.c - terminates the execution of calling thread |
---|
[1] | 3 | * |
---|
[409] | 4 | * Authors Alain Greiner (2016,2017) |
---|
[1] | 5 | * |
---|
[23] | 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 7 | * |
---|
[23] | 8 | * This file is part of ALMOS-MKH. |
---|
| 9 | * |
---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
[23] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
[23] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[23] | 24 | #include <hal_types.h> |
---|
[1] | 25 | #include <thread.h> |
---|
[23] | 26 | #include <core.h> |
---|
[409] | 27 | #include <vmm.h> |
---|
[1] | 28 | #include <scheduler.h> |
---|
[23] | 29 | #include <printk.h> |
---|
[1] | 30 | |
---|
[23] | 31 | //////////////////////////////////////// |
---|
| 32 | int sys_thread_exit( void * exit_value ) |
---|
[1] | 33 | { |
---|
[409] | 34 | thread_t * this = CURRENT_THREAD; |
---|
| 35 | process_t * process = this->process; |
---|
[1] | 36 | |
---|
[436] | 37 | // check exit_value argument |
---|
| 38 | if( exit_value != NULL ) |
---|
| 39 | { |
---|
| 40 | |
---|
| 41 | #if CONFIG_DEBUG_SYSCALLS_ERROR |
---|
| 42 | printk("\n[ERROR] in %s : exit_value argument must be NULL for thread %x in process %x\n", |
---|
| 43 | __FUNCTION__ , exit_value, this->trdid , process->pid ); |
---|
| 44 | #endif |
---|
[409] | 45 | this->errno = EINVAL; |
---|
| 46 | return -1; |
---|
| 47 | } |
---|
[1] | 48 | |
---|
[436] | 49 | #if CONFIG_DEBUG_SYS_THREAD_EXIT |
---|
| 50 | uint64_t tm_start; |
---|
| 51 | uint64_t tm_end; |
---|
| 52 | tm_start = hal_get_cycles(); |
---|
| 53 | if( CONFIG_DEBUG_SYS_THREAD_EXIT < tm_start ) |
---|
| 54 | printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n", |
---|
| 55 | __FUNCTION__ , this, process->pid , (uint32_t)tm_start ); |
---|
| 56 | #endif |
---|
[1] | 57 | |
---|
[436] | 58 | // cal the relevant kernel function |
---|
| 59 | thread_kill( XPTR( local_cxy , this ), |
---|
| 60 | 1, // is_exit |
---|
| 61 | 0 ); // is forced |
---|
[1] | 62 | |
---|
[436] | 63 | #if CONFIG_DEBUG_SYS_THREAD_EXIT |
---|
[409] | 64 | tm_end = hal_get_cycles(); |
---|
[436] | 65 | if( CONFIG_DEBUG_SYS_THREAD_EXIT < tm_end ) |
---|
| 66 | printk("\n[DBG] %s : thread %x exit / process %x / cost %d / cycle %d\n", |
---|
| 67 | __FUNCTION__, this, this->process->pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
[409] | 68 | #endif |
---|
| 69 | |
---|
[436] | 70 | // deschedule <=> suicide, because blocked by thread_kill() |
---|
| 71 | sched_yield( "suicide after thread_exit" ); |
---|
| 72 | |
---|
[409] | 73 | return 0; // never executed but required by compiler |
---|
| 74 | |
---|
| 75 | } // end sys_thread exit |
---|