source: trunk/kernel/syscalls/sys_thread_exit.c

Last change on this file was 683, checked in by alain, 11 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
Line 
1/*
2 * sys_thread_exit.c - terminates the execution of calling thread
3 *
4 * Authors   Alain Greiner (2016,2017,2018,2019,2020)
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 <thread.h>
27#include <process.h>
28#include <core.h>
29#include <vmm.h>
30#include <scheduler.h>
31#include <printk.h>
32
33#include <syscalls.h>
34
35/////////////////////////////////////////
36int sys_thread_exit( void * exit_status )
37{
38    error_t     error;
39    vseg_t    * vseg;
40
41        thread_t  * this      = CURRENT_THREAD;
42    trdid_t     trdid     = this->trdid;
43    process_t * process   = this->process;
44    pid_t       pid       = process->pid;
45   
46#if DEBUG_SYS_THREAD_EXIT || DEBUG_SYSCALLS_ERROR
47uint64_t     tm_start = hal_get_cycles();
48#endif
49
50    // check exit_value pointer in user space if required
51    if( exit_status != NULL )
52    {
53        error = vmm_get_vseg( process , (intptr_t)exit_status  , &vseg );
54
55        if( error )
56            {
57
58#if DEBUG_SYSCALLS_ERROR
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 );
62#endif
63            this->errno = EINVAL;
64                return -1;
65        }
66    }
67
68        // check busylocks
69    uint32_t count = this->busylocks;
70        if( count )
71        {
72
73#if DEBUG_SYSCALLS_ERROR
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 );
77#endif
78            this->errno = EINVAL;
79                return -1;
80    }
81
82    // If calling thread is the main thread, the process must be deleted.
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
89    {
90
91#if DEBUG_SYS_THREAD_EXIT
92if( DEBUG_SYS_THREAD_EXIT < (uint32_t)tm_start ) 
93printk("\n[%s] thread[%x,%x] is main => delete process / cycle %d\n",
94__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
95#endif
96        // delete process
97        sys_exit( 0 );
98    }
99    else                                                             // not the main
100    {
101
102#if DEBUG_SYS_THREAD_EXIT
103if( DEBUG_SYS_THREAD_EXIT < (uint32_t)tm_start )
104printk("\n[%s] thread[%x,%x] is not main => delete thread / cycle %d\n",
105__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
106#endif
107        // register exit_status in thread descriptor
108        this->exit_status = exit_status;
109
110        // block calling thread and mark it for delete,
111        thread_delete_request( XPTR( local_cxy , this ) , false );   // not forced
112
113        // deschedule
114        sched_yield( "suicide after thread_exit" );
115    }
116
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.