source: trunk/kernel/syscalls/sys_wait.c @ 609

Last change on this file since 609 was 566, checked in by alain, 6 years ago

Complete restructuration of kernel locks.

File size: 6.8 KB
Line 
1/*
2 * sys_wait.c - wait termination or blocking of a child process.
3 *
4 * Author    Alain Greiner (2016,2017,2018)
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_uspace.h>
26#include <hal_irqmask.h>
27#include <remote_queuelock.h>
28#include <core.h>
29#include <thread.h>
30#include <process.h>
31#include <vmm.h>
32#include <printk.h>
33
34#include <syscalls.h>
35
36/////////////////////////////////
37int sys_wait( uint32_t * status )
38{
39        error_t     error;
40    vseg_t    * vseg;
41    xptr_t      iter_xp;
42    xptr_t      child_xp;
43    process_t * child_ptr;
44    cxy_t       child_cxy;
45    pid_t       child_pid;
46    uint32_t    child_state;
47    thread_t  * child_thread;
48    reg_t       save_sr;
49
50    thread_t  * this    = CURRENT_THREAD;
51    process_t * process = this->process;
52    pid_t       pid     = process->pid;
53
54#if DEBUG_SYS_WAIT
55uint64_t    cycle = hal_get_cycles();
56if( DEBUG_SYS_WAIT < cycle )
57printk("\n[DBG] %s : thread %x in process %x enter / cycle %d\n",
58__FUNCTION__, this, process->pid, (uint32_t)cycle );
59#endif
60
61    // check status in user space
62    error = vmm_get_vseg( process, (intptr_t)status , &vseg );
63
64        if( error )
65        {
66
67#if DEBUG_SYSCALLS_ERROR
68printk("\n[ERROR] in %s : status buffer %x unmapped for thread %x in process %x\n",
69__FUNCTION__ , (intptr_t)status, this->trdid , process->pid );
70vmm_display( process , false );
71#endif
72        this->errno = EINVAL;
73                return -1;
74        }
75
76    // get calling process owner cluster
77    cxy_t   owner_cxy = CXY_FROM_PID( pid );
78
79    // get calling thread trdid
80    trdid_t trdid = this->trdid;
81
82    // this function must be executed by the process main thread
83    if( (owner_cxy != local_cxy) || (LTID_FROM_TRDID(trdid) != 0) )
84    {
85
86#if DEBUG_SYSCALLS_ERROR
87printk("\n[ERROR] in %s : calling thread %x is not thread 0 in owner cluster %x\n",
88__FUNCTION__ , trdid , owner_cxy );
89#endif
90        this->errno = EINVAL;
91                return -1;
92        }
93
94    // get extended pointer on children list root and lock
95    xptr_t children_root_xp = XPTR( owner_cxy , &process->children_root );
96    xptr_t children_lock_xp = XPTR( owner_cxy , &process->children_lock );
97
98    // exit this loop only when a child processes change state
99    while( 1 )
100    {
101        // enable IRQS
102        hal_enable_irq( &save_sr );
103 
104        // get lock protecting children list
105        remote_queuelock_acquire( children_lock_xp );
106
107        // scan the list of child process
108        XLIST_FOREACH( children_root_xp , iter_xp )
109        {
110            // get child process owner cluster and local pointer
111            child_xp  = XLIST_ELEMENT( iter_xp , process_t , children_list );
112            child_ptr = GET_PTR( child_xp );
113            child_cxy = GET_CXY( child_xp );
114
115            // get PID, term_state, and main thread from child process
116            child_pid    = hal_remote_l32 (XPTR( child_cxy , &child_ptr->pid ));
117            child_state  = hal_remote_l32 ( XPTR(child_cxy , &child_ptr->term_state ) );
118            child_thread = hal_remote_lpt(XPTR( child_cxy , &child_ptr->th_tbl[0] ));
119
120#if (DEBUG_SYS_WAIT & 1)
121cycle = hal_get_cycles();
122if( DEBUG_SYS_WAIT < cycle )
123printk("\n[DBG] %s : thread %x in process %x check child %x / state %x\n",
124__FUNCTION__, this, process->pid, child_pid, child_state );
125#endif
126            // test if this child process is terminated,
127            // but termination not yet reported to parent process
128            if( ((child_state & PROCESS_TERM_EXIT)  || 
129                 (child_state & PROCESS_TERM_KILL)  ||
130                 (child_state & PROCESS_TERM_STOP)) &&
131                 ((child_state & PROCESS_TERM_WAIT) == 0) )
132            {
133                // set the PROCESS_TERM_WAIT in child process descriptor
134                hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ),
135                                      PROCESS_TERM_WAIT );
136
137                // set the THREAD_FLAG_REQ_DELETE in main thread if kill or exit
138                if((child_state & PROCESS_TERM_EXIT) || (child_state & PROCESS_TERM_KILL))
139                hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) ,
140                                            THREAD_FLAG_REQ_DELETE );
141
142                // release lock protecting children list
143                remote_queuelock_release( children_lock_xp );
144
145#if DEBUG_SYS_WAIT
146cycle = hal_get_cycles();
147if( DEBUG_SYS_WAIT < cycle )
148{
149    if     ( child_state & PROCESS_TERM_EXIT )
150        printk("\n[DBG] %s : thread %x in process %x exit / child %x exit / cycle %d\n",
151        __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle );
152    if( child_state & PROCESS_TERM_KILL )
153        printk("\n[DBG] %s : thread %x in process %x exit / child %x killed / cycle %d\n",
154        __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle );
155    if( child_state & PROCESS_TERM_STOP )
156        printk("\n[DBG] %s : thread %x in process %x exit / child %x stopped / cycle %d\n",
157        __FUNCTION__, this, process->pid, child_pid, (uint32_t)cycle );
158}
159#endif
160                 // return child termination state  to parent process
161                 hal_copy_to_uspace( status , &child_state , sizeof(int) );
162                 return child_pid;
163            }
164        }  // end loop on children
165       
166        // we execute this code when no child terminated:
167        // - release the lock protecting children list,
168        // - block on the WAIT condition
169        // - deschedule to keep waiting in the while loop
170
171        // release lock protecting children list
172        remote_queuelock_release( children_lock_xp );
173
174        // block on WAIT condition
175        thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_WAIT );
176
177#if (DEBUG_SYS_WAIT & 1)
178cycle = hal_get_cycles();
179if( DEBUG_SYS_WAIT < cycle )
180printk("\n[DBG] %s : thread %x in process %x block & deschedule / cycle %d\n",
181__FUNCTION__, this, process->pid, (uint32_t)cycle );
182#endif
183
184        // deschedule
185        sched_yield( "parent process wait children processes termination" );
186
187#if (DEBUG_SYS_WAIT & 1)
188cycle = hal_get_cycles();
189if( DEBUG_SYS_WAIT < cycle )
190printk("\n[DBG] %s : thread %x in process %x unblock & resume / cycle %d\n",
191__FUNCTION__, this, process->pid, (uint32_t)cycle );
192#endif
193
194    }  // end while
195
196    // never executed
197        return -1; 
198
199}  // end sys_wait()
Note: See TracBrowser for help on using the repository browser.