1 | % ====================================================================== |
---|
2 | % ====================================================================== |
---|
3 | \chapter{Cforall crash course} |
---|
4 | % ====================================================================== |
---|
5 | % ====================================================================== |
---|
6 | |
---|
7 | As mentionned in the introduction, the document presents the design for the concurrency features in \CFA. Since it is a new language here is a quick review of the language specifically tailored to the features needed to support concurrency. |
---|
8 | |
---|
9 | \CFA is a extension of ISO C and therefore supports much of the same paradigms as C. It is a non-object oriented system level language, meaning it has very most of the major abstractions have either no runtime cost or can be opt-out easily. Like C, the basics of \CFA revolve around structures and routines, which are thin abstractions over assembly. The vast majority of the code produced by a \CFA compiler respects memory-layouts and calling-conventions laid out by C. However, while \CFA is not an object-oriented language according to a strict definition. It does have some notion of objects, most importantly construction and destruction of objects. Most of the following pieces of code can be found as is on the \CFA website : \cite{www-cfa} |
---|
10 | |
---|
11 | \section{References} |
---|
12 | |
---|
13 | Like \CC, \CFA introduces references as an alternative to pointers. In regards to concurrency, the semantics difference between pointers and references aren't particularly relevant but since this document uses mostly references here is a quick overview of the semantics : |
---|
14 | \begin{cfacode} |
---|
15 | int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, |
---|
16 | &r1 = x, &&r2 = r1, &&&r3 = r2; |
---|
17 | ***p3 = 3; // change x |
---|
18 | r3 = 3; // change x, ***r3 |
---|
19 | **p3 = ...; // change p1 |
---|
20 | &r3 = ...; // change r1, (&*)**r3 |
---|
21 | *p3 = ...; // change p2 |
---|
22 | &&r3 = ...; // change r2, (&(&*)*)*r3 |
---|
23 | &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3 |
---|
24 | int y, z, & ar[3] = { x, y, z }; // initialize array of references |
---|
25 | &ar[1] = &z; // change reference array element |
---|
26 | typeof( ar[1] ) p; // is int, i.e., the type of referenced object |
---|
27 | typeof( &ar[1] ) q; // is int &, i.e., the type of reference |
---|
28 | sizeof( ar[1] ) == sizeof( int ); // is true, i.e., the size of referenced object |
---|
29 | sizeof( &ar[1] ) == sizeof( int *); // is true, i.e., the size of a reference |
---|
30 | \end{cfacode} |
---|
31 | The important thing to take away from this code snippet is that references offer a handle to an object much like pointers but which is automatically derefferenced when convinient. |
---|
32 | |
---|
33 | \section{Overloading} |
---|
34 | |
---|
35 | Another important feature \CFA has in common with \CC is function overloading : |
---|
36 | \begin{cfacode} |
---|
37 | // selection based on type and number of parameters |
---|
38 | void f( void ); // (1) |
---|
39 | void f( char ); // (2) |
---|
40 | void f( int, double ); // (3) |
---|
41 | f(); // select (1) |
---|
42 | f( 'a' ); // select (2) |
---|
43 | f( 3, 5.2 ); // select (3) |
---|
44 | |
---|
45 | // selection based on type and number of returns |
---|
46 | char f( int ); // (1) |
---|
47 | double f( int ); // (2) |
---|
48 | [ int, double ] f( int ); // (3) |
---|
49 | char c = f( 3 ); // select (1) |
---|
50 | double d = f( 4 ); // select (2) |
---|
51 | [ int, double ] t = f( 5 ); // select (3) |
---|
52 | \end{cfacode} |
---|
53 | This feature is particularly important for concurrency since the runtime system relies on creating different types do represent concurrency objects. Therefore, overloading is necessary to prevent the need for long prefixes and other naming conventions that prevent clashes. As seen in chapter \ref{basics}, the main is an example of routine that benefits from overloading when concurrency in introduced. |
---|
54 | |
---|
55 | \section{Operators} |
---|
56 | 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 would be, like so : |
---|
57 | \begin{cfacode} |
---|
58 | int ++?( int op ); // unary prefix increment |
---|
59 | int ?++( int op ); // unary postfix increment |
---|
60 | int ?+?( int op1, int op2 ); // binary plus |
---|
61 | int ?<=?( int op1, int op2 ); // binary less than |
---|
62 | int ?=?( int & op1, int op2 ); // binary assignment |
---|
63 | int ?+=?( int & op1, int op2 ); // binary plus-assignment |
---|
64 | |
---|
65 | struct S { int i, j; }; |
---|
66 | S ?+?( S op1, S op2 ) { // add two structures |
---|
67 | return (S){ op1.i + op2.i, op1.j + op2.j }; |
---|
68 | } |
---|
69 | S s1 = { 1, 2 }, s2 = { 2, 3 }, s3; |
---|
70 | s3 = s1 + s2; // compute sum: s3 == { 2, 5 } |
---|
71 | \end{cfacode} |
---|
72 | |
---|
73 | Since concurrency does not use operator overloading, this feature is more important as an introduction for the syntax of constructors. |
---|
74 | |
---|
75 | \section{Constructors/Destructors} |
---|
76 | \CFA uses the following syntax for constructors and destructors : |
---|
77 | \begin{cfacode} |
---|
78 | struct S { |
---|
79 | size_t size; |
---|
80 | int * ia; |
---|
81 | }; |
---|
82 | void ?{}( S & s, int asize ) with s { // constructor operator |
---|
83 | size = asize; // initialize fields |
---|
84 | ia = calloc( size, sizeof( S ) ); |
---|
85 | } |
---|
86 | void ^?{}( S & s ) with s { // destructor operator |
---|
87 | free( ia ); // de-initialization fields |
---|
88 | } |
---|
89 | int main() { |
---|
90 | S x = { 10 }, y = { 100 }; // implict calls: ?{}( x, 10 ), ?{}( y, 100 ) |
---|
91 | ... // use x and y |
---|
92 | ^x{}; ^y{}; // explicit calls to de-initialize |
---|
93 | x{ 20 }; y{ 200 }; // explicit calls to reinitialize |
---|
94 | ... // reuse x and y |
---|
95 | } // implict calls: ^?{}( y ), ^?{}( x ) |
---|
96 | \end{cfacode} |
---|
97 | The language guarantees that every object and all their fields are constructed. Like \CC construction is automatically done on declaration and destruction done when the declared variables reach the end of its scope. |
---|
98 | |
---|
99 | For more information see \cite{cforall-ug,rob-thesis,www-cfa}. |
---|