| 1 | /* |
|---|
| 2 | * sys_thread_exit.c - terminates the execution of calling thread |
|---|
| 3 | * |
|---|
| 4 | * Authors Alain Greiner (2016,2017) |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 7 | * |
|---|
| 8 | * This file is part of ALMOS-MKH. |
|---|
| 9 | * |
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 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 | * |
|---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 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 |
|---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #include <hal_types.h> |
|---|
| 25 | #include <thread.h> |
|---|
| 26 | #include <core.h> |
|---|
| 27 | #include <vmm.h> |
|---|
| 28 | #include <scheduler.h> |
|---|
| 29 | #include <printk.h> |
|---|
| 30 | |
|---|
| 31 | //////////////////////////////////////// |
|---|
| 32 | int sys_thread_exit( void * exit_value ) |
|---|
| 33 | { |
|---|
| 34 | thread_t * this = CURRENT_THREAD; |
|---|
| 35 | process_t * process = this->process; |
|---|
| 36 | |
|---|
| 37 | // check exit_value argument |
|---|
| 38 | if( exit_value != NULL ) |
|---|
| 39 | { |
|---|
| 40 | |
|---|
| 41 | #if 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 |
|---|
| 45 | this->errno = EINVAL; |
|---|
| 46 | return -1; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | #if DEBUG_SYS_THREAD_EXIT |
|---|
| 50 | uint64_t tm_start; |
|---|
| 51 | uint64_t tm_end; |
|---|
| 52 | tm_start = hal_get_cycles(); |
|---|
| 53 | if( 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 |
|---|
| 57 | |
|---|
| 58 | // cal the relevant kernel function |
|---|
| 59 | thread_kill( XPTR( local_cxy , this ), |
|---|
| 60 | 1, // is_exit |
|---|
| 61 | 0 ); // is forced |
|---|
| 62 | |
|---|
| 63 | #if DEBUG_SYS_THREAD_EXIT |
|---|
| 64 | tm_end = hal_get_cycles(); |
|---|
| 65 | if( 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 ); |
|---|
| 68 | #endif |
|---|
| 69 | |
|---|
| 70 | // deschedule <=> suicide, because blocked by thread_kill() |
|---|
| 71 | sched_yield( "suicide after thread_exit" ); |
|---|
| 72 | |
|---|
| 73 | return 0; // never executed but required by compiler |
|---|
| 74 | |
|---|
| 75 | } // end sys_thread exit |
|---|