% ====================================================================== % ====================================================================== \chapter{Cforall crash course} % ====================================================================== % ====================================================================== 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. \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} \section{References} 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 : \begin{cfacode} int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, &r1 = x, &&r2 = r1, &&&r3 = r2; ***p3 = 3; // change x r3 = 3; // change x, ***r3 **p3 = ...; // change p1 &r3 = ...; // change r1, (&*)**r3 *p3 = ...; // change p2 &&r3 = ...; // change r2, (&(&*)*)*r3 &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3 int y, z, & ar[3] = { x, y, z }; // initialize array of references &ar[1] = &z; // change reference array element typeof( ar[1] ) p; // is int, i.e., the type of referenced object typeof( &ar[1] ) q; // is int &, i.e., the type of reference sizeof( ar[1] ) == sizeof( int ); // is true, i.e., the size of referenced object sizeof( &ar[1] ) == sizeof( int *); // is true, i.e., the size of a reference \end{cfacode} 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. \section{Overloading} Another important feature \CFA has in common with \CC is function overloading : \begin{cfacode} // selection based on type and number of parameters void f( void ); // (1) void f( char ); // (2) void f( int, double ); // (3) f(); // select (1) f( 'a' ); // select (2) f( 3, 5.2 ); // select (3) // selection based on type and number of returns char f( int ); // (1) double f( int ); // (2) [ int, double ] f( int ); // (3) char c = f( 3 ); // select (1) double d = f( 4 ); // select (2) [ int, double ] t = f( 5 ); // select (3) \end{cfacode} 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. \section{Operators} 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 : \begin{cfacode} int ++?( int op ); // unary prefix increment int ?++( int op ); // unary postfix increment int ?+?( int op1, int op2 ); // binary plus int ?<=?( int op1, int op2 ); // binary less than int ?=?( int & op1, int op2 ); // binary assignment int ?+=?( int & op1, int op2 ); // binary plus-assignment struct S { int i, j; }; S ?+?( S op1, S op2 ) { // add two structures return (S){ op1.i + op2.i, op1.j + op2.j }; } S s1 = { 1, 2 }, s2 = { 2, 3 }, s3; s3 = s1 + s2; // compute sum: s3 == { 2, 5 } \end{cfacode} Since concurrency does not use operator overloading, this feature is more important as an introduction for the syntax of constructors. \section{Constructors/Destructors} 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 \& Destructors are a the core of the features required for concurrency and parallelism. \CFA uses the following syntax for constructors and destructors : \begin{cfacode} struct S { size_t size; int * ia; }; void ?{}( S & s, int asize ) with s { // constructor operator size = asize; // initialize fields ia = calloc( size, sizeof( S ) ); } void ^?{}( S & s ) with s { // destructor operator free( ia ); // de-initialization fields } int main() { S x = { 10 }, y = { 100 }; // implict calls: ?{}( x, 10 ), ?{}( y, 100 ) ... // use x and y ^x{}; ^y{}; // explicit calls to de-initialize x{ 20 }; y{ 200 }; // explicit calls to reinitialize ... // reuse x and y } // implict calls: ^?{}( y ), ^?{}( x ) \end{cfacode} 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. For more information see \cite{cforall-ug,rob-thesis,www-cfa}.