Created
October 6, 2020 07:37
-
-
Save Cheng-EnC/ea317edb62f01f55b85a9406f6093217 to your computer and use it in GitHub Desktop.
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
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