Skip to content

Instantly share code, notes, and snippets.

@Cheng-EnC
Created October 6, 2020 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cheng-EnC/ea317edb62f01f55b85a9406f6093217 to your computer and use it in GitHub Desktop.
Save Cheng-EnC/ea317edb62f01f55b85a9406f6093217 to your computer and use it in GitHub Desktop.
fun pchannel insert =
let val (sendch, recvch) = (channel (), channel ())
val sendEvtp = wrap (recvEvt sendch, fn (p,v,e) => (p, SEND (v,e)))
val recvEvtp = wrap (recvEvt recvch, fn (p,e) => (p, RECV (e)))
fun loop pq = case pq of
[] => loop [select [sendEvtp, recvEvtp]]
| (\_, SEND (v1,e1))::xs => (case select [sendEvtp, recvEvtp] of
(p, SEND(v2,e2)) => loop (insert (p, SEND(v2,e2)) pq)
| (\_, RECV(e2)) => (sync (e2 v1); sync e1; loop xs))
| (\_, RECV (e1))::xs => (case select [sendEvtp, recvEvtp] of
(p, RECV(e2)) => loop (insert (p, RECV(e2)) pq)
| (\_, SEND(v2,e2)) => (sync (e1 v2); sync e2; loop xs))
in
spawn (fn () => loop []);
PCHAN\{sendch=sendch, recvch=recvch\}
end
fun sendP (PCHAN\{sendch=sendch, ...\}, v, p) =
let val c = channel ()
in send (sendch, (v, p, sendEvt (c, ()))); recv c
end
fun recvP (PCHAN\{recvch=recvch, ...\}, p) =
let val c = channel ()
in send (recvch, (p, fn v => sendEvt (c, v))); recv c
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment