Ignore:
File:
1 edited

Legend:

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

    rcf966b5 ra2ea829  
    11% ======================================================================
    22% ======================================================================
    3 \chapter{\CFA Overview}
     3\chapter{Cforall Overview}
    44% ======================================================================
    55% ======================================================================
     
    77The following is a quick introduction to the \CFA language, specifically tailored to the features needed to support concurrency.
    88
    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
    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}
     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
     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}
    1111
    12 % ======================================================================
    1312\section{References}
    1413
    15 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:
     14Like \CC, \CFA introduces rebindable references providing multiple dereferecing 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:
    1615\begin{cfacode}
    1716int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
     
    2221*p3   = ...;                                            //change p2
    2322int y, z, & ar[3] = {x, y, z};          //initialize array of references
    24 typeof( ar[1]) p;                                       //is int, referenced object type
    25 typeof(&ar[1]) q;                                       //is int &, reference type
    26 sizeof( ar[1]) == sizeof(int);          //is true, referenced object size
    27 sizeof(&ar[1]) == sizeof(int *);        //is true, reference size
     23typeof( ar[1]) p;                                       //is int, i.e., the type of referenced object
     24typeof(&ar[1]) q;                                       //is int &, i.e., the type of reference
     25sizeof( ar[1]) == sizeof(int);          //is true, i.e., the size of referenced object
     26sizeof(&ar[1]) == sizeof(int *);        //is true, i.e., the size of a reference
    2827\end{cfacode}
    29 The 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.
     28The 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 convinience.
    3029
    31 % ======================================================================
    3230\section{Overloading}
    3331
    34 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.
     32Another 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.
    3533\begin{cfacode}
    3634//selection based on type and number of parameters
     
    5048This 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.
    5149
    52 % ======================================================================
    5350\section{Operators}
    5451Overloading 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.:
     
    7067While concurrency does not use operator overloading directly, this feature is more important as an introduction for the syntax of constructors.
    7168
    72 % ======================================================================
    7369\section{Constructors/Destructors}
    7470Object 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 :
     
    8682}
    8783int main() {
    88         S x = {10}, y = {100};          //implicit calls: ?{}(x, 10), ?{}(y, 100)
     84        S x = {10}, y = {100};          //implict calls: ?{}(x, 10), ?{}(y, 100)
    8985        ...                                                     //use x and y
    9086        ^x{};  ^y{};                            //explicit calls to de-initialize
    9187        x{20};  y{200};                         //explicit calls to reinitialize
    9288        ...                                                     //reuse x and y
    93 }                                                               //implicit calls: ^?{}(y), ^?{}(x)
     89}                                                               //implict calls: ^?{}(y), ^?{}(x)
    9490\end{cfacode}
    9591The 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.
     
    10399delete(s);                              //deallocation, call destructor
    104100\end{cfacode}
    105 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.
     101Note 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.
    106102
    107 % ======================================================================
    108103\section{Parametric Polymorphism}
    109 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 :
     104Routines 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 :
    110105\begin{cfacode}
    111106//constraint type, 0 and +
     
    135130\end{cfacode}
    136131
    137 Note 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 % ======================================================================
    140132\section{with Clause/Statement}
    141133Since \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).
     
    143135struct S { int i, j; };
    144136int mem(S & this) with (this)           //with clause
    145         i = 1;                                                  //this->i
    146         j = 2;                                                  //this->j
     137        i = 1;                                          //this->i
     138        j = 2;                                          //this->j
    147139}
    148140int foo() {
    149141        struct S1 { ... } s1;
    150142        struct S2 { ... } s2;
    151         with (s1)                                               //with statement
     143        with (s1)                                       //with statement
    152144        {
    153                 //access fields of s1 without qualification
     145                //access fields of s1
     146                //without qualification
    154147                with (s2)                                       //nesting
    155148                {
    156                         //access fields of s1 and s2 without qualification
     149                        //access fields of s1 and s2
     150                        //without qualification
    157151                }
    158152        }
    159         with (s1, s2)                                   //scopes open in parallel
     153        with (s1, s2)                           //scopes open in parallel
    160154        {
    161                 //access fields of s1 and s2 without qualification
     155                //access fields of s1 and s2
     156                //without qualification
    162157        }
    163158}
Note: See TracChangeset for help on using the changeset viewer.