source: doc/proposals/concurrency/text/cforall.tex @ cf966b5

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since cf966b5 was cf966b5, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Results need to be updated but otherwise, tentative final draft

  • Property mode set to 100644
File size: 8.9 KB
Line 
1% ======================================================================
2% ======================================================================
3\chapter{\CFA Overview}
4% ======================================================================
5% ======================================================================
6
7The following is a quick introduction to the \CFA language, specifically tailored to the features needed to support concurrency.
8
9\CFA is an extension of ISO-C and therefore supports all of the same paradigms as C. It is a non-object-oriented system-language, meaning most of the major abstractions have either no runtime overhead or can be opt-out easily. Like C, the basics of \CFA revolve around structures and routines, which are thin abstractions over machine code. The vast majority of the code produced by the \CFA translator respects memory-layouts and calling-conventions laid out by C. Interestingly, while \CFA is not an object-oriented language, lacking the concept of a receiver (e.g., {\tt this}), it does have some notion of objects\footnote{C defines the term objects as : ``region of data storage in the execution environment, the contents of which can represent
10values''~\cite[3.15]{C11}}, most importantly construction and destruction of objects. Most of the following code examples can be found on the \CFA website~\cite{www-cfa}
11
12% ======================================================================
13\section{References}
14
15Like \CC, \CFA introduces rebind-able references providing multiple dereferencing as an alternative to pointers. In regards to concurrency, the semantic difference between pointers and references are not particularly relevant, but since this document uses mostly references, here is a quick overview of the semantics:
16\begin{cfacode}
17int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
18        &r1 = x,    &&r2 = r1,   &&&r3 = r2;
19***p3 = 3;                                                      //change x
20r3    = 3;                                                      //change x, ***r3
21**p3  = ...;                                            //change p1
22*p3   = ...;                                            //change p2
23int y, z, & ar[3] = {x, y, z};          //initialize array of references
24typeof( ar[1]) p;                                       //is int, referenced object type
25typeof(&ar[1]) q;                                       //is int &, reference type
26sizeof( ar[1]) == sizeof(int);          //is true, referenced object size
27sizeof(&ar[1]) == sizeof(int *);        //is true, reference size
28\end{cfacode}
29The important take away from this code example is that a reference offers a handle to an object, much like a pointer, but which is automatically dereferenced for convenience.
30
31% ======================================================================
32\section{Overloading}
33
34Another important feature of \CFA is function overloading as in Java and \CC, where routines with the same name are selected based on the number and type of the arguments. As well, \CFA uses the return type as part of the selection criteria, as in Ada~\cite{Ada}. For routines with multiple parameters and returns, the selection is complex.
35\begin{cfacode}
36//selection based on type and number of parameters
37void f(void);                   //(1)
38void f(char);                   //(2)
39void f(int, double);    //(3)
40f();                                    //select (1)
41f('a');                                 //select (2)
42f(3, 5.2);                              //select (3)
43
44//selection based on  type and number of returns
45char   f(int);                  //(1)
46double f(int);                  //(2)
47char   c = f(3);                //select (1)
48double d = f(4);                //select (2)
49\end{cfacode}
50This feature is particularly important for concurrency since the runtime system relies on creating different types to represent concurrency objects. Therefore, overloading is necessary to prevent the need for long prefixes and other naming conventions that prevent name clashes. As seen in chapter \ref{basics}, routine \code{main} is an example that benefits from overloading.
51
52% ======================================================================
53\section{Operators}
54Overloading also extends to operators. The syntax for denoting operator-overloading is to name a routine with the symbol of the operator and question marks where the arguments of the operation occur, e.g.:
55\begin{cfacode}
56int ++? (int op);                       //unary prefix increment
57int ?++ (int op);                       //unary postfix increment
58int ?+? (int op1, int op2);             //binary plus
59int ?<=?(int op1, int op2);             //binary less than
60int ?=? (int & op1, int op2);           //binary assignment
61int ?+=?(int & op1, int op2);           //binary plus-assignment
62
63struct S {int i, j;};
64S ?+?(S op1, S op2) {                           //add two structures
65        return (S){op1.i + op2.i, op1.j + op2.j};
66}
67S s1 = {1, 2}, s2 = {2, 3}, s3;
68s3 = s1 + s2;                                           //compute sum: s3 == {2, 5}
69\end{cfacode}
70While concurrency does not use operator overloading directly, this feature is more important as an introduction for the syntax of constructors.
71
72% ======================================================================
73\section{Constructors/Destructors}
74Object life-time is often a challenge in concurrency. \CFA uses the approach of giving concurrent meaning to object life-time as a mean of synchronization and/or mutual exclusion. Since \CFA relies heavily on the life time of objects, constructors and destructors are a core feature required for concurrency and parallelism. \CFA uses the following syntax for constructors and destructors :
75\begin{cfacode}
76struct S {
77        size_t size;
78        int * ia;
79};
80void ?{}(S & s, int asize) {    //constructor operator
81        s.size = asize;                         //initialize fields
82        s.ia = calloc(size, sizeof(S));
83}
84void ^?{}(S & s) {                              //destructor operator
85        free(ia);                                       //de-initialization fields
86}
87int main() {
88        S x = {10}, y = {100};          //implicit calls: ?{}(x, 10), ?{}(y, 100)
89        ...                                                     //use x and y
90        ^x{}^y{};                            //explicit calls to de-initialize
91        x{20};  y{200};                         //explicit calls to reinitialize
92        ...                                                     //reuse x and y
93}                                                               //implicit calls: ^?{}(y), ^?{}(x)
94\end{cfacode}
95The language guarantees that every object and all their fields are constructed. Like \CC, construction of an object is automatically done on allocation and destruction of the object is done on deallocation. Allocation and deallocation can occur on the stack or on the heap.
96\begin{cfacode}
97{
98        struct S s = {10};      //allocation, call constructor
99        ...
100}                                               //deallocation, call destructor
101struct S * s = new();   //allocation, call constructor
102...
103delete(s);                              //deallocation, call destructor
104\end{cfacode}
105Note that like \CC, \CFA introduces \code{new} and \code{delete}, which behave like \code{malloc} and \code{free} in addition to constructing and destructing objects, after calling \code{malloc} and before calling \code{free}, respectively.
106
107% ======================================================================
108\section{Parametric Polymorphism}
109Routines in \CFA can also be reused for multiple types. This capability is done using the \code{forall} clause, which gives \CFA its name. \code{forall} clauses allow separately compiled routines to support generic usage over multiple types. For example, the following sum function works for any type that supports construction from 0 and addition :
110\begin{cfacode}
111//constraint type, 0 and +
112forall(otype T | { void ?{}(T *, zero_t); T ?+?(T, T); })
113T sum(T a[ ], size_t size) {
114        T total = 0;                            //construct T from 0
115        for(size_t i = 0; i < size; i++)
116                total = total + a[i];   //select appropriate +
117        return total;
118}
119
120S sa[5];
121int i = sum(sa, 5);                             //use S's 0 construction and +
122\end{cfacode}
123
124Since writing constraints on types can become cumbersome for more constrained functions, \CFA also has the concept of traits. Traits are named collection of constraints that can be used both instead and in addition to regular constraints:
125\begin{cfacode}
126trait sumable( otype T ) {
127        void ?{}(T *, zero_t);          //constructor from 0 literal
128        T ?+?(T, T);                            //assortment of additions
129        T ?+=?(T *, T);
130        T ++?(T *);
131        T ?++(T *);
132};
133forall( otype T | sumable(T) )  //use trait
134T sum(T a[], size_t size);
135\end{cfacode}
136
137Note that the type use for assertions can be either an \code{otype} or a \code{dtype}. Types declares as \code{otype} refer to ``complete'' objects, i.e., objects with a size, a default constructor, a copy constructor, a destructor and an assignment operator. Using \code{dtype} on the other hand has none of these assumptions but is extremely restrictive, it only guarantees the object is addressable.
138
139% ======================================================================
140\section{with Clause/Statement}
141Since \CFA lacks the concept of a receiver, certain functions end-up needing to repeat variable names often. To remove this inconvenience, \CFA provides the \code{with} statement, which opens an aggregate scope making its fields directly accessible (like Pascal).
142\begin{cfacode}
143struct S { int i, j; };
144int mem(S & this) with (this)           //with clause
145        i = 1;                                                  //this->i
146        j = 2;                                                  //this->j
147}
148int foo() {
149        struct S1 { ... } s1;
150        struct S2 { ... } s2;
151        with (s1)                                               //with statement
152        {
153                //access fields of s1 without qualification
154                with (s2)                                       //nesting
155                {
156                        //access fields of s1 and s2 without qualification
157                }
158        }
159        with (s1, s2)                                   //scopes open in parallel
160        {
161                //access fields of s1 and s2 without qualification
162        }
163}
164\end{cfacode}
165
166For more information on \CFA see \cite{cforall-ug,rob-thesis,www-cfa}.
Note: See TracBrowser for help on using the repository browser.