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

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

cosmetic

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