source: trunk/kernel/syscalls/sys_thread_cancel.c @ 517

Last change on this file since 517 was 506, checked in by viala@…, 6 years ago

[syscalls] add interface in implementation.

Add const where possible, fix protoypes to follow interface.

File size: 3.7 KB
RevLine 
[410]1/*
[436]2 * sys_thread_cancel.c - terminate execution of an user thread.
[410]3 *
[436]4 * Authors   Alain Greiner (2016,2017, 2018)
[410]5 *
[436]6 * Copyright (c) UPMC Sorbonne Universites
[410]7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
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 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
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
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
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>
[410]26#include <hal_remote.h>
27#include <hal_special.h>
28#include <thread.h>
29#include <errno.h>
30#include <printk.h>
31
[506]32#include <syscalls.h>
33
[410]34//////////////////////////////////////
35int sys_thread_cancel( trdid_t trdid )
36{
[440]37    reg_t        save_sr;       // required to enable IRQs
[410]38    xptr_t       target_xp;     // target thread extended pointer
[440]39    cxy_t        target_cxy;    // target thread cluster identifier
40    ltid_t       target_ltid;   // target thread local index
[443]41    xptr_t       owner_xp;      // extended pointer on owner process
[440]42    cxy_t        owner_cxy;     // process owner cluster identifier
[410]43
[436]44    // get killer thread pointers
[410]45        thread_t   * this    = CURRENT_THREAD;
46    process_t  * process = this->process;
[440]47    pid_t        pid     = process->pid;
[410]48
[443]49#if DEBUG_SYS_THREAD_CANCEL
50uint64_t     tm_start;
51uint64_t     tm_end;
52tm_start = hal_get_cycles();
53if( DEBUG_SYS_THREAD_CANCEL < tm_start )
54printk("\n[DBG] %s : thread %x enter to kill thread %x in process %x / cycle %d\n",
55__FUNCTION__, this , trdid , pid , (uint32_t)tm_start );
56#endif
57
[410]58    // get extended pointer on target thread
[440]59        target_xp  = thread_get_xptr( pid , trdid );
[410]60
[436]61    // check target_xp
[410]62    if( target_xp == XPTR_NULL )
63    {
[436]64
[438]65#if DEBUG_SYSCALLS_ERROR
[436]66printk("\n[ERROR] in %s : target thread %x not found\n", __FUNCTION__, trdid );
67#endif
[410]68        this->errno = EINVAL;
69        return -1;
70    }
71
[443]72    // get process owner cluster
73    owner_xp  = process->owner_xp;
74    owner_cxy = GET_CXY( owner_xp );
[410]75
[440]76    // get target thread ltid and cluster
77    target_cxy  = CXY_FROM_TRDID( trdid );
78    target_ltid = LTID_FROM_TRDID( trdid );
79
80    // If target thread is the main thread, the process must be deleted,
81    // This require synchronisation with parent process
82    if( (target_cxy == owner_cxy) && (target_ltid == 0) )
83    {
84        // mark for delete all threads but the main
85        hal_enable_irq( &save_sr );
86        process_sigaction( pid , DELETE_ALL_THREADS );
87        hal_restore_irq( save_sr );
88
89        // remove process from TXT list
90        process_txt_detach( owner_xp );
91
92        // block the main thread
93        thread_block( XPTR( local_cxy ,this ) , THREAD_BLOCKED_GLOBAL );
94
95        // atomically update owner process descriptor term_state to ask
96        // the parent process sys_wait() function to delete the main thread
97        hal_remote_atomic_or( XPTR( local_cxy , &process->term_state ) ,
98                              PROCESS_TERM_EXIT );
99    }
100    else
101    {
102        // block target thread and mark it for delete
103        thread_delete( target_xp , pid , false );
104    }
105
[438]106#if DEBUG_SYS_THREAD_CANCEL
[410]107tm_end = hal_get_cycles();
[438]108if( DEBUG_SYS_THREAD_CANCEL < tm_end )
[443]109printk("\n[DBG] %s : thread %x exit after kill thread %x in process %x / cycle %d\n",
110__FUNCTION__, this , trdid , pid , (uint32_t)tm_end );
[410]111#endif
112
113        return 0;
114
115}  // end sys_thread_cancel()
Note: See TracBrowser for help on using the repository browser.