-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementation of poll in OS.IO #188
Open
melsman
wants to merge
27
commits into
master
Choose a base branch
from
poll-in-basislib
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@athas : You mentioned a test... |
This following should be a decent test. It will succeed if it terminates. It's quite verbose, but I think that is just the nature of this domain: (* These produce nonblocking streams. *)
fun fdToInstream fd =
TextIO.mkInstream (TextIO.StreamIO.mkInstream
(Posix.IO.mkTextReader {fd = fd, name = "<pipe>", initBlkMode = true}, ""))
fun fdToOutstream fd =
TextIO.mkOutstream (TextIO.StreamIO.mkOutstream
( Posix.IO.mkTextWriter
{ fd = fd
, name = "<pipe>"
, initBlkMode = true
, appendMode = false
, chunkSize = 1024
}
, IO.NO_BUF
))
val pollIn = OS.IO.pollIn o valOf o OS.IO.pollDesc o Posix.FileSys.fdToIOD
val pollOut = OS.IO.pollOut o valOf o OS.IO.pollDesc o Posix.FileSys.fdToIOD
fun forever f =
(f (); forever f)
fun modelProc (inf_fd, inf) (outf_fd, outf) =
let
val in_poll = pollIn inf_fd
fun read () =
case OS.IO.poll ([in_poll], SOME (Time.fromSeconds 0)) of
[] => ()
| _ =>
let
fun loop () =
case TextIO.canInput (inf, 1) of
NONE => ()
| SOME x =>
( print ("model: received: " ^ valOf (TextIO.inputLine inf))
; loop ()
)
in
loop ()
end
fun react () =
let
val () = TextIO.output (outf, "status update\n")
val () = read ()
in
OS.Process.sleep (Time.fromSeconds 1)
end
in
forever react
end
fun viewProc (inf_fd, inf) (outf_fd, outf) =
let
exception Stop
val in_poll = pollIn inf_fd
val out_poll = pollOut outf_fd
val pending = ref ["foo", "bar"]
val stdin_poll = pollIn Posix.FileSys.stdin
val counter = ref 0
fun onPoll ready =
if !counter = 3 then
raise Stop
else if OS.IO.infoToPollDesc ready = in_poll then
( print "view: received: "
; print (valOf (TextIO.inputLine inf))
; counter := !counter + 1
)
else if OS.IO.infoToPollDesc ready = stdin_poll then
case TextIO.inputLine TextIO.stdIn of
NONE => raise Fail "stdin closed"
| SOME l => pending := (l :: !pending)
else if OS.IO.infoToPollDesc ready = out_poll then
( List.app (fn l => TextIO.output (outf, l ^ "\n")) (!pending)
; pending := []
; TextIO.flushOut outf
)
else
()
fun react () =
List.app onPoll (OS.IO.poll ([in_poll, out_poll, stdin_poll], NONE))
in
forever react
handle Stop => ()
end
fun main () =
let
val {infd = ctp_in_fd, outfd = ctp_out_fd} = Posix.IO.pipe {}
val {infd = ptc_in_fd, outfd = ptc_out_fd} = Posix.IO.pipe {}
val ctp_in = fdToInstream ctp_in_fd
val ctp_out = fdToOutstream ctp_out_fd
val ptc_in = fdToInstream ptc_in_fd
val ptc_out = fdToOutstream ptc_out_fd
in
case Posix.Process.fork () of
NONE =>
( TextIO.closeOut ctp_out
; TextIO.closeIn ptc_in
; modelProc (ctp_in_fd, ctp_in) (ptc_in_fd, ptc_out)
)
| SOME model_pid =>
( TextIO.closeIn ctp_in
; TextIO.closeOut ptc_out
; viewProc (ptc_in_fd, ptc_in) (ctp_out_fd, ctp_out)
)
end
val () = main () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
First go...