[1] | 1 | /* |
---|
[409] | 2 | * sys_thread_exit.c - terminates the execution of calling thread |
---|
[1] | 3 | * |
---|
[440] | 4 | * Authors Alain Greiner (2016,2017,2018) |
---|
[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 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[440] | 25 | #include <hal_irqmask.h> |
---|
[1] | 26 | #include <thread.h> |
---|
[440] | 27 | #include <process.h> |
---|
[23] | 28 | #include <core.h> |
---|
[409] | 29 | #include <vmm.h> |
---|
[1] | 30 | #include <scheduler.h> |
---|
[23] | 31 | #include <printk.h> |
---|
[1] | 32 | |
---|
[506] | 33 | #include <syscalls.h> |
---|
| 34 | |
---|
[23] | 35 | //////////////////////////////////////// |
---|
| 36 | int sys_thread_exit( void * exit_value ) |
---|
[1] | 37 | { |
---|
[440] | 38 | thread_t * this = CURRENT_THREAD; |
---|
| 39 | trdid_t trdid = this->trdid; |
---|
| 40 | process_t * process = this->process; |
---|
| 41 | pid_t pid = process->pid; |
---|
[1] | 42 | |
---|
[436] | 43 | // check exit_value argument |
---|
| 44 | if( exit_value != NULL ) |
---|
| 45 | { |
---|
| 46 | |
---|
[438] | 47 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 48 | printk("\n[ERROR] in %s : exit_value argument must be NULL / thread %x in process %x\n", |
---|
| 49 | __FUNCTION__ , this , pid ); |
---|
[436] | 50 | #endif |
---|
[409] | 51 | this->errno = EINVAL; |
---|
| 52 | return -1; |
---|
| 53 | } |
---|
[1] | 54 | |
---|
| 55 | |
---|
[440] | 56 | // If calling thread is the main thread, the process must be deleted. |
---|
[584] | 57 | // => delete all process threads and synchronise with parent process. |
---|
| 58 | // If calling thread is not the main thread, it must be deleted. |
---|
| 59 | // => block the thread and mark it for delete. |
---|
| 60 | if( (CXY_FROM_PID( pid ) == local_cxy) && (LTID_FROM_TRDID(trdid) == 0) ) |
---|
[440] | 61 | { |
---|
[1] | 62 | |
---|
[584] | 63 | #if DEBUG_SYS_THREAD_EXIT |
---|
| 64 | uint64_t tm_start = hal_get_cycles(); |
---|
| 65 | if( DEBUG_SYS_THREAD_EXIT < tm_start ) |
---|
| 66 | printk("\n[DBG] %s : thread[%x,%x] / main => delete process / cycle %d\n", |
---|
| 67 | __FUNCTION__ , pid , trdid , (uint32_t)tm_start ); |
---|
| 68 | #endif |
---|
| 69 | // delete process |
---|
| 70 | sys_exit( 0 ); |
---|
[440] | 71 | } |
---|
| 72 | else |
---|
| 73 | { |
---|
[584] | 74 | |
---|
| 75 | #if DEBUG_SYS_THREAD_EXIT |
---|
| 76 | uint64_t tm_start = hal_get_cycles(); |
---|
| 77 | if( DEBUG_SYS_THREAD_EXIT < tm_start ) |
---|
| 78 | printk("\n[DBG] %s : thread[%x,%x] / not main => delete thread / cycle %d\n", |
---|
| 79 | __FUNCTION__ , pid , trdid , (uint32_t)tm_start ); |
---|
| 80 | #endif |
---|
[440] | 81 | // block calling thread and mark it for delete, |
---|
| 82 | thread_delete( XPTR( local_cxy , this ) , pid , false ); |
---|
[584] | 83 | |
---|
| 84 | // deschedule |
---|
| 85 | sched_yield( "suicide after thread_exit" ); |
---|
[440] | 86 | } |
---|
| 87 | |
---|
[409] | 88 | return 0; // never executed but required by compiler |
---|
| 89 | |
---|
| 90 | } // end sys_thread exit |
---|