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