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

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

This version modifies the exec syscall and fixes a large number of small bugs.
The version number has been updated (0.1)

File size: 5.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_kernel_types.h>
25#include <hal_remote.h>
26#include <hal_special.h>
27#include <hal_irqmask.h>
28#include <thread.h>
29#include <vmm.h>
30#include <scheduler.h>
31#include <errno.h>
32#include <printk.h>
33#include <remote_spinlock.h>
34
35///////////////////////////////////////
36int sys_thread_join ( trdid_t    trdid,
37                      void    ** exit_value )
38{
39    reg_t         save_sr;
40    xptr_t        target_xp;
41    thread_t    * target_ptr;
42    cxy_t         target_cxy;
43    ltid_t        target_ltid;
44    xptr_t        target_join_lock_xp;
45    xptr_t        target_flags_xp;
46    xptr_t        target_blocked_xp;
47    xptr_t        target_join_xp_xp;
48    xptr_t        killer_xp;
49    xptr_t        joining_xp;
50    thread_t    * joining_ptr;
51    process_t   * process;
52
53    // get joining thread pointers
54        joining_ptr = CURRENT_THREAD;
55    joining_xp  = XPTR( local_cxy , joining_ptr );
56    process     = joining_ptr->process;
57
58    // get target thread ltid and cxy
59    target_ltid = LTID_FROM_TRDID( trdid );
60    target_cxy  = CXY_FROM_TRDID( trdid );
61
62#if DEBUG_SYS_THREAD_JOIN
63uint64_t     tm_start;
64uint64_t     tm_end;
65tm_start = hal_get_cycles();
66if( DEBUG_SYS_THREAD_JOIN < tm_start )
67printk("\n[DBG] %s : parent thread %x enter / process %x / target trdid %x / cycle %d\n",
68__FUNCTION__ , joining_ptr , process->pid , trdid , (uint32_t)tm_start );
69#endif
70
71    // check trdid argument
72        if( (target_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) ) 
73        {
74
75#if DEBUG_SYSCALLS_ERROR
76printk("\n[ERROR] in %s : illegal trdid argument %x\n", __FUNCTION__, trdid );
77#endif
78                joining_ptr->errno = EINVAL;
79                return -1;
80        }
81
82    // check exit_value argument
83        if( exit_value != NULL )
84        {
85
86#if DEBUG_SYSCALLS_ERROR
87printk("\n[ERROR] in %s : exit_value argument must be NULL\n", __FUNCTION__ );
88#endif
89                joining_ptr->errno = EINVAL;
90                return -1;
91        }
92
93    // check target thread != this thread
94    if( joining_ptr->trdid == trdid )
95    {
96
97#if DEBUG_SYSCALLS_ERROR
98printk("\n[ERROR] in %s : this thread == target thread\n", __FUNCTION__ );
99#endif
100        joining_ptr->errno = EDEADLK;
101        return -1;
102    }
103
104    // get pointers on target thread
105        target_xp  = thread_get_xptr( process->pid , trdid );
106    target_ptr = GET_PTR( target_xp );
107
108    if( target_xp == XPTR_NULL )
109    {
110
111#if DEBUG_SYSCALLS_ERROR
112printk("\n[ERROR] in %s : target thread %x not found\n", __FUNCTION__, trdid );
113#endif
114        joining_ptr->errno = ESRCH;
115        return -1;
116    }
117
118    // get extended pointers on various fields in target thread
119    target_join_lock_xp = XPTR( target_cxy , &target_ptr->join_lock );
120    target_flags_xp     = XPTR( target_cxy , &target_ptr->flags );
121    target_blocked_xp   = XPTR( target_cxy , &target_ptr->blocked );
122    target_join_xp_xp   = XPTR( target_cxy , &target_ptr->join_xp );
123
124    // check target thread joinable
125    if( (hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) == 0 )
126    {
127
128#if DEBUG_SYSCALLS_ERROR
129printk("\n[ERROR] in %s : target thread %x not joinable\n", __FUNCTION__, trdid );
130#endif
131        joining_ptr->errno = EINVAL; 
132        return -1;
133    }
134
135    // mask IRQs
136    hal_disable_irq( &save_sr );
137
138    // get the lock protecting the join in target thread
139    remote_spinlock_lock( target_join_lock_xp );
140
141    // test the kill_done flag from the target thread
142    if( hal_remote_lw( target_flags_xp ) & THREAD_FLAG_KILL_DONE )  // killer thread is first
143    {
144        // get pointers on killer thread
145        killer_xp  = (xptr_t)hal_remote_lwd( target_join_xp_xp );
146
147        // reset the kill_done flag in target thread
148        hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_KILL_DONE );
149
150        // unblock the killer thread
151        thread_unblock( killer_xp , THREAD_BLOCKED_JOIN );
152
153        // release the lock protecting join     
154        remote_spinlock_unlock( target_join_lock_xp );
155
156        // restore IRQs
157        hal_restore_irq( save_sr );
158    }
159    else                                                          // joining thread is first
160    {
161        // set the join_done flag in target thread
162        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_JOIN_DONE );
163
164        // block joining thread on BLOCKED_JOIN
165        thread_block( joining_xp , THREAD_BLOCKED_JOIN );
166
167        // register the joining thread extended pointer in target thread
168        hal_remote_swd( target_join_xp_xp , joining_xp );
169
170        // release the lock protecting the join     
171        remote_spinlock_unlock( target_join_lock_xp );
172
173        // deschedule
174        sched_yield( "joining thread waiting killer thread" );
175   
176        // restore IRQs
177        hal_restore_irq( save_sr );
178    }
179
180#if DEBUG_SYS_THREAD_JOIN
181tm_end = hal_get_cycles();
182if( DEBUG_SYS_THREAD_JOIN < tm_end )
183printk("\n[DBG] %s : parent thread %x exit / process %x / target trdid %x / cycle %d\n",
184__FUNCTION__, joining_ptr, process->pid, trdid, (uint32_t)tm_end );
185#endif
186
187    return 0;
188
189}  // end sys_thread_join()
Note: See TracBrowser for help on using the repository browser.