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

Last change on this file since 410 was 410, checked in by alain, 4 years ago

Introduce new syscalls.

File size: 2.9 KB
Line 
1/*
2 * sys_thread_cancel.c - terminate execution of a target thread.
3 *
4 * Authors   Alain Greiner (2016,2017)
5 *
6 * Copyright (c) 2011,2012 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_types.h>
25#include <hal_remote.h>
26#include <hal_special.h>
27#include <thread.h>
28#include <errno.h>
29#include <printk.h>
30
31//////////////////////////////////////
32int sys_thread_cancel( trdid_t trdid )
33{
34    xptr_t       target_xp;     // target thread extended pointer
35        thread_t   * target_ptr;    // target thread local pointer
36    cxy_t        target_cxy;    // target thread cluster
37    ltid_t       target_ltid;   // target thread local index
38
39#if CONFIG_SYSCALL_DEBUG
40uint32_t     tm_start;
41uint32_t     tm_end;
42tm_start  = hal_get_cycles();
43#endif
44
45        thread_t   * this    = CURRENT_THREAD;
46    process_t  * process = this->process;
47
48    // check kernel stack overflow
49    assert( (this->signature == THREAD_SIGNATURE), __FUNCTION__, "kernel stack overflow\n" );
50
51    // get target thread ltid and cxy
52    target_ltid = LTID_FROM_TRDID( trdid );
53    target_cxy  = CXY_FROM_TRDID( trdid );
54
55    // check trdid argument
56        if( (target_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) ) 
57        {
58        printk("\n[ERROR] in %s : illegal trdid argument\n", __FUNCTION__ );
59                this->errno = EINVAL;
60                return -1;
61        }
62
63    // get extended pointer on target thread
64        target_xp  = thread_get_xptr( process->pid , trdid );
65
66    if( target_xp == XPTR_NULL )
67    {
68        printk("\n[ERROR] in %s : target thread not found\n", __FUNCTION__ );
69        this->errno = EINVAL;
70        return -1;
71    }
72
73    // get target thread local pointer
74    target_ptr = (thread_t *)GET_PTR( target_xp );
75
76    // cal the relevant kernel function
77    if( target_cxy == local_cxy )    // target thread is local
78    {
79        thread_kill( target_ptr );
80    }
81    else
82    {
83        rpc_thread_kill_client( target_cxy , target_ptr );
84    }
85
86#if CONFIG_SYSCALL_DEBUG
87tm_end = hal_get_cycles();
88syscall_dmsg("\n[DBG] %s : core[%x,%d] / thread %x in process %x / cycle %d\n"
89"thread %x killed / cost = %d\n",
90__FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid , tm_start ,
91trdid , (uint32_t)(tm_end - tm_start) );
92#endif
93
94        return 0;
95
96}  // end sys_thread_cancel()
Note: See TracBrowser for help on using the repository browser.