source: doc/proposals/concurrency/text/cforall.tex @ 21a1efb

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 21a1efb was 21a1efb, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Sent a draft to peter

  • Property mode set to 100644
File size: 5.4 KB
RevLine 
[7c17511]1% ======================================================================
2% ======================================================================
3\chapter{Cforall crash course}
4% ======================================================================
[21a1efb]5% ======================================================================
6
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.
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
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 :
14\begin{cfacode}
15int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
16&r1 = x,    &&r2 = r1,   &&&r3 = r2;
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
30\end{cfacode}
31The 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
35Another important feature \CFA has in common with \CC is function overloading :
36\begin{cfacode}
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)
44
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)
52\end{cfacode}
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.
54
55\section{Operators}
56Overloading 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}
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
64
65struct S { int i, j; };
66S ?+?( S op1, S op2 ) {                         // add two structures
67        return (S){ op1.i + op2.i, op1.j + op2.j };
68}
69S s1 = { 1, 2 }, s2 = { 2, 3 }, s3;
70s3 = s1 + s2;                                   // compute sum: s3 == { 2, 5 }
71\end{cfacode}
72
73Since 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}
78struct S {
79        size_t size;
80        int * ia;
81};
82void ?{}( S & s, int asize ) with s {           // constructor operator
83        size = asize;                           // initialize fields
84        ia = calloc( size, sizeof( S ) );
85}
86void ^?{}( S & s ) with s {                     // destructor operator
87        free( ia );                             // de-initialization fields
88}
89int 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}
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.
98
99For more information see \cite{cforall-ug,rob-thesis,www-cfa}.
Note: See TracBrowser for help on using the repository browser.