ref: 4eae5641fda676dbb1085560b993971d32b38935
parent: f1432bf26c12fe0f005c81cf9401a531a55935f1
author: Ori Bernstein <[email protected]>
date: Tue Dec 30 19:58:44 EST 2014
Make a generic, platform agnostic wait. If you want more info, use sys.wait() or whatever the nonportable interface is.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -68,7 +68,7 @@
units.myr \
utf.myr \
varargs.myr \
- waitstatus.myr \
+ wait.myr \
include ../config.mk
--- a/libstd/syswrap+posixy.myr
+++ b/libstd/syswrap+posixy.myr
@@ -100,7 +100,6 @@
const execv = {cmd, args; -> sys.execv(cmd, args)}
const execve = {cmd, args, env; -> sys.execve(cmd, args, env)}
const exit = {status; sys.exit(status)}
-const waitpid = {pid, loc, opt; -> sys.waitpid(pid castto(sys.pid), loc, opt)}
/* memory stuff */
const getmem = {sz; -> sys.mmap(0 castto(byte#), sz castto(sys.size), sys.Mprotrw, sys.Mpriv | sys.Manon, -1, 0)}
--- /dev/null
+++ b/libstd/wait+freebsd.myr
@@ -1,0 +1,39 @@
+use sys
+
+use "die.use"
+use "syswrap.use"
+
+pkg std =
+ type waitstatus = union
+ `Wsuccess
+ `Wfailure
+ `Wsignal
+ `Waiterror
+ ;;
+
+ const wait : (pid : pid -> waitstatus)
+;;
+
+const wait = {pid
+ var st
+
+ if sys.waitpid(pid castto(sys.pid), &st, 0) > 0
+ match st & 0o177
+ | 0:
+ if (st >> 8) == 0
+ -> `Wsuccess
+ else
+ -> `Wfailure
+ ;;
+ | sig: -> `Wstop
+ | 0x7f:-
+ /*
+ when a process stops, eg, if paused by a debugger,
+ wait() will return. This API is for waiting until
+ a process exits. Loop instead.
+ */
+ goto again
+ ;;
+ ;;
+ -> `Waiterror
+}
--- /dev/null
+++ b/libstd/wait+linux.myr
@@ -1,0 +1,41 @@
+use sys
+
+use "die.use"
+use "syswrap.use"
+
+pkg std =
+ type waitstatus = union
+ `Wsuccess
+ `Wfailure
+ `Wsignal
+ `Waiterror
+ ;;
+
+ const wait : (pid : pid -> waitstatus)
+;;
+
+const wait = {pid
+ var st
+
+:again
+ if sys.waitpid(pid castto(sys.pid), &st, 0) > 0
+ if st & 0x7f == 0 /* if exited */
+ if ((st & 0xff00) >> 8) == 0
+ -> `Wsuccess
+ else
+ -> `Wfailure
+ ;;
+ elif ((st & 0xffff)-1) < 0xff /* if signaled */
+ -> `Wsignal
+ elif (((st & 0xffff)*0x10001)>>8) > 0x7f00
+ /*
+ when a process stops, eg, if paused by a debugger,
+ wait() will return. This API is for waiting until
+ a process exits. Loop instead.
+ */
+ goto again
+ ;;
+ ;;
+ -> `Waiterror
+}
+
--- /dev/null
+++ b/libstd/wait+osx.myr
@@ -1,0 +1,40 @@
+use sys
+
+use "die.use"
+use "syswrap.use"
+
+pkg std =
+ type waitstatus = union
+ `Wsuccess
+ `Wfailure
+ `Wsignal
+ `Waiterror
+ ;;
+
+ const wait : (pid : pid -> waitstatus)
+;;
+
+const wait = {pid
+ var st
+
+:again
+ if sys.waitpid(pid castto(sys.pid), &st, 0) > 0
+ match st & 0o177
+ | 0: ->
+ if (st >> 8) == 0
+ -> `Wsuccess
+ else
+ -> `Wfailure
+ ;;
+ | sig: -> `Wsignal
+ | 0o177: /* stopped */
+ /*
+ when a process stops, eg, if paused by a debugger,
+ wait() will return. This API is for waiting until
+ a process exits. Loop instead.
+ */
+ goto again
+ ;;
+ ;;
+ -> `Waiterror
+}
--- /dev/null
+++ b/libstd/wait+plan9.myr
@@ -1,0 +1,15 @@
+use sys
+
+use "die.use"
+use "syswrap.use"
+
+pkg std =
+ type waitstatus = union
+ `Waitexit int32
+ `Waitsig int32
+ `Waitstop int32
+ ;;
+
+ const wait : (pid : pid -> waitstatus)
+;;
+
--- a/libstd/waitstatus+freebsd.myr
+++ /dev/null
@@ -1,20 +1,0 @@
-use "die.use"
-
-pkg std =
- type waitstatus = union
- `Waitexit int32
- `Waitsig int32
- `Waitstop int32
- ;;
-
- const waitstatus : (st : int32 -> waitstatus)
-;;
-
-const waitstatus = {st
- match st & 0o177
- | 0: -> `Waitexit (st >> 8)
- | 0x7f:-> `Waitstop (st >> 8)
- | sig: -> `Waitsig sig
- ;;
- die("unreachable")
-}
--- a/libstd/waitstatus+linux.myr
+++ /dev/null
@@ -1,22 +1,0 @@
-use "die.use"
-pkg std =
- type waitstatus = union
- `Waitexit int32
- `Waitsig int32
- `Waitstop int32
- ;;
-
- const waitstatus : (st : int32 -> waitstatus)
-;;
-
-const waitstatus = {st
- if st & 0x7f == 0 /* if exited */
- -> `Waitexit ((st & 0xff00) >> 8)
- elif ((st & 0xffff)-1) < 0xff /* if signaled */
- -> `Waitsig ((st) & 0x7f)
- elif (((st & 0xffff)*0x10001)>>8) > 0x7f00
- -> `Waitstop ((st & 0xff00) >> 8)
- ;;
- die("unreachable")
-}
-
--- a/libstd/waitstatus+osx.myr
+++ /dev/null
@@ -1,19 +1,0 @@
-use "die.use"
-pkg std =
- type waitstatus = union
- `Waitexit int32
- `Waitsig int32
- `Waitstop int32
- ;;
-
- const waitstatus : (st : int32 -> waitstatus)
-;;
-
-const waitstatus = {st
- match st & 0o177
- | 0: -> `Waitexit (st >> 8)
- | 0o177:-> `Waitstop (st >> 8)
- | sig: -> `Waitsig sig
- ;;
- die("unreachable")
-}