shithub: mc

ref: ddb327c92ebe3f96c5c816ec61996440551de401
dir: /lib/std/wait+posixy.myr/

View raw version
use sys

use "die"
use "syswrap"

pkg std =
	type waitstatus = union
		`Wsuccess
		`Wfailure
		`Wsignalled
		`Waiterror
	;;

	const wait	: (pid : pid -> waitstatus)
;;

const wait = {pid
	var st

:again
	if sys.waitpid((pid : sys.pid), &st, 0) > 0
		match sys.waitstatus(st)
		/* 
		when a process stops, eg, if paused by a debugger,
		wait() will return. This API is for waiting until
		a process exits. Loop instead.
		*/
		| `sys.Waitstop sig:	goto again
		| `sys.Waitfail fail:	-> `Waiterror
		| `sys.Waitsig sig:	-> `Wsignalled
		| `sys.Waitexit status:
			if status == 0
				-> `Wsuccess
			else
				-> `Wfailure
			;;
		;;
	;;
	-> `Waiterror
}