[7c17511] | 1 | % ====================================================================== |
---|
| 2 | % ====================================================================== |
---|
[07c1e595] | 3 | \chapter{\CFA Overview} |
---|
[7c17511] | 4 | % ====================================================================== |
---|
[21a1efb] | 5 | % ====================================================================== |
---|
| 6 | |
---|
[64b272a] | 7 | The following is a quick introduction to the \CFA language, specifically tailored to the features needed to support concurrency. |
---|
[21a1efb] | 8 | |
---|
[a2ea829] | 9 | \CFA is a 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., 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 |
---|
| 10 | values''\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} |
---|
[21a1efb] | 11 | |
---|
| 12 | \section{References} |
---|
| 13 | |
---|
[07c1e595] | 14 | Like \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: |
---|
[21a1efb] | 15 | \begin{cfacode} |
---|
| 16 | int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, |
---|
[a2ea829] | 17 | &r1 = x, &&r2 = r1, &&&r3 = r2; |
---|
[dcfc4b35] | 18 | ***p3 = 3; //change x |
---|
| 19 | r3 = 3; //change x, ***r3 |
---|
| 20 | **p3 = ...; //change p1 |
---|
| 21 | *p3 = ...; //change p2 |
---|
| 22 | int y, z, & ar[3] = {x, y, z}; //initialize array of references |
---|
| 23 | typeof( ar[1]) p; //is int, i.e., the type of referenced object |
---|
| 24 | typeof(&ar[1]) q; //is int &, i.e., the type of reference |
---|
| 25 | sizeof( ar[1]) == sizeof(int); //is true, i.e., the size of referenced object |
---|
| 26 | sizeof(&ar[1]) == sizeof(int *); //is true, i.e., the size of a reference |
---|
[21a1efb] | 27 | \end{cfacode} |
---|
[07c1e595] | 28 | The important take away from this code example is that references offer a handle to an object, much like pointers, but which is automatically dereferenced for convenience. |
---|
[21a1efb] | 29 | |
---|
| 30 | \section{Overloading} |
---|
| 31 | |
---|
[a2ea829] | 32 | Another 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. |
---|
[21a1efb] | 33 | \begin{cfacode} |
---|
[dcfc4b35] | 34 | //selection based on type and number of parameters |
---|
| 35 | void f(void); //(1) |
---|
| 36 | void f(char); //(2) |
---|
| 37 | void f(int, double); //(3) |
---|
| 38 | f(); //select (1) |
---|
| 39 | f('a'); //select (2) |
---|
| 40 | f(3, 5.2); //select (3) |
---|
[21a1efb] | 41 | |
---|
[dcfc4b35] | 42 | //selection based on type and number of returns |
---|
| 43 | char f(int); //(1) |
---|
| 44 | double f(int); //(2) |
---|
| 45 | char c = f(3); //select (1) |
---|
| 46 | double d = f(4); //select (2) |
---|
[21a1efb] | 47 | \end{cfacode} |
---|
[a2ea829] | 48 | This 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. |
---|
[21a1efb] | 49 | |
---|
| 50 | \section{Operators} |
---|
[a2ea829] | 51 | Overloading 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.: |
---|
[21a1efb] | 52 | \begin{cfacode} |
---|
[dcfc4b35] | 53 | int ++? (int op); //unary prefix increment |
---|
| 54 | int ?++ (int op); //unary postfix increment |
---|
| 55 | int ?+? (int op1, int op2); //binary plus |
---|
| 56 | int ?<=?(int op1, int op2); //binary less than |
---|
| 57 | int ?=? (int & op1, int op2); //binary assignment |
---|
| 58 | int ?+=?(int & op1, int op2); //binary plus-assignment |
---|
[21a1efb] | 59 | |
---|
[dcfc4b35] | 60 | struct S {int i, j;}; |
---|
| 61 | S ?+?(S op1, S op2) { //add two structures |
---|
| 62 | return (S){op1.i + op2.i, op1.j + op2.j}; |
---|
[21a1efb] | 63 | } |
---|
[dcfc4b35] | 64 | S s1 = {1, 2}, s2 = {2, 3}, s3; |
---|
| 65 | s3 = s1 + s2; //compute sum: s3 == {2, 5} |
---|
[21a1efb] | 66 | \end{cfacode} |
---|
[dcfc4b35] | 67 | While concurrency does not use operator overloading directly, this feature is more important as an introduction for the syntax of constructors. |
---|
[21a1efb] | 68 | |
---|
| 69 | \section{Constructors/Destructors} |
---|
[dcfc4b35] | 70 | Object 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 : |
---|
[21a1efb] | 71 | \begin{cfacode} |
---|
| 72 | struct S { |
---|
| 73 | size_t size; |
---|
| 74 | int * ia; |
---|
| 75 | }; |
---|
[dcfc4b35] | 76 | void ?{}(S & s, int asize) { //constructor operator |
---|
| 77 | s.size = asize; //initialize fields |
---|
| 78 | s.ia = calloc(size, sizeof(S)); |
---|
[21a1efb] | 79 | } |
---|
[dcfc4b35] | 80 | void ^?{}(S & s) { //destructor operator |
---|
| 81 | free(ia); //de-initialization fields |
---|
[21a1efb] | 82 | } |
---|
| 83 | int main() { |
---|
[07c1e595] | 84 | S x = {10}, y = {100}; //implicit calls: ?{}(x, 10), ?{}(y, 100) |
---|
[dcfc4b35] | 85 | ... //use x and y |
---|
| 86 | ^x{}; ^y{}; //explicit calls to de-initialize |
---|
| 87 | x{20}; y{200}; //explicit calls to reinitialize |
---|
| 88 | ... //reuse x and y |
---|
[07c1e595] | 89 | } //implicit calls: ^?{}(y), ^?{}(x) |
---|
[dcfc4b35] | 90 | \end{cfacode} |
---|
| 91 | The 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. |
---|
| 92 | \begin{cfacode} |
---|
| 93 | { |
---|
| 94 | struct S s = {10}; //allocation, call constructor |
---|
| 95 | ... |
---|
| 96 | } //deallocation, call destructor |
---|
| 97 | struct S * s = new(); //allocation, call constructor |
---|
| 98 | ... |
---|
| 99 | delete(s); //deallocation, call destructor |
---|
[21a1efb] | 100 | \end{cfacode} |
---|
[dcfc4b35] | 101 | Note 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. |
---|
[21a1efb] | 102 | |
---|
[3628765] | 103 | \section{Parametric Polymorphism} |
---|
[a2ea829] | 104 | Routines 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 : |
---|
[3628765] | 105 | \begin{cfacode} |
---|
| 106 | //constraint type, 0 and + |
---|
| 107 | forall(otype T | { void ?{}(T *, zero_t); T ?+?(T, T); }) |
---|
| 108 | T sum(T a[ ], size_t size) { |
---|
| 109 | T total = 0; //construct T from 0 |
---|
| 110 | for(size_t i = 0; i < size; i++) |
---|
| 111 | total = total + a[i]; //select appropriate + |
---|
| 112 | return total; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | S sa[5]; |
---|
| 116 | int i = sum(sa, 5); //use S's 0 construction and + |
---|
| 117 | \end{cfacode} |
---|
| 118 | |
---|
[a2ea829] | 119 | Since 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: |
---|
[3628765] | 120 | \begin{cfacode} |
---|
| 121 | trait sumable( otype T ) { |
---|
| 122 | void ?{}(T *, zero_t); //constructor from 0 literal |
---|
| 123 | T ?+?(T, T); //assortment of additions |
---|
| 124 | T ?+=?(T *, T); |
---|
| 125 | T ++?(T *); |
---|
| 126 | T ?++(T *); |
---|
| 127 | }; |
---|
| 128 | forall( otype T | sumable(T) ) //use trait |
---|
| 129 | T sum(T a[], size_t size); |
---|
| 130 | \end{cfacode} |
---|
| 131 | |
---|
| 132 | \section{with Clause/Statement} |
---|
[a2ea829] | 133 | Since \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). |
---|
[3628765] | 134 | \begin{cfacode} |
---|
| 135 | struct S { int i, j; }; |
---|
[a2ea829] | 136 | int mem(S & this) with (this) //with clause |
---|
[3628765] | 137 | i = 1; //this->i |
---|
| 138 | j = 2; //this->j |
---|
| 139 | } |
---|
| 140 | int foo() { |
---|
| 141 | struct S1 { ... } s1; |
---|
| 142 | struct S2 { ... } s2; |
---|
[a2ea829] | 143 | with (s1) //with statement |
---|
[3628765] | 144 | { |
---|
| 145 | //access fields of s1 |
---|
| 146 | //without qualification |
---|
[a2ea829] | 147 | with (s2) //nesting |
---|
[3628765] | 148 | { |
---|
| 149 | //access fields of s1 and s2 |
---|
| 150 | //without qualification |
---|
| 151 | } |
---|
| 152 | } |
---|
[a2ea829] | 153 | with (s1, s2) //scopes open in parallel |
---|
[3628765] | 154 | { |
---|
| 155 | //access fields of s1 and s2 |
---|
| 156 | //without qualification |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | \end{cfacode} |
---|
| 160 | |
---|
[dcfc4b35] | 161 | For more information on \CFA see \cite{cforall-ug,rob-thesis,www-cfa}. |
---|