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

Last change on this file since 657 was 651, checked in by alain, 5 years ago

1) Improve the VMM MMAP allocator: implement the "buddy" algorithm
to allocate only aligned blocks.
2) fix a bug in the pthread_join() / pthread_exit() mmechanism.

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#include <syscalls.h>
33
34//////////////////////////////////////
35int sys_thread_cancel( trdid_t trdid )
36{
37    reg_t        save_sr;       // required to enable IRQs
38    xptr_t       target_xp;     // target thread extended pointer
39    cxy_t        target_cxy;    // target thread cluster identifier
40    ltid_t       target_ltid;   // target thread local index
41    xptr_t       owner_xp;      // extended pointer on owner process
42    cxy_t        owner_cxy;     // process owner cluster identifier
43
44    // get killer thread pointers
45        thread_t   * this    = CURRENT_THREAD;
46    process_t  * process = this->process;
47    pid_t        pid     = process->pid;
48
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
58    // get extended pointer on target thread
59        target_xp  = thread_get_xptr( pid , trdid );
60
61    // check target_xp
62    if( target_xp == XPTR_NULL )
63    {
64
65#if DEBUG_SYSCALLS_ERROR
66printk("\n[ERROR] in %s : target thread %x not found\n", __FUNCTION__, trdid );
67#endif
68        this->errno = EINVAL;
69        return -1;
70    }
71
72    // get process owner cluster
73    owner_xp  = process->owner_xp;
74    owner_cxy = GET_CXY( owner_xp );
75
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 , false );              // not forced
104    }
105
106#if DEBUG_SYS_THREAD_CANCEL
107tm_end = hal_get_cycles();
108if( DEBUG_SYS_THREAD_CANCEL < tm_end )
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 );
111#endif
112
113        return 0;
114
115}  // end sys_thread_cancel()
Note: See TracBrowser for help on using the repository browser.