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

Last change on this file since 458 was 457, checked in by alain, 6 years ago

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

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