source: trunk/kernel/syscalls/sys_thread_exit.c @ 433

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

blip

File size: 3.5 KB
Line 
1/*
2 * sys_thread_exit.c - terminates the execution of calling thread
3 *
4 * Authors   Alain Greiner (2016,2017)
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_types.h>
25#include <thread.h>
26#include <core.h>
27#include <vmm.h>
28#include <scheduler.h>
29#include <printk.h>
30
31////////////////////////////////////////
32int sys_thread_exit( void * exit_value )
33{
34    paddr_t      paddr;
35    error_t          error;
36
37#if CONFIG_SYSCALL_DEBUG
38uint32_t     tm_start;
39uint32_t     tm_end;
40tm_start = hal_get_cycles();
41#endif
42
43        thread_t  * this    = CURRENT_THREAD;
44    process_t * process = this->process;
45
46    // check all locks released
47        if( !thread_can_yield() )
48        {
49        printk("\n[ERROR] in %s : locks not released / thread %x in process %x\n",
50        __FUNCTION__, this->trdid, process->pid );
51        this->errno = EINVAL;
52        return -1;
53    }
54
55    // register the exit_value pointer in this thread descriptor
56    this->join_value = exit_value;
57
58    if( (this->flags & THREAD_FLAG_DETACHED) == 0 )    // this thread is joinable
59    {
60        // check exit_value in user space
61        error = vmm_v2p_translate( false , exit_value , &paddr );
62            if( error )
63        {
64            printk("\n[ERROR] in %s : illegal pointer = %x / thread %x in process %x\n",
65            __FUNCTION__ , (intptr_t)exit_value, this->trdid , process->pid );
66            this->errno = EINVAL;
67            return -1;
68        }
69
70        // take the lock protecting the join
71        remote_spinlock_lock( XPTR( local_cxy, &this->join_lock ) );
72
73        if( this->flags & THREAD_FLAG_JOIN_DONE )       // parent thread arrived first
74        {
75            // unblock the parent thread
76            thread_unblock( this->join_xp , THREAD_BLOCKED_EXIT );
77
78            // reset the JOIN_DONE flag in this thread
79            this->flags &= ~THREAD_FLAG_JOIN_DONE;
80
81            // release the lock protecting the flags
82                remote_spinlock_unlock( XPTR( local_cxy, &this->join_lock ) );
83        }
84        else                                           // this thread arrived first
85        {
86            // block this thread
87            thread_block( this , THREAD_BLOCKED_JOIN );
88
89            // release the lock protecting the join
90                remote_spinlock_unlock( XPTR( local_cxy, &this->join_lock ) );
91
92            // deschedule
93            sched_yield( "WAITING JOIN" );
94        }     
95    }
96
97#if CONFIG_SYSCALL_DEBUG
98tm_end = hal_get_cycles();
99syscall_dmsg("\n[DBG] %s : core[%x,%d] / thread %x in process %x / cycle %d\n"
100"thread %x killed / cost = %d\n",
101__FUNCTION__ , local_cxy , this->core->lid , this->trdid , this->process->pid , tm_start ,
102this->trdid , (uint32_t)(tm_end - tm_start) );
103#endif
104
105    // suicide using a rpc because  a thread cannot kill itself
106    rpc_thread_kill_client( local_cxy , this );
107
108    return 0;   // never executed but required by compiler
109
110}  // end sys_thread exit
Note: See TracBrowser for help on using the repository browser.