| [697c957] | 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 2 | %%
|
|---|
| 3 | %% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
|---|
| 4 | %%
|
|---|
| 5 | %% The contents of this file are covered under the licence agreement in the
|
|---|
| 6 | %% file "LICENCE" distributed with Cforall.
|
|---|
| 7 | %%
|
|---|
| 8 | %% user.tex --
|
|---|
| 9 | %%
|
|---|
| 10 | %% Author : Peter A. Buhr
|
|---|
| 11 | %% Created On : Wed Apr 6 14:53:29 2016
|
|---|
| 12 | %% Last Modified By : Peter A. Buhr
|
|---|
| [8617ee90] | 13 | %% Last Modified On : Sat Mar 15 13:38:53 2025
|
|---|
| 14 | %% Update Count : 6302
|
|---|
| [697c957] | 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 16 |
|
|---|
| 17 | % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
|
|---|
| 18 |
|
|---|
| 19 | \documentclass[11pt]{article}
|
|---|
| 20 |
|
|---|
| 21 | \makeatletter
|
|---|
| 22 | \def\@maketitle{%
|
|---|
| 23 | \newpage
|
|---|
| 24 | \null
|
|---|
| 25 | % \vskip 2em%
|
|---|
| 26 | \begin{center}%
|
|---|
| 27 | \let \footnote \thanks
|
|---|
| 28 | {\LARGE\bf \@title \par}%
|
|---|
| 29 | \@ifundefined{@author}{}
|
|---|
| 30 | {
|
|---|
| 31 | \ifx\@empty\@author
|
|---|
| 32 | \else
|
|---|
| 33 | \vskip 1.5em%
|
|---|
| 34 | {\large
|
|---|
| 35 | \lineskip .5em%
|
|---|
| 36 | \begin{tabular}[t]{c}%
|
|---|
| 37 | \@author
|
|---|
| 38 | \end{tabular}%
|
|---|
| 39 | \par
|
|---|
| 40 | }%
|
|---|
| 41 | \fi
|
|---|
| 42 | }%
|
|---|
| 43 | \ifx\@empty\@date
|
|---|
| 44 | \else
|
|---|
| 45 | \vskip 1em%
|
|---|
| 46 | {\large \@date}%
|
|---|
| 47 | \fi
|
|---|
| 48 | \end{center}%
|
|---|
| 49 | \par
|
|---|
| 50 | % \vskip 1.5em
|
|---|
| 51 | }%maketitle
|
|---|
| 52 | \makeatother
|
|---|
| 53 |
|
|---|
| 54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 55 |
|
|---|
| 56 | % Latex packages used in the document.
|
|---|
| 57 | %\usepackage[T1]{fontenc} % allow Latin1 (extended ASCII) characters
|
|---|
| 58 | %\usepackage{textcomp}
|
|---|
| 59 | %\usepackage[latin1]{inputenc}
|
|---|
| 60 |
|
|---|
| 61 | \usepackage{fullpage,times,comment}
|
|---|
| 62 | \usepackage{epic,eepic}
|
|---|
| 63 | \usepackage{upquote} % switch curled `'" to straight
|
|---|
| 64 | \usepackage[labelformat=simple,aboveskip=0pt,farskip=0pt]{subfig}
|
|---|
| 65 | \renewcommand{\thesubfigure}{\alph{subfigure})}
|
|---|
| 66 | \usepackage{latexsym} % \Box glyph
|
|---|
| 67 | \usepackage{mathptmx} % better math font with "times"
|
|---|
| 68 | \usepackage[usenames]{color}
|
|---|
| 69 | \usepackage[pagewise]{lineno}
|
|---|
| 70 | \renewcommand{\linenumberfont}{\scriptsize\sffamily}
|
|---|
| 71 | \input{common} % common CFA document macros
|
|---|
| 72 | \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
|
|---|
| 73 | \usepackage{breakurl}
|
|---|
| 74 |
|
|---|
| 75 | \renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt}
|
|---|
| 76 | \newcommand{\uC}{$\mu$\CC}
|
|---|
| 77 |
|
|---|
| 78 | % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
|
|---|
| 79 | % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
|
|---|
| 80 | % AFTER HYPERREF.
|
|---|
| 81 | \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
|
|---|
| 82 |
|
|---|
| 83 | \setlength{\topmargin}{-0.45in} % move running title into header
|
|---|
| 84 | \setlength{\headsep}{0.25in}
|
|---|
| 85 |
|
|---|
| 86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 87 |
|
|---|
| 88 | \setlength{\gcolumnposn}{3in}
|
|---|
| 89 | \CFAStyle % use default CFA format-style
|
|---|
| 90 | \lstset{language=CFA} % CFA default lnaguage
|
|---|
| 91 | \lstnewenvironment{C++}[1][] % use C++ style
|
|---|
| 92 | {\lstset{language=C++,escapechar=\$,mathescape=false,moredelim=**[is][\protect\color{red}]{@}{@},#1}}{}
|
|---|
| 93 | \lstnewenvironment{uC++}[1][]
|
|---|
| 94 | {\lstset{language=uC++,escapechar=\$,mathescape=false,moredelim=**[is][\protect\color{red}]{@}{@},#1}}{}
|
|---|
| 95 |
|
|---|
| 96 | \newsavebox{\myboxA}
|
|---|
| 97 | \newsavebox{\myboxB}
|
|---|
| 98 |
|
|---|
| 99 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 100 |
|
|---|
| 101 | % Names used in the document.
|
|---|
| 102 | \newcommand{\Version}{\input{build/version}}
|
|---|
| 103 | \newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}}
|
|---|
| 104 | \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}}
|
|---|
| 105 | \newcommand{\R}[1]{{\color{red}#1}}
|
|---|
| 106 | \newcommand{\RB}[1]{\Textbf{#1}}
|
|---|
| 107 | \newcommand{\B}[1]{{\Textbf[blue]{#1}}}
|
|---|
| 108 | \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
|
|---|
| 109 | \newcommand{\Sp}{\R{\textvisiblespace}}
|
|---|
| 110 | \newcommand{\KWC}{K-W C\xspace}
|
|---|
| 111 |
|
|---|
| 112 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 113 |
|
|---|
| 114 | \setcounter{secnumdepth}{3} % number subsubsections
|
|---|
| 115 | \setcounter{tocdepth}{3} % subsubsections in table of contents
|
|---|
| 116 | \makeindex
|
|---|
| 117 |
|
|---|
| 118 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 119 |
|
|---|
| 120 | \title{\vspace*{-0.5in}
|
|---|
| [946a6e4] | 121 | \uC to \CFA Cheat Sheet}
|
|---|
| [697c957] | 122 | %\author{Peter A. Buhr}
|
|---|
| 123 | \date{}
|
|---|
| 124 |
|
|---|
| 125 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|---|
| 126 |
|
|---|
| 127 | \begin{document}
|
|---|
| 128 | \pagestyle{headings}
|
|---|
| 129 | % changed after setting pagestyle
|
|---|
| 130 | \renewcommand{\sectionmark}[1]{\markboth{\thesection\quad #1}{\thesection\quad #1}}
|
|---|
| 131 | \renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}}
|
|---|
| 132 |
|
|---|
| 133 | %\linenumbers % comment out to turn off line numbering
|
|---|
| 134 |
|
|---|
| 135 | \maketitle
|
|---|
| 136 | \vspace*{-0.55in}
|
|---|
| 137 |
|
|---|
| 138 | \section{Introduction}
|
|---|
| 139 |
|
|---|
| 140 | \CFA is NOT an object-oriented programming-language.
|
|---|
| 141 | \CFA uses parametric polymorphism and allows overloading of variables and routines:
|
|---|
| 142 | \begin{cfa}
|
|---|
| [2b6db03] | 143 | int i; char i; double i; $\C[2in]{// overload name i}$
|
|---|
| [697c957] | 144 | int i(); double i(); char i();
|
|---|
| [2b6db03] | 145 | i += 1; $\C{// int i}$
|
|---|
| 146 | i += 1.0; $\C{// double i}$
|
|---|
| 147 | i += 'a'; $\C{// char i}$
|
|---|
| 148 | int j = i(); $\C{// int i()}$
|
|---|
| 149 | double j = i(); $\C{// double i();}$
|
|---|
| 150 | char j = i(); $\C{// char i()}\CRT$
|
|---|
| [697c957] | 151 | \end{cfa}
|
|---|
| 152 | \CFA has rebindable references.
|
|---|
| 153 | \begin{cquote}
|
|---|
| [2b6db03] | 154 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 155 | \multicolumn{2}{@{}l}{\lstinline{ int x = 1, y = 2, * p1x = &x, * p1y = &y, ** p2i = &p1x,}} \\
|
|---|
| [3733643] | 156 | \multicolumn{2}{@{}l}{\lstinline{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ & r1x = x, & r1y = y, && r2i = r1x;}} \\
|
|---|
| [697c957] | 157 | \begin{uC++}
|
|---|
| 158 | **p2i = 3;
|
|---|
| 159 | p2i = &p1y;
|
|---|
| 160 | **p2i = 3;
|
|---|
| 161 | p1x = p1y;
|
|---|
| 162 | **p2i = 4;
|
|---|
| 163 | p1x = @nullptr@;
|
|---|
| 164 | \end{uC++}
|
|---|
| 165 | &
|
|---|
| 166 | \begin{cfa}
|
|---|
| 167 | r2i = 3; $\C[1.0in]{// change x}$
|
|---|
| 168 | &r2i = &r1y; $\C{// change p2i / r2i}$
|
|---|
| 169 | r2i = 3; $\C{// change y}$
|
|---|
| 170 | &r1x = &r1y; $\C{// change p1x / r1x}$
|
|---|
| 171 | r2i = 4; $\C{// change y}$
|
|---|
| 172 | &r1x = @0p@; $\C{// reset}\CRT$
|
|---|
| 173 | \end{cfa}
|
|---|
| 174 | \end{tabular}
|
|---|
| 175 | \end{cquote}
|
|---|
| 176 | Non-rebindable reference (\CC reference) is a @const@ reference (@const@ pointer).
|
|---|
| 177 | \begin{cfa}
|
|---|
| 178 | int & @const@ cr = x; // must initialize, no null pointer
|
|---|
| 179 | int & @const@ & @const@ crcr = cr; // generalize
|
|---|
| 180 | \end{cfa}
|
|---|
| 181 | Aggregate qualification is reduced or eliminated by opening scopes using the @with@ clause.
|
|---|
| 182 | \begin{cfa}
|
|---|
| 183 | struct S { int i; int j; double m; }; // field i has same type in structures S and T
|
|---|
| 184 | struct T { int i; int k; int m; };
|
|---|
| [bf91d1d] | 185 | void foo( S s, T t ) @with( s, t )@ { // open structure scope s and t in parallel
|
|---|
| [697c957] | 186 | j + k; $\C[1.6in]{// unambiguous, s.j + t.k}$
|
|---|
| 187 | m = 5.0; $\C{// unambiguous, s.m = 5.0}$
|
|---|
| 188 | m = 1; $\C{// unambiguous, t.m = 1}$
|
|---|
| 189 | int a = m; $\C{// unambiguous, a = t.m}$
|
|---|
| 190 | double b = m; $\C{// unambiguous, b = s.m}$
|
|---|
| 191 | int c = s.i + t.i; $\C{// unambiguous with qualification}$
|
|---|
| 192 | (double)m; $\C{// unambiguous with cast s.m}\CRT$
|
|---|
| 193 | }
|
|---|
| 194 | \end{cfa}
|
|---|
| 195 | \noindent
|
|---|
| [946a6e4] | 196 | In subsequent code examples, the left example is \uC and the right example is \CFA.
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| [697c957] | 199 | \section{Looping}
|
|---|
| 200 |
|
|---|
| 201 | \begin{cquote}
|
|---|
| [2b6db03] | 202 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [697c957] | 203 | \begin{uC++}
|
|---|
| [946a6e4] | 204 | for ( @;;@ ) { ... } / while ( @true@ ) { ... }
|
|---|
| 205 | for ( int i = 0; i < @10@; i += 1 ) { ... }
|
|---|
| 206 | for ( int i = @5@; i < @15@; i += @2@ ) { ... }
|
|---|
| 207 | for ( int i = -1; i <@=@ 10; i += 3 ) { ... }
|
|---|
| 208 | for ( int i = 10; i > 0; i @-@= 1 ) { ... }
|
|---|
| 209 | \end{uC++}
|
|---|
| 210 | &
|
|---|
| 211 | \begin{cfa}
|
|---|
| 212 | for () { ... } / while () { ... }
|
|---|
| 213 | for ( @10@ ) { ... } / for ( i; @10@ ) { ... }
|
|---|
| 214 | for ( i; @5@ ~ @15@ ~ @2@ ) { ... }
|
|---|
| 215 | for ( i; -1 ~@=@ 10 ~ 3 ) { ... }
|
|---|
| 216 | for ( i; 0 @-@~ 10 ) { ... }
|
|---|
| 217 | \end{cfa}
|
|---|
| [3b21c96] | 218 | \end{tabular}
|
|---|
| 219 | \end{cquote}
|
|---|
| 220 |
|
|---|
| 221 | \begin{cquote}
|
|---|
| 222 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [946a6e4] | 223 | \begin{uC++}
|
|---|
| [697c957] | 224 | int i = 0
|
|---|
| 225 | for ( i = 0; i < 10; i += 1 ) { ... }
|
|---|
| [946a6e4] | 226 | @if ( i == 10 )@ { ... }
|
|---|
| [697c957] | 227 | \end{uC++}
|
|---|
| 228 | &
|
|---|
| 229 | \begin{cfa}
|
|---|
| 230 |
|
|---|
| 231 | for ( i; 10 ) { ... }
|
|---|
| [946a6e4] | 232 | @else@ { ... } // i == 10
|
|---|
| 233 | \end{cfa}
|
|---|
| [3b21c96] | 234 | \end{tabular}
|
|---|
| 235 | \end{cquote}
|
|---|
| 236 |
|
|---|
| 237 | \begin{cquote}
|
|---|
| 238 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [946a6e4] | 239 | \begin{uC++}
|
|---|
| [b723b63] | 240 | @L1:@ for ( ;; ) {
|
|---|
| 241 | @L2:@ for ( ;; ) {
|
|---|
| 242 | ... if ( ... ) @break L1@; ...
|
|---|
| 243 | ... if ( ... ) @break L2@; ...
|
|---|
| [946a6e4] | 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | \end{uC++}
|
|---|
| 247 | &
|
|---|
| 248 | \begin{cfa}
|
|---|
| [b723b63] | 249 | @L1:@ for () {
|
|---|
| 250 | @L2:@ for () {
|
|---|
| 251 | ... if ( ... ) @break L1@; ...
|
|---|
| 252 | ... if ( ... ) @break L2@; ...
|
|---|
| [946a6e4] | 253 | }
|
|---|
| 254 | }
|
|---|
| [697c957] | 255 | \end{cfa}
|
|---|
| 256 | \end{tabular}
|
|---|
| 257 | \end{cquote}
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| [3b21c96] | 260 | \section{Stream I/O}
|
|---|
| 261 |
|
|---|
| 262 | \CFA output streams automatically separate values and insert a newline at the end of the print.
|
|---|
| 263 | \begin{cquote}
|
|---|
| 264 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 265 | \begin{uC++}
|
|---|
| 266 | #include <@iostream@>
|
|---|
| 267 | using namespace std;
|
|---|
| 268 | int i; double d; char c;
|
|---|
| 269 | cin >> i >> d >> c;
|
|---|
| 270 | cout << i << ' ' << d << ' ' << c << endl;
|
|---|
| 271 | \end{uC++}
|
|---|
| 272 | &
|
|---|
| 273 | \begin{cfa}
|
|---|
| 274 | #include <@fstream.hfa@>
|
|---|
| 275 |
|
|---|
| 276 | int i; double d; char c;
|
|---|
| 277 | sin | i | d | c;
|
|---|
| 278 | sout | i | d | c
|
|---|
| 279 | \end{cfa}
|
|---|
| 280 | \end{tabular}
|
|---|
| 281 | \end{cquote}
|
|---|
| 282 | To disable/enable automatic newline at the end of printing, use @nlOff@/@nlOn@ and @nl@.
|
|---|
| 283 | \begin{cquote}
|
|---|
| 284 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 285 | \begin{uC++}
|
|---|
| 286 |
|
|---|
| 287 | for ( int i = 0; i < 5; i += 1 ) cout << i << ' ';
|
|---|
| 288 | cout << @endl@;
|
|---|
| 289 |
|
|---|
| 290 | 0 1 2 3 4
|
|---|
| 291 | \end{uC++}
|
|---|
| 292 | &
|
|---|
| 293 | \begin{cfa}
|
|---|
| 294 | sout | @nlOff@; // disable auto nl
|
|---|
| 295 | for ( i; 5 ) sout | i;
|
|---|
| 296 | sout | @nl@;
|
|---|
| 297 | sout | @nlOn@; // reenable auto nl
|
|---|
| 298 | 0 1 2 3 4
|
|---|
| 299 | \end{cfa}
|
|---|
| 300 | \end{tabular}
|
|---|
| 301 | \end{cquote}
|
|---|
| 302 | Floating-point numbers without a fraction print with a decimal point, which can be disabled with @nodp@.
|
|---|
| 303 | \begin{cquote}
|
|---|
| 304 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 305 | \begin{uC++}
|
|---|
| 306 | cout << 3.0 << ' ' << showpoint << setprecision(0) << 3.0 << endl;
|
|---|
| 307 | 3 3.
|
|---|
| 308 | \end{uC++}
|
|---|
| 309 | &
|
|---|
| 310 | \begin{cfa}
|
|---|
| 311 | sout | @nodp( 3.0 )@ | 3.0;
|
|---|
| 312 | 3 3.
|
|---|
| 313 | \end{cfa}
|
|---|
| 314 | \end{tabular}
|
|---|
| 315 | \end{cquote}
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| [946a6e4] | 318 | \section{Exception}
|
|---|
| [697c957] | 319 |
|
|---|
| 320 | Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception.
|
|---|
| 321 | \begin{cquote}
|
|---|
| [2b6db03] | 322 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 323 | \begin{uC++}
|
|---|
| 324 |
|
|---|
| [946a6e4] | 325 | @_Exception@ E { // local or global scope
|
|---|
| [697c957] | 326 | ... // exception fields
|
|---|
| 327 | };
|
|---|
| 328 | try {
|
|---|
| 329 | ...
|
|---|
| [946a6e4] | 330 | if ( ... ) @_Resume@ E( /* initialization */ );
|
|---|
| 331 | if ( ... ) @_Throw@ E( /* initialization */ );
|
|---|
| 332 | ...
|
|---|
| [3b21c96] | 333 | } @_CatchResume@( E & /* reference */ ) { ... }
|
|---|
| 334 | catch( E & ) { ... }
|
|---|
| 335 | catch( ... /* catch any */ ) { ... }
|
|---|
| 336 | _Finally { ... }
|
|---|
| [697c957] | 337 | \end{uC++}
|
|---|
| 338 | &
|
|---|
| 339 | \begin{cfa}
|
|---|
| 340 | #include <Exception.hfa>
|
|---|
| 341 | @ExceptionDecl@( E, // must be global scope
|
|---|
| 342 | ... // exception fields
|
|---|
| 343 | );
|
|---|
| 344 | try {
|
|---|
| 345 | ...
|
|---|
| [946a6e4] | 346 | if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ );
|
|---|
| 347 | if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ );
|
|---|
| [697c957] | 348 | ...
|
|---|
| [3b21c96] | 349 | } @catchResume@( E @*@ /* pointer */ ) { ... }
|
|---|
| 350 | catch( E * ) { ... }
|
|---|
| 351 | catch( exception_t @*@ /* catch any */ ) { ... }
|
|---|
| 352 | finally { ... }
|
|---|
| [697c957] | 353 | \end{cfa}
|
|---|
| 354 | \end{tabular}
|
|---|
| 355 | \end{cquote}
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| [946a6e4] | 358 | \section{Non-local Exception}
|
|---|
| 359 |
|
|---|
| 360 | \begin{cquote}
|
|---|
| [2b6db03] | 361 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [946a6e4] | 362 | \begin{uC++}
|
|---|
| [697c957] | 363 |
|
|---|
| [946a6e4] | 364 |
|
|---|
| 365 | void main() {
|
|---|
| 366 | try {
|
|---|
| 367 | _Enable {
|
|---|
| 368 | ... suspend(); ...
|
|---|
| 369 | }
|
|---|
| [3b21c96] | 370 | } @_CatchResume@( E & /* reference */ ) { ... }
|
|---|
| 371 | catch( E & ) { ... }
|
|---|
| [946a6e4] | 372 | }
|
|---|
| 373 | \end{uC++}
|
|---|
| 374 | &
|
|---|
| 375 | \begin{cfa}
|
|---|
| [ef9f11c] | 376 | #define resumePoll( coroutine ) resume( coroutine ); poll()
|
|---|
| 377 | #define suspendPoll suspend; poll()
|
|---|
| [946a6e4] | 378 | void main() {
|
|---|
| 379 | try {
|
|---|
| 380 | enable_ehm();
|
|---|
| 381 | ... suspendPoll ...
|
|---|
| 382 | disable_ehm();
|
|---|
| [3b21c96] | 383 | } @catchResume@( E * ) { ... }
|
|---|
| 384 | catch( E & ) { ... }
|
|---|
| [946a6e4] | 385 | }
|
|---|
| 386 | \end{cfa}
|
|---|
| 387 | \end{tabular}
|
|---|
| 388 | \end{cquote}
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| [e255902b] | 391 | \section{Constructor / Destructor}
|
|---|
| 392 |
|
|---|
| 393 | \begin{cquote}
|
|---|
| 394 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 395 | \begin{uC++}
|
|---|
| 396 |
|
|---|
| 397 | struct S {
|
|---|
| 398 | int i, j;
|
|---|
| [8617ee90] | 399 | @S@() { i = j = 3; }
|
|---|
| [e255902b] | 400 | @S@( int i, int j ) { S::i = i; S::j = j; }
|
|---|
| [8617ee90] | 401 | @S@( const S & s ) { *this = s; }
|
|---|
| [e255902b] | 402 | @~S@() {}
|
|---|
| 403 | };
|
|---|
| [8617ee90] | 404 | S s0;
|
|---|
| [e255902b] | 405 | S s1 = { 1, 2 };
|
|---|
| 406 |
|
|---|
| 407 | S * s2 = new S{ 1, 2 };
|
|---|
| 408 | delete s2;
|
|---|
| 409 | s2 = new S{ 1, 2 };
|
|---|
| 410 | delete s2;
|
|---|
| 411 | S & s3 = *new S{ 1, 2 };
|
|---|
| 412 | delete &s3;
|
|---|
| 413 | s3 = *new S{ 1, 2 };
|
|---|
| 414 | delete &s3;
|
|---|
| 415 | \end{uC++}
|
|---|
| 416 | &
|
|---|
| 417 | \begin{cfa}
|
|---|
| 418 | #include <stdlib.hfa> // new (malloc)
|
|---|
| [8617ee90] | 419 | struct S { int i, j; };
|
|---|
| 420 |
|
|---|
| 421 | void @?{}@( S & s ) { s.i = s.j = 3; } $\C[3in]{// default}$
|
|---|
| 422 | void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; } $\C{// initializer}$
|
|---|
| 423 | void @?{}@( S & s, const S rhs ) { s.[i,j] = rhs.[i,j]; } $\C{// copy}$
|
|---|
| 424 | void @^?{}@( S & s ) { s.i = 0; s.j = 0; } $\C{// destructor}\CRT$
|
|---|
| [e255902b] | 425 |
|
|---|
| [8617ee90] | 426 | S s0;
|
|---|
| [e255902b] | 427 | S s1 = { 1, 2 };
|
|---|
| 428 | // cannot use 0/1 (zero_t/one_t) with "new"
|
|---|
| 429 | S * s2 = new( 1@n@, 2 ); // n => (int)
|
|---|
| 430 | delete( s2 );
|
|---|
| 431 | s2 = new( 1n, 2 );
|
|---|
| 432 | delete( s2 );
|
|---|
| 433 | S & s3 = *new( 1n, 2 );
|
|---|
| [8617ee90] | 434 | delete( &s3 );
|
|---|
| [e255902b] | 435 | &s3 = &*new( 1n, 2 );
|
|---|
| [8617ee90] | 436 | delete( &s3 );
|
|---|
| [e255902b] | 437 | \end{cfa}
|
|---|
| 438 | \end{tabular}
|
|---|
| 439 | \end{cquote}
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| [bf91d1d] | 442 | \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
|
|---|
| 443 |
|
|---|
| 444 | \begin{cquote}
|
|---|
| 445 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 446 | \begin{uC++}
|
|---|
| 447 | struct S {
|
|---|
| 448 | int i = 0; // cheat, implicit default constructor
|
|---|
| 449 | int setter( int j ) { int t = i; i = j; return t; }
|
|---|
| 450 | int getter() { return i; }
|
|---|
| 451 | };
|
|---|
| 452 | S s;
|
|---|
| 453 | @s.@setter( 3 ); // object calls
|
|---|
| 454 | int k = @s.@getter();
|
|---|
| 455 | \end{uC++}
|
|---|
| 456 | &
|
|---|
| 457 | \begin{cfa}
|
|---|
| 458 | struct S { int i; };
|
|---|
| 459 | void ?{}( S & s ) { s.i = 0; } // explicit default constructor
|
|---|
| 460 | int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
|
|---|
| 461 | int getter( @S & s@ ) @with( s )@ { return i; }
|
|---|
| 462 |
|
|---|
| 463 | S s;
|
|---|
| 464 | setter( @s,@ 3 ); // normal calls
|
|---|
| 465 | int k = getter( @s@ );
|
|---|
| 466 | \end{cfa}
|
|---|
| 467 | \end{tabular}
|
|---|
| 468 | \end{cquote}
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| [697c957] | 471 | \section{String}
|
|---|
| 472 |
|
|---|
| 473 | \begin{cquote}
|
|---|
| [2b6db03] | 474 | \begin{tabular}{@{}l|l@{}}
|
|---|
| 475 | \multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\
|
|---|
| [697c957] | 476 | \begin{uC++}
|
|---|
| [3733643] | 477 | s1 = "abcdefg";
|
|---|
| [697c957] | 478 | s2 = s1;
|
|---|
| 479 | s1 += s2;
|
|---|
| 480 | s1 == s2; s1 != s2;
|
|---|
| 481 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
|
|---|
| 482 | s1.length();
|
|---|
| 483 | s1[3];
|
|---|
| 484 | s1.substr( 2 ); s1.substr( 2, 3 );
|
|---|
| 485 | s1.replace( 2, 5, s2 );
|
|---|
| [3733643] | 486 | s1.find( s2 ); s1.rfind( s2 );
|
|---|
| [697c957] | 487 | s1.find_first_of( s2 ); s1.find_last_of( s2 );
|
|---|
| [3733643] | 488 | s1.find_first_not_of( s2 ); s1.find_last_not_of( s2 );
|
|---|
| [697c957] | 489 | getline( cin, s1 );
|
|---|
| 490 | cout << s1 << endl;
|
|---|
| 491 | \end{uC++}
|
|---|
| 492 | &
|
|---|
| 493 | \begin{cfa}
|
|---|
| [3733643] | 494 | s1 = "abcdefg";
|
|---|
| [697c957] | 495 | s2 = s1;
|
|---|
| 496 | s1 += s2;
|
|---|
| 497 | s1 == s2; s1 != s2;
|
|---|
| 498 | s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
|
|---|
| 499 | size( s1 );
|
|---|
| 500 | s1[3];
|
|---|
| 501 | s1( 2 ); s1( 2, 3 );
|
|---|
| [3733643] | 502 | // replace( s1, 2, 5, s2 );
|
|---|
| 503 | // find( s1, s2 ), rfind( s1, s2 );
|
|---|
| 504 | // find_first_of( s2 ); find_last_of( s2 );
|
|---|
| 505 | // find_first_not_of( s1, s2 ); find_last_not_of( s1, s2 );
|
|---|
| [697c957] | 506 | sin | getline( s1 );
|
|---|
| 507 | sout | s1;
|
|---|
| 508 | \end{cfa}
|
|---|
| 509 | \end{tabular}
|
|---|
| 510 | \end{cquote}
|
|---|
| 511 |
|
|---|
| 512 |
|
|---|
| [3733643] | 513 | \section{\texorpdfstring{\lstinline{uArray}}{uArray}}
|
|---|
| [697c957] | 514 |
|
|---|
| 515 | \begin{cquote}
|
|---|
| [2b6db03] | 516 | \begin{tabular}{@{}l|l@{}}
|
|---|
| [697c957] | 517 | \begin{uC++}
|
|---|
| [3733643] | 518 | #include <iostream>
|
|---|
| 519 | using namespace std;
|
|---|
| [697c957] | 520 | struct S {
|
|---|
| 521 | int i;
|
|---|
| [538cc35] | 522 | S( int i ) { S::i = i; cout << "ctor " << S::i << endl; }
|
|---|
| 523 | ~S() { S::i = i; cout << "dtor " << S::i << endl; }
|
|---|
| [697c957] | 524 | };
|
|---|
| 525 | int main() {
|
|---|
| [538cc35] | 526 | enum { N = 5 };
|
|---|
| [3733643] | 527 | @uArray( S, s, N );@ // no constructor calls
|
|---|
| 528 | for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // constructor calls
|
|---|
| [538cc35] | 529 | for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl;
|
|---|
| [697c957] | 530 | }
|
|---|
| 531 | \end{uC++}
|
|---|
| 532 | &
|
|---|
| 533 | \begin{cfa}
|
|---|
| [3733643] | 534 | #include <fstream.hfa>
|
|---|
| 535 | #include <array.hfa>
|
|---|
| [697c957] | 536 | struct S {
|
|---|
| 537 | int i;
|
|---|
| 538 | };
|
|---|
| [538cc35] | 539 | void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
|
|---|
| 540 | void ^?{}( S & s ) { sout | "dtor" | s.i; }
|
|---|
| [697c957] | 541 | int main() {
|
|---|
| [538cc35] | 542 | enum { N = 5 };
|
|---|
| [3733643] | 543 | @array( S, N ) s = { delay_init };@ // no constructor calls
|
|---|
| 544 | for ( i; N ) @s[i]{ i }@; // constructor calls
|
|---|
| [538cc35] | 545 | for ( i; N ) sout | s[i]@.@i;
|
|---|
| [697c957] | 546 | }
|
|---|
| 547 | \end{cfa}
|
|---|
| 548 | \end{tabular}
|
|---|
| 549 | \end{cquote}
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| [0cb3479] | 552 | \section{Coroutine}
|
|---|
| [697c957] | 553 |
|
|---|
| 554 | \begin{cquote}
|
|---|
| [2b6db03] | 555 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 556 | \begin{uC++}
|
|---|
| 557 |
|
|---|
| [135a2d8] | 558 | @_Coroutine@ C {
|
|---|
| [697c957] | 559 | // private coroutine fields
|
|---|
| 560 | void main() {
|
|---|
| [135a2d8] | 561 | ... @suspend();@ ...
|
|---|
| 562 | ... @_Resume E( ... ) _At partner;@
|
|---|
| 563 | ... @uThisCoroutine();@ ...
|
|---|
| [bf91d1d] | 564 |
|
|---|
| [697c957] | 565 | }
|
|---|
| 566 | public:
|
|---|
| 567 | void mem( ... ) {
|
|---|
| [135a2d8] | 568 | ... @resume();@ ...
|
|---|
| [697c957] | 569 | }
|
|---|
| 570 | };
|
|---|
| 571 | \end{uC++}
|
|---|
| 572 | &
|
|---|
| 573 | \begin{cfa}
|
|---|
| 574 | #include <$coroutine$.hfa>
|
|---|
| [135a2d8] | 575 | @coroutine@ C {
|
|---|
| [697c957] | 576 | // private coroutine fields
|
|---|
| 577 |
|
|---|
| 578 | };
|
|---|
| 579 | void main( C & c ) {
|
|---|
| [135a2d8] | 580 | ... @suspend;@ ... // keyword not routine
|
|---|
| 581 | ... @resumeAt( partner, ExceptionInst( E, ... ) );@
|
|---|
| 582 | ... @active_coroutine();@ ...
|
|---|
| [697c957] | 583 | }
|
|---|
| 584 | void mem( C & c, ... ) {
|
|---|
| [135a2d8] | 585 | ... @resume( c );@ ...
|
|---|
| [697c957] | 586 | }
|
|---|
| 587 | \end{cfa}
|
|---|
| 588 | \\
|
|---|
| [3733643] | 589 | \multicolumn{2}{@{}l@{}}{\lstinline{C c;}}
|
|---|
| [697c957] | 590 | \end{tabular}
|
|---|
| 591 | \end{cquote}
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| [bf91d1d] | 594 | \section{\lstinline{COBEGIN}/\lstinline{COFOR}}
|
|---|
| [697c957] | 595 |
|
|---|
| 596 | \begin{cquote}
|
|---|
| [2b6db03] | 597 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 598 | \begin{uC++}
|
|---|
| 599 |
|
|---|
| [bf91d1d] | 600 | #include <uCobegin.h>
|
|---|
| 601 | int main() {
|
|---|
| [135a2d8] | 602 | @COBEGIN@
|
|---|
| [bf91d1d] | 603 | BEGIN osacquire( cout ) << "A" << endl; END
|
|---|
| 604 | BEGIN osacquire( cout ) << "B" << endl; END
|
|---|
| 605 | BEGIN osacquire( cout ) << "C" << endl; END
|
|---|
| 606 | BEGIN osacquire( cout ) << "D" << endl; END
|
|---|
| [135a2d8] | 607 | @COEND@
|
|---|
| 608 | @COFOR@( i, 1, 10,
|
|---|
| [bf91d1d] | 609 | osacquire( cout ) << i << endl;
|
|---|
| 610 | )
|
|---|
| [697c957] | 611 | }
|
|---|
| 612 | \end{uC++}
|
|---|
| 613 | &
|
|---|
| 614 | \begin{cfa}
|
|---|
| [bf91d1d] | 615 | #include <mutex_stmt.hfa>
|
|---|
| 616 | #include <$cofor$.hfa>
|
|---|
| 617 | int main() {
|
|---|
| 618 | {
|
|---|
| [135a2d8] | 619 | @corun@ { mutex( sout ) sout | "A"; }
|
|---|
| [bf91d1d] | 620 | corun { mutex( sout ) sout | "B"; }
|
|---|
| 621 | corun { mutex( sout ) sout | "C"; }
|
|---|
| 622 | corun { mutex( sout ) sout | "D"; }
|
|---|
| 623 | }
|
|---|
| [135a2d8] | 624 | @cofor@( i; 10 ) {
|
|---|
| [bf91d1d] | 625 | mutex( sout ) sout | i;
|
|---|
| 626 | }
|
|---|
| [697c957] | 627 | }
|
|---|
| 628 | \end{cfa}
|
|---|
| 629 | \end{tabular}
|
|---|
| 630 | \end{cquote}
|
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| [bf91d1d] | 633 | \section{Actor}
|
|---|
| [697c957] | 634 |
|
|---|
| 635 | \begin{cquote}
|
|---|
| [2b6db03] | 636 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| [697c957] | 637 | \begin{uC++}
|
|---|
| [bf91d1d] | 638 | #include <iostream>
|
|---|
| 639 | using namespace std;
|
|---|
| 640 | #include <uActor.h>
|
|---|
| [697c957] | 641 |
|
|---|
| [bf91d1d] | 642 | struct StrMsg : @public uActor::Message@ {
|
|---|
| [697c957] | 643 |
|
|---|
| [0cb3479] | 644 | const char * val; // string message
|
|---|
| [bf91d1d] | 645 |
|
|---|
| 646 | StrMsg( const char * val ) :
|
|---|
| 647 | @Message( uActor::Delete )@, // delete after use
|
|---|
| 648 | val( val ) {}
|
|---|
| 649 | };
|
|---|
| 650 | _Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
|
|---|
| 651 | Allocation receive( Message & msg ) {
|
|---|
| [0cb3479] | 652 | Case( @StartMsg@, msg ) { // discriminate
|
|---|
| 653 |
|
|---|
| 654 | } else Case( StrMsg, msg ) {
|
|---|
| [bf91d1d] | 655 | osacquire( cout ) << msg_d->val << endl;
|
|---|
| [0cb3479] | 656 |
|
|---|
| 657 | } else Case( @StopMsg@, msg )
|
|---|
| 658 | return Delete; // delete actor
|
|---|
| 659 | return Nodelete; // reuse actor
|
|---|
| [697c957] | 660 | }
|
|---|
| 661 | };
|
|---|
| [bf91d1d] | 662 | int main() {
|
|---|
| 663 | @uActor::start();@ // start actor system
|
|---|
| [0cb3479] | 664 | *new Hello() | uActor::startMsg
|
|---|
| 665 | | *new StrMsg( "hello" ) | uActor::stopMsg;
|
|---|
| 666 | *new Hello() | uActor::startMsg
|
|---|
| 667 | | *new StrMsg( "bonjour" ) | uActor::stopMsg;
|
|---|
| 668 | @uActor::stop();@ // wait for actors to terminate
|
|---|
| [bf91d1d] | 669 | }
|
|---|
| [697c957] | 670 | \end{uC++}
|
|---|
| 671 | &
|
|---|
| 672 | \begin{cfa}
|
|---|
| [bf91d1d] | 673 | #include <fstream.hfa>
|
|---|
| 674 | #include <mutex_stmt.hfa>
|
|---|
| 675 | #include <actor.hfa>
|
|---|
| 676 |
|
|---|
| 677 | struct StrMsg {
|
|---|
| 678 | @inline message;@ // derived message
|
|---|
| 679 | const char * val; // string message
|
|---|
| [697c957] | 680 | };
|
|---|
| [0cb3479] | 681 | void ?{}( StrMsg & msg, const char * str ) {
|
|---|
| [bf91d1d] | 682 | @set_allocation( msg, Delete );@ // delete after use
|
|---|
| [0cb3479] | 683 | msg.val = str;
|
|---|
| 684 | }
|
|---|
| 685 | struct Hello { @inline actor;@ }; // derived actor
|
|---|
| 686 | allocation receive( Hello & receiver, @start_msg_t@ & ) {
|
|---|
| 687 | return Nodelete;
|
|---|
| [bf91d1d] | 688 | }
|
|---|
| 689 | allocation receive( Hello & receiver, StrMsg & msg ) {
|
|---|
| 690 | mutex( sout ) sout | msg.val;
|
|---|
| [0cb3479] | 691 | return Nodelete; // reuse actor
|
|---|
| 692 | }
|
|---|
| 693 | allocation receive( Hello & receiver, @stop_msg_t@ & ) {
|
|---|
| 694 | return Delete; // delete actor
|
|---|
| [697c957] | 695 | }
|
|---|
| 696 |
|
|---|
| [bf91d1d] | 697 | int main() {
|
|---|
| [0cb3479] | 698 | @actor_start();@ // start actor system
|
|---|
| 699 | *(Hello *)new() | start_msg
|
|---|
| 700 | | *(StrMsg *)new( "hello" ) | stop_msg;
|
|---|
| 701 | *(Hello *)new() | start_msg
|
|---|
| 702 | | *(StrMsg *)new( "bonjour" ) | stop_msg;
|
|---|
| 703 | @actor_stop();@ // wait for actors to terminate
|
|---|
| [bf91d1d] | 704 | }
|
|---|
| [697c957] | 705 | \end{cfa}
|
|---|
| 706 | \end{tabular}
|
|---|
| 707 | \end{cquote}
|
|---|
| 708 |
|
|---|
| 709 |
|
|---|
| [d96f7c4] | 710 | \section{Thread}
|
|---|
| 711 |
|
|---|
| 712 | \begin{cquote}
|
|---|
| 713 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| 714 | \begin{uC++}
|
|---|
| 715 |
|
|---|
| 716 | @_Task@ T {
|
|---|
| 717 | // private task fields
|
|---|
| 718 | void main() {
|
|---|
| 719 | ... @_Resume E( ... ) _At partner@;
|
|---|
| 720 | ... @uThisTask();@ ...
|
|---|
| 721 | }
|
|---|
| 722 | public:
|
|---|
| 723 | };
|
|---|
| 724 | \end{uC++}
|
|---|
| 725 | &
|
|---|
| 726 | \begin{cfa}
|
|---|
| 727 | #include <$thread$.hfa>
|
|---|
| 728 | @thread@ T {
|
|---|
| 729 | // private task fields
|
|---|
| 730 |
|
|---|
| 731 | };
|
|---|
| 732 | void main( @T & t@ ) {
|
|---|
| 733 | ... @resumeAt( partner, ExceptionInst( E, ... )@ );
|
|---|
| 734 | ... @active_thread();@ ...
|
|---|
| 735 | }
|
|---|
| 736 | \end{cfa}
|
|---|
| 737 | \\
|
|---|
| 738 | \multicolumn{2}{@{}l@{}}{\lstinline{T t; // start thread in main routine}}
|
|---|
| 739 | \end{tabular}
|
|---|
| 740 | \end{cquote}
|
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| [bf91d1d] | 743 | \section{Locks}
|
|---|
| 744 |
|
|---|
| 745 | \begin{cquote}
|
|---|
| 746 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| 747 | \begin{uC++}
|
|---|
| 748 |
|
|---|
| 749 | uOwnerLock m;
|
|---|
| 750 | uCondLock s;
|
|---|
| 751 | m.acquire();
|
|---|
| 752 | if ( ! s.empty() ) s.wait( m );
|
|---|
| 753 | else {
|
|---|
| 754 | m.release();
|
|---|
| 755 | }
|
|---|
| 756 | @osacquire( cout )@ << i << endl;
|
|---|
| 757 | \end{uC++}
|
|---|
| 758 | &
|
|---|
| 759 | \begin{cfa}
|
|---|
| 760 | #include <locks.hfa>
|
|---|
| 761 | owner_lock m;
|
|---|
| 762 | condition_variable( owner_lock ) s; // generic type on mutex lock
|
|---|
| 763 | lock( m );
|
|---|
| 764 | if ( ! empty( s ) ) wait( s, m );
|
|---|
| 765 | else {
|
|---|
| 766 | unlock( m );
|
|---|
| 767 | }
|
|---|
| 768 | @mutex( sout )@ sout | i; // thread safe I/O
|
|---|
| 769 | \end{cfa}
|
|---|
| 770 | \end{tabular}
|
|---|
| 771 | \end{cquote}
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| [1b56a7f] | 774 | \section{Barrier}
|
|---|
| 775 |
|
|---|
| 776 | \begin{cquote}
|
|---|
| 777 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| 778 | \begin{uC++}
|
|---|
| 779 | #include <iostream>
|
|---|
| 780 | using namespace std;
|
|---|
| 781 | #include <uBarrier.h>
|
|---|
| 782 |
|
|---|
| 783 | @_Cormonitor@ Barrier
|
|---|
| 784 | : @public uBarrier@ { // inheritance
|
|---|
| 785 | int total;
|
|---|
| [e255902b] | 786 | void @last@() { cout << total << endl; }
|
|---|
| [1b56a7f] | 787 | public:
|
|---|
| 788 | Barrier( unsigned int group ) :
|
|---|
| 789 | @uBarrier( group )@ {
|
|---|
| 790 | total = 0;
|
|---|
| 791 | }
|
|---|
| 792 | void @block@( int subtotal ) {
|
|---|
| [e255902b] | 793 |
|
|---|
| 794 |
|
|---|
| [1b56a7f] | 795 | total += subtotal;
|
|---|
| 796 | @uBarrier::block();@
|
|---|
| 797 | }
|
|---|
| 798 | };
|
|---|
| 799 | enum { N = 3 };
|
|---|
| 800 | Barrier b{ N };
|
|---|
| 801 |
|
|---|
| 802 | _Task T {
|
|---|
| 803 | void main() {
|
|---|
| 804 | for ( int i = 0; i < 10; i += 1 ) {
|
|---|
| 805 | b.block( 1 );
|
|---|
| 806 | }
|
|---|
| 807 | }
|
|---|
| 808 | };
|
|---|
| 809 | int main() {
|
|---|
| 810 | uProcessor p[N - 1];
|
|---|
| 811 | T t[N];
|
|---|
| 812 | }
|
|---|
| 813 | \end{uC++}
|
|---|
| 814 | &
|
|---|
| 815 | \begin{cfa}
|
|---|
| 816 | #include <fstream.hfa>
|
|---|
| 817 | #include <$thread$.hfa>
|
|---|
| 818 | #include <barrier.hfa>
|
|---|
| 819 | #include <mutex_stmt.hfa>
|
|---|
| 820 | struct Barrier {
|
|---|
| 821 | @barrier b;@ // containment
|
|---|
| 822 | int total;
|
|---|
| [e255902b] | 823 |
|
|---|
| [1b56a7f] | 824 | };
|
|---|
| 825 | void ?{}( Barrier & B, unsigned int group ) with(B) {
|
|---|
| 826 | @?{}( b, group );@ // initialize barrier
|
|---|
| 827 | total = 0;
|
|---|
| 828 | }
|
|---|
| 829 | unsigned int block( Barrier & B, int subtotal ) with(B) {
|
|---|
| 830 | void @last@() { sout | total; } // called by Gth arriving thread
|
|---|
| [e255902b] | 831 | @mutex( b )@ { // use barrier's mutual exclusion
|
|---|
| 832 | total += subtotal;
|
|---|
| 833 | return @block@( b, last ); // wait for barrier trigger
|
|---|
| 834 | }
|
|---|
| [1b56a7f] | 835 | }
|
|---|
| 836 | enum { N = 3 };
|
|---|
| 837 | Barrier b{ N };
|
|---|
| 838 |
|
|---|
| 839 | thread T {};
|
|---|
| 840 | void main( T & ) {
|
|---|
| 841 | for ( 10 ) {
|
|---|
| 842 | block( b, 1 );
|
|---|
| 843 | }
|
|---|
| 844 | }
|
|---|
| 845 |
|
|---|
| 846 | int main() {
|
|---|
| 847 | processor p[N - 1];
|
|---|
| 848 | T t[N];
|
|---|
| 849 | }
|
|---|
| 850 | \end{cfa}
|
|---|
| 851 | \end{tabular}
|
|---|
| 852 | \end{cquote}
|
|---|
| 853 |
|
|---|
| 854 | \newpage
|
|---|
| 855 |
|
|---|
| [0cb3479] | 856 | \section{Monitor}
|
|---|
| [bf91d1d] | 857 |
|
|---|
| [135a2d8] | 858 | Internal Scheduling
|
|---|
| [bf91d1d] | 859 | \begin{cquote}
|
|---|
| 860 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| 861 | \begin{uC++}
|
|---|
| 862 |
|
|---|
| [135a2d8] | 863 | @_Monitor@ BoundedBufferI {
|
|---|
| 864 | @uCondition@ full, empty;
|
|---|
| 865 | int front = 0, back = 0, count = 0;
|
|---|
| 866 | int elements[20];
|
|---|
| [bf91d1d] | 867 | public:
|
|---|
| 868 |
|
|---|
| [135a2d8] | 869 |
|
|---|
| 870 |
|
|---|
| 871 | @_Nomutex@ int query() const { return count; }
|
|---|
| 872 |
|
|---|
| 873 | void insert( int elem ) {
|
|---|
| 874 | if ( count == 20 ) @empty.wait();@
|
|---|
| 875 | elements[back] = elem;
|
|---|
| 876 | back = ( back + 1 ) % 20;
|
|---|
| 877 | count += 1;
|
|---|
| 878 | @full.signal();@
|
|---|
| 879 | }
|
|---|
| 880 | int remove() {
|
|---|
| 881 | if ( count == 0 ) @full.wait();@
|
|---|
| 882 | int elem = elements[front];
|
|---|
| 883 | front = ( front + 1 ) % 20;
|
|---|
| 884 | count -= 1;
|
|---|
| 885 | @empty.signal();@
|
|---|
| 886 | return elem;
|
|---|
| [bf91d1d] | 887 | }
|
|---|
| 888 | };
|
|---|
| 889 | \end{uC++}
|
|---|
| 890 | &
|
|---|
| 891 | \begin{cfa}
|
|---|
| 892 | #include <$monitor$.hfa>
|
|---|
| [135a2d8] | 893 | @monitor@ BoundedBufferI {
|
|---|
| 894 | @condition@ full, empty;
|
|---|
| 895 | int front, back, count;
|
|---|
| 896 | int elements[20];
|
|---|
| [bf91d1d] | 897 | };
|
|---|
| [135a2d8] | 898 | void ?{}( BoundedBufferI & buf ) with( buf ) {
|
|---|
| 899 | front = back = count = 0;
|
|---|
| 900 | }
|
|---|
| 901 | int query( BoundedBufferI & buf ) { return buf.count; }
|
|---|
| 902 | int remove( BoundedBufferI & @mutex@ buf ); // forward
|
|---|
| 903 | void insert( BoundedBufferI & @mutex@ buf, int elem ) with( buf ) {
|
|---|
| 904 | if ( count == 20 ) @wait( empty );@
|
|---|
| 905 | elements[back] = elem;
|
|---|
| 906 | back = ( back + 1 ) % 20;
|
|---|
| 907 | count += 1
|
|---|
| 908 | @signal( full );@
|
|---|
| 909 | }
|
|---|
| 910 | int remove( BoundedBufferI & @mutex@ buf ) with( buf ) {
|
|---|
| 911 | if ( count == 0 ) @wait( full );@
|
|---|
| 912 | int elem = elements[front];
|
|---|
| 913 | front = ( front + 1 ) % 20;
|
|---|
| 914 | count -= 1;
|
|---|
| 915 | @signal( empty );@
|
|---|
| 916 | return elem;
|
|---|
| [bf91d1d] | 917 | }
|
|---|
| 918 |
|
|---|
| 919 | \end{cfa}
|
|---|
| [135a2d8] | 920 | \end{tabular}
|
|---|
| 921 | \end{cquote}
|
|---|
| [0cb3479] | 922 |
|
|---|
| [1b56a7f] | 923 | \enlargethispage{1000pt}
|
|---|
| [0cb3479] | 924 |
|
|---|
| [e255902b] | 925 | \noindent
|
|---|
| [135a2d8] | 926 | External Scheduling
|
|---|
| 927 | \begin{cquote}
|
|---|
| 928 | \begin{tabular}{@{}l|ll@{}}
|
|---|
| 929 | \begin{uC++}
|
|---|
| 930 |
|
|---|
| 931 | _Monitor BoundedBuffer {
|
|---|
| 932 | int front = 0, back = 0, count = 0;
|
|---|
| 933 | int elements[20];
|
|---|
| 934 | public:
|
|---|
| 935 | _Nomutex int query() const { return count; }
|
|---|
| 936 | void insert( int elem );
|
|---|
| 937 | int remove();
|
|---|
| 938 | };
|
|---|
| 939 |
|
|---|
| 940 | void BoundedBuffer::insert( int elem ) {
|
|---|
| 941 | if ( count == 20 ) @_Accept( remove );@
|
|---|
| 942 | elements[back] = elem;
|
|---|
| 943 | back = ( back + 1 ) % 20;
|
|---|
| 944 | count += 1;
|
|---|
| 945 | }
|
|---|
| 946 | int BoundedBuffer::remove() {
|
|---|
| 947 | if ( count == 0 ) @_Accept( insert );@
|
|---|
| 948 | int elem = elements[front];
|
|---|
| 949 | front = ( front + 1 ) % 20;
|
|---|
| 950 | count -= 1;
|
|---|
| 951 | return elem;
|
|---|
| 952 | }
|
|---|
| 953 | \end{uC++}
|
|---|
| 954 | &
|
|---|
| 955 | \begin{cfa}
|
|---|
| 956 | #include <$monitor$.hfa>
|
|---|
| 957 | monitor BoundedBuffer {
|
|---|
| 958 | int front, back, count;
|
|---|
| 959 | int elements[20];
|
|---|
| 960 | };
|
|---|
| 961 | void ?{}( BoundedBuffer & buf ) with( buf ) {
|
|---|
| 962 | front = back = count = 0;
|
|---|
| 963 | }
|
|---|
| 964 | int query( BoundedBuffer & buf ) { return buf.count; }
|
|---|
| 965 | int remove( BoundedBuffer & @mutex@ buf ); // forward
|
|---|
| 966 | void insert( BoundedBuffer & @mutex@ buf, int elem ) with( buf ) {
|
|---|
| 967 | if ( count == 20 ) @waitfor( remove : buf );@
|
|---|
| 968 | elements[back] = elem;
|
|---|
| 969 | back = ( back + 1 ) % 20;
|
|---|
| 970 | count += 1;
|
|---|
| 971 | }
|
|---|
| 972 | int remove( BoundedBuffer & @mutex@ buf ) with( buf ) {
|
|---|
| 973 | if ( count == 0 ) @waitfor( insert : buf );@
|
|---|
| 974 | int elem = elements[front];
|
|---|
| 975 | front = ( front + 1 ) % 20;
|
|---|
| 976 | count -= 1;
|
|---|
| 977 | return elem;
|
|---|
| 978 | }
|
|---|
| 979 | \end{cfa}
|
|---|
| [bf91d1d] | 980 | \end{tabular}
|
|---|
| 981 | \end{cquote}
|
|---|
| 982 |
|
|---|
| 983 |
|
|---|
| [697c957] | 984 | \input{uC++toCFA.ind}
|
|---|
| 985 |
|
|---|
| 986 | % \bibliographystyle{plain}
|
|---|
| 987 | % \bibliography{pl}
|
|---|
| 988 |
|
|---|
| 989 | \end{document}
|
|---|
| 990 |
|
|---|
| 991 | % Local Variables: %
|
|---|
| 992 | % tab-width: 4 %
|
|---|
| 993 | % fill-column: 100 %
|
|---|
| 994 | % compile-command: "make" %
|
|---|
| 995 | % End: %
|
|---|