Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Tracker SCM

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log

Revision 2 - (download) (annotate)
Sat Oct 4 23:33:09 1997 UTC (25 years ago) by monnier
File size: 1898 byte(s)
Initial revision
(* multicast.sml
 * COPYRIGHT (c) 1994 AT&T Bell Laboratories.
 * Asynchronous multicast (one-to-many) channels.  This implementation
 * is based on a condition variable implementation of multicast channels.
 * See Chapter 5 of "Concurrent Programming in ML" for details.

structure Multicast : MULTICAST =

    structure V = SyncVar

    type 'a event = 'a CML.event

    datatype 'a mchan = MChan of ('a request CML.chan * 'a port CML.chan)

    and 'a port
      = Port of (('a * 'a mc_state V.ivar) CML.chan * 'a mc_state V.ivar V.mvar)

    and 'a request
      = Message of 'a
      | NewPort

    and 'a mc_state = MCState of ('a * 'a mc_state V.ivar)

    fun mkPort cv = let
	  val outCh = CML.channel()
	  val stateVar = V.mVarInit cv
	  fun tee cv = let
		val (MCState(v, nextCV)) = V.iGet cv
		  CML.send (outCh, (v, nextCV));
		  tee nextCV
	    CML.spawn (fn () => tee cv);
	    Port(outCh, stateVar)

    fun mChannel () = let
          val reqCh = CML.channel() and replyCh = CML.channel()
          fun server cv = (case (CML.recv reqCh)
		 of NewPort => (
		      CML.send (replyCh, mkPort cv);
		      server cv)
		  | (Message m) => let
		      val nextCV = V.iVar()
			V.iPut (cv, MCState(m, nextCV));
			server nextCV
		(* end case *))
            CML.spawn (fn () => server (V.iVar()));
            MChan(reqCh, replyCh)

    fun multicast (MChan(ch, _), m) = CML.send (ch, Message m)

    fun port (MChan(reqCh, replyCh)) = (
          CML.send (reqCh, NewPort);
          CML.recv replyCh)

    fun copy (Port(_, stateV)) = mkPort(V.mGet stateV)

    fun recvMsg stateV (v, nextCV) = (V.mSwap (stateV, nextCV); v)

    fun recv (Port(ch, stateV)) = recvMsg stateV (CML.recv ch)
    fun recvEvt (Port(ch, stateV)) = CML.wrap(CML.recvEvt ch, recvMsg stateV)

  end (* Multicast *)

ViewVC Help
Powered by ViewVC 1.0.0