[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 | paddr_t paddr; |
---|
| 35 | error_t error; |
---|
[1] | 36 | |
---|
[409] | 37 | #if CONFIG_SYSCALL_DEBUG |
---|
| 38 | uint32_t tm_start; |
---|
| 39 | uint32_t tm_end; |
---|
| 40 | tm_start = hal_get_cycles(); |
---|
| 41 | #endif |
---|
[1] | 42 | |
---|
[409] | 43 | thread_t * this = CURRENT_THREAD; |
---|
| 44 | process_t * process = this->process; |
---|
[1] | 45 | |
---|
[409] | 46 | // check all locks released |
---|
| 47 | if( !thread_can_yield() ) |
---|
| 48 | { |
---|
| 49 | printk("\n[ERROR] in %s : locks not released / thread %x in process %x\n", |
---|
| 50 | __FUNCTION__, this->trdid, process->pid ); |
---|
| 51 | this->errno = EINVAL; |
---|
| 52 | return -1; |
---|
| 53 | } |
---|
[1] | 54 | |
---|
[409] | 55 | // register the exit_value pointer in this thread descriptor |
---|
| 56 | this->join_value = exit_value; |
---|
[1] | 57 | |
---|
[409] | 58 | if( (this->flags & THREAD_FLAG_DETACHED) == 0 ) // this thread is joinable |
---|
| 59 | { |
---|
| 60 | // check exit_value in user space |
---|
| 61 | error = vmm_v2p_translate( false , exit_value , &paddr ); |
---|
| 62 | if( error ) |
---|
| 63 | { |
---|
| 64 | printk("\n[ERROR] in %s : illegal pointer = %x / thread %x in process %x\n", |
---|
| 65 | __FUNCTION__ , (intptr_t)exit_value, this->trdid , process->pid ); |
---|
| 66 | this->errno = EINVAL; |
---|
| 67 | return -1; |
---|
| 68 | } |
---|
[1] | 69 | |
---|
[409] | 70 | // take the lock protecting the join |
---|
| 71 | remote_spinlock_lock( XPTR( local_cxy, &this->join_lock ) ); |
---|
[1] | 72 | |
---|
[409] | 73 | if( this->flags & THREAD_FLAG_JOIN_DONE ) // parent thread arrived first |
---|
| 74 | { |
---|
| 75 | // unblock the parent thread |
---|
| 76 | thread_unblock( this->join_xp , THREAD_BLOCKED_EXIT ); |
---|
[1] | 77 | |
---|
[409] | 78 | // reset the JOIN_DONE flag in this thread |
---|
| 79 | this->flags &= ~THREAD_FLAG_JOIN_DONE; |
---|
| 80 | |
---|
| 81 | // release the lock protecting the flags |
---|
| 82 | remote_spinlock_unlock( XPTR( local_cxy, &this->join_lock ) ); |
---|
[23] | 83 | } |
---|
[409] | 84 | else // this thread arrived first |
---|
| 85 | { |
---|
| 86 | // block this thread |
---|
| 87 | thread_block( this , THREAD_BLOCKED_JOIN ); |
---|
[23] | 88 | |
---|
[409] | 89 | // release the lock protecting the flags |
---|
| 90 | remote_spinlock_unlock( XPTR( local_cxy, &this->join_lock ) ); |
---|
[1] | 91 | |
---|
[409] | 92 | // deschedule |
---|
| 93 | sched_yield( "WAITING JOIN" ); |
---|
| 94 | } |
---|
| 95 | } |
---|
[1] | 96 | |
---|
[409] | 97 | #if CONFIG_SYSCALL_DEBUG |
---|
| 98 | tm_end = hal_get_cycles(); |
---|
| 99 | syscall_dmsg("\n[DBG] %s : core[%x,%d] / thread %x in process %x / cycle %d\n" |
---|
| 100 | "thread %x killed / cost = %d\n", |
---|
| 101 | __FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid , tm_start , |
---|
| 102 | this->trdid , (uint32_t)(tm_end - tm_start) ); |
---|
| 103 | #endif |
---|
| 104 | |
---|
| 105 | // suicide using a rpc because a thread cannot kill itself |
---|
| 106 | rpc_thread_kill_client( local_cxy , this ); |
---|
| 107 | |
---|
| 108 | return 0; // never executed but required by compiler |
---|
| 109 | |
---|
| 110 | } // end sys_thread exit |
---|