source: trunk/kernel/syscalls/sys_thread_exit.c

Last change on this file was 683, checked in by alain, 12 months ago

All modifications required to support the <tcp_chat> application
including error recovery in case of packet loss.A

File size: 3.6 KB
RevLine 
[1]1/*
[409]2 * sys_thread_exit.c - terminates the execution of calling thread
[1]3 *
[683]4 * Authors   Alain Greiner (2016,2017,2018,2019,2020)
[1]5 *
[23]6 * Copyright (c) UPMC Sorbonne Universites
[1]7 *
[23]8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]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 *
[23]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]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
[23]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]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>
[1]26#include <thread.h>
[440]27#include <process.h>
[23]28#include <core.h>
[409]29#include <vmm.h>
[1]30#include <scheduler.h>
[23]31#include <printk.h>
[1]32
[506]33#include <syscalls.h>
34
[651]35/////////////////////////////////////////
36int sys_thread_exit( void * exit_status )
[1]37{
[651]38    error_t     error;
39    vseg_t    * vseg;
40
[440]41        thread_t  * this      = CURRENT_THREAD;
42    trdid_t     trdid     = this->trdid;
43    process_t * process   = this->process;
44    pid_t       pid       = process->pid;
[651]45   
[683]46#if DEBUG_SYS_THREAD_EXIT || DEBUG_SYSCALLS_ERROR
47uint64_t     tm_start = hal_get_cycles();
48#endif
49
[651]50    // check exit_value pointer in user space if required
51    if( exit_status != NULL )
[436]52    {
[651]53        error = vmm_get_vseg( process , (intptr_t)exit_status  , &vseg );
[436]54
[651]55        if( error )
56            {
57
[438]58#if DEBUG_SYSCALLS_ERROR
[683]59if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
60printk("\n[WARNING] in %s : exit_status buffer %x unmapped / thread[%x,%x]\n",
61__FUNCTION__, (intptr_t)exit_status, pid, trdid );
[436]62#endif
[651]63            this->errno = EINVAL;
64                return -1;
65        }
[409]66    }
[1]67
[651]68        // check busylocks
69    uint32_t count = this->busylocks;
70        if( count )
71        {
72
73#if DEBUG_SYSCALLS_ERROR
[683]74if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 
75printk("\n[WARNING] in %s : busylocks count = %d  / thread[%x,%x]\n",
76__FUNCTION__ , count, pid, trdid );
[651]77#endif
78            this->errno = EINVAL;
79                return -1;
80    }
81
[440]82    // If calling thread is the main thread, the process must be deleted.
[651]83    // => delete all process threads and synchronize with parent process.
84    // If calling thread is not the main thread, only this thread must be deleted.
85    // => register exit_status in thread descriptor, block the thread,
86    //    mark it for delete, and synchronize with joining thread.
87
88    if( (CXY_FROM_PID( pid ) == local_cxy) && (LTID_FROM_TRDID(trdid) == 0) )  // main
[440]89    {
[1]90
[584]91#if DEBUG_SYS_THREAD_EXIT
[683]92if( DEBUG_SYS_THREAD_EXIT < (uint32_t)tm_start ) 
[625]93printk("\n[%s] thread[%x,%x] is main => delete process / cycle %d\n",
[584]94__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
95#endif
96        // delete process
97        sys_exit( 0 );
[440]98    }
[651]99    else                                                             // not the main
[440]100    {
[584]101
102#if DEBUG_SYS_THREAD_EXIT
[683]103if( DEBUG_SYS_THREAD_EXIT < (uint32_t)tm_start )
[625]104printk("\n[%s] thread[%x,%x] is not main => delete thread / cycle %d\n",
[584]105__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
106#endif
[651]107        // register exit_status in thread descriptor
108        this->exit_status = exit_status;
109
[440]110        // block calling thread and mark it for delete,
[670]111        thread_delete_request( XPTR( local_cxy , this ) , false );   // not forced
[584]112
113        // deschedule
114        sched_yield( "suicide after thread_exit" );
[440]115    }
116
[409]117    return 0;   // never executed but required by compiler
118
119}  // end sys_thread exit
Note: See TracBrowser for help on using the repository browser.