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

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

Fix several bugs in the fork() syscall.

File size: 4.7 KB
Line 
1/*
2 * sys_thread_join.c - passive wait on the end of a given thread.
3 *
4 * Authors    Alain Greiner (2016,2017)
5 *
6 * Copyright (c) 2011,2012 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_types.h>
25#include <hal_remote.h>
26#include <hal_special.h>
27#include <thread.h>
28#include <vmm.h>
29#include <scheduler.h>
30#include <errno.h>
31#include <printk.h>
32#include <remote_spinlock.h>
33
34///////////////////////////////////////
35int sys_thread_join ( trdid_t    trdid,
36                      void    ** exit_value )
37{
38    xptr_t        target_xp;
39    thread_t    * target_ptr;
40    cxy_t         target_cxy;
41    ltid_t        target_ltid;
42    uint32_t      flags;        // target thread flags
43    intptr_t      value;        // value returned by target thread
44    paddr_t       paddr;        // required for vmm_v2p_translate()
45
46        thread_t  * this    = CURRENT_THREAD;
47    process_t * process = this->process;
48
49    // get target thread ltid and cxy
50    target_ltid = LTID_FROM_TRDID( trdid );
51    target_cxy  = CXY_FROM_TRDID( trdid );
52
53    // check trdid argument
54        if( (target_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) ) 
55        {
56        printk("\n[ERROR] in %s : illegal trdid argument\n", __FUNCTION__ );
57                this->errno = EINVAL;
58                return -1;
59        }
60
61    // check exit_value argument
62        if( (exit_value != NULL) && (vmm_v2p_translate( false , exit_value , &paddr ) != 0 ) )
63        {
64        printk("\n[ERROR] in %s : illegal exit_value argument\n", __FUNCTION__ );
65                this->errno = EINVAL;
66                return -1;
67        }
68
69    // check target thread != this thread
70    if( this->trdid == trdid )
71    {
72        printk("\n[ERROR] in %s : this thread == target thread\n", __FUNCTION__ );
73        this->errno = EDEADLK;
74        return -1;
75    }
76
77    // get extended pointer on target thread
78        target_xp  = thread_get_xptr( process->pid , trdid );
79
80    if( target_xp == XPTR_NULL )
81    {
82        printk("\n[ERROR] in %s : target thread not found\n", __FUNCTION__ );
83        this->errno = ESRCH;
84        return -1;
85    }
86
87    // get cluster and local pointer on target thread
88    target_ptr = (thread_t *)GET_PTR( target_xp );
89
90    // check target thread joinable
91    flags = hal_remote_lw( XPTR( target_cxy , &target_ptr->flags ) );
92    if( flags & THREAD_FLAG_DETACHED )
93    {
94        printk("\n[ERROR] in %s : target thread not joinable\n", __FUNCTION__ );
95        this->errno = EINVAL; 
96        return -1;
97    }
98
99    // check kernel stack overflow
100    if( target_ptr->signature != THREAD_SIGNATURE )
101    {
102        printk("\n[PANIC] in %s : kernel stack overflow\n", __FUNCTION__ );
103        hal_core_sleep();
104    }
105
106    // wait target thread exit
107    while( 1 )
108    {
109        // take the target thread lock protecting flags     
110        remote_spinlock_lock( XPTR( target_cxy , &target_ptr->flags_lock ) );
111
112        // get the remote thread flags
113        flags = hal_remote_lw( XPTR( target_cxy , &target_ptr->flags ) );
114
115        // check the EXIT flag
116        if( flags & THREAD_FLAG_EXIT )   // target made an exit
117        {
118            // unblock the target thread
119            thread_unblock( target_xp , THREAD_BLOCKED_EXIT );
120
121            // release the target thread lock protecting flags     
122            remote_spinlock_unlock( XPTR( target_cxy , &target_ptr->flags_lock ) );
123
124            // exit while
125            break;
126        }
127        else                             // no exit done by target thread
128        {
129            // set the JOIN flag in target thread
130            hal_remote_atomic_or( XPTR( target_xp , &target_ptr->flags ) , 
131                                  THREAD_BLOCKED_JOIN );
132
133            // block this thread
134            thread_block( this , THREAD_BLOCKED_JOIN );
135
136            // release the target thread lock protecting flags
137                remote_spinlock_unlock( XPTR( target_cxy , &target_ptr->flags_lock ) );
138
139            // deschedule
140            sched_yield("waiting child exit");
141        }
142    }
143
144    // return exit_value from target thread descriptor
145    value = (intptr_t)hal_remote_lpt( XPTR( target_cxy , &target_ptr->exit_value ) );
146    *exit_value = (void *)value;
147    return 0;
148
149}  // end sys_thread_join()
Note: See TracBrowser for help on using the repository browser.