Download presentation
Presentation is loading. Please wait.
1
Programming Language: Concurrent ML
Matt Defenthaler John Maskasky Vinash Seenath 7/15/2009
2
Presentation Outline CML Origins/History ML in CaML Shell
Features of CML TraceCML CML vs. SML Dynamic Semantics Definitions Classes Grammar Evaluation Context Event Matching Syntax of CML Concurrency Synchronization Producer/Consumer in CML File I/O Conclusions 7/15/2009
3
CML Origins ML originally released in the 1980’s
specifically purposed as a meta language First CaML implementations came about in the late 1980's. O-CaML is an object-oriented, concurrent variation of the ML programming language CML was created by Dr. John H. Reppy 7/15/2009
4
ML in CaML Shell Objective Caml version 3.11.0 # 1+2*3;; - : int = 7
# let rec fib n = if n < 2 then n else fib(n-1) + fib(n-2) ;; val fib : int -> int = <fun> # fib 10;; - : int = 55 # fib 2;; - : int = 1 These are some examples of code run in the Shell of the CaML compiler. The first function is a simple addition multiplication problem in which it returned 7 The second is defined as a function fib. to determine the Fibonacci number. (sequence starting with 1, 1) 7/15/2009
5
Features of CML Inherited from SML Functions as first-class values
Strong static typing Polymorphism Datatypes and pattern matching Lexical scoping Exception handling State of the art module facility Written in SML: should run on anything running SML 7/15/2009
6
Features of CML Those added with CML
Concurrency with dynamic thread creation Preemptive scheduling (to prevent processor monopolization) Automatic reclamation of threads and channels Synchronous I/O operations TraceCML 7/15/2009
7
Features of CML: TraceCML
Provides good debugging support for CML 3 facilities Trace modules: for controlling debugging output Thread watching: for detecting thread termination Reporting of uncaught exceptions on 'per thread' basis Trace modules have been implemented in such a way that invocation of them can occur regardless of the current running state of CML. 7/15/2009
8
Features of CML: TraceCML
TraceCML Interface type trace_module datatype trace_to = TraceToOut | TraceToErr | TraceToNull | TraceToFile of string | TraceToStream of TextIO.outstream val setTraceFile : trace_to -> unit val traceRoot : trace_module exception NoSuchModule val traceModule : trace_module * string -> trace_module val nameOf : trace_module -> string val moduleOf : string -> trace_module val traceOn : trace_module -> unit val traceOff : trace_module -> unit val traceOnly : trace_module -> unit val amTracing : trace_module -> bool val status : trace_module -> (trace_module * bool) list val trace : trace_module * (unit -> string list) -> unit val watcher : trace_module val watch : string * CML.thread_id -> unit val unwatch : CML.thread_id -> unit val setUncaughtFn : (CML.thread_id * exn -> unit) -> unit val setHandleFn : (CML.thread_id * exn -> bool) -> unit val resetUncaughtFn : unit -> unit 7/15/2009
9
CML vs. SML Concurrent ML is embedded in the standard ML language for programming concurrent systems. CML supports explicit thread creation. CML possesses event values for communications between threads. These event values are an abstraction for synchronous communications. 7/15/2009
10
CML vs. SML Concurrency in ML is achieved mostly via libraries.
Programs in CML are more likely to spawn processes when they are needed, instead of at the beginning of the program. CML like SML supports polymorphism, has user defined variables, and an automatic garbage collector. 7/15/2009
11
Dynamic Semantics of λcv
λcv is a concurrent extension of λv–calculus λcv is useful as it does posses synchronous operations 7/15/2009
12
Dynamic Semantics - Definitions
Here we have the foundation of λcv Channel names are needed to send and receive information 7/15/2009
13
Dynamic Semantics - Classes
3 Syntactic classes: Expressions Values Event values 7/15/2009
14
Dynamic Semantics - Classes
e can be: 7/15/2009
15
Dynamic Semantics - Classes
v can be: 7/15/2009
16
Dynamic Semantics - Grammar
Of note are Channel output Channel input Wrapper Choice 7/15/2009
17
Dynamic Semantics – Evaluation Context
Similar to the production rules of denotational semantics Spawn E creates a new process Sync E synchronizes a channel after a non-deterministic choice is offered 7/15/2009
18
Dynamic Semantics – Event Matching
Key concept in semantics of concurrent evaluation The semantic of rendezvous By synchronizing on matching events, values can be exchanged 7/15/2009
19
Dynamic Semantics – Event Matching
Read as: ev1 matches ev2 on channel k with results e1 and e2 7/15/2009
20
Dynamic Semantics – Event Matching
Recall that k is the channel name 7/15/2009
21
Dynamic Semantics – Event Matching
ev1 and ev2 can be 7/15/2009
22
Dynamic Semantics – Event Matching
And e1 e1 are : 7/15/2009
23
Dynamic Semantics – Event Matching
An example is show to illustrate an event matching situation 7/15/2009
24
Syntax of CML Bold text denotes elements found in CML and not in SML
7/15/2009
25
Concurrency To achieve concurrency in ML we need to create and run multiple threads simultaneously. We may have to force the threads to synchronize in order to protect the integrity of the data. 7/15/2009
26
Synchronization CML has various ways in which threads can synchronize with each other. Events Threads can Synchronize on an event Communication channels Send/Recv functions -- Blocking Events SendPoll/RecvPoll functions -- Non-Blocking Events SendEvt/RecvEvt – will create an event associated with sending or receiving a message. Multicasting -- this is where a message can be transmitted to multiple threads simultaneously. SyncVars – variables that can be filled or emptied Processes will synchronize on reading from an emptied variable 7/15/2009
27
Synchronization MailBoxes are a combination of SyncVars and Communication channels. A producer may put objects in a mailbox where a consumer will retrieve them. Events Associated with a MailBox Send - non-blocking Recv - blocking RecvPoll – non-blocking 7/15/2009
28
Buffered Producer/Consumer in CML
datatype 'a buffer = BUF of { insCh : 'a chan, remCh : 'a chan } fun buffer () = let val insCh = channel() and remCh = channel() fun loop [] = loop [recv inCh] | loop buf = if (length buf > maxlen) then (send (remCh, hd buf); loop (tl, buf)) else (select remCh!(hd buf) => loop (tl buf) or insCh?x => loop [x])) in spawn loop; BUF{ insCh = insCh, remCh = remCh } end fun insert (BUF{insCh, ...}, v) = send (insCh, v) fun remove (BUF{remCh, ...}) = recv remCh 7/15/2009
29
Buffered Producer/Consumer in CML
creates buffer datatype containing two channels (insCh-for inserting elements into the buffer and remCh-for removing elements from the buffer datatype 'a buffer = BUF of { insCh : 'a chan, remCh : 'a chan } fun buffer () = let val insCh = channel() and remCh = channel() fun loop [] = loop [recv inCh] | loop buf = if (length buf > maxlen) then (send (remCh, hd buf); loop (tl, buf)) else (select remCh!(hd buf) => loop (tl buf) or insCh?x => loop [x])) creates two synchronous channels checks to see if the buffer is full ! is an attempt to send, select blocks all channels on a list of send/recv calls and executes the associated code with whichever call returns first (and drops the rest) sends the head of the buffer on the remCh channel, then calls loop with the tail of the buffer and the buffer as arguments ? is an attempt to receive, => connects the associated code to execute
30
Buffered Producer/Consumer in CML
in spawn loop; BUF{ insCh = insCh, remCh = remCh } end fun insert (BUF{insCh, ...}, v) = send (insCh, v) fun remove (BUF{remCh, ...}) = recv remCh creates new thread of control to evaluate body of loop function. A unique ID for thread is returned. inserts item into buffer by sending item v on insCh removes item from buffer by receiving on remCh 7/15/2009
31
File I/O in CML Because CML is based in SML, file I/O is similar between the two. The functions needed to perform file I/O are included in the IMPERATIVE_IO file. This file includes functions such as openIn : name openOut : name These open the file for reading and writing. inputLine : strm This function will take in one line of the file 7/15/2009
32
File I/O in CML TextIO in CML is accessed in a similar manner to TextIO in SML The implementation was changed because “two different threads should not access the same queue without synchronization” 7/15/2009
33
File I/O in CML Example: function to copy one text file to another.
Include IMPERATIVE_IO fun copyFile(infile: string, outfile: string) let (* Opening files for input and output *) val ins = TextIO.openIn infile val outs = TextIO.openOut outfile (* Recursive statement, copying one character *) (* from the input file to the output file *) fun recurs(copt: char option) = case copt of NONE => (TextIO.closeIn ins; TextIO.closeOut outs | SOME(c) => (TextIO.output1(outs, c)); recurs(TextIO.intput1 ins)) in recurs(TextIO.input1 ins) end 7/15/2009
34
Conclusions Concurrent ML is a well-formed language due to its basis on a pre-established language, SML. CML can provide an introduction to the concepts and challenges inherent to concurrent programming for students and hobbyists. Unfortunately, there seems to be a lack of example code and sufficient documentation. These facts may prevent many from becoming familiar with the language. 7/15/2009
35
Programming Language: Concurrent ML
Matt Defenthaler John Maskasky Vinash Seenath 7/15/2009
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.