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

Last change on this file since 654 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.5 KB
RevLine 
[1]1/*
[409]2 * sys_thread_exit.c - terminates the execution of calling thread
[1]3 *
[635]4 * Authors   Alain Greiner (2016,2017,2018,2019)
[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   
46    // check exit_value pointer in user space if required
47    if( exit_status != NULL )
[436]48    {
[651]49        error = vmm_get_vseg( process , (intptr_t)exit_status  , &vseg );
[436]50
[651]51        if( error )
52            {
53
[438]54#if DEBUG_SYSCALLS_ERROR
[651]55printk("\n[ERROR] in %s : exit_status buffer %x unmapped / thread[%x,%x]\n",
56__FUNCTION__, (intptr_t)exit_status, process->pid, this->trdid );
[436]57#endif
[651]58            this->errno = EINVAL;
59                return -1;
60        }
[409]61    }
[1]62
[651]63        // check busylocks
64    uint32_t count = this->busylocks;
65        if( count )
66        {
67
68#if DEBUG_SYSCALLS_ERROR
69printk("\n[ERROR] in %s : busylocks count = %d  / thread[%x,%x]\n",
70__FUNCTION__ , count, process->pid, this->trdid );
71#endif
72            this->errno = EINVAL;
73                return -1;
74    }
75
[440]76    // If calling thread is the main thread, the process must be deleted.
[651]77    // => delete all process threads and synchronize with parent process.
78    // If calling thread is not the main thread, only this thread must be deleted.
79    // => register exit_status in thread descriptor, block the thread,
80    //    mark it for delete, and synchronize with joining thread.
81
82    if( (CXY_FROM_PID( pid ) == local_cxy) && (LTID_FROM_TRDID(trdid) == 0) )  // main
[440]83    {
[1]84
[584]85#if DEBUG_SYS_THREAD_EXIT
86uint64_t     tm_start = hal_get_cycles();
87if( DEBUG_SYS_THREAD_EXIT < tm_start )
[625]88printk("\n[%s] thread[%x,%x] is main => delete process / cycle %d\n",
[584]89__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
90#endif
91        // delete process
92        sys_exit( 0 );
[440]93    }
[651]94    else                                                             // not the main
[440]95    {
[584]96
97#if DEBUG_SYS_THREAD_EXIT
98uint64_t     tm_start = hal_get_cycles();
99if( DEBUG_SYS_THREAD_EXIT < tm_start )
[625]100printk("\n[%s] thread[%x,%x] is not main => delete thread / cycle %d\n",
[584]101__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
102#endif
[651]103        // register exit_status in thread descriptor
104        this->exit_status = exit_status;
105
[440]106        // block calling thread and mark it for delete,
[651]107        thread_delete( XPTR( local_cxy , this ) , false );   // not forced
[584]108
109        // deschedule
110        sched_yield( "suicide after thread_exit" );
[440]111    }
112
[409]113    return 0;   // never executed but required by compiler
114
115}  // end sys_thread exit
Note: See TracBrowser for help on using the repository browser.