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

Last change on this file since 433 was 433, checked in by alain, 4 years ago

blip

File size: 4.9 KB
Line 
1/*
2 * sys_wait.c - wait termination or blocking of a child process.
3 *
4 * Author    Alain Greiner (2016,2017)
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_types.h>
25#include <hal_uspace.h>
26#include <core.h>
27#include <thread.h>
28#include <process.h>
29#include <vmm.h>
30#include <printk.h>
31
32/////////////////////////////////
33int sys_wait( uint32_t * status )
34{
35        error_t     error;
36    paddr_t     paddr;
37    xptr_t      iter_xp;
38    xptr_t      child_xp;
39    process_t * child_ptr;
40    cxy_t       child_cxy;
41    pid_t       child_pid;
42    int         child_state;
43    thread_t  * child_thread;
44
45    thread_t  * this    = CURRENT_THREAD;
46    process_t * process = this->process;
47    pid_t       pid     = process->pid;
48
49#if CONFIG_DEBUG_SYS_WAIT
50uint64_t    tm_start;
51uint64_t    tm_end;
52tm_start = hal_get_cycles();
53if( CONFIG_DEBUG_SYS_WAIT < tm_start )
54printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
55__FUNCTION__, this, process->pid, (uint32_t)tm_start );
56#endif
57
58    // check status in user space
59    error = vmm_v2p_translate( false , status , &paddr );
60
61        if( error )
62        {
63        printk("\n[ERROR] in %s : status buffer unmapped for thread %x in process %x\n",
64        __FUNCTION__ , this->trdid , process->pid );
65        this->errno = EFAULT;
66                return -1;
67        }
68
69    // get process owner cluster
70    cxy_t owner_cxy = CXY_FROM_PID( pid );
71
72    // This function must be executed in owner cluster
73    assert( (owner_cxy == local_cxy) , __FUNCTION__ , 
74    "calling thread must execute in owner cluster" );
75
76    // This function must be executed by the main thread
77    assert( (process->th_tbl[0] == this) , __FUNCTION__ ,
78    "this function must be executed by the main thread" );
79   
80    // get extended pointer on children list root and lock
81    xptr_t children_root_xp = XPTR( owner_cxy , &process->children_root );
82    xptr_t children_lock_xp = XPTR( owner_cxy , &process->children_lock );
83
84    // exit this blocking loop only when a child processes change state
85    while( 1 )
86    {
87        // get lock protecting children list
88        remote_spinlock_lock( children_lock_xp );
89
90        // scan the list of owner child process
91        XLIST_FOREACH( children_root_xp , iter_xp )
92        {
93            // get child process owner cluster and local pointer
94            child_xp  = XLIST_ELEMENT( iter_xp , process_t , children_list );
95            child_ptr = GET_PTR( child_xp );
96            child_cxy = GET_CXY( child_xp );
97
98            // get term_state from child owner process
99            child_state = (int)hal_remote_lw ( XPTR(child_cxy,&child_ptr->term_state));
100
101            // test if child process is terminated,
102            // but termination not yet reported to parent process
103            if( ((child_state & PROCESS_FLAG_EXIT)   || 
104                 (child_state & PROCESS_FLAG_KILL)   ||
105                 (child_state & PROCESS_FLAG_BLOCK)) &&
106                 ((child_state & PROCESS_FLAG_WAIT) == 0) )
107            {
108                // get pointer on main thread and PID from child owner process
109                child_pid    = (pid_t)     hal_remote_lw ( XPTR(child_cxy,&child_ptr->pid));
110                child_thread = (thread_t *)hal_remote_lpt( XPTR(child_cxy,&child_ptr->th_tbl[0]));
111
112                // set the PROCESS_FLAG_WAIT in owner child descriptor
113                hal_remote_atomic_or( XPTR( child_cxy , &child_ptr->term_state ),
114                                      PROCESS_FLAG_WAIT );
115
116                // set the THREAD_FLAG_REQ_DELETE in child main thread
117                hal_remote_atomic_or( XPTR( child_cxy , &child_thread->flags ) ,
118                                            THREAD_FLAG_REQ_DELETE );
119
120#if CONFIG_DEBUG_SYS_WAIT
121tm_end = hal_get_cycles();
122if( CONFIG_DEBUG_SYS_WAIT < tm_end )
123printk("\n[DBG] %s : thread %x exit / process %x / cycle %d\n",
124__FUNCTION__, this, process->pid, (uint32_t)tm_end );
125#endif
126
127                 // return relevant info to calling parent process
128                 hal_copy_to_uspace( status , &child_state , sizeof(int) );
129                 return child_pid;
130            }
131        }
132       
133        // release lock
134        remote_spinlock_unlock( children_lock_xp );
135
136        // deschedule without blocking
137        sched_yield( "parent wait children termination" );
138    }
139
140    // never executed
141        return -1; 
142
143}  // end sys_wait()
Note: See TracBrowser for help on using the repository browser.