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

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

Fix several bugs in the fork() syscall.

File size: 2.7 KB
Line 
1/*
2 * sys_thread_exit.c - terminates the execution of current thread
3 *
4 * Authors       Ghassan Almaless (2008,2009,2010,2011,2012)
5 *               Alain Greiner (2016,2017)
6 *
7 * Copyright (c) UPMC Sorbonne Universites
8 *
9 * This file is part of ALMOS-MKH.
10 *
11 * ALMOS-MKH is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2.0 of the License.
14 *
15 * ALMOS-MKH is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25#include <hal_types.h>
26#include <hal_irqmask.h>
27#include <thread.h>
28#include <core.h>
29#include <scheduler.h>
30#include <printk.h>
31
32
33////////////////////////////////////////
34int sys_thread_exit( void * exit_value )
35{
36        thread_t  * this = CURRENT_THREAD;
37    core_t    * core = this->core;
38    reg_t       irq_state;
39
40    // register the exit_value pointer in thread descriptor
41    this->exit_value = exit_value;
42
43    // enter the join loop to wait the join if thread is joinable
44    if( (this->flags & THREAD_FLAG_DETACHED) == 0 )
45    {
46            while( 1 ) 
47            {
48            // take the lock protecting the flags
49                remote_spinlock_lock( XPTR( local_cxy, &this->flags_lock ) );
50
51            // check the JOIN flag
52            if( this->flags & THREAD_FLAG_JOIN )       // parent made a join
53            {
54                // unblock the parent thread
55                thread_unblock( this->parent , THREAD_BLOCKED_JOIN );
56
57                // release the lock protecting the flags
58                    remote_spinlock_unlock( XPTR( local_cxy, &this->flags_lock ) );
59
60                // exit while
61                break;
62            }
63            else                                       // no join done by parent thread
64            {
65                // set the EXIT flag
66                this->flags |= THREAD_FLAG_EXIT;
67
68                // block this thread
69                thread_block( this , THREAD_BLOCKED_EXIT );
70
71                // release the lock protecting the flags
72                    remote_spinlock_unlock( XPTR( local_cxy, &this->flags_lock ) );
73
74                // deschedule
75                sched_yield("waiting parent join");
76            }     
77        }
78        }
79
80        // Release FPU if required
81        hal_disable_irq( &irq_state );
82        if( core->fpu_owner == this )  core->fpu_owner = NULL;
83        hal_restore_irq( irq_state );
84
85        // suicide
86    thread_kill( this );
87        return 0;       
88
89}  // end sys_thread_exit()
Note: See TracBrowser for help on using the repository browser.