- Timestamp:
- Jun 16, 2017, 12:01:18 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, 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:
- f13ee31
- Parents:
- 46e4440e
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
r46e4440e r816d61c 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Tue Jun 13 11:50:27201714 %% Update Count : 24 0313 %% Last Modified On : Fri Jun 16 12:00:01 2017 14 %% Update Count : 2433 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 43 43 \usepackage[pagewise]{lineno} 44 44 \renewcommand{\linenumberfont}{\scriptsize\sffamily} 45 \input{common} % bespoke macros used in the document45 \input{common} % common CFA document macros 46 46 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref} 47 47 \usepackage{breakurl} … … 110 110 \renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}} 111 111 \pagenumbering{roman} 112 \linenumbers % comment out to turn off line numbering112 %\linenumbers % comment out to turn off line numbering 113 113 114 114 \maketitle … … 881 881 882 882 883 \subsection{Initialization} 884 885 \Index{Initialization} is different than \Index{assignment} because initialization occurs on the empty (uninitialized) storage on an object, while assignment occurs on possibly initialized storage of an object. 886 There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding. 887 Because the object being initialized has no value, there is only one meaningful semantics with respect to address duality: it must mean address as there is no pointed-to value. 888 In contrast, the left-hand side of assignment has an address that has a duality. 889 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 890 \begin{cfa} 891 int * p = &x; §\C{// assign address of x}§ 892 ®int * p = x;® §\C{// assign value of x}§ 893 int & r = x; §\C{// must have address of x}§ 894 \end{cfa} 895 Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of ©x© into a pointer is meaningful (again, a warning is usually given). 896 Therefore, for safety, this context requires an address, so it is superfluous to require explicitly taking the address of the initialization object, even though the type is incorrect. 897 Note, this is strictly a convenience and safety feature for a programmer. 898 Hence, \CFA allows ©r© to be assigned ©x© because it infers a reference for ©x©, by implicitly inserting a address-of operator, ©&©, and it is an error to put an ©&© because the types no longer match due to the implicit dereference. 899 Unfortunately, C allows ©p© to be assigned with ©&x© (address) or ©x© (value), but most compilers warn about the latter assignment as being potentially incorrect. 900 Similarly, when a reference type is used for a parameter/return type, the call-site argument does not require a reference operator for the same reason. 901 \begin{cfa} 902 int & f( int & r ); §\C{// reference parameter and return}§ 903 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§ 904 \end{cfa} 905 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©. 906 Since operator routine ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references. 907 \begin{cfa} 908 int temp1 = f( x ), temp2 = f( y ); 909 z = temp1 + temp2; 910 \end{cfa} 911 This \Index{implicit referencing} is crucial for reducing the syntactic burden for programmers when using references; 912 otherwise references have the same syntactic burden as pointers in these contexts. 913 914 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. 915 \begin{cfa} 916 void f( ®const® int & cr ); 917 void g( ®const® int * cp ); 918 f( 3 ); g( ®&®3 ); 919 f( x + y ); g( ®&®(x + y) ); 920 \end{cfa} 921 Here, the compiler passes the address to the literal 3 or the temporary for the expression ©x + y©, knowing the argument cannot be changed through the parameter. 922 The ©&© before the constant/expression for the pointer-type parameter (©g©) is a \CFA extension necessary to type match and is a common requirement before a variable in C (\eg ©scanf©). 923 Importantly, ©&3© may not be equal to ©&3©, where the references occur across calls because the temporaries maybe different on each call. 924 925 \CFA \emph{extends} this semantics to a mutable pointer/reference parameter, and the compiler implicitly creates the necessary temporary (copying the argument), which is subsequently pointed-to by the reference parameter and can be changed.\footnote{ 926 If whole program analysis is possible, and shows the parameter is not assigned, \ie it is ©const©, the temporary is unnecessary.} 927 \begin{cfa} 928 void f( int & r ); 929 void g( int * p ); 930 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§ 931 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§ 932 \end{cfa} 933 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ 934 This conversion attempts to address the \newterm{const hell} problem, when the innocent addition of a ©const© qualifier causes a cascade of type failures, requiring an unknown number of additional ©const© qualifiers, until it is discovered a ©const© qualifier cannot be added and all the ©const© qualifiers must be removed.} 935 The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. 936 937 %\CFA attempts to handle pointers and references in a uniform, symmetric manner. 938 Finally, C handles \Index{routine object}s in an inconsistent way. 939 A routine object is both a pointer and a reference (\Index{particle and wave}). 940 \begin{cfa} 941 void f( int i ); 942 void (*fp)( int ); §\C{// routine pointer}§ 943 fp = f; §\C{// reference initialization}§ 944 fp = &f; §\C{// pointer initialization}§ 945 fp = *f; §\C{// reference initialization}§ 946 fp(3); §\C{// reference invocation}§ 947 (*fp)(3); §\C{// pointer invocation}§ 948 \end{cfa} 949 While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (©f©, ©&f©, ©*f©) without regard for type. 950 Instead, a routine object should be referenced by a ©const© reference: 951 \begin{cfa} 952 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§ 953 fr = ... §\C{// error, cannot change code}§ 954 &fr = ...; §\C{// changing routine reference}§ 955 fr( 3 ); §\C{// reference call to f}§ 956 (*fr)(3); §\C{// error, incorrect type}§ 957 \end{cfa} 958 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ 959 Dynamic code rewriting is possible but only in special circumstances.} 960 \CFA allows this additional use of references for routine objects in an attempt to give a more consistent meaning for them. 961 962 883 963 \subsection{Address-of Semantics} 884 964 … … 958 1038 959 1039 960 \subsection{Initialization}961 962 \Index{Initialization} is different than \Index{assignment} because initialization occurs on the empty (uninitialized) storage on an object, while assignment occurs on possibly initialized storage of an object.963 There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding.964 Because the object being initialized has no value, there is only one meaningful semantics with respect to address duality: it must mean address as there is no pointed-to value.965 In contrast, the left-hand side of assignment has an address that has a duality.966 Therefore, for pointer/reference initialization, the initializing value must be an address not a value.967 \begin{cfa}968 int * p = &x; §\C{// assign address of x}§969 ®int * p = x;® §\C{// assign value of x}§970 int & r = x; §\C{// must have address of x}§971 \end{cfa}972 Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of ©x© into a pointer is meaningful (again, a warning is usually given).973 Therefore, for safety, this context requires an address, so it is superfluous to require explicitly taking the address of the initialization object, even though the type is incorrect.974 Note, this is strictly a convenience and safety feature for a programmer.975 Hence, \CFA allows ©r© to be assigned ©x© because it infers a reference for ©x©, by implicitly inserting a address-of operator, ©&©, and it is an error to put an ©&© because the types no longer match due to the implicit dereference.976 Unfortunately, C allows ©p© to be assigned with ©&x© (address) or ©x© (value), but most compilers warn about the latter assignment as being potentially incorrect.977 Similarly, when a reference type is used for a parameter/return type, the call-site argument does not require a reference operator for the same reason.978 \begin{cfa}979 int & f( int & r ); §\C{// reference parameter and return}§980 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§981 \end{cfa}982 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©.983 Since operator routine ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references.984 \begin{cfa}985 int temp1 = f( x ), temp2 = f( y );986 z = temp1 + temp2;987 \end{cfa}988 This \Index{implicit referencing} is crucial for reducing the syntactic burden for programmers when using references;989 otherwise references have the same syntactic burden as pointers in these contexts.990 991 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions.992 \begin{cfa}993 void f( ®const® int & cr );994 void g( ®const® int * cp );995 f( 3 ); g( ®&®3 );996 f( x + y ); g( ®&®(x + y) );997 \end{cfa}998 Here, the compiler passes the address to the literal 3 or the temporary for the expression ©x + y©, knowing the argument cannot be changed through the parameter.999 The ©&© before the constant/expression for the pointer-type parameter (©g©) is a \CFA extension necessary to type match and is a common requirement before a variable in C (\eg ©scanf©).1000 Importantly, ©&3© may not be equal to ©&3©, where the references occur across calls because the temporaries maybe different on each call.1001 1002 \CFA \emph{extends} this semantics to a mutable pointer/reference parameter, and the compiler implicitly creates the necessary temporary (copying the argument), which is subsequently pointed-to by the reference parameter and can be changed.\footnote{1003 If whole program analysis is possible, and shows the parameter is not assigned, \ie it is ©const©, the temporary is unnecessary.}1004 \begin{cfa}1005 void f( int & r );1006 void g( int * p );1007 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§1008 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§1009 \end{cfa}1010 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{1011 This conversion attempts to address the \newterm{const hell} problem, when the innocent addition of a ©const© qualifier causes a cascade of type failures, requiring an unknown number of additional ©const© qualifiers, until it is discovered a ©const© qualifier cannot be added and all the ©const© qualifiers must be removed.}1012 The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call.1013 1014 %\CFA attempts to handle pointers and references in a uniform, symmetric manner.1015 Finally, C handles \Index{routine object}s in an inconsistent way.1016 A routine object is both a pointer and a reference (\Index{particle and wave}).1017 \begin{cfa}1018 void f( int i );1019 void (*fp)( int ); §\C{// routine pointer}§1020 fp = f; §\C{// reference initialization}§1021 fp = &f; §\C{// pointer initialization}§1022 fp = *f; §\C{// reference initialization}§1023 fp(3); §\C{// reference invocation}§1024 (*fp)(3); §\C{// pointer invocation}§1025 \end{cfa}1026 While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (©f©, ©&f©, ©*f©) without regard for type.1027 Instead, a routine object should be referenced by a ©const© reference:1028 \begin{cfa}1029 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§1030 fr = ... §\C{// error, cannot change code}§1031 &fr = ...; §\C{// changing routine reference}§1032 fr( 3 ); §\C{// reference call to f}§1033 (*fr)(3); §\C{// error, incorrect type}§1034 \end{cfa}1035 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{1036 Dynamic code rewriting is possible but only in special circumstances.}1037 \CFA allows this additional use of references for routine objects in an attempt to give a more consistent meaning for them.1038 1039 1040 1041 1040 \begin{comment} 1042 \section{References}1043 1044 By introducing references in parameter types, users are given an easy way to pass a value by reference, without the need for NULL pointer checks.1045 In structures, a reference can replace a pointer to an object that should always have a valid value.1046 When a structure contains a reference, all of its constructors must initialize the reference and all instances of this structure must initialize it upon definition.1047 1048 The syntax for using references in \CFA is the same as \CC with the exception of reference initialization.1049 Use ©&© to specify a reference, and access references just like regular objects, not like pointers (use dot notation to access fields).1050 When initializing a reference, \CFA uses a different syntax which differentiates reference initialization from assignment to a reference.1051 The ©&© is used on both sides of the expression to clarify that the address of the reference is being set to the address of the variable to which it refers.1052 1053 1054 1041 From: Richard Bilson <rcbilson@gmail.com> 1055 1042 Date: Wed, 13 Jul 2016 01:58:58 +0000 … … 1213 1200 \section{Routine Definition} 1214 1201 1215 \CFA also supports a new syntax for routine definition, as well as ISO Cand K\&R routine syntax.1202 \CFA also supports a new syntax for routine definition, as well as \Celeven and K\&R routine syntax. 1216 1203 The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: 1217 1204 \begin{cfa} … … 1233 1220 in both cases the type is assumed to be void as opposed to old style C defaults of int return type and unknown parameter types, respectively, as in: 1234 1221 \begin{cfa} 1235 [§\,§] g(); §\C{// no input or output parameters}§1236 [ void ] g( void ); §\C{// no input or output parameters}§1222 [§\,§] g(); §\C{// no input or output parameters}§ 1223 [ void ] g( void ); §\C{// no input or output parameters}§ 1237 1224 \end{cfa} 1238 1225 … … 1252 1239 \begin{cfa} 1253 1240 typedef int foo; 1254 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§1241 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 1255 1242 \end{cfa} 1256 1243 The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo. … … 1260 1247 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 1261 1248 \begin{cfa} 1262 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§1263 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§1249 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§ 1250 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§ 1264 1251 \end{cfa} 1265 1252 The reason for allowing both declaration styles in the new context is for backwards compatibility with existing preprocessor macros that generate C-style declaration-syntax, as in: 1266 1253 \begin{cfa} 1267 1254 #define ptoa( n, d ) int (*n)[ d ] 1268 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§1269 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§1255 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§ 1256 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§ 1270 1257 \end{cfa} 1271 1258 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. … … 1289 1276 int z; 1290 1277 ... x = 0; ... y = z; ... 1291 ®return;® 1278 ®return;® §\C{// implicitly return x, y}§ 1292 1279 } 1293 1280 \end{cfa} … … 1299 1286 [ int x, int y ] f() { 1300 1287 ... 1301 } 1288 } §\C{// implicitly return x, y}§ 1302 1289 \end{cfa} 1303 1290 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. 1291 1292 Named return values may be used in conjunction with named parameter values; 1293 specifically, a return and parameter can have the same name. 1294 \begin{cfa} 1295 [ int x, int y ] f( int, x, int y ) { 1296 ... 1297 } §\C{// implicitly return x, y}§ 1298 \end{cfa} 1299 This notation allows the compiler to eliminate temporary variables in nested routine calls. 1300 \begin{cfa} 1301 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§ 1302 int a, b; 1303 [a, b] = f( f( f( a, b ) ) ); 1304 \end{cfa} 1305 While the compiler normally ignores parameters names in prototype declarations, here they are used to eliminate temporary return-values by inferring that the results of each call are the inputs of the next call, and ultimately, the left-hand side of the assignment. 1306 Hence, even without the body of routine ©f© (separate compilation), it is possible to perform a global optimization across routine calls. 1307 The compiler warns about naming inconsistencies between routine prototype and definition in this case, and behaviour is \Index{undefined} if the programmer is inconsistent. 1304 1308 1305 1309 … … 1309 1313 as well, parameter names are optional, \eg: 1310 1314 \begin{cfa} 1311 [ int x ] f (); §\C{// returning int with no parameters}§1312 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§1313 [ ] h ( int,char);§\C{// returning no result with int and char parameters}§1314 [ * int, int ] j (int);§\C{// returning pointer to int and int, with int parameter}§1315 [ int x ] f (); §\C{// returning int with no parameters}§ 1316 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§ 1317 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§ 1318 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§ 1315 1319 \end{cfa} 1316 1320 This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). … … 1320 1324 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1321 1325 \begin{cfa} 1322 [ int ] f( int), g;1326 [ int ] f( int ), g; 1323 1327 \end{cfa} 1324 1328 & 1325 1329 \begin{cfa} 1326 int f( int), g(int);1330 int f( int ), g( int ); 1327 1331 \end{cfa} 1328 1332 \end{tabular} … … 1330 1334 Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg: 1331 1335 \begin{cfa} 1332 extern [ int ] f ( int);1333 static [ int ] g ( int);1336 extern [ int ] f ( int ); 1337 static [ int ] g ( int ); 1334 1338 \end{cfa} 1335 1339 … … 1339 1343 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 1340 1344 \begin{cfa} 1341 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§1342 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§1343 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§1344 * [ * int,int ] ( int) jp;§\C{// pointer to routine returning pointer to int and int, with int parameter}§1345 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§ 1346 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§ 1347 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§ 1348 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§ 1345 1349 \end{cfa} 1346 1350 While parameter names are optional, \emph{a routine name cannot be specified}; 1347 1351 for example, the following is incorrect: 1348 1352 \begin{cfa} 1349 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§1353 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§ 1350 1354 \end{cfa} 1351 1355 … … 1534 1538 int ; §\C{// disallowed, unnamed field}§ 1535 1539 int *; §\C{// disallowed, unnamed field}§ 1536 int (*)( int); §\C{// disallowed, unnamed field}§1540 int (*)( int ); §\C{// disallowed, unnamed field}§ 1537 1541 }; 1538 1542 \end{cfa} … … 1657 1661 } 1658 1662 int main() { 1659 * [int]( int) fp = foo(); §\C{// int (*fp)(int)}§1663 * [int]( int ) fp = foo(); §\C{// int (*fp)( int )}§ 1660 1664 sout | fp( 3 ) | endl; 1661 1665 } … … 2778 2782 2779 2783 2780 \s ubsection{Constructors and Destructors}2784 \section{Constructors and Destructors} 2781 2785 2782 2786 \CFA supports C initialization of structures, but it also adds constructors for more advanced initialization. … … 3109 3113 3110 3114 3115 \begin{comment} 3111 3116 \section{Generics} 3112 3117 … … 3315 3320 } 3316 3321 \end{cfa} 3322 \end{comment} 3317 3323 3318 3324 … … 3374 3380 Complex *p3 = new(0.5, 1.0); // allocate + 2 param constructor 3375 3381 } 3376 3377 3382 \end{cfa} 3378 3383 … … 3386 3391 3387 3392 3393 \begin{comment} 3388 3394 \subsection{Unsafe C Constructs} 3389 3395 … … 3396 3402 The exact set of unsafe C constructs that will be disallowed in \CFA has not yet been decided, but is sure to include pointer arithmetic, pointer casting, etc. 3397 3403 Once the full set is decided, the rules will be listed here. 3404 \end{comment} 3398 3405 3399 3406 3400 3407 \section{Concurrency} 3401 3402 Today's processors for nearly all use cases, ranging from embedded systems to large cloud computing servers, are composed of multiple cores, often heterogeneous.3403 As machines grow in complexity, it becomes more difficult for a program to make the most use of the hardware available.3404 \CFA includes built-in concurrency features to enable high performance and improve programmer productivity on these multi-/many-core machines.3405 3408 3406 3409 Concurrency support in \CFA is implemented on top of a highly efficient runtime system of light-weight, M:N, user level threads. … … 3409 3412 This enables a very familiar interface to all programmers, even those with no parallel programming experience. 3410 3413 It also allows the compiler to do static type checking of all communication, a very important safety feature. 3411 This controlled communication with type safety has some similarities with channels in \Index*{Go}, and can actually implement 3412 channels exactly, as well as create additional communication patterns that channels cannot. 3414 This controlled communication with type safety has some similarities with channels in \Index*{Go}, and can actually implement channels exactly, as well as create additional communication patterns that channels cannot. 3413 3415 Mutex objects, monitors, are used to contain mutual exclusion within an object and synchronization across concurrent threads. 3414 3416 3415 Three new keywords are added to support these features: 3416 3417 monitor creates a structure with implicit locking when accessing fields 3418 3419 mutex implies use of a monitor requiring the implicit locking 3420 3421 task creates a type with implicit locking, separate stack, and a thread 3417 \begin{figure} 3418 \begin{cfa} 3419 #include <fstream> 3420 #include <coroutine> 3421 3422 coroutine Fibonacci { 3423 int fn; §\C{// used for communication}§ 3424 }; 3425 void ?{}( Fibonacci * this ) { 3426 this->fn = 0; 3427 } 3428 void main( Fibonacci * this ) { 3429 int fn1, fn2; §\C{// retained between resumes}§ 3430 this->fn = 0; §\C{// case 0}§ 3431 fn1 = this->fn; 3432 suspend(); §\C{// return to last resume}§ 3433 3434 this->fn = 1; §\C{// case 1}§ 3435 fn2 = fn1; 3436 fn1 = this->fn; 3437 suspend(); §\C{// return to last resume}§ 3438 3439 for ( ;; ) { §\C{// general case}§ 3440 this->fn = fn1 + fn2; 3441 fn2 = fn1; 3442 fn1 = this->fn; 3443 suspend(); §\C{// return to last resume}§ 3444 } // for 3445 } 3446 int next( Fibonacci * this ) { 3447 resume( this ); §\C{// transfer to last suspend}§ 3448 return this->fn; 3449 } 3450 int main() { 3451 Fibonacci f1, f2; 3452 for ( int i = 1; i <= 10; i += 1 ) { 3453 sout | next( &f1 ) | ' ' | next( &f2 ) | endl; 3454 } // for 3455 } 3456 \end{cfa} 3457 \caption{Fibonacci Coroutine} 3458 \label{f:FibonacciCoroutine} 3459 \end{figure} 3460 3461 3462 \subsection{Coroutine} 3463 3464 \Index{Coroutines} are the precursor to tasks. 3465 \VRef[Figure]{f:FibonacciCoroutine} shows a coroutine that computes the \Index*{Fibonacci} numbers. 3422 3466 3423 3467 … … 3434 3478 \end{cfa} 3435 3479 3480 \begin{figure} 3481 \begin{cfa} 3482 #include <fstream> 3483 #include <kernel> 3484 #include <monitor> 3485 #include <thread> 3486 3487 monitor global_t { 3488 int value; 3489 }; 3490 3491 void ?{}(global_t * this) { 3492 this->value = 0; 3493 } 3494 3495 static global_t global; 3496 3497 void increment3( global_t * mutex this ) { 3498 this->value += 1; 3499 } 3500 void increment2( global_t * mutex this ) { 3501 increment3( this ); 3502 } 3503 void increment( global_t * mutex this ) { 3504 increment2( this ); 3505 } 3506 3507 thread MyThread {}; 3508 3509 void main( MyThread* this ) { 3510 for(int i = 0; i < 1_000_000; i++) { 3511 increment( &global ); 3512 } 3513 } 3514 int main(int argc, char* argv[]) { 3515 processor p; 3516 { 3517 MyThread f[4]; 3518 } 3519 sout | global.value | endl; 3520 } 3521 \end{cfa} 3522 \caption{Atomic-Counter Monitor} 3523 \caption{f:AtomicCounterMonitor} 3524 \end{figure} 3525 3526 \begin{comment} 3436 3527 Since a monitor structure includes an implicit locking mechanism, it does not make sense to copy a monitor; 3437 3528 it is always passed by reference. … … 3480 3571 } 3481 3572 \end{cfa} 3573 \end{comment} 3482 3574 3483 3575 … … 3487 3579 A task provides mutual exclusion like a monitor, and also has its own execution state and a thread of control. 3488 3580 Similar to a monitor, a task is defined like a structure: 3581 3582 \begin{figure} 3583 \begin{cfa} 3584 #include <fstream> 3585 #include <kernel> 3586 #include <stdlib> 3587 #include <thread> 3588 3589 thread First { signal_once * lock; }; 3590 thread Second { signal_once * lock; }; 3591 3592 void ?{}( First * this, signal_once* lock ) { this->lock = lock; } 3593 void ?{}( Second * this, signal_once* lock ) { this->lock = lock; } 3594 3595 void main( First * this ) { 3596 for ( int i = 0; i < 10; i += 1 ) { 3597 sout | "First : Suspend No." | i + 1 | endl; 3598 yield(); 3599 } 3600 signal( this->lock ); 3601 } 3602 3603 void main( Second * this ) { 3604 wait( this->lock ); 3605 for ( int i = 0; i < 10; i += 1 ) { 3606 sout | "Second : Suspend No." | i + 1 | endl; 3607 yield(); 3608 } 3609 } 3610 3611 int main( void ) { 3612 signal_once lock; 3613 sout | "User main begin" | endl; 3614 { 3615 processor p; 3616 { 3617 First f = { &lock }; 3618 Second s = { &lock }; 3619 } 3620 } 3621 sout | "User main end" | endl; 3622 } 3623 \end{cfa} 3624 \caption{Simple Tasks} 3625 \label{f:SimpleTasks} 3626 \end{figure} 3627 3628 3629 \begin{comment} 3489 3630 \begin{cfa} 3490 3631 type Adder = task { … … 3540 3681 \end{cfa} 3541 3682 3542 3543 3683 \subsection{Cooperative Scheduling} 3544 3684 … … 3653 3793 } 3654 3794 \end{cfa} 3655 3656 3795 \end{comment} 3796 3797 3798 \begin{comment} 3657 3799 \section{Modules and Packages } 3658 3800 3659 \begin{comment}3660 3801 High-level encapsulation is useful for organizing code into reusable units, and accelerating compilation speed. 3661 3802 \CFA provides a convenient mechanism for creating, building and sharing groups of functionality that enhances productivity and improves compile time. … … 4321 4462 4322 4463 4464 \begin{comment} 4323 4465 \subsection[Comparing Key Features of CFA]{Comparing Key Features of \CFA} 4324 4466 … … 4698 4840 4699 4841 4700 \begin{comment}4701 4842 \subsubsection{Modules / Packages} 4702 4843 … … 4778 4919 } 4779 4920 \end{cfa} 4780 \end{comment}4781 4921 4782 4922 … … 4939 5079 4940 5080 \subsection{Summary of Language Comparison} 4941 4942 4943 \subsubsection[C++]{\CC} 5081 \end{comment} 5082 5083 5084 \subsection[C++]{\CC} 4944 5085 4945 5086 \Index*[C++]{\CC{}} is a general-purpose programming language. … … 4962 5103 4963 5104 4964 \subs ubsection{Go}5105 \subsection{Go} 4965 5106 4966 5107 \Index*{Go}, also commonly referred to as golang, is a programming language developed at Google in 2007 [.]. … … 4978 5119 4979 5120 4980 \subs ubsection{Rust}5121 \subsection{Rust} 4981 5122 4982 5123 \Index*{Rust} is a general-purpose, multi-paradigm, compiled programming language developed by Mozilla Research. … … 4992 5133 4993 5134 4994 \subs ubsection{D}5135 \subsection{D} 4995 5136 4996 5137 The \Index*{D} programming language is an object-oriented, imperative, multi-paradigm system programming … … 5104 5245 \item[Rationale:] keywords added to implement new semantics of \CFA. 5105 5246 \item[Effect on original feature:] change to semantics of well-defined feature. \\ 5106 Any ISO Cprograms using these keywords as identifiers are invalid \CFA programs.5247 Any \Celeven programs using these keywords as identifiers are invalid \CFA programs. 5107 5248 \item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism (see~\VRef{s:BackquoteIdentifiers}). 5108 5249 \item[How widely used:] clashes among new \CFA keywords and existing identifiers are rare.
Note: See TracChangeset
for help on using the changeset viewer.