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

Last change on this file since 615 was 584, checked in by alain, 6 years ago

Introduce sys_place_fork() function.

File size: 2.7 KB
Line 
1/*
2 * sys_thread_exit.c - terminates the execution of calling thread
3 *
4 * Authors   Alain Greiner (2016,2017,2018)
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_value )
37{
38        thread_t  * this      = CURRENT_THREAD;
39    trdid_t     trdid     = this->trdid;
40    process_t * process   = this->process;
41    pid_t       pid       = process->pid;
42
43    // check exit_value argument
44    if( exit_value != NULL )
45    {
46
47#if DEBUG_SYSCALLS_ERROR
48printk("\n[ERROR] in %s : exit_value argument must be NULL / thread %x in process %x\n",
49__FUNCTION__ , this , pid );
50#endif
51        this->errno = EINVAL;
52        return -1;
53    }
54
55
56    // If calling thread is the main thread, the process must be deleted.
57    // => delete all process threads and synchronise with parent process.
58    // If calling thread is not the main thread, it must be deleted.
59    // => block the thread and mark it for delete.
60    if( (CXY_FROM_PID( pid ) == local_cxy) && (LTID_FROM_TRDID(trdid) == 0) )
61    {
62
63#if DEBUG_SYS_THREAD_EXIT
64uint64_t     tm_start = hal_get_cycles();
65if( DEBUG_SYS_THREAD_EXIT < tm_start )
66printk("\n[DBG] %s : thread[%x,%x] / main => delete process / cycle %d\n",
67__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
68#endif
69        // delete process
70        sys_exit( 0 );
71    }
72    else
73    {
74
75#if DEBUG_SYS_THREAD_EXIT
76uint64_t     tm_start = hal_get_cycles();
77if( DEBUG_SYS_THREAD_EXIT < tm_start )
78printk("\n[DBG] %s : thread[%x,%x] / not main => delete thread / cycle %d\n",
79__FUNCTION__ , pid , trdid , (uint32_t)tm_start );
80#endif
81        // block calling thread and mark it for delete,
82        thread_delete( XPTR( local_cxy , this ) , pid , false );
83
84        // deschedule
85        sched_yield( "suicide after thread_exit" );
86    }
87
88    return 0;   // never executed but required by compiler
89
90}  // end sys_thread exit
Note: See TracBrowser for help on using the repository browser.