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

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

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

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