[1] | 1 | /* |
---|
[409] | 2 | * sys_thread_exit.c - terminates the execution of calling thread |
---|
[1] | 3 | * |
---|
[635] | 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
[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 |
---|
[635] | 48 | printk("\n[ERROR] in %s : thread[%x,%x] / exit_value argument %x must be NULL\n", |
---|
| 49 | __FUNCTION__ , pid, trdid , exit_value ); |
---|
[436] | 50 | #endif |
---|
[409] | 51 | this->errno = EINVAL; |
---|
| 52 | return -1; |
---|
| 53 | } |
---|
[1] | 54 | |
---|
[440] | 55 | // If calling thread is the main thread, the process must be deleted. |
---|
[584] | 56 | // => delete all process threads and synchronise with parent process. |
---|
| 57 | // If calling thread is not the main thread, it must be deleted. |
---|
| 58 | // => block the thread and mark it for delete. |
---|
| 59 | if( (CXY_FROM_PID( pid ) == local_cxy) && (LTID_FROM_TRDID(trdid) == 0) ) |
---|
[440] | 60 | { |
---|
[1] | 61 | |
---|
[584] | 62 | #if DEBUG_SYS_THREAD_EXIT |
---|
| 63 | uint64_t tm_start = hal_get_cycles(); |
---|
| 64 | if( DEBUG_SYS_THREAD_EXIT < tm_start ) |
---|
[625] | 65 | printk("\n[%s] thread[%x,%x] is main => delete process / cycle %d\n", |
---|
[584] | 66 | __FUNCTION__ , pid , trdid , (uint32_t)tm_start ); |
---|
| 67 | #endif |
---|
| 68 | // delete process |
---|
| 69 | sys_exit( 0 ); |
---|
[440] | 70 | } |
---|
| 71 | else |
---|
| 72 | { |
---|
[584] | 73 | |
---|
| 74 | #if DEBUG_SYS_THREAD_EXIT |
---|
| 75 | uint64_t tm_start = hal_get_cycles(); |
---|
| 76 | if( DEBUG_SYS_THREAD_EXIT < tm_start ) |
---|
[625] | 77 | printk("\n[%s] thread[%x,%x] is not main => delete thread / cycle %d\n", |
---|
[584] | 78 | __FUNCTION__ , pid , trdid , (uint32_t)tm_start ); |
---|
| 79 | #endif |
---|
[440] | 80 | // block calling thread and mark it for delete, |
---|
| 81 | thread_delete( XPTR( local_cxy , this ) , pid , false ); |
---|
[584] | 82 | |
---|
| 83 | // deschedule |
---|
| 84 | sched_yield( "suicide after thread_exit" ); |
---|
[440] | 85 | } |
---|
| 86 | |
---|
[409] | 87 | return 0; // never executed but required by compiler |
---|
| 88 | |
---|
| 89 | } // end sys_thread exit |
---|