summaryrefslogtreecommitdiff
path: root/lib/std/wait+posixy.myr
blob: b04aab441ce65236105e430b571e78d07b9e173e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use sys

use "die"
use "syswrap"

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

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

const waitany = {
	match dowait(-1)
	| (p, st):	-> ((p : pid), st)
	;;
}

const wait = {pid
	match dowait(pid)
	| (_, st):	-> st
	;;
}

const dowait = {pid
	var st, p

:again
	p = sys.waitpid((pid : sys.pid), &st, 0)
	if p > 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:	-> (p, `Waiterror)
		| `sys.Waitsig sig:	-> (p, `Wsignalled)
		| `sys.Waitexit 0:	-> (p, `Wsuccess)
		| `sys.Waitexit _:	-> (p, `Wfailure)
		;;
	;;
	-> (-1, `Waiterror)
}