[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 | reg_t save_sr; // required to enable IRQs |
---|
| 39 | xptr_t owner_xp; // extended pointer on owner process |
---|
| 40 | |
---|
| 41 | thread_t * this = CURRENT_THREAD; |
---|
| 42 | trdid_t trdid = this->trdid; |
---|
| 43 | process_t * process = this->process; |
---|
| 44 | pid_t pid = process->pid; |
---|
| 45 | cxy_t owner_cxy = CXY_FROM_PID( pid ); |
---|
[1] | 46 | |
---|
[436] | 47 | // check exit_value argument |
---|
| 48 | if( exit_value != NULL ) |
---|
| 49 | { |
---|
| 50 | |
---|
[438] | 51 | #if DEBUG_SYSCALLS_ERROR |
---|
[440] | 52 | printk("\n[ERROR] in %s : exit_value argument must be NULL / thread %x in process %x\n", |
---|
| 53 | __FUNCTION__ , this , pid ); |
---|
[436] | 54 | #endif |
---|
[409] | 55 | this->errno = EINVAL; |
---|
| 56 | return -1; |
---|
| 57 | } |
---|
[1] | 58 | |
---|
[438] | 59 | #if DEBUG_SYS_THREAD_EXIT |
---|
[436] | 60 | uint64_t tm_start; |
---|
| 61 | uint64_t tm_end; |
---|
| 62 | tm_start = hal_get_cycles(); |
---|
[438] | 63 | if( DEBUG_SYS_THREAD_EXIT < tm_start ) |
---|
[566] | 64 | printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n", |
---|
| 65 | __FUNCTION__ , this->trdid, pid , (uint32_t)tm_start ); |
---|
[436] | 66 | #endif |
---|
[1] | 67 | |
---|
[440] | 68 | // If calling thread is the main thread, the process must be deleted. |
---|
| 69 | // This require to delete all process threads and synchronise with parent process |
---|
| 70 | if( (local_cxy == owner_cxy) && (LTID_FROM_TRDID(trdid) == 0) ) |
---|
| 71 | { |
---|
| 72 | // get extended pointer on owner cluster |
---|
| 73 | owner_xp = cluster_get_owner_process_from_pid( pid ); |
---|
[1] | 74 | |
---|
[440] | 75 | // mark for delete all threads but the main |
---|
| 76 | hal_enable_irq( &save_sr ); |
---|
| 77 | process_sigaction( pid , DELETE_ALL_THREADS ); |
---|
| 78 | hal_restore_irq( save_sr ); |
---|
| 79 | |
---|
| 80 | // remove process from TXT list |
---|
| 81 | process_txt_detach( owner_xp ); |
---|
| 82 | |
---|
| 83 | // block the main thread |
---|
| 84 | thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_GLOBAL ); |
---|
| 85 | |
---|
| 86 | // atomically update owner process descriptor term_state to ask |
---|
| 87 | // the parent process sys_wait() function to delete the main thread |
---|
| 88 | hal_remote_atomic_or( XPTR( local_cxy , &process->term_state ) , |
---|
| 89 | PROCESS_TERM_EXIT ); |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | // block calling thread and mark it for delete, |
---|
| 94 | thread_delete( XPTR( local_cxy , this ) , pid , false ); |
---|
| 95 | } |
---|
| 96 | |
---|
[438] | 97 | #if DEBUG_SYS_THREAD_EXIT |
---|
[409] | 98 | tm_end = hal_get_cycles(); |
---|
[438] | 99 | if( DEBUG_SYS_THREAD_EXIT < tm_end ) |
---|
[566] | 100 | printk("\n[DBG] %s : thread %x in process %x exit / cost %d / cycle %d\n", |
---|
| 101 | __FUNCTION__, this->trdid, pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end ); |
---|
[409] | 102 | #endif |
---|
| 103 | |
---|
[440] | 104 | // deschedule <=> suicide, because blocked by thread_delete() |
---|
[436] | 105 | sched_yield( "suicide after thread_exit" ); |
---|
| 106 | |
---|
[409] | 107 | return 0; // never executed but required by compiler |
---|
| 108 | |
---|
| 109 | } // end sys_thread exit |
---|