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

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

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

File size: 3.9 KB
RevLine 
[421]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
44    thread_t  * this    = CURRENT_THREAD;
45    process_t * process = this->process;
46
47#if CONFIG_SYSCALL_DEBUG
48uint64_t    tm_start;
49uint64_t    tm_end;
50tm_start = hal_get_cycles();
51printk("\n[DBG] %s : core[%x,%d] enter / process %x / cycle %d\n",
52__FUNCTION__ , local_cxy , this->core->lid , process->pid, (uint32_t)tm_start );
53#endif
54
55    // check status in user space
56    error = vmm_v2p_translate( false , status , &paddr );
57
58        if( error )
59        {
60        printk("\n[ERROR] in %s : status buffer unmapped for thread %x in process %x\n",
61        __FUNCTION__ , this->trdid , process->pid );
62        this->errno = EFAULT;
63                return -1;
64        }
65
66    // get cluster and local pointer on reference process
67    xptr_t      ref_xp  = process->ref_xp;
68    cxy_t       ref_cxy = GET_CXY( ref_xp );
69    process_t * ref_ptr = GET_PTR( ref_xp );
70
71    // get extended pointer on children list root
72    xptr_t root_xp = XPTR( ref_cxy , &ref_ptr->children_root );
73
74    // get extended pointer on lock protecting the children list
75    xptr_t lock_xp = XPTR( ref_cxy , &ref_ptr->children_lock );
76
77    // exit this blocking loop only when a child processes change state
78    while( 1 )
79    {
80        // get lock
81        remote_spinlock_lock( lock_xp );
82
83        // scan the list of child process
84        XLIST_FOREACH( root_xp , iter_xp )
85        {
86            // get child process cluster and local pointer
87            child_xp  = XLIST_ELEMENT( iter_xp , process_t , children_list );
88            child_ptr = GET_PTR( child_xp );
89            child_cxy = GET_CXY( child_xp );
90
91            // get the child PID
92            child_pid = (int)hal_remote_lw( XPTR( child_cxy , &child_ptr->pid ) );
93
94            // get the child process state
95            child_state = hal_remote_lw( XPTR( child_cxy , &child_ptr->state ) );
96
97            // check child process state
98            if( child_state != PROCESS_STATE_RUNNING )
99            {
100                // release lock
101                remote_spinlock_unlock( lock_xp );
102
103#if CONFIG_SYSCALL_DEBUG
104tm_end = hal_get_cycles();
105printk("\n[DBG] %s : core[%x,%d] exit / process %x / cost = %d\n",
106__FUNCTION__ , local_cxy, this->core->lid, process->pid, (uint32_t)(tm_end - tm_start) );
107#endif
108
109                // return relevant info to process
110                hal_copy_to_uspace( status , &child_state , sizeof(int) );
111                return child_pid;
112            }
113        }
114       
115        // release lock
116        remote_spinlock_unlock( lock_xp );
117
118        // block the calling thread until a child process change state
119        thread_block( this , THREAD_BLOCKED_WAIT );
120        sched_yield( "wait child termination" );
121    }
122
123    // never executed
124        return -1; 
125
126}  // end sys_wait()
Note: See TracBrowser for help on using the repository browser.