source: doc/uC++toCFA/uC++toCFA.tex@ 1a40870

Last change on this file since 1a40870 was 8617ee90, checked in by Peter A. Buhr <pabuhr@…>, 7 months ago

update constructor/destructor section for uC++toCFA

  • Property mode set to 100644
File size: 22.0 KB
Line 
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
13%% Last Modified On : Sat Mar 15 13:38:53 2025
14%% Update Count : 6302
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}
121\uC to \CFA Cheat Sheet}
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}
143int i; char i; double i; $\C[2in]{// overload name i}$
144int i(); double i(); char i();
145i += 1; $\C{// int i}$
146i += 1.0; $\C{// double i}$
147i += 'a'; $\C{// char i}$
148int j = i(); $\C{// int i()}$
149double j = i(); $\C{// double i();}$
150char j = i(); $\C{// char i()}\CRT$
151\end{cfa}
152\CFA has rebindable references.
153\begin{cquote}
154\begin{tabular}{@{}l|l@{}}
155\multicolumn{2}{@{}l}{\lstinline{ int x = 1, y = 2, * p1x = &x, * p1y = &y, ** p2i = &p1x,}} \\
156\multicolumn{2}{@{}l}{\lstinline{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ & r1x = x, & r1y = y, && r2i = r1x;}} \\
157\begin{uC++}
158**p2i = 3;
159p2i = &p1y;
160**p2i = 3;
161p1x = p1y;
162**p2i = 4;
163p1x = @nullptr@;
164\end{uC++}
165&
166\begin{cfa}
167r2i = 3; $\C[1.0in]{// change x}$
168&r2i = &r1y; $\C{// change p2i / r2i}$
169r2i = 3; $\C{// change y}$
170&r1x = &r1y; $\C{// change p1x / r1x}$
171r2i = 4; $\C{// change y}$
172&r1x = @0p@; $\C{// reset}\CRT$
173\end{cfa}
174\end{tabular}
175\end{cquote}
176Non-rebindable reference (\CC reference) is a @const@ reference (@const@ pointer).
177\begin{cfa}
178int & @const@ cr = x; // must initialize, no null pointer
179int & @const@ & @const@ crcr = cr; // generalize
180\end{cfa}
181Aggregate qualification is reduced or eliminated by opening scopes using the @with@ clause.
182\begin{cfa}
183struct S { int i; int j; double m; }; // field i has same type in structures S and T
184struct T { int i; int k; int m; };
185void foo( S s, T t ) @with( s, t )@ { // open structure scope s and t in parallel
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
196In subsequent code examples, the left example is \uC and the right example is \CFA.
197
198
199\section{Looping}
200
201\begin{cquote}
202\begin{tabular}{@{}l|l@{}}
203\begin{uC++}
204for ( @;;@ ) { ... } / while ( @true@ ) { ... }
205for ( int i = 0; i < @10@; i += 1 ) { ... }
206for ( int i = @5@; i < @15@; i += @2@ ) { ... }
207for ( int i = -1; i <@=@ 10; i += 3 ) { ... }
208for ( int i = 10; i > 0; i @-@= 1 ) { ... }
209\end{uC++}
210&
211\begin{cfa}
212for () { ... } / while () { ... }
213for ( @10@ ) { ... } / for ( i; @10@ ) { ... }
214for ( i; @5@ ~ @15@ ~ @2@ ) { ... }
215for ( i; -1 ~@=@ 10 ~ 3 ) { ... }
216for ( i; 0 @-@~ 10 ) { ... }
217\end{cfa}
218\end{tabular}
219\end{cquote}
220
221\begin{cquote}
222\begin{tabular}{@{}l|l@{}}
223\begin{uC++}
224int i = 0
225for ( i = 0; i < 10; i += 1 ) { ... }
226@if ( i == 10 )@ { ... }
227\end{uC++}
228&
229\begin{cfa}
230
231for ( i; 10 ) { ... }
232@else@ { ... } // i == 10
233\end{cfa}
234\end{tabular}
235\end{cquote}
236
237\begin{cquote}
238\begin{tabular}{@{}l|l@{}}
239\begin{uC++}
240@L1:@ for ( ;; ) {
241 @L2:@ for ( ;; ) {
242 ... if ( ... ) @break L1@; ...
243 ... if ( ... ) @break L2@; ...
244 }
245}
246\end{uC++}
247&
248\begin{cfa}
249@L1:@ for () {
250 @L2:@ for () {
251 ... if ( ... ) @break L1@; ...
252 ... if ( ... ) @break L2@; ...
253 }
254}
255\end{cfa}
256\end{tabular}
257\end{cquote}
258
259
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@>
267using namespace std;
268int i; double d; char c;
269cin >> i >> d >> c;
270cout << i << ' ' << d << ' ' << c << endl;
271\end{uC++}
272&
273\begin{cfa}
274#include <@fstream.hfa@>
275
276int i; double d; char c;
277sin | i | d | c;
278sout | i | d | c
279\end{cfa}
280\end{tabular}
281\end{cquote}
282To 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
287for ( int i = 0; i < 5; i += 1 ) cout << i << ' ';
288cout << @endl@;
289
2900 1 2 3 4
291\end{uC++}
292&
293\begin{cfa}
294sout | @nlOff@; // disable auto nl
295for ( i; 5 ) sout | i;
296sout | @nl@;
297sout | @nlOn@; // reenable auto nl
2980 1 2 3 4
299\end{cfa}
300\end{tabular}
301\end{cquote}
302Floating-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++}
306cout << 3.0 << ' ' << showpoint << setprecision(0) << 3.0 << endl;
3073 3.
308\end{uC++}
309&
310\begin{cfa}
311sout | @nodp( 3.0 )@ | 3.0;
3123 3.
313\end{cfa}
314\end{tabular}
315\end{cquote}
316
317
318\section{Exception}
319
320Currently, \CFA uses macros @ExceptionDecl@ and @ExceptionInst@ to declare and instantiate an exception.
321\begin{cquote}
322\begin{tabular}{@{}l|ll@{}}
323\begin{uC++}
324
325@_Exception@ E { // local or global scope
326 ... // exception fields
327};
328try {
329 ...
330 if ( ... ) @_Resume@ E( /* initialization */ );
331 if ( ... ) @_Throw@ E( /* initialization */ );
332 ...
333} @_CatchResume@( E & /* reference */ ) { ... }
334 catch( E & ) { ... }
335 catch( ... /* catch any */ ) { ... }
336 _Finally { ... }
337\end{uC++}
338&
339\begin{cfa}
340#include <Exception.hfa>
341@ExceptionDecl@( E, // must be global scope
342 ... // exception fields
343);
344try {
345 ...
346 if ( ... ) @throwResume@ @ExceptionInst@( E, /* intialization */ );
347 if ( ... ) @throw@ @ExceptionInst@( E, /* intialization */ );
348 ...
349} @catchResume@( E @*@ /* pointer */ ) { ... }
350 catch( E * ) { ... }
351 catch( exception_t @*@ /* catch any */ ) { ... }
352 finally { ... }
353\end{cfa}
354\end{tabular}
355\end{cquote}
356
357
358\section{Non-local Exception}
359
360\begin{cquote}
361\begin{tabular}{@{}l|ll@{}}
362\begin{uC++}
363
364
365void main() {
366 try {
367 _Enable {
368 ... suspend(); ...
369 }
370 } @_CatchResume@( E & /* reference */ ) { ... }
371 catch( E & ) { ... }
372}
373\end{uC++}
374&
375\begin{cfa}
376#define resumePoll( coroutine ) resume( coroutine ); poll()
377#define suspendPoll suspend; poll()
378void main() {
379 try {
380 enable_ehm();
381 ... suspendPoll ...
382 disable_ehm();
383 } @catchResume@( E * ) { ... }
384 catch( E & ) { ... }
385}
386\end{cfa}
387\end{tabular}
388\end{cquote}
389
390
391\section{Constructor / Destructor}
392
393\begin{cquote}
394\begin{tabular}{@{}l|l@{}}
395\begin{uC++}
396
397struct S {
398 int i, j;
399 @S@() { i = j = 3; }
400 @S@( int i, int j ) { S::i = i; S::j = j; }
401 @S@( const S & s ) { *this = s; }
402 @~S@() {}
403};
404S s0;
405S s1 = { 1, 2 };
406
407S * s2 = new S{ 1, 2 };
408delete s2;
409s2 = new S{ 1, 2 };
410delete s2;
411S & s3 = *new S{ 1, 2 };
412delete &s3;
413s3 = *new S{ 1, 2 };
414delete &s3;
415\end{uC++}
416&
417\begin{cfa}
418#include <stdlib.hfa> // new (malloc)
419struct S { int i, j; };
420
421void @?{}@( S & s ) { s.i = s.j = 3; } $\C[3in]{// default}$
422void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; } $\C{// initializer}$
423void @?{}@( S & s, const S rhs ) { s.[i,j] = rhs.[i,j]; } $\C{// copy}$
424void @^?{}@( S & s ) { s.i = 0; s.j = 0; } $\C{// destructor}\CRT$
425
426S s0;
427S s1 = { 1, 2 };
428// cannot use 0/1 (zero_t/one_t) with "new"
429S * s2 = new( 1@n@, 2 ); // n => (int)
430delete( s2 );
431s2 = new( 1n, 2 );
432delete( s2 );
433S & s3 = *new( 1n, 2 );
434delete( &s3 );
435&s3 = &*new( 1n, 2 );
436delete( &s3 );
437\end{cfa}
438\end{tabular}
439\end{cquote}
440
441
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++}
447struct 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};
452S s;
453@s.@setter( 3 ); // object calls
454int k = @s.@getter();
455\end{uC++}
456&
457\begin{cfa}
458struct S { int i; };
459void ?{}( S & s ) { s.i = 0; } // explicit default constructor
460int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
461int getter( @S & s@ ) @with( s )@ { return i; }
462
463S s;
464setter( @s,@ 3 ); // normal calls
465int k = getter( @s@ );
466\end{cfa}
467\end{tabular}
468\end{cquote}
469
470
471\section{String}
472
473\begin{cquote}
474\begin{tabular}{@{}l|l@{}}
475\multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\
476\begin{uC++}
477s1 = "abcdefg";
478s2 = s1;
479s1 += s2;
480s1 == s2; s1 != s2;
481s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
482s1.length();
483s1[3];
484s1.substr( 2 ); s1.substr( 2, 3 );
485s1.replace( 2, 5, s2 );
486s1.find( s2 ); s1.rfind( s2 );
487s1.find_first_of( s2 ); s1.find_last_of( s2 );
488s1.find_first_not_of( s2 ); s1.find_last_not_of( s2 );
489getline( cin, s1 );
490cout << s1 << endl;
491\end{uC++}
492&
493\begin{cfa}
494s1 = "abcdefg";
495s2 = s1;
496s1 += s2;
497s1 == s2; s1 != s2;
498s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
499size( s1 );
500s1[3];
501s1( 2 ); s1( 2, 3 );
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 );
506sin | getline( s1 );
507sout | s1;
508\end{cfa}
509\end{tabular}
510\end{cquote}
511
512
513\section{\texorpdfstring{\lstinline{uArray}}{uArray}}
514
515\begin{cquote}
516\begin{tabular}{@{}l|l@{}}
517\begin{uC++}
518#include <iostream>
519using namespace std;
520struct S {
521 int i;
522 S( int i ) { S::i = i; cout << "ctor " << S::i << endl; }
523 ~S() { S::i = i; cout << "dtor " << S::i << endl; }
524};
525int main() {
526 enum { N = 5 };
527 @uArray( S, s, N );@ // no constructor calls
528 for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // constructor calls
529 for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl;
530}
531\end{uC++}
532&
533\begin{cfa}
534#include <fstream.hfa>
535#include <array.hfa>
536struct S {
537 int i;
538};
539void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
540void ^?{}( S & s ) { sout | "dtor" | s.i; }
541int main() {
542 enum { N = 5 };
543 @array( S, N ) s = { delay_init };@ // no constructor calls
544 for ( i; N ) @s[i]{ i }@; // constructor calls
545 for ( i; N ) sout | s[i]@.@i;
546}
547\end{cfa}
548\end{tabular}
549\end{cquote}
550
551
552\section{Coroutine}
553
554\begin{cquote}
555\begin{tabular}{@{}l|ll@{}}
556\begin{uC++}
557
558@_Coroutine@ C {
559 // private coroutine fields
560 void main() {
561 ... @suspend();@ ...
562 ... @_Resume E( ... ) _At partner;@
563 ... @uThisCoroutine();@ ...
564
565 }
566 public:
567 void mem( ... ) {
568 ... @resume();@ ...
569 }
570};
571\end{uC++}
572&
573\begin{cfa}
574#include <$coroutine$.hfa>
575@coroutine@ C {
576 // private coroutine fields
577
578};
579void main( C & c ) {
580 ... @suspend;@ ... // keyword not routine
581 ... @resumeAt( partner, ExceptionInst( E, ... ) );@
582 ... @active_coroutine();@ ...
583}
584void mem( C & c, ... ) {
585 ... @resume( c );@ ...
586}
587\end{cfa}
588\\
589\multicolumn{2}{@{}l@{}}{\lstinline{C c;}}
590\end{tabular}
591\end{cquote}
592
593
594\section{\lstinline{COBEGIN}/\lstinline{COFOR}}
595
596\begin{cquote}
597\begin{tabular}{@{}l|ll@{}}
598\begin{uC++}
599
600#include <uCobegin.h>
601int main() {
602 @COBEGIN@
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
607 @COEND@
608 @COFOR@( i, 1, 10,
609 osacquire( cout ) << i << endl;
610 )
611}
612\end{uC++}
613&
614\begin{cfa}
615#include <mutex_stmt.hfa>
616#include <$cofor$.hfa>
617int main() {
618 {
619 @corun@ { mutex( sout ) sout | "A"; }
620 corun { mutex( sout ) sout | "B"; }
621 corun { mutex( sout ) sout | "C"; }
622 corun { mutex( sout ) sout | "D"; }
623 }
624 @cofor@( i; 10 ) {
625 mutex( sout ) sout | i;
626 }
627}
628\end{cfa}
629\end{tabular}
630\end{cquote}
631
632
633\section{Actor}
634
635\begin{cquote}
636\begin{tabular}{@{}l|ll@{}}
637\begin{uC++}
638#include <iostream>
639using namespace std;
640#include <uActor.h>
641
642struct StrMsg : @public uActor::Message@ {
643
644 const char * val; // string message
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 ) {
652 Case( @StartMsg@, msg ) { // discriminate
653
654 } else Case( StrMsg, msg ) {
655 osacquire( cout ) << msg_d->val << endl;
656
657 } else Case( @StopMsg@, msg )
658 return Delete; // delete actor
659 return Nodelete; // reuse actor
660 }
661};
662int main() {
663 @uActor::start();@ // start actor system
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
669}
670\end{uC++}
671&
672\begin{cfa}
673#include <fstream.hfa>
674#include <mutex_stmt.hfa>
675#include <actor.hfa>
676
677struct StrMsg {
678 @inline message;@ // derived message
679 const char * val; // string message
680};
681void ?{}( StrMsg & msg, const char * str ) {
682 @set_allocation( msg, Delete );@ // delete after use
683 msg.val = str;
684}
685struct Hello { @inline actor;@ }; // derived actor
686allocation receive( Hello & receiver, @start_msg_t@ & ) {
687 return Nodelete;
688}
689allocation receive( Hello & receiver, StrMsg & msg ) {
690 mutex( sout ) sout | msg.val;
691 return Nodelete; // reuse actor
692}
693allocation receive( Hello & receiver, @stop_msg_t@ & ) {
694 return Delete; // delete actor
695}
696
697int main() {
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
704}
705\end{cfa}
706\end{tabular}
707\end{cquote}
708
709
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};
732void 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
743\section{Locks}
744
745\begin{cquote}
746\begin{tabular}{@{}l|ll@{}}
747\begin{uC++}
748
749uOwnerLock m;
750uCondLock s;
751m.acquire();
752if ( ! s.empty() ) s.wait( m );
753else {
754 m.release();
755}
756@osacquire( cout )@ << i << endl;
757\end{uC++}
758&
759\begin{cfa}
760#include <locks.hfa>
761owner_lock m;
762condition_variable( owner_lock ) s; // generic type on mutex lock
763lock( m );
764if ( ! empty( s ) ) wait( s, m );
765else {
766 unlock( m );
767}
768@mutex( sout )@ sout | i; // thread safe I/O
769\end{cfa}
770\end{tabular}
771\end{cquote}
772
773
774\section{Barrier}
775
776\begin{cquote}
777\begin{tabular}{@{}l|ll@{}}
778\begin{uC++}
779#include <iostream>
780using namespace std;
781#include <uBarrier.h>
782
783@_Cormonitor@ Barrier
784 : @public uBarrier@ { // inheritance
785 int total;
786 void @last@() { cout << total << endl; }
787 public:
788 Barrier( unsigned int group ) :
789 @uBarrier( group )@ {
790 total = 0;
791 }
792 void @block@( int subtotal ) {
793
794
795 total += subtotal;
796 @uBarrier::block();@
797 }
798};
799enum { N = 3 };
800Barrier 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};
809int 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>
820struct Barrier {
821 @barrier b;@ // containment
822 int total;
823
824};
825void ?{}( Barrier & B, unsigned int group ) with(B) {
826 @?{}( b, group );@ // initialize barrier
827 total = 0;
828}
829unsigned int block( Barrier & B, int subtotal ) with(B) {
830 void @last@() { sout | total; } // called by Gth arriving thread
831 @mutex( b )@ { // use barrier's mutual exclusion
832 total += subtotal;
833 return @block@( b, last ); // wait for barrier trigger
834 }
835}
836enum { N = 3 };
837Barrier b{ N };
838
839thread T {};
840void main( T & ) {
841 for ( 10 ) {
842 block( b, 1 );
843 }
844}
845
846int main() {
847 processor p[N - 1];
848 T t[N];
849}
850\end{cfa}
851\end{tabular}
852\end{cquote}
853
854\newpage
855
856\section{Monitor}
857
858Internal Scheduling
859\begin{cquote}
860\begin{tabular}{@{}l|ll@{}}
861\begin{uC++}
862
863@_Monitor@ BoundedBufferI {
864 @uCondition@ full, empty;
865 int front = 0, back = 0, count = 0;
866 int elements[20];
867 public:
868
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;
887 }
888};
889\end{uC++}
890&
891\begin{cfa}
892#include <$monitor$.hfa>
893@monitor@ BoundedBufferI {
894 @condition@ full, empty;
895 int front, back, count;
896 int elements[20];
897};
898void ?{}( BoundedBufferI & buf ) with( buf ) {
899 front = back = count = 0;
900}
901int query( BoundedBufferI & buf ) { return buf.count; }
902int remove( BoundedBufferI & @mutex@ buf ); // forward
903void 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}
910int 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;
917}
918
919\end{cfa}
920\end{tabular}
921\end{cquote}
922
923\enlargethispage{1000pt}
924
925\noindent
926External 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
940void BoundedBuffer::insert( int elem ) {
941 if ( count == 20 ) @_Accept( remove );@
942 elements[back] = elem;
943 back = ( back + 1 ) % 20;
944 count += 1;
945}
946int 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>
957monitor BoundedBuffer {
958 int front, back, count;
959 int elements[20];
960};
961void ?{}( BoundedBuffer & buf ) with( buf ) {
962 front = back = count = 0;
963}
964int query( BoundedBuffer & buf ) { return buf.count; }
965int remove( BoundedBuffer & @mutex@ buf ); // forward
966void 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}
972int 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}
980\end{tabular}
981\end{cquote}
982
983
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: %
Note: See TracBrowser for help on using the repository browser.