Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cml/cml-lib/multicast.sml
ViewVC logotype

Annotation of /sml/trunk/src/cml/cml-lib/multicast.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (view) (download)

1 : monnier 2 (* multicast.sml
2 :     *
3 :     * COPYRIGHT (c) 1994 AT&T Bell Laboratories.
4 :     *
5 :     * Asynchronous multicast (one-to-many) channels. This implementation
6 :     * is based on a condition variable implementation of multicast channels.
7 :     * See Chapter 5 of "Concurrent Programming in ML" for details.
8 :     *)
9 :    
10 :     structure Multicast : MULTICAST =
11 :     struct
12 :    
13 :     structure V = SyncVar
14 :    
15 :     type 'a event = 'a CML.event
16 :    
17 :     datatype 'a mchan = MChan of ('a request CML.chan * 'a port CML.chan)
18 :    
19 :     and 'a port
20 :     = Port of (('a * 'a mc_state V.ivar) CML.chan * 'a mc_state V.ivar V.mvar)
21 :    
22 :     and 'a request
23 :     = Message of 'a
24 :     | NewPort
25 :    
26 :     and 'a mc_state = MCState of ('a * 'a mc_state V.ivar)
27 :    
28 :     fun mkPort cv = let
29 :     val outCh = CML.channel()
30 :     val stateVar = V.mVarInit cv
31 :     fun tee cv = let
32 :     val (MCState(v, nextCV)) = V.iGet cv
33 :     in
34 :     CML.send (outCh, (v, nextCV));
35 :     tee nextCV
36 :     end
37 :     in
38 :     CML.spawn (fn () => tee cv);
39 :     Port(outCh, stateVar)
40 :     end
41 :    
42 :     fun mChannel () = let
43 :     val reqCh = CML.channel() and replyCh = CML.channel()
44 :     fun server cv = (case (CML.recv reqCh)
45 :     of NewPort => (
46 :     CML.send (replyCh, mkPort cv);
47 :     server cv)
48 :     | (Message m) => let
49 :     val nextCV = V.iVar()
50 :     in
51 :     V.iPut (cv, MCState(m, nextCV));
52 :     server nextCV
53 :     end
54 :     (* end case *))
55 :     in
56 :     CML.spawn (fn () => server (V.iVar()));
57 :     MChan(reqCh, replyCh)
58 :     end
59 :    
60 :     fun multicast (MChan(ch, _), m) = CML.send (ch, Message m)
61 :    
62 :     fun port (MChan(reqCh, replyCh)) = (
63 :     CML.send (reqCh, NewPort);
64 :     CML.recv replyCh)
65 :    
66 :     fun copy (Port(_, stateV)) = mkPort(V.mGet stateV)
67 :    
68 :     fun recvMsg stateV (v, nextCV) = (V.mSwap (stateV, nextCV); v)
69 :    
70 :     fun recv (Port(ch, stateV)) = recvMsg stateV (CML.recv ch)
71 :     fun recvEvt (Port(ch, stateV)) = CML.wrap(CML.recvEvt ch, recvMsg stateV)
72 :    
73 :     end (* Multicast *)
74 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0