[1] | 1 | /* |
---|
[23] | 2 | * sys_thread_detach.c - detach a joinable thread |
---|
[1] | 3 | * |
---|
[637] | 4 | * Authors Alain Greiner (2016,2017,2018,2019) |
---|
[23] | 5 | * |
---|
[637] | 6 | * Copyright (c) UPMC Sorbonne Universites |
---|
[1] | 7 | * |
---|
[23] | 8 | * This file is part of ALMOS-MKH. |
---|
[1] | 9 | * |
---|
[23] | 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> |
---|
[23] | 25 | #include <hal_remote.h> |
---|
| 26 | #include <hal_special.h> |
---|
[1] | 27 | #include <thread.h> |
---|
| 28 | #include <errno.h> |
---|
[23] | 29 | #include <printk.h> |
---|
[1] | 30 | |
---|
[506] | 31 | #include <syscalls.h> |
---|
| 32 | |
---|
[23] | 33 | ////////////////////////////////////// |
---|
| 34 | int sys_thread_detach( trdid_t trdid ) |
---|
[1] | 35 | { |
---|
[23] | 36 | xptr_t target_xp; |
---|
| 37 | thread_t * target_ptr; |
---|
| 38 | cxy_t target_cxy; |
---|
| 39 | ltid_t target_ltid; |
---|
| 40 | uint32_t flags; |
---|
[1] | 41 | |
---|
[23] | 42 | thread_t * this = CURRENT_THREAD; |
---|
| 43 | process_t * process = this->process; |
---|
[1] | 44 | |
---|
[23] | 45 | // get target thread ltid and cxy |
---|
| 46 | target_ltid = LTID_FROM_TRDID( trdid ); |
---|
| 47 | target_cxy = CXY_FROM_TRDID( trdid ); |
---|
[1] | 48 | |
---|
[23] | 49 | // check trdid argument |
---|
[637] | 50 | if( (target_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || |
---|
| 51 | (cluster_is_active( target_cxy ) == false) ) |
---|
[1] | 52 | { |
---|
[23] | 53 | printk("\n[ERROR] in %s : illegal trdid argument\n", __FUNCTION__ ); |
---|
| 54 | this->errno = EINVAL; |
---|
| 55 | return -1; |
---|
[1] | 56 | } |
---|
| 57 | |
---|
[23] | 58 | // get extended pointer on target thread |
---|
| 59 | target_xp = thread_get_xptr( process->pid , trdid ); |
---|
[1] | 60 | |
---|
[23] | 61 | if( target_xp == XPTR_NULL ) |
---|
| 62 | { |
---|
| 63 | printk("\n[ERROR] in %s : target thread not found\n", __FUNCTION__ ); |
---|
| 64 | this->errno = ESRCH; |
---|
| 65 | return -1; |
---|
| 66 | } |
---|
[1] | 67 | |
---|
[23] | 68 | // get local pointer on target thread |
---|
| 69 | target_ptr = (thread_t *)GET_PTR( target_xp ); |
---|
[1] | 70 | |
---|
[23] | 71 | // get target thread flags |
---|
[566] | 72 | flags = hal_remote_l32( XPTR( target_cxy , &target_ptr->flags ) ); |
---|
[1] | 73 | |
---|
[23] | 74 | // check target thread joinable |
---|
| 75 | if( flags & THREAD_FLAG_DETACHED ) |
---|
| 76 | { |
---|
| 77 | printk("\n[ERROR] in %s : target thread not joinable\n", __FUNCTION__ ); |
---|
| 78 | this->errno = EINVAL; |
---|
| 79 | return -1; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | // atomically set the thread DETACHED flag |
---|
| 83 | hal_remote_atomic_or( XPTR( target_cxy , &target_ptr->flags ) , THREAD_FLAG_DETACHED ); |
---|
| 84 | |
---|
| 85 | return 0; |
---|
| 86 | |
---|
| 87 | } // end sys_thread_detach() |
---|