- Timestamp:
- Apr 4, 2023, 10:12:57 PM (14 months ago)
- Branches:
- ADT, ast-experimental, master
- Children:
- 9bb8ee42
- Parents:
- ff71057 (diff), bb7422a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- doc
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/LaTeXmacros/common.sty
rff71057 re02e13f 11 11 %% Created On : Sat Apr 9 10:06:17 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Fri Feb 10 12:09:30202314 %% Update Count : 58 113 %% Last Modified On : Tue Apr 4 12:03:19 2023 14 %% Update Count : 585 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 266 266 \newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}} 267 267 \newcommand{\LstStringStyle}[1]{{\lst@basicstyle{\lst@stringstyle{#1}}}} 268 \newcommand{\LstNumberStyle}[1]{{\lst@basicstyle{\lst@numberstyle{#1}}}} 268 269 269 270 \newlength{\gcolumnposn} % temporary hack because lstlisting does not handle tabs correctly … … 286 287 columns=fullflexible, 287 288 basicstyle=\linespread{0.9}\sf, % reduce line spacing and use sanserif font 288 stringstyle=\ tt,% use typewriter font289 stringstyle=\small\tt, % use typewriter font 289 290 tabsize=5, % N space tabbing 290 291 xleftmargin=\parindentlnth, % indent code to paragraph indentation -
doc/LaTeXmacros/common.tex
rff71057 re02e13f 11 11 %% Created On : Sat Apr 9 10:06:17 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Fri Feb 17 08:32:47202314 %% Update Count : 56 513 %% Last Modified On : Tue Apr 4 12:03:18 2023 14 %% Update Count : 567 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 291 291 columns=fullflexible, 292 292 basicstyle=\linespread{0.9}\sf, % reduce line spacing and use sanserif font 293 stringstyle=\ tt,% use typewriter font293 stringstyle=\small\tt, % use typewriter font 294 294 tabsize=5, % N space tabbing 295 295 xleftmargin=\parindentlnth, % indent code to paragraph indentation -
doc/theses/colby_parsons_MMAth/Makefile
rff71057 re02e13f 96 96 97 97 ${BASE}.dvi : Makefile ${GRAPHS} ${PROGRAMS} ${PICTURES} ${FIGURES} ${SOURCES} ${DATA} \ 98 ${Macros}/common.tex ${Macros}/indexstyle local.bib ../../bibliography/pl.bib | ${Build}98 style/style.tex ${Macros}/common.tex ${Macros}/indexstyle local.bib ../../bibliography/pl.bib | ${Build} 99 99 # Must have *.aux file containing citations for bibtex 100 100 if [ ! -r ${basename $@}.aux ] ; then ${LaTeX} ${basename $@}.tex ; fi -
doc/theses/colby_parsons_MMAth/style/style.tex
rff71057 re02e13f 1 1 \input{common} 2 \CFAStyle % CFA code-style 3 \lstset{language=CFA} % default language 2 4 3 \lstdefinestyle{defaultStyle}{ 4 escapeinside={@@}, 5 % basicstyle=\linespread{0.9}\tt\footnotesize, % reduce line spacing and use typewriter font 6 basicstyle=\linespread{0.9}\sf, % reduce line spacing and use typewriter font 7 % keywordstyle=\bfseries\color{blue}, 8 % keywordstyle=[2]\bfseries\color{Plum}, 9 % commentstyle=\sf\itshape\color{OliveGreen}, % green and italic comments 10 % identifierstyle=\color{identifierCol}, 11 % stringstyle=\sf\color{Mahogany}, % use sanserif font 12 stringstyle=\tt, % use sanserif font 13 mathescape=true, 14 % columns=fixed, 15 columns=fullflexible, 16 % aboveskip=4pt, % spacing above/below code block 17 % belowskip=3pt, 18 keepspaces=true, 19 tabsize=4, 20 % frame=lines, 21 literate=, 22 showlines=true, % show blank lines at end of code 23 showspaces=false, 24 showstringspaces=false, 25 showlines=true, % show blank lines at end of code 26 escapechar=\$, 27 xleftmargin=\parindentlnth, % indent code to paragraph indentation 28 moredelim=[is][\color{red}\bfseries]{**R**}{**R**}, % red highlighting 29 morekeywords=[2]{accept, signal, signal_block, wait, waitfor, waituntil}, 30 abovecaptionskip=5pt, 31 } 32 33 \lstdefinestyle{cfaStyle}{ 34 escapeinside={@@}, 35 basicstyle=\linespread{0.9}\sf, % reduce line spacing and use typewriter font 36 stringstyle=\tt, % use sanserif font 37 mathescape=true, 38 columns=fullflexible, 39 keepspaces=true, 40 tabsize=4, 41 literate=, 42 showlines=true, % show blank lines at end of code 43 showspaces=false, 44 showstringspaces=false, 45 showlines=true, % show blank lines at end of code 46 escapechar=\$, 47 xleftmargin=\parindentlnth, % indent code to paragraph indentation 48 moredelim=[is][\color{red}\bfseries]{**R**}{**R**}, % red highlighting 49 morekeywords=[2]{accept, signal, signal_block, wait, waitfor, waituntil}, 50 abovecaptionskip=5pt, 51 } 52 53 \lstnewenvironment{cfacode}[1][]{ 54 \lstset{ 55 language = CFA, 56 style=cfaStyle, 57 captionpos=b, 58 #1 59 } 60 }{} 61 62 \lstnewenvironment{cppcode}[1][]{ 63 \lstset{ 64 language = c++, 65 style=defaultStyle, 66 captionpos=b, 67 #1 68 } 69 }{} 70 71 \newcommand{\code}[1]{\lstinline[language=CFA,style=cfaStyle]{#1}} 5 \newcommand{\code}[1]{\lstinline[language=CFA]{#1}} 72 6 \newcommand{\uC}{$\mu$\CC} 73 7 -
doc/theses/colby_parsons_MMAth/text/CFA_concurrency.tex
rff71057 re02e13f 1 1 \chapter{Concurrency in \CFA}\label{s:cfa_concurrency} 2 2 3 The groundwork for concurrency in \CFA was laid by Thierry Delisle in his Master's Thesis \cite{Delisle18}.4 In that work he introduced coroutines, user level threading, and monitors.5 Not listed in that work were the other concurrency features that were needed as building blocks, such as locks, futures, and condition variableswhich he also added to \CFA.3 The groundwork for concurrency in \CFA was laid by Thierry Delisle in his Master's Thesis~\cite{Delisle18}. 4 In that work, he introduced generators, coroutines, monitors, and user-level threading. 5 Not listed in that work were basic concurrency features needed as building blocks, such as locks, futures, and condition variables, which he also added to \CFA. 6 6 7 7 \section{Threading Model}\label{s:threading} 8 \CFA has user level threading and supports a $M:N$ threading model where $M$ user threads are scheduled on $N$ cores, where both $M$ and $N$ can be explicitly set by the user. 9 Cores are used by a program by creating instances of a \code{processor} struct. 10 User threads types are defined using the \code{thread} keyword, in the place where a \code{struct} keyword is typically used. 11 For each thread type a corresponding main must be defined, which is where the thread starts running once it is created. 12 Listing~\ref{l:cfa_thd_init} shows an example of processor and thread creation. 13 When processors are added, they are added alongside the existing processor given to each program. 14 Thus if you want $N$ processors you need to allocate $N-1$. 15 To join a thread the thread must be deallocated, either deleted if it is allocated on the heap, or go out of scope if stack allocated. 16 The thread performing the deallocation will wait for the thread being deallocated to terminate before the deallocation can occur. 17 A thread terminates by returning from the main routine where it starts. 8 \CFA provides user-level threading and supports an $M$:$N$ threading model where $M$ user threads are scheduled on $N$ kernel threads, where both $M$ and $N$ can be explicitly set by the user. 9 Kernel threads are created by instancing a @processor@ structure. 10 User-thread types are defined by creating a @thread@ aggregate-type, \ie replace @struct@ with @thread@. 11 For each thread type a corresponding @main@ routine must be defined, which is where the thread starts running once it is created. 12 Examples of \CFA user thread and processor creation are shown in \VRef[Listing]{l:cfa_thd_init}. 18 13 19 \begin{cfacode}[tabsize=3,caption={\CFA user thread and processor creation},label={l:cfa_thd_init}] 20 21 thread my_thread {} // user thread type 22 void main( my_thread & this ) { // thread start routine 23 printf("Hello threading world\n"); 14 \begin{cfa}[caption={Example of \CFA user thread and processor creation},label={l:cfa_thd_init}] 15 @thread@ my_thread {...}; $\C{// user thread type}$ 16 void @main@( my_thread & this ) { $\C{// thread start routine}$ 17 sout | "Hello threading world"; 24 18 } 25 19 26 20 int main() { 27 // add 2 processors, now 3 total 28 processor p[2]; 29 { 30 my_thread t1; 31 my_thread t2; 32 } // waits for threads to end before going out of scope21 @processor@ p[2]; $\C{// add 2 processors = 3 total with starting processor}$ 22 { 23 my_thread t[2], * t3 = new(); $\C{// create 3 user threads, running in main routine}$ 24 ... // execute concurrently 25 delete( t3 ); $\C{// wait for thread to end and deallocate}$ 26 } // wait for threads to end and deallocate 33 27 } 28 \end{cfa} 34 29 35 \end{cfacode} 30 When processors are added, they are added alongside the existing processor given to each program. 31 Thus, for $N$ processors, allocate $N-1$ processors. 32 A thread is implicitly joined at deallocated, either implicitly at block exit for stack allocation or explicitly at @delete@ for heap allocation. 33 The thread performing the deallocation must wait for the thread to terminate before the deallocation can occur. 34 A thread terminates by returning from the main routine where it starts. 35 36 % Local Variables: % 37 % tab-width: 4 % 38 % End: % -
doc/theses/colby_parsons_MMAth/text/CFA_intro.tex
rff71057 re02e13f 7 7 \section{Overview} 8 8 The following serves as an introduction to \CFA. 9 \CFA is a layer over C, is transpiled to Cand is largely considered to be an extension of C.10 Beyond C, it adds productivity features, libraries, a type system, and many other languageconstructions.11 However, \CFA stays true to C as a language, with most code revolving around \code{struct}'s and routines, and respects the same rules as C.12 \CFA is not object oriented as it has no notion of \code{this} and no classes or methods, but supports some object oriented adjacent ideas including costructors, destructors, and limitedinheritance.13 \CFA is rich with interesting features, but a subset that is pertinent to this work will bediscussed.9 \CFA is a layer over C, is transpiled\footnote{Source to source translator.} to C, and is largely considered to be an extension of C. 10 Beyond C, it adds productivity features, extended libraries, an advanced type system, and many control-flow/concurrency constructions. 11 However, \CFA stays true to the C programming style, with most code revolving around @struct@'s and routines, and respects the same rules as C. 12 \CFA is not object oriented as it has no notion of @this@ (receiver) and no structures with methods, but supports some object oriented ideas including constructors, destructors, and limited containment inheritance. 13 While \CFA is rich with interesting features, only the subset pertinent to this work is discussed. 14 14 15 15 \section{References} 16 References in \CFA are similar to references in \CC , however in\CFA references are rebindable, and support multi-level referencing.16 References in \CFA are similar to references in \CC; however \CFA references are rebindable, and support multi-level referencing. 17 17 References in \CFA are a layer of syntactic sugar over pointers to reduce the number of ref/deref operations needed with pointer usage. 18 Some examples of references in \CFA are shown in Listing~\ref{l:cfa_ref}. 19 Another related item to note is that the \CFA equivalent of \CC's \code{nullptr} is \code{0p}. 20 21 \begin{cfa code}[caption={Example of \CFA references},label={l:cfa_ref}]18 Another difference is the use of @0p@ instead of C's @NULL@ or \CC's @nullptr@. 19 Examples of references are shown in \VRef[Listing]{l:cfa_ref}. 20 21 \begin{cfa}[caption={Example of \CFA references},label={l:cfa_ref}] 22 22 int i = 2; 23 int & ref_i = i; // declare ref to i24 int * ptr_i = &i; // ptr to i23 int & ref_i = i; $\C{// declare ref to i}$ 24 int * ptr_i = &i; $\C{// ptr to i}$ 25 25 26 26 // address of ref_i is the same as address of i 27 27 assert( &ref_i == ptr_i ); 28 28 29 int && ref_ref_i = ref_i; // can have a ref to a ref30 ref_i = 3; // set i to 329 int && ref_ref_i = ref_i; $\C{// can have a ref to a ref}$ 30 ref_i = 3; $\C{// set i to 3}$ 31 31 int new_i = 4; 32 32 33 33 // syntax to rebind ref_i (must cancel implicit deref) 34 &ref_i = &new_i; // (&*)ref_i = &new_i; (sets underlying ptr)35 \end{cfa code}36 37 38 \section{Overloading} 39 In \CFA routines can be overloaded on parameter type, number of parameters, and return type.34 &ref_i = &new_i; $\C{// (\&*)ref\_i = \&new\_i; (sets underlying ptr)}$ 35 \end{cfa} 36 37 38 \section{Overloading}\label{s:Overloading} 39 \CFA routines can be overloaded on parameter type, number of parameters, and \emph{return type}. 40 40 Variables can also be overloaded on type, meaning that two variables can have the same name so long as they have different types. 41 The variables will be disambiguated via type, sometimes requiring a cast. 42 The code snippet in Listing~\ref{l:cfa_overload} contains examples of overloading. 43 44 45 \begin{cfa code}[caption={Example of \CFA functionoverloading},label={l:cfa_overload}]46 int foo() { printf("A\n"); return 0;}47 int foo( int bar ) { printf("B\n"); return 1; }48 int foo( double bar ) { printf("C\n"); return 2; }49 double foo( double bar ) { printf("D\n"); return 3;}50 void foo( double bar ) { printf("%.0f\n", bar); }51 52 int main() { 53 foo(); // prints A 54 foo( 0 ); // prints B 55 int a = foo( 0.0 ); // prints C 56 double a = foo( 0.0 ); // prints D 57 foo( a ); // prints 3 58 } 59 \end{cfa code}41 A routine or variable is disambiguated at each usage site via its type and surrounding expression context. 42 A cast is used to disambiguate any conflicting usage. 43 Examples of overloading are shown in \VRef[Listing]{l:cfa_overload}. 44 45 \begin{cfa}[caption={Example of \CFA overloading},label={l:cfa_overload}] 46 int foo() { sout | "A"; return 0;} 47 int foo( int bar ) { sout | "B"; return 1; } 48 int foo( double bar ) { sout | "C"; return 2; } 49 double foo( double bar ) { sout | "D"; return 3; } 50 void foo( double bar ) { sout | bar; } 51 52 int main() { 53 foo(); $\C{// prints A}$ 54 foo( 0 ); $\C{// prints B}$ 55 int foo = foo( 0.0 ); $\C{// prints C}$ 56 double foo = foo( 0.0 ); $\C{// prints D}$ 57 foo( foo ); $\C{// prints 3., where left-hand side of expression is void}$ 58 } 59 \end{cfa} 60 60 61 61 62 62 \section{With Statement} 63 The with statement is a tool for exposing members of aggregate types within a scope in \CFA. 64 It allows users to use fields of aggregate types without using their fully qualified name. 65 This feature is also implemented in Pascal. 66 It can exist as a stand-alone statement or it can be used on routines to expose fields in the body of the routine. 67 An example is shown in Listing~\ref{l:cfa_with}. 68 69 70 \begin{cfacode}[tabsize=3,caption={Usage of \CFA with statement},label={l:cfa_with}] 71 struct obj { 72 int a, b, c; 73 }; 74 struct pair { 75 double x, y; 76 }; 77 78 // Stand-alone with stmt: 63 The \CFA @with@ statement is for exposing fields of an aggregate type within a scope, allowing field names without qualification. 64 This feature is also implemented in Pascal~\cite{Pascal}. 65 It can exist as a stand-alone statement or wrap a routine body to expose aggregate fields. 66 Examples of the @with@ statement are shown in \VRef[Listing]{l:cfa_with}. 67 68 \begin{cfa}[caption={Example of \CFA \lstinline{with} statement},label={l:cfa_with}] 69 struct pair { double x, y; }; 70 struct triple { int a, b, c; }; 79 71 pair p; 80 with( p ) { 81 x = 6.28; 82 y = 1.73; 83 } 84 85 // Can be used on routines: 86 void foo( obj o, pair p ) with( o, p ) { 87 a = 1; 88 b = 2; 89 c = 3; 90 x = 3.14; 91 y = 2.71; 92 } 93 94 // routine foo is equivalent to routine bar: 95 void bar( obj o, pair p ) { 96 o.a = 1; 97 o.b = 2; 98 o.c = 3; 99 p.x = 3.14; 100 p.y = 2.71; 101 } 102 \end{cfacode} 72 73 @with( p )@ { $\C{// stand-alone with}$ 74 p.x = 6.28; p.y = 1.73; $\C{// long form}$ 75 x = 6.28; y = 1.73; $\C{// short form}$ 76 } 77 void foo( triple t, pair p ) @with( t, p )@ { $\C{// routine with}$ 78 t.a = 1; t.b = 2; t.c = 3; p.x = 3.14; p.y = 2.71; $\C{// long form}$ 79 a = 1; b = 2; c = 3; x = 3.14; y = 2.71; $\C{// short form}$ 80 } 81 \end{cfa} 103 82 104 83 105 84 \section{Operators} 106 85 Operators can be overloaded in \CFA with operator routines. 107 Operators in \CFA are named using the operator symbol and '?' to respresent operands. 108 An example is shown in Listing~\ref{l:cfa_operate}. 109 110 111 \begin{cfacode}[tabsize=3,caption={Example of \CFA operators},label={l:cfa_operate}] 86 Operators in \CFA are named using an operator symbol and '@?@' to represent operands. 87 Examples of \CFA operators are shown in \VRef[Listing]{l:cfa_operate}. 88 89 \begin{cfa}[caption={Example of \CFA operators},label={l:cfa_operate}] 112 90 struct coord { 113 double x; 114 double y; 115 double z; 116 }; 117 coord ++?( coord & c ) with(c) { 118 x++; 119 y++; 120 z++; 121 return c; 122 } 123 coord ?<=?( coord op1, coord op2 ) with( op1 ) { 124 return (x*x + y*y + z*z) <= 125 (op2.x*op2.x + op2.y*op2.y + op2.z*op2.z); 126 } 127 \end{cfacode} 91 double x, y, z; 92 }; 93 coord ++@?@( coord & c ) with( c ) { $\C{// post increment}$ 94 x++; y++; z++; 95 return c; 96 } 97 coord @?@<=@?@( coord op1, coord op2 ) with( op1 ) { $\C{// ambiguous with both parameters}$ 98 return (x * x + y * y + z * z) <= (op2.x * op2.x + op2.y * op2.y + op2.z * op2.z); 99 } 100 \end{cfa} 128 101 129 102 130 103 \section{Constructors and Destructors} 131 Constructors and destructors in \CFA are two special operator routines that are used for creation and destruction of objects. 132 The default constructor and destructor for a type are called implicitly upon creation and deletion respectively if they are defined. 133 An example is shown in Listing~\ref{l:cfa_ctor}. 134 135 136 \begin{cfacode}[tabsize=3,caption={Example of \CFA constructors and destructors},label={l:cfa_ctor}] 104 Constructors and destructors in \CFA are special operator routines used for creation and destruction of objects. 105 The default constructor and destructor for a type are called implicitly upon creation and deletion, respectively. 106 Examples of \CFA constructors and destructors are shown in \VRef[Listing]{l:cfa_ctor}. 107 108 \begin{cfa}[caption={Example of \CFA constructors and destructors},label={l:cfa_ctor}] 137 109 struct discrete_point { 138 int x; 139 int y; 140 }; 141 void ?{}( discrete_point & this ) with(this) { // ctor 142 x = 0; 143 y = 0; 144 } 145 void ?{}( discrete_point & this, int x, int y ) { // ctor 146 this.x = x; 147 this.y = y; 148 } 149 void ^?{}( discrete_point & this ) with(this) { // dtor 150 x = 0; 151 y = 0; 152 } 153 154 int main() { 155 discrete_point d; // implicit call to ?{} 156 discrete_point p{}; // same call as line above 157 discrete_point dp{ 2, -4 }; // specialized ctor 158 } // ^d{}, ^p{}, ^dp{} all called as they go out of scope 159 \end{cfacode} 110 int x, y; 111 }; 112 void ?{}( discrete_point & this ) with(this) { $\C{// default constructor}$ 113 [x, y] = 0; 114 } 115 void ?{}( discrete_point & this, int x, int y ) { $\C{// explicit constructor}$ 116 this.[x, y] = [x, y]; 117 } 118 void ^?{}( discrete_point & this ) with(this) { $\C{// destructor}$ 119 ?{}( this ); $\C{// reset by calling default constructor}$ 120 } 121 int main() { 122 discrete_point x, y{}; $\C{// implicit call to default ctor, ?\{\}}$ 123 discrete_point s = { 2, -4 }, t{ 4, 2 }; $\C{// explicit call to specialized ctor}$ 124 } // ^t{}, ^s{}, ^y{}, ^x{} implicit calls in reverse allocation order 125 \end{cfa} 160 126 161 127 162 128 \section{Polymorphism}\label{s:poly} 163 C does not natively support polymorphism, and requires users to implement polymorphism themselves if they want to use it.164 \CFA extends C with two styles of polymorphism that it supports, parametric polymorphism and nominal inheritance.129 C supports limited polymorphism, often requiring users to implement polymorphism using a @void *@ (type erasure) approach. 130 \CFA extends C with generalized overloading polymorphism (see \VRef{s:Overloading}), as well as, parametric polymorphism and nominal inheritance. 165 131 166 132 \subsection{Parametric Polymorphism} 167 \CFA provides parametric polymorphism in the form of \code{forall}, and \code{trait}s. 168 A \code{forall} takes in a set of types and a list of constraints. 169 The declarations that follow the \code{forall} are parameterized over the types listed that satisfy the constraints. 170 Sometimes the list of constraints can be long, which is where a \code{trait} can be used. 171 A \code{trait} is a collection of constraints that is given a name and can be reused in foralls. 172 An example of the usage of parametric polymorphism in \CFA is shown in Listing~\ref{l:cfa_poly}. 173 174 \begin{cfacode}[tabsize=3,caption={Example of \CFA polymorphism},label={l:cfa_poly}] 133 \CFA provides parametric polymorphism in the form of @forall@, and @trait@s. 134 A @forall@ takes in a set of types and a list of constraints. 135 The declarations that follow the @forall@ are parameterized over the types listed that satisfy the constraints. 136 A list of @forall@ constraints can be refactored into a named @trait@ and reused in @forall@s. 137 Examples of \CFA parametric polymorphism are shown in \VRef[Listing]{l:cfa_poly}. 138 139 \begin{cfa}[caption={Example of \CFA parametric polymorphism},label={l:cfa_poly}] 175 140 // sized() is a trait that means the type has a size 176 forall( V & | sized(V) ) // type params for trait141 forall( V & | sized(V) ) $\C{// type params for trait}$ 177 142 trait vector_space { 178 V add( V, V ); // vector addition 179 V scalar_mult( int, V ); // scalar multiplication 180 181 // dtor and copy ctor needed in constraints to pass by copy 182 void ?{}( V &, V & ); // copy ctor for return 183 void ^?{}( V & ); // dtor 184 }; 185 186 forall( V & | vector_space( V )) { 187 V get_inverse( V v1 ) { 188 return scalar_mult( -1, v1 ); // can use ?*? routine defined in trait 189 } 190 V add_and_invert( V v1, V v2 ) { 191 return get_inverse( add( v1, v2 ) ); // can use ?*? routine defined in trait 192 } 143 // dtor and copy ctor needed in constraints to pass by copy 144 void ?{}( V &, V & ); $\C{// copy ctor for return}$ 145 void ^?{}( V & ); $\C{// dtor}$ 146 V ?+?( V, V ); $\C{// vector addition}$ 147 V ?*?( int, V ); $\C{// scalar multiplication}$ 148 }; 149 150 forall( V & | vector_space( V ) ) { 151 V get_inverse( V v1 ) { 152 return -1 * v1; $\C{// can use ?*? routine defined in trait}$ 153 } 154 V add_and_invert( V v1, V v2 ) { 155 return get_inverse( v1 + v2 ); $\C{// can use ?+? routine defined in trait}$ 156 } 193 157 } 194 158 struct Vec1 { int x; }; … … 196 160 void ?{}( Vec1 & this, int x ) { this.x = x; } 197 161 void ^?{}( Vec1 & this ) {} 198 Vec1 add( Vec1 v1, Vec1 v2 ) { v1.x += v2.x; return v1; }199 Vec1 scalar_mult( int c, Vec1 v1 ) { v1.x = v1.x * c; return v1; }162 Vec1 ?+?( Vec1 v1, Vec1 v2 ) { v1.x += v2.x; return v1; } 163 Vec1 ?*?( int c, Vec1 v1 ) { v1.x = v1.x * c; return v1; } 200 164 201 165 struct Vec2 { int x; int y; }; … … 203 167 void ?{}( Vec2 & this, int x ) { this.x = x; this.y = x; } 204 168 void ^?{}( Vec2 & this ) {} 205 Vec2 add( Vec2 v1, Vec2 v2 ) { v1.x += v2.x; v1.y += v2.y; return v1; } 206 Vec2 scalar_mult( int c, Vec2 v1 ) { v1.x = v1.x * c; v1.y = v1.y * c; return v1; } 207 208 int main() { 209 Vec1 v1{ 1 }; // create Vec1 and call ctor 210 Vec2 v2{ 2 }; // create Vec2 and call ctor 211 212 // can use forall defined routines since types satisfy trait 213 add_and_invert( get_inverse( v1 ), v1 ); 214 add_and_invert( get_inverse( v2 ), v2 ); 215 } 216 217 \end{cfacode} 169 Vec2 ?+?( Vec2 v1, Vec2 v2 ) { v1.x += v2.x; v1.y += v2.y; return v1; } 170 Vec2 ?*?( int c, Vec2 v1 ) { v1.x = v1.x * c; v1.y = v1.y * c; return v1; } 171 172 int main() { 173 Vec1 v1{ 1 }; $\C{// create Vec1 and call ctor}$ 174 Vec2 v2{ 2 }; $\C{// create Vec2 and call ctor}$ 175 // can use forall defined routines since types satisfy trait 176 add_and_invert( get_inverse( v1 ), v1 ); 177 add_and_invert( get_inverse( v2 ), v2 ); 178 } 179 \end{cfa} 218 180 219 181 \subsection{Inheritance} 220 Inheritance in \CFA copies its style from Plan-9 C nominalinheritance.221 In \CFA structs can \code{inline} another struct type to gain its fields and to be able to be passed to routines that require a parameter of the inlinedtype.222 An example of \CFA inheritance is shown in Listing~\ref{l:cfa_inherit}. 223 224 \begin{cfa code}[tabsize=3,caption={Example of \CFAinheritance},label={l:cfa_inherit}]182 Inheritance in \CFA is taken from Plan-9 C's containment inheritance. 183 In \CFA, @struct@s can @inline@ another struct type to gain its fields and masquerade as that type. 184 Examples of \CFA containment inheritance are shown in \VRef[Listing]{l:cfa_inherit}. 185 186 \begin{cfa}[caption={Example of \CFA containment inheritance},label={l:cfa_inherit}] 225 187 struct one_d { double x; }; 226 188 struct two_d { 227 inlineone_d;228 189 @inline@ one_d; 190 double y; 229 191 }; 230 192 struct three_d { 231 inlinetwo_d;232 193 @inline@ two_d; 194 double z; 233 195 }; 234 196 double get_x( one_d & d ){ return d.x; } … … 236 198 struct dog {}; 237 199 struct dog_food { 238 200 int count; 239 201 }; 240 202 struct pet { 241 inline dog; 242 inline dog_food; 243 }; 244 void pet_dog( dog & d ){printf("woof\n");} 245 void print_food( dog_food & f ){printf("%d\n", f.count);} 246 247 int main() { 248 one_d x; 249 two_d y; 250 three_d z; 251 x.x = 1; 252 y.x = 2; 253 z.x = 3; 254 get_x( x ); // returns 1; 255 get_x( y ); // returns 2; 256 get_x( z ); // returns 3; 257 pet p; 258 p.count = 5; 259 pet_dog( p ); // prints woof 260 print_food( p ); // prints 5 261 } 262 \end{cfacode} 263 264 203 @inline@ dog; 204 @inline@ dog_food; 205 }; 206 void pet_dog( dog & d ) { sout | "woof"; } 207 void print_food( dog_food & f ) { sout | f.count; } 208 209 int main() { 210 one_d x; 211 two_d y; 212 three_d z; 213 x.x = 1; 214 y.x = 2; 215 z.x = 3; 216 get_x( x ); $\C{// returns 1;}$ 217 get_x( y ); $\C{// returns 2;}$ 218 get_x( z ); $\C{// returns 3;}$ 219 pet p; 220 p.count = 5; 221 pet_dog( p ); $\C{// prints woof}$ 222 print_food( p ); $\C{// prints 5}$ 223 } 224 \end{cfa} 225 226 % Local Variables: % 227 % tab-width: 4 % 228 % End: % -
doc/theses/colby_parsons_MMAth/text/actors.tex
rff71057 re02e13f 88 88 Similarly to create a message type a user must define a struct which \code{inline}'s the base \code{message} struct. 89 89 90 \begin{cfa code}90 \begin{cfa} 91 91 struct derived_actor { 92 92 inline actor; // Plan-9 C inheritance … … 118 118 return 0; 119 119 } 120 \end{cfa code}120 \end{cfa} 121 121 122 122 The above code is a simple actor program in \CFA. … … 125 125 Key things to highlight include the \code{receive} signature, and calls to \code{start_actor_system}, and \code{stop_actor_system}. 126 126 To define a behaviour for some derived actor and derived message type, one must declare a routine with the signature: 127 \begin{cfa code}127 \begin{cfa} 128 128 Allocation receive( derived_actor & receiver, derived_msg & msg ) 129 \end{cfa code}129 \end{cfa} 130 130 Where \code{derived_actor} and \code{derived_msg} can be any derived actor and derived message types respectively. 131 131 The return value of \code{receive} must be a value from the \code{Allocation} enum: 132 \begin{cfa code}132 \begin{cfa} 133 133 enum Allocation { Nodelete, Delete, Destroy, Finished }; 134 \end{cfa code}134 \end{cfa} 135 135 The \code{Allocation} enum is a set of actions that dictate what the executor should do with a message or actor after a given behaviour has been completed. 136 136 In the case of an actor, the \code{receive} routine returns the \code{Allocation} status to the executor which sets the status on the actor and takes the appropriate action. 137 137 For messages, they either default to \code{Nodelete}, or they can be passed an \code{Allocation} via the \code{message} constructor. 138 138 Message state can be updated via a call to: 139 \begin{cfa code}139 \begin{cfa} 140 140 void set_allocation( message & this, Allocation state ) 141 \end{cfa code}141 \end{cfa} 142 142 143 143 The following describes the use of each of the \code{Allocation} values: … … 186 186 All message sends are done using the left shift operater, \ie <<, similar to the syntax of \CC's output. 187 187 The signature of the left shift operator is: 188 \begin{cfa code}188 \begin{cfa} 189 189 Allocation ?<<?( derived_actor & receiver, derived_msg & msg ) 190 \end{cfa code}190 \end{cfa} 191 191 192 192 An astute eye will notice that this is the same signature as the \code{receive} routine which is no coincidence. … … 368 368 To first verify sequential correctness, consider the equivalent sequential swap below: 369 369 370 \begin{cfa code}370 \begin{cfa} 371 371 void swap( uint victim_idx, uint my_idx ) { 372 372 // Step 0: … … 380 380 request_queues[my_idx] = vic_queue; 381 381 } 382 \end{cfa code}382 \end{cfa} 383 383 384 384 Step 1 is missing in the sequential example since in only matter in the concurrent context presented later. … … 386 386 Temporary copies of each pointer being swapped are stored, and then the original values of each pointer are set using the copy of the other pointer. 387 387 388 \begin{cfa code}388 \begin{cfa} 389 389 // This routine is atomic 390 390 bool CAS( work_queue ** ptr, work_queue ** old, work_queue * new ) { … … 427 427 return true; 428 428 } 429 \end{cfa code}\label{c:swap}429 \end{cfa}\label{c:swap} 430 430 431 431 Now consider the concurrent implementation of the swap. -
doc/theses/colby_parsons_MMAth/text/channels.tex
rff71057 re02e13f 118 118 Also note that in the Go version~\ref{l:go_chan_bar}, the size of the barrier channels has to be larger than in the \CFA version to ensure that the main thread does not block when attempting to clear the barrier. 119 119 120 \begin{cfa code}[tabsize=3,caption={\CFA channel barrier termination},label={l:cfa_chan_bar}]120 \begin{cfa}[tabsize=3,caption={\CFA channel barrier termination},label={l:cfa_chan_bar}] 121 121 struct barrier { 122 122 channel( int ) barWait; … … 171 171 return 0; 172 172 } 173 \end{cfa code}174 175 \begin{cfa code}[tabsize=3,caption={Go channel barrier termination},label={l:go_chan_bar}]173 \end{cfa} 174 175 \begin{cfa}[tabsize=3,caption={Go channel barrier termination},label={l:go_chan_bar}] 176 176 177 177 struct barrier { … … 237 237 return 0; 238 238 } 239 \end{cfa code}239 \end{cfa} 240 240 241 241 In Listing~\ref{l:cfa_resume} an example of channel closing with resumption is used. … … 244 244 If the same program was implemented in Go it would require explicit synchronization with both producers and consumers by some mechanism outside the channel to ensure that all elements were removed before task termination. 245 245 246 \begin{cfa code}[tabsize=3,caption={\CFA channel resumption usage},label={l:cfa_resume}]246 \begin{cfa}[tabsize=3,caption={\CFA channel resumption usage},label={l:cfa_resume}] 247 247 channel( int ) chan{ 128 }; 248 248 … … 280 280 return 0; 281 281 } 282 \end{cfa code}282 \end{cfa} 283 283 284 284 \section{Performance} -
doc/theses/colby_parsons_MMAth/text/mutex_stmt.tex
rff71057 re02e13f 12 12 Additionally, it provides the safety guarantee of deadlock-freedom, both by acquiring the locks in a deadlock-free manner, and by ensuring that the locks release on error, or normal program execution via \gls{raii}. 13 13 14 \begin{cfa code}[tabsize=3,caption={\CFA mutex statement usage},label={l:cfa_mutex_ex}]14 \begin{cfa}[tabsize=3,caption={\CFA mutex statement usage},label={l:cfa_mutex_ex}] 15 15 owner_lock lock1, lock2, lock3; 16 16 int count = 0; … … 20 20 } 21 21 mutex( lock2, lock3 ) count++; // or inline statement 22 \end{cfa code}22 \end{cfa} 23 23 24 24 \section{Other Languages} … … 32 32 An example of \CC scoped\_lock usage is shown in Listing~\ref{l:cc_scoped_lock}. 33 33 34 \begin{c ppcode}[tabsize=3,caption={\CC scoped\_lock usage},label={l:cc_scoped_lock}]34 \begin{cfa}[tabsize=3,caption={\CC scoped\_lock usage},label={l:cc_scoped_lock}] 35 35 std::mutex lock1, lock2, lock3; 36 36 { … … 38 38 // locks are released via raii at end of scope 39 39 } 40 \end{c ppcode}40 \end{cfa} 41 41 42 42 \section{\CFA implementation} … … 58 58 This use case is shown in Listing~\ref{l:sout}. 59 59 60 \begin{cfa code}[tabsize=3,caption={\CFA sout with mutex statement},label={l:sout}]60 \begin{cfa}[tabsize=3,caption={\CFA sout with mutex statement},label={l:sout}] 61 61 mutex( sout ) 62 62 sout | "This output is protected by mutual exclusion!"; 63 \end{cfa code}63 \end{cfa} 64 64 65 65 \section{Deadlock Avoidance} … … 70 70 The algorithm presented is taken directly from the source code of the \code{<mutex>} header, with some renaming and comments for clarity. 71 71 72 \begin{c ppcode}[caption={\CC scoped\_lock deadlock avoidance algorithm},label={l:cc_deadlock_avoid}]72 \begin{cfa}[caption={\CC scoped\_lock deadlock avoidance algorithm},label={l:cc_deadlock_avoid}] 73 73 int first = 0; // first lock to attempt to lock 74 74 do { … … 86 86 // if first lock is still held then all have been acquired 87 87 } while (!locks[first].owns_lock()); // is first lock held? 88 \end{c ppcode}88 \end{cfa} 89 89 90 90 The algorithm in \ref{l:cc_deadlock_avoid} successfully avoids deadlock, however there is a potential livelock scenario. -
doc/theses/colby_parsons_MMAth/thesis.tex
rff71057 re02e13f 98 98 \hypersetup{ 99 99 plainpages=false, % needed if Roman numbers in frontpages 100 unicode=false, % non-Latin characters in Acrobat ’s bookmarks101 pdftoolbar=true, % show Acrobat ’s toolbar?102 pdfmenubar=true, % show Acrobat ’s menu?100 unicode=false, % non-Latin characters in Acrobat's bookmarks 101 pdftoolbar=true, % show Acrobat's toolbar? 102 pdfmenubar=true, % show Acrobat's menu? 103 103 pdffitwindow=false, % window fit to page when opened 104 104 pdfstartview={FitH}, % fits the width of the page to the window … … 110 110 colorlinks=true, % false: boxed links; true: colored links 111 111 linkcolor=blue, % color of internal links 112 citecolor= green, % color of links to bibliography112 citecolor=blue, % color of links to bibliography 113 113 filecolor=magenta, % color of file links 114 114 urlcolor=cyan % color of external links
Note: See TracChangeset
for help on using the changeset viewer.