Changeset d67cdb7 for doc/proposals/concurrency/text/cforall.tex
- Timestamp:
- Sep 26, 2017, 11:27:38 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 5dc26f5
- Parents:
- 201aeb9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/concurrency/text/cforall.tex
r201aeb9 rd67cdb7 15 15 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 16 16 &r1 = x, &&r2 = r1, &&&r3 = r2; 17 ***p3 = 3; 18 r3 = 3; 19 **p3 = ...; 20 &r3 = ...; 21 *p3 = ...; 22 &&r3 = ...; 23 &&&r3 = p3; 24 int y, z, & ar[3] = { x, y, z }; 25 &ar[1] = &z; 26 typeof( ar[1] ) p; 27 typeof( &ar[1] ) q; 28 sizeof( ar[1] ) == sizeof( int ); 29 sizeof( &ar[1] ) == sizeof( int *); 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 30 \end{cfacode} 31 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. … … 36 36 \begin{cfacode} 37 37 // selection based on type and number of parameters 38 void f( void ); 39 void f( char ); 40 void f( int, double ); 41 f(); 42 f( 'a' ); 43 f( 3, 5.2 ); 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 44 45 45 // selection based on type and number of returns 46 char f( int ); 47 double f( int ); 48 [ int, double ] f( int ); 49 char c = f( 3 ); 50 double d = f( 4 ); 51 [ int, double ] t = f( 5 ); 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 52 \end{cfacode} 53 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. … … 56 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 57 \begin{cfacode} 58 int ++?( int op ); 59 int ?++( int op ); 60 int ?+?( int op1, int op2 ); 61 int ?<=?( int op1, int op2 ); 62 int ?=?( int & op1, int op2 ); 63 int ?+=?( int & op1, int op2 ); 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 64 65 65 struct S { int i, j; }; 66 S ?+?( S op1, S op2 ) { 66 S ?+?( S op1, S op2 ) { // add two structures 67 67 return (S){ op1.i + op2.i, op1.j + op2.j }; 68 68 } 69 69 S s1 = { 1, 2 }, s2 = { 2, 3 }, s3; 70 s3 = s1 + s2; 70 s3 = s1 + s2; // compute sum: s3 == { 2, 5 } 71 71 \end{cfacode} 72 72 … … 74 74 75 75 \section{Constructors/Destructors} 76 \CFA uses the following syntax for constructors and destructors :76 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 : 77 77 \begin{cfacode} 78 78 struct S { … … 80 80 int * ia; 81 81 }; 82 void ?{}( S & s, int asize ) with s { 83 size = asize; 82 void ?{}( S & s, int asize ) with s { // constructor operator 83 size = asize; // initialize fields 84 84 ia = calloc( size, sizeof( S ) ); 85 85 } 86 void ^?{}( S & s ) with s { 87 free( ia ); 86 void ^?{}( S & s ) with s { // destructor operator 87 free( ia ); // de-initialization fields 88 88 } 89 89 int main() { 90 S x = { 10 }, y = { 100 }; 91 ... 92 ^x{}; ^y{}; 93 x{ 20 }; y{ 200 }; 94 ... 95 } 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 96 \end{cfacode} 97 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.
Note: See TracChangeset
for help on using the changeset viewer.