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

Last change on this file since 654 was 651, checked in by alain, 5 years ago

1) Improve the VMM MMAP allocator: implement the "buddy" algorithm
to allocate only aligned blocks.
2) fix a bug in the pthread_join() / pthread_exit() mmechanism.

File size: 7.1 KB
Line 
1/*
2 * sys_thread_join.c - wait termination of of an attached 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_remote.h>
26#include <hal_special.h>
27#include <hal_irqmask.h>
28#include <hal_uspace.h>
29#include <thread.h>
30#include <vmm.h>
31#include <scheduler.h>
32#include <errno.h>
33#include <printk.h>
34#include <remote_busylock.h>
35
36#include <syscalls.h>
37
38///////////////////////////////////////
39int sys_thread_join ( trdid_t    trdid,
40                      void    ** exit_status )
41{
42    error_t       error;
43    reg_t         save_sr;
44    xptr_t        target_xp;
45    thread_t    * target_ptr;
46    cxy_t         target_cxy;
47    ltid_t        target_ltid;
48    xptr_t        target_join_lock_xp;
49    xptr_t        target_flags_xp;
50    xptr_t        target_join_xp_xp;
51    xptr_t        target_exit_status_xp;
52    xptr_t        killer_xp;
53    xptr_t        joining_xp;
54    thread_t    * joining_ptr;
55    process_t   * process;
56    vseg_t      * vseg;
57    void        * status;
58
59    // get joining thread pointers
60        joining_ptr = CURRENT_THREAD;
61    joining_xp  = XPTR( local_cxy , joining_ptr );
62    process     = joining_ptr->process;
63
64    // get target thread ltid and cxy
65    target_ltid = LTID_FROM_TRDID( trdid );
66    target_cxy  = CXY_FROM_TRDID( trdid );
67
68#if DEBUG_SYS_THREAD_JOIN
69uint64_t  tm_start = hal_get_cycles();
70if( DEBUG_SYS_THREAD_JOIN < tm_start )
71printk("\n[%s] joining thread[%x,%x] enter / target thread[%x,%x] / cycle %d\n",
72__FUNCTION__ , process->pid, joining_ptr->trdid,
73process->pid, trdid, (uint32_t)tm_start );
74#endif
75
76    // check trdid argument
77        if( (target_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || 
78        (cluster_is_active(target_cxy) == false) )
79        {
80
81#if DEBUG_SYSCALLS_ERROR
82printk("\n[ERROR] in %s : illegal trdid argument %x / joining thread[%x,%x]\n",
83__FUNCTION__, trdid, joining_ptr->process->pid, joining_ptr-trdid );
84#endif
85                joining_ptr->errno = EINVAL;
86                return -1;
87        }
88
89    // check exit_value argument in user space
90    error = vmm_get_vseg( process , (intptr_t)exit_status  , &vseg );
91        if( error )
92        {
93
94#if DEBUG_SYSCALLS_ERROR
95printk("\n[ERROR] in %s : exit_status argument %x not mapped / joining thread[%x,%x]\n",
96__FUNCTION__, exit_status, joining_ptr->process->pid, joining_ptr-trdid );
97#endif
98                joining_ptr->errno = EINVAL;
99                return -1;
100        }
101
102    // check target thread != this thread
103    if( joining_ptr->trdid == trdid )
104    {
105
106#if DEBUG_SYSCALLS_ERROR
107printk("\n[ERROR] in %s : joinig thread[%x,%x] == target thread\n",
108__FUNCTION__, joining_ptr->process->pid, joining_ptr->trdid );
109#endif
110        joining_ptr->errno = EDEADLK;
111        return -1;
112    }
113
114    // get pointers on target thread
115        target_xp  = thread_get_xptr( process->pid , trdid );
116    target_ptr = GET_PTR( target_xp );
117
118    if( target_xp == XPTR_NULL )
119    {
120
121#if DEBUG_SYSCALLS_ERROR
122printk("\n[ERROR] in %s : target thread[%x,%x] not found / joining_thread[%x,%x]\n",
123__FUNCTION__, process->pid, trdid, joining_ptr->process->pid, joining_ptr-trdid );
124#endif
125        joining_ptr->errno = ESRCH;
126        return -1;
127    }
128
129    // get extended pointers on various fields in target thread
130    target_join_lock_xp   = XPTR( target_cxy , &target_ptr->join_lock );
131    target_flags_xp       = XPTR( target_cxy , &target_ptr->flags );
132    target_join_xp_xp     = XPTR( target_cxy , &target_ptr->join_xp );
133    target_exit_status_xp = XPTR( target_cxy , &target_ptr->exit_status );
134
135    // check target thread joinable
136    if( (hal_remote_l32( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0 )
137    {
138
139#if DEBUG_SYSCALLS_ERROR
140printk("\n[ERROR] in %s : target thread[%x,‰x] not attached / joining thread[%x,%x]\n",
141__FUNCTION__, process->pid, trdid, joining_ptr->process->pid, joining_ptr-trdid );
142#endif
143        joining_ptr->errno = EINVAL; 
144        return -1;
145    }
146
147    // mask IRQs
148    hal_disable_irq( &save_sr );
149
150    // get the lock protecting the join in target thread
151    remote_busylock_acquire( target_join_lock_xp );
152
153    // test the kill_done flag from the target thread
154    if( hal_remote_l32( target_flags_xp ) & THREAD_FLAG_KILL_DONE )  // killer is first
155    {
156        // get exit_status from target thread
157        status = (void*)hal_remote_lpt( target_exit_status_xp );
158
159        // get pointers on killer thread
160        killer_xp  = (xptr_t)hal_remote_l64( target_join_xp_xp );
161
162        // reset the kill_done flag in target thread
163        hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_KILL_DONE );
164
165        // unblock the killer thread
166        thread_unblock( killer_xp , THREAD_BLOCKED_JOIN );
167
168        // release the lock protecting join     
169        remote_busylock_release( target_join_lock_xp );
170    }
171    else                                                          // joining is first
172    {
173        // set the join_done flag in target thread
174        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_JOIN_DONE );
175
176        // block joining thread on BLOCKED_JOIN
177        thread_block( joining_xp , THREAD_BLOCKED_JOIN );
178
179        // register the joining thread extended pointer in target thread
180        hal_remote_s64( target_join_xp_xp , joining_xp );
181
182        // release the lock protecting the join     
183        remote_busylock_release( target_join_lock_xp );
184
185#if DEBUG_SYS_THREAD_JOIN
186if( DEBUG_SYS_THREAD_JOIN < tm_start )
187printk("\n[%s] joining thread[%x,%x] deschedules / target thread[%x,%x] not completed\n",
188__FUNCTION__ , process->pid, joining_ptr->trdid, process->pid, trdid );
189#endif
190        // deschedule
191        sched_yield( "joining thread waiting killer thread" );
192
193        // returns exit_status from joining thread
194        status = joining_ptr->exit_status;
195    }
196   
197    // returns exit_status to user space
198    hal_copy_to_uspace( exit_status,
199                        XPTR( local_cxy , &status ),
200                        sizeof( void* ) );
201
202    // restore IRQs
203    hal_restore_irq( save_sr );
204
205    hal_fence();
206
207#if (DEBUG_SYS_THREAD_JOIN || CONFIG_INSTRUMENTATION_SYSCALLS)
208uint64_t     tm_end = hal_get_cycles();
209#endif
210
211#if CONFIG_INSTRUMENTATION_SYSCALLS
212hal_atomic_add( &syscalls_cumul_cost[SYS_THREAD_JOIN] , tm_end - tm_start );
213hal_atomic_add( &syscalls_occurences[SYS_THREAD_JOIN] , 1 );
214#endif
215
216#if DEBUG_SYS_THREAD_JOIN
217tm_end = hal_get_cycles();
218if( DEBUG_SYS_THREAD_JOIN < tm_end )
219printk("\n[%s] joining thread[%x,%x] exit / target thread[%x,%x] completed / cycle %d\n",
220__FUNCTION__ , process->pid, joining_ptr->trdid, process->pid, trdid, (uint32_t)tm_end );
221#endif
222
223    return 0;
224
225}  // end sys_thread_join()
Note: See TracBrowser for help on using the repository browser.