[1] | 1 | /* |
---|
[23] | 2 | * sys_thread_exit.c - terminates the execution of current thread |
---|
[1] | 3 | * |
---|
[23] | 4 | * Authors Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
| 5 | * Alain Greiner (2016,2017) |
---|
[1] | 6 | * |
---|
[23] | 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 8 | * |
---|
[23] | 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
[23] | 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
[23] | 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
[23] | 25 | #include <hal_types.h> |
---|
| 26 | #include <hal_irqmask.h> |
---|
[1] | 27 | #include <thread.h> |
---|
[23] | 28 | #include <core.h> |
---|
[1] | 29 | #include <scheduler.h> |
---|
[23] | 30 | #include <printk.h> |
---|
[1] | 31 | |
---|
[23] | 32 | |
---|
| 33 | //////////////////////////////////////// |
---|
| 34 | int sys_thread_exit( void * exit_value ) |
---|
[1] | 35 | { |
---|
[16] | 36 | thread_t * this = CURRENT_THREAD; |
---|
| 37 | core_t * core = this->core; |
---|
[60] | 38 | reg_t irq_state; |
---|
[1] | 39 | |
---|
[23] | 40 | // register the exit_value in thread descriptor |
---|
| 41 | this->exit_value = exit_value; |
---|
[1] | 42 | |
---|
[23] | 43 | // we enter the join loop to wait the join |
---|
| 44 | // only if thread is joinable |
---|
| 45 | if( (this->flags & THREAD_FLAG_DETACHED) == 0 ) |
---|
| 46 | { |
---|
| 47 | while( 1 ) |
---|
| 48 | { |
---|
| 49 | // take the lock protecting the flags |
---|
| 50 | remote_spinlock_lock( XPTR( local_cxy, &this->flags_lock ) ); |
---|
[1] | 51 | |
---|
[23] | 52 | // check the JOIN flag |
---|
| 53 | if( this->flags & THREAD_FLAG_JOIN ) // parent made a join |
---|
| 54 | { |
---|
| 55 | // unblock the parent thread |
---|
| 56 | thread_unblock( this->parent , THREAD_BLOCKED_JOIN ); |
---|
[1] | 57 | |
---|
[23] | 58 | // release the lock protecting the flags |
---|
| 59 | remote_spinlock_unlock( XPTR( local_cxy, &this->flags_lock ) ); |
---|
[1] | 60 | |
---|
[23] | 61 | // exit while |
---|
| 62 | break; |
---|
| 63 | } |
---|
| 64 | else // no join done by parent thread |
---|
| 65 | { |
---|
| 66 | // set the EXIT flag |
---|
| 67 | this->flags |= THREAD_FLAG_EXIT; |
---|
[1] | 68 | |
---|
[23] | 69 | // block this thread |
---|
| 70 | thread_block( this , THREAD_BLOCKED_EXIT ); |
---|
[1] | 71 | |
---|
[23] | 72 | // release the lock protecting the flags |
---|
| 73 | remote_spinlock_unlock( XPTR( local_cxy, &this->flags_lock ) ); |
---|
[1] | 74 | |
---|
[23] | 75 | // deschedule |
---|
[296] | 76 | sched_yield( NULL ); |
---|
[23] | 77 | } |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
[1] | 81 | // Release FPU if required |
---|
[16] | 82 | hal_disable_irq( &irq_state ); |
---|
[23] | 83 | if( core->fpu_owner == this ) core->fpu_owner = NULL; |
---|
[16] | 84 | hal_restore_irq( irq_state ); |
---|
[1] | 85 | |
---|
[23] | 86 | // suicide |
---|
| 87 | thread_kill( this ); |
---|
| 88 | return 0; |
---|
[1] | 89 | |
---|
[23] | 90 | } // end sys_thread_exit() |
---|