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

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

Complete restructuration of kernel locks.

File size: 6.8 KB
RevLine 
[421]1/*
2 * sys_wait.c - wait termination or blocking of a child process.
3 *
[440]4 * Author    Alain Greiner (2016,2017,2018)
[421]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
[457]24#include <hal_kernel_types.h>
[421]25#include <hal_uspace.h>
[435]26#include <hal_irqmask.h>
[566]27#include <remote_queuelock.h>
[421]28#include <core.h>
29#include <thread.h>
30#include <process.h>
31#include <vmm.h>
32#include <printk.h>
33
[506]34#include <syscalls.h>
35
[421]36/////////////////////////////////
37int sys_wait( uint32_t * status )
38{
39        error_t     error;
[440]40    vseg_t    * vseg;
[421]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;
[446]46    uint32_t    child_state;
[433]47    thread_t  * child_thread;
[435]48    reg_t       save_sr;
[421]49
50    thread_t  * this    = CURRENT_THREAD;
51    process_t * process = this->process;
[433]52    pid_t       pid     = process->pid;
[421]53
[438]54#if DEBUG_SYS_WAIT
[446]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 );
[421]59#endif
60
61    // check status in user space
[440]62    error = vmm_get_vseg( process, (intptr_t)status , &vseg );
[421]63
64        if( error )
65        {
[435]66
[438]67#if DEBUG_SYSCALLS_ERROR
[440]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 );
[435]71#endif
[436]72        this->errno = EINVAL;
[421]73                return -1;
74        }
75
[443]76    // get calling process owner cluster
[436]77    cxy_t   owner_cxy = CXY_FROM_PID( pid );
[421]78
[443]79    // get calling thread trdid
80    trdid_t trdid = this->trdid;
81
82    // this function must be executed by the process main thread
[436]83    if( (owner_cxy != local_cxy) || (LTID_FROM_TRDID(trdid) != 0) )
84    {
[421]85
[446]86#if DEBUG_SYSCALLS_ERROR
[436]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
[433]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 );
[421]97
[566]98    // exit this loop only when a child processes change state
[421]99    while( 1 )
100    {
[435]101        // enable IRQS
102        hal_enable_irq( &save_sr );
103 
[433]104        // get lock protecting children list
[566]105        remote_queuelock_acquire( children_lock_xp );
[421]106
[436]107        // scan the list of child process
[433]108        XLIST_FOREACH( children_root_xp , iter_xp )
[421]109        {
[433]110            // get child process owner cluster and local pointer
[421]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
[446]115            // get PID, term_state, and main thread from child process
[566]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 ) );
[446]118            child_thread = hal_remote_lpt(XPTR( child_cxy , &child_ptr->th_tbl[0] ));
[421]119
[566]120#if (DEBUG_SYS_WAIT & 1)
[446]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
[443]126            // test if this child process is terminated,
[433]127            // but termination not yet reported to parent process
[435]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) )
[421]132            {
[566]133                // set the PROCESS_TERM_WAIT in child process descriptor
[433]134                hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ),
[435]135                                      PROCESS_TERM_WAIT );
[433]136
[446]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))
[433]139                hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) ,
140                                            THREAD_FLAG_REQ_DELETE );
141
[435]142                // release lock protecting children list
[566]143                remote_queuelock_release( children_lock_xp );
[435]144
[438]145#if DEBUG_SYS_WAIT
[446]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}
[421]159#endif
[436]160                 // return child termination state  to parent process
[433]161                 hal_copy_to_uspace( status , &child_state , sizeof(int) );
162                 return child_pid;
[421]163            }
[436]164        }  // end loop on children
[421]165       
[566]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
[446]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
[421]183
[446]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
[435]194    }  // end while
195
[421]196    // never executed
197        return -1; 
198
199}  // end sys_wait()
Note: See TracBrowser for help on using the repository browser.