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

Last change on this file since 421 was 421, checked in by alain, 9 years ago

Introduce sys_fg() , sys_display() , sys_wait() syscalls.

File size: 4.9 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 target_blocked; // target thread blocked bit-vector
43 uint32_t target_flags; // target thread flags bit-bector
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 target_flags = hal_remote_lw( XPTR( target_cxy , &target_ptr->flags ) );
92 if( target_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 assert( false , __FUNCTION__ , "kernel stack overflow\n" );
103 }
104
105 // get the lock protecting the join in target thread
106 remote_spinlock_lock( XPTR( target_cxy , &target_ptr->join_lock ) );
107
108 // get the blocked bit_vector from the target thread
109 target_blocked = hal_remote_lw( XPTR( target_cxy , &target_ptr->blocked ) );
110
111 if( target_blocked & THREAD_BLOCKED_JOIN ) // target thread arrived first
112 {
113 // unblock the target thread
114 thread_unblock( target_xp , THREAD_BLOCKED_JOIN );
115
116 // release the lock protecting flags
117 remote_spinlock_unlock( XPTR( target_cxy , &target_ptr->join_lock ) );
118
119 // get the exit value from target thread
120 *exit_value = hal_remote_lpt( XPTR( target_cxy , &target_ptr->join_value ) );
121 }
122 else // this thread arrived first
123 {
124 // register this thread extended pointer in target thread
125 hal_remote_swd( XPTR( target_cxy , &target_ptr->join_xp ) ,
126 XPTR( local_cxy , this ) );
127
128 // set the JOIN_DONE flag in target thread
129 hal_remote_atomic_or( XPTR( target_cxy , &target_ptr->flags ) ,
130 THREAD_FLAG_JOIN_DONE );
131
132 // block this thread on BLOCKED_EXIT
133 thread_block( this , THREAD_BLOCKED_EXIT );
134
135 // release the lock protecting flags
136 remote_spinlock_unlock( XPTR( target_cxy , &target_ptr->join_lock ) );
137
138 // deschedule
139 sched_yield( "WAITING_EXIT" );
140
141 // get the exit value from target thread when resume
142 *exit_value = hal_remote_lpt( XPTR( target_cxy , &target_ptr->join_value ) );
143 }
144
145 return 0;
146
147} // end sys_thread_join()
Note: See TracBrowser for help on using the repository browser.