/* --- Copyright University of Sussex 1990. All rights reserved. ----------
 > File:            C.vms/src/syswait.p
 > Purpose:
 > Author:          John Gibson (see revisions)
 > Documentation:	REF *SYSUTIL
 */


;;; -------------- WAIT FOR A SPAWNED VMS PROCESS ----------------------------

#_INCLUDE 'declare.ph'
#_INCLUDE 'vmsdefs.ph'

global constant
		procedure syshibernate
	;

global vars
		pop_status
	;

section $-Sys;

global constant
		_vmsproc_tab, _vmsproc_tab_lim
	;

endsection;


;;; ----------------------------------------------------------------------

section $-Sys => syswait;

define syswait();
	lvars _entry = _vmsproc_tab, _noprocs = true;

	;;; find a dead process in proc table
	while _entry <@(struct PROC_ENTRY) _vmsproc_tab_lim do
		if _neg(_entry!PROC_COND) then
			;;; found dead process - free entry and return pid
			_0 -> _entry!PROC_COND;					;;; free entry
			Uint_->_bigint(_entry!PROC_STATUS) -> pop_status;	;;; completion status
			return(Uint_->_bigint(_entry!PROC_PID))	;;; return pid
		endif;
		if _nonzero(_entry!PROC_COND) then false -> _noprocs endif;
		_entry@(struct PROC_ENTRY)++ -> _entry
	endwhile;

	;;; no dead procs in table
	if _noprocs then
		;;; table empty -- no processes not waited for
		mishap(0, 'syswait: NO CHILD PROCESSES TO WAIT FOR')
	else
		;;; else hibernate until a proc dies
		define dlocal interrupt();
			exitfrom(false, syswait)
		enddefine;

		syshibernate();
		chain(syswait)
	endif
enddefine;

endsection;		/* $-Sys */



/* --- Revision History ---------------------------------------------------
--- John Gibson, Nov 21 1990
		Changed to use syshibernate
--- John Gibson, Mar 16 1988
		Moved out of sysutil.p
 */
