% $Id: NewPortSemantics.oz,v 1.1 2007/11/13 22:34:05 leavens Exp leavens $ % Don't feed this buffer as a whole! % Instead feed it one contiguous region at a time. % Basic semantics of NewPort and Send declare Strm Port in {Browse Port} {Browse Strm} % NewPort takes an variable representing a stream % and initializes the Port argument {NewPort Strm Port} % Sending on the port adds to the stream thread {Delay 1000} {Send Port 3} end thread {Delay 999} {Send Port 4} end thread {Delay 3001} {Send Port 5} end thread {Delay 3000} {Send Port 6} end % You can think of NewPort as a function declare S2 P2 U1 U2 in P2 = {NewPort S2} % You can send anything to a port {Browse S2} {Delay 3000} thread {Send P2 7} end thread {Send P2 unit} end thread {Send P2 true} end % You can send unbound store variables thread {Send P2 U1} end % You can send partial values {Send P2 hmmm(x:U2)} thread U1 = 4020 end {Browse S2.1} % You can't bind a stream directly after giving it to NewPort % (It's put in the read-only store, see section 3.7.5) declare S3 P3 in P3 = {NewPort S3} {Browse S3} local Z in S3 = 1|Z end % suspends {Browse ok} {Send P3 1} % Similarly, you can't reuse a stream once passed to NewPort % As the second use will suspend... declare S4 P4a P4b in P4a = {NewPort S4} {Browse gotHere} P4b = {NewPort S4} % suspends {Browse P4a == P4b} % You have to pass an unbound store variable to NewPort declare S5 S5a P5 in {Browse S5} {Browse S5a} S5=5|S5a {Browse 'calling NewPort with S5...'} P5 = {NewPort S5} % suspends {Browse pastIt}