Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/concurrency/text/cforall.tex

    r3628765 r21a1efb  
    55% ======================================================================
    66
    7 This thesis presents the design for a set of concurrency features in \CFA. Since it is a new dialect of C, the following is a quick introduction to the language, specifically tailored to the features needed to support concurrency.
     7As 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.
    88
    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 received (e.g.: this), it does have some notion of objects\footnote{C defines the term objects as : [Where to I get the C11 reference manual?]}, most importantly construction and destruction of objects. Most of the following pieces of code can be found on the \CFA website \cite{www-cfa}
     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}
    1010
    1111\section{References}
    1212
    13 Like \CC, \CFA introduces references as an alternative to pointers. In regards to concurrency, the semantics difference between pointers and references are not particularly relevant but since this document uses mostly references here is a quick overview of the semantics :
     13Like \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 :
    1414\begin{cfacode}
    1515int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
    1616&r1 = x,    &&r2 = r1,   &&&r3 = r2;
    17 ***p3 = 3;                                                      //change x
    18 r3    = 3;                                                      //change x, ***r3
    19 **p3  = ...;                                            //change p1
    20 *p3   = ...;                                            //change p2
    21 int y, z, & ar[3] = {x, y, z};          //initialize array of references
    22 typeof( ar[1]) p;                                       //is int, i.e., the type of referenced object
    23 typeof(&ar[1]) q;                                       //is int &, i.e., the type of reference
    24 sizeof( ar[1]) == sizeof(int);          //is true, i.e., the size of referenced object
    25 sizeof(&ar[1]) == sizeof(int *);        //is true, i.e., the size of a reference
     17***p3 = 3;                                      // change x
     18r3 = 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
     24int y, z, & ar[3] = { x, y, z };                // initialize array of references
     25&ar[1] = &z;                                    // change reference array element
     26typeof( ar[1] ) p;                              // is int, i.e., the type of referenced object
     27typeof( &ar[1] ) q;                             // is int &, i.e., the type of reference
     28sizeof( ar[1] ) == sizeof( int );               // is true, i.e., the size of referenced object
     29sizeof( &ar[1] ) == sizeof( int *);             // is true, i.e., the size of a reference
    2630\end{cfacode}
    2731The 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.
     
    2933\section{Overloading}
    3034
    31 Another important feature of \CFA is function overloading as in Java and \CC, where routine with the same name are selected based on the numbers 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.
     35Another important feature \CFA has in common with \CC is function overloading :
    3236\begin{cfacode}
    33 //selection based on type and number of parameters
    34 void f(void);                   //(1)
    35 void f(char);                   //(2)
    36 void f(int, double);    //(3)
    37 f();                                    //select (1)
    38 f('a');                                 //select (2)
    39 f(3, 5.2);                              //select (3)
     37// selection based on type and number of parameters
     38void f( void );                                 // (1)
     39void f( char );                                 // (2)
     40void f( int, double );                          // (3)
     41f();                                            // select (1)
     42f( 'a' );                                       // select (2)
     43f( 3, 5.2 );                                    // select (3)
    4044
    41 //selection based on  type and number of returns
    42 char   f(int);                  //(1)
    43 double f(int);                  //(2)
    44 char   c = f(3);                //select (1)
    45 double d = f(4);                //select (2)
     45// selection based on  type and number of returns
     46char f( int );                                  // (1)
     47double f( int );                                // (2)
     48[ int, double ] f( int );                       // (3)
     49char c = f( 3 );                                // select (1)
     50double d = f( 4 );                              // select (2)
     51[ int, double ] t = f( 5 );                     // select (3)
    4652\end{cfacode}
    47 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}, routines main is an example that benefits from overloading.
     53This 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.
    4854
    4955\section{Operators}
    5056Overloading 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 :
    5157\begin{cfacode}
    52 int ++? (int op);                       //unary prefix increment
    53 int ?++ (int op);                       //unary postfix increment
    54 int ?+? (int op1, int op2);             //binary plus
    55 int ?<=?(int op1, int op2);             //binary less than
    56 int ?=? (int & op1, int op2);           //binary assignment
    57 int ?+=?(int & op1, int op2);           //binary plus-assignment
     58int ++?( int op );                              // unary prefix increment
     59int ?++( int op );                              // unary postfix increment
     60int ?+?( int op1, int op2 );                    // binary plus
     61int ?<=?( int op1, int op2 );                   // binary less than
     62int ?=?( int & op1, int op2 );                  // binary assignment
     63int ?+=?( int & op1, int op2 );                 // binary plus-assignment
    5864
    59 struct S {int i, j;};
    60 S ?+?(S op1, S op2) {                           //add two structures
    61         return (S){op1.i + op2.i, op1.j + op2.j};
     65struct S { int i, j; };
     66S ?+?( S op1, S op2 ) {                         // add two structures
     67        return (S){ op1.i + op2.i, op1.j + op2.j };
    6268}
    63 S s1 = {1, 2}, s2 = {2, 3}, s3;
    64 s3 = s1 + s2;                                           //compute sum: s3 == {2, 5}
     69S s1 = { 1, 2 }, s2 = { 2, 3 }, s3;
     70s3 = s1 + s2;                                   // compute sum: s3 == { 2, 5 }
    6571\end{cfacode}
    66 While concurrency does not use operator overloading directly, this feature is more important as an introduction for the syntax of constructors.
     72
     73Since concurrency does not use operator overloading, this feature is more important as an introduction for the syntax of constructors.
    6774
    6875\section{Constructors/Destructors}
    69 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 :
     76\CFA uses the following syntax for constructors and destructors :
    7077\begin{cfacode}
    7178struct S {
     
    7380        int * ia;
    7481};
    75 void ?{}(S & s, int asize) {    //constructor operator
    76         s.size = asize;                         //initialize fields
    77         s.ia = calloc(size, sizeof(S));
     82void ?{}( S & s, int asize ) with s {           // constructor operator
     83        size = asize;                           // initialize fields
     84        ia = calloc( size, sizeof( S ) );
    7885}
    79 void ^?{}(S & s) {                              //destructor operator
    80         free(ia);                                       //de-initialization fields
     86void ^?{}( S & s ) with s {                     // destructor operator
     87        free( ia );                             // de-initialization fields
    8188}
    8289int main() {
    83         S x = {10}, y = {100};          //implict calls: ?{}(x, 10), ?{}(y, 100)
    84         ...                                                     //use x and y
    85         ^x{};  ^y{};                            //explicit calls to de-initialize
    86         x{20};  y{200};                         //explicit calls to reinitialize
    87         ...                                                     //reuse x and y
    88 }                                                               //implict calls: ^?{}(y), ^?{}(x)
     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 )
    8996\end{cfacode}
    90 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.
    91 \begin{cfacode}
    92 {
    93         struct S s = {10};      //allocation, call constructor
    94         ...
    95 }                                               //deallocation, call destructor
    96 struct S * s = new();   //allocation, call constructor
    97 ...
    98 delete(s);                              //deallocation, call destructor
    99 \end{cfacode}
    100 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.
     97The 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.
    10198
    102 \section{Parametric Polymorphism}
    103 Routines in \CFA can also be reused for multiple types. This is done using the \code{forall} clause which gives \CFA it's name. \code{forall} clauses allow seperatly compiled routines to support generic usage over multiple types. For example, the following sum function will work for any type which support construction from 0 and addition :
    104 \begin{cfacode}
    105 //constraint type, 0 and +
    106 forall(otype T | { void ?{}(T *, zero_t); T ?+?(T, T); })
    107 T sum(T a[ ], size_t size) {
    108         T total = 0;                            //construct T from 0
    109         for(size_t i = 0; i < size; i++)
    110                 total = total + a[i];   //select appropriate +
    111         return total;
    112 }
    113 
    114 S sa[5];
    115 int i = sum(sa, 5);                             //use S's 0 construction and +
    116 \end{cfacode}
    117 
    118 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 which can be used both instead and in addition to regular constraints:
    119 \begin{cfacode}
    120 trait sumable( otype T ) {
    121         void ?{}(T *, zero_t);          //constructor from 0 literal
    122         T ?+?(T, T);                            //assortment of additions
    123         T ?+=?(T *, T);
    124         T ++?(T *);
    125         T ?++(T *);
    126 };
    127 forall( otype T | sumable(T) )  //use trait
    128 T sum(T a[], size_t size);
    129 \end{cfacode}
    130 
    131 \section{with Clause/Statement}
    132 Since \CFA lacks the concept of a receiver, certain functions end-up needing to repeat variable names often, to solve this \CFA offers the \code{with} statement which opens an aggregate scope making its fields directly accessible (like Pascal).
    133 \begin{cfacode}
    134 struct S { int i, j; };
    135 int mem(S & this) with this             //with clause
    136         i = 1;                                          //this->i
    137         j = 2;                                          //this->j
    138 }
    139 int foo() {
    140         struct S1 { ... } s1;
    141         struct S2 { ... } s2;
    142         with s1                                         //with statement
    143         {
    144                 //access fields of s1
    145                 //without qualification
    146                 with s2                                 //nesting
    147                 {
    148                         //access fields of s1 and s2
    149                         //without qualification
    150                 }
    151         }
    152         with s1, s2                             //scopes open in parallel
    153         {
    154                 //access fields of s1 and s2
    155                 //without qualification
    156         }
    157 }
    158 \end{cfacode}
    159 
    160 For more information on \CFA see \cite{cforall-ug,rob-thesis,www-cfa}.
     99For more information see \cite{cforall-ug,rob-thesis,www-cfa}.
Note: See TracChangeset for help on using the changeset viewer.