source: doc/uC++toCFA/uC++toCFA.tex@ dfe8f78

Last change on this file since dfe8f78 was d96f7c4, checked in by Peter A. Buhr <pabuhr@…>, 12 months ago

expunge fallthru keyword and replace its usages with fallthrough

  • Property mode set to 100644
File size: 21.8 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 : Fri Jan 17 07:39:09 2025
14%% Update Count : 6284
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 ); checked_poll()
377#define suspendPoll suspend; checked_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
400 @S@( int i, int j ) { S::i = i; S::j = j; }
401 @~S@() {}
402};
403S s1 = { 1, 2 };
404
405S * s2 = new S{ 1, 2 };
406delete s2;
407s2 = new S{ 1, 2 };
408delete s2;
409S & s3 = *new S{ 1, 2 };
410delete &s3;
411s3 = *new S{ 1, 2 };
412delete &s3;
413\end{uC++}
414&
415\begin{cfa}
416#include <stdlib.hfa> // new (malloc)
417struct S {
418 int i, j;
419};
420void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; }
421void @^?{}@( S & s ) { s.i = 0; s.j = 0; }
422
423S s1 = { 1, 2 };
424// cannot use 0/1 (zero_t/one_t) with "new"
425S * s2 = new( 1@n@, 2 ); // n => (int)
426delete( s2 );
427s2 = new( 1n, 2 );
428delete( s2 );
429S & s3 = *new( 1n, 2 );
430delete( s3 );
431&s3 = &*new( 1n, 2 );
432delete( s3 );
433\end{cfa}
434\end{tabular}
435\end{cquote}
436
437
438\section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
439
440\begin{cquote}
441\begin{tabular}{@{}l|l@{}}
442\begin{uC++}
443struct S {
444 int i = 0; // cheat, implicit default constructor
445 int setter( int j ) { int t = i; i = j; return t; }
446 int getter() { return i; }
447};
448S s;
449@s.@setter( 3 ); // object calls
450int k = @s.@getter();
451\end{uC++}
452&
453\begin{cfa}
454struct S { int i; };
455void ?{}( S & s ) { s.i = 0; } // explicit default constructor
456int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
457int getter( @S & s@ ) @with( s )@ { return i; }
458
459S s;
460setter( @s,@ 3 ); // normal calls
461int k = getter( @s@ );
462\end{cfa}
463\end{tabular}
464\end{cquote}
465
466
467\section{String}
468
469\begin{cquote}
470\begin{tabular}{@{}l|l@{}}
471\multicolumn{2}{@{}l@{}}{\lstinline{string s1, s2;}} \\
472\begin{uC++}
473s1 = "abcdefg";
474s2 = s1;
475s1 += s2;
476s1 == s2; s1 != s2;
477s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
478s1.length();
479s1[3];
480s1.substr( 2 ); s1.substr( 2, 3 );
481s1.replace( 2, 5, s2 );
482s1.find( s2 ); s1.rfind( s2 );
483s1.find_first_of( s2 ); s1.find_last_of( s2 );
484s1.find_first_not_of( s2 ); s1.find_last_not_of( s2 );
485getline( cin, s1 );
486cout << s1 << endl;
487\end{uC++}
488&
489\begin{cfa}
490s1 = "abcdefg";
491s2 = s1;
492s1 += s2;
493s1 == s2; s1 != s2;
494s1 < s2; s1 <= s2; s1 > s2; s1 >= s2;
495size( s1 );
496s1[3];
497s1( 2 ); s1( 2, 3 );
498// replace( s1, 2, 5, s2 );
499// find( s1, s2 ), rfind( s1, s2 );
500// find_first_of( s2 ); find_last_of( s2 );
501// find_first_not_of( s1, s2 ); find_last_not_of( s1, s2 );
502sin | getline( s1 );
503sout | s1;
504\end{cfa}
505\end{tabular}
506\end{cquote}
507
508
509\section{\texorpdfstring{\lstinline{uArray}}{uArray}}
510
511\begin{cquote}
512\begin{tabular}{@{}l|l@{}}
513\begin{uC++}
514#include <iostream>
515using namespace std;
516struct S {
517 int i;
518 S( int i ) { S::i = i; cout << "ctor " << S::i << endl; }
519 ~S() { S::i = i; cout << "dtor " << S::i << endl; }
520};
521int main() {
522 enum { N = 5 };
523 @uArray( S, s, N );@ // no constructor calls
524 for ( int i = 0; i < N; i += 1 ) @s[i]( i )@; // constructor calls
525 for ( int i = 0; i < N; i += 1 ) cout << s[i]@->@i << endl;
526}
527\end{uC++}
528&
529\begin{cfa}
530#include <fstream.hfa>
531#include <array.hfa>
532struct S {
533 int i;
534};
535void ?{}( S & s, int i ) { s.i = i; sout | "ctor" | s.i; }
536void ^?{}( S & s ) { sout | "dtor" | s.i; }
537int main() {
538 enum { N = 5 };
539 @array( S, N ) s = { delay_init };@ // no constructor calls
540 for ( i; N ) @s[i]{ i }@; // constructor calls
541 for ( i; N ) sout | s[i]@.@i;
542}
543\end{cfa}
544\end{tabular}
545\end{cquote}
546
547
548\section{Coroutine}
549
550\begin{cquote}
551\begin{tabular}{@{}l|ll@{}}
552\begin{uC++}
553
554@_Coroutine@ C {
555 // private coroutine fields
556 void main() {
557 ... @suspend();@ ...
558 ... @_Resume E( ... ) _At partner;@
559 ... @uThisCoroutine();@ ...
560
561 }
562 public:
563 void mem( ... ) {
564 ... @resume();@ ...
565 }
566};
567\end{uC++}
568&
569\begin{cfa}
570#include <$coroutine$.hfa>
571@coroutine@ C {
572 // private coroutine fields
573
574};
575void main( C & c ) {
576 ... @suspend;@ ... // keyword not routine
577 ... @resumeAt( partner, ExceptionInst( E, ... ) );@
578 ... @active_coroutine();@ ...
579}
580void mem( C & c, ... ) {
581 ... @resume( c );@ ...
582}
583\end{cfa}
584\\
585\multicolumn{2}{@{}l@{}}{\lstinline{C c;}}
586\end{tabular}
587\end{cquote}
588
589
590\section{\lstinline{COBEGIN}/\lstinline{COFOR}}
591
592\begin{cquote}
593\begin{tabular}{@{}l|ll@{}}
594\begin{uC++}
595
596#include <uCobegin.h>
597int main() {
598 @COBEGIN@
599 BEGIN osacquire( cout ) << "A" << endl; END
600 BEGIN osacquire( cout ) << "B" << endl; END
601 BEGIN osacquire( cout ) << "C" << endl; END
602 BEGIN osacquire( cout ) << "D" << endl; END
603 @COEND@
604 @COFOR@( i, 1, 10,
605 osacquire( cout ) << i << endl;
606 )
607}
608\end{uC++}
609&
610\begin{cfa}
611#include <mutex_stmt.hfa>
612#include <$cofor$.hfa>
613int main() {
614 {
615 @corun@ { mutex( sout ) sout | "A"; }
616 corun { mutex( sout ) sout | "B"; }
617 corun { mutex( sout ) sout | "C"; }
618 corun { mutex( sout ) sout | "D"; }
619 }
620 @cofor@( i; 10 ) {
621 mutex( sout ) sout | i;
622 }
623}
624\end{cfa}
625\end{tabular}
626\end{cquote}
627
628
629\section{Actor}
630
631\begin{cquote}
632\begin{tabular}{@{}l|ll@{}}
633\begin{uC++}
634#include <iostream>
635using namespace std;
636#include <uActor.h>
637
638struct StrMsg : @public uActor::Message@ {
639
640 const char * val; // string message
641
642 StrMsg( const char * val ) :
643 @Message( uActor::Delete )@, // delete after use
644 val( val ) {}
645};
646_Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
647 Allocation receive( Message & msg ) {
648 Case( @StartMsg@, msg ) { // discriminate
649
650 } else Case( StrMsg, msg ) {
651 osacquire( cout ) << msg_d->val << endl;
652
653 } else Case( @StopMsg@, msg )
654 return Delete; // delete actor
655 return Nodelete; // reuse actor
656 }
657};
658int main() {
659 @uActor::start();@ // start actor system
660 *new Hello() | uActor::startMsg
661 | *new StrMsg( "hello" ) | uActor::stopMsg;
662 *new Hello() | uActor::startMsg
663 | *new StrMsg( "bonjour" ) | uActor::stopMsg;
664 @uActor::stop();@ // wait for actors to terminate
665}
666\end{uC++}
667&
668\begin{cfa}
669#include <fstream.hfa>
670#include <mutex_stmt.hfa>
671#include <actor.hfa>
672
673struct StrMsg {
674 @inline message;@ // derived message
675 const char * val; // string message
676};
677void ?{}( StrMsg & msg, const char * str ) {
678 @set_allocation( msg, Delete );@ // delete after use
679 msg.val = str;
680}
681struct Hello { @inline actor;@ }; // derived actor
682allocation receive( Hello & receiver, @start_msg_t@ & ) {
683 return Nodelete;
684}
685allocation receive( Hello & receiver, StrMsg & msg ) {
686 mutex( sout ) sout | msg.val;
687 return Nodelete; // reuse actor
688}
689allocation receive( Hello & receiver, @stop_msg_t@ & ) {
690 return Delete; // delete actor
691}
692
693int main() {
694 @actor_start();@ // start actor system
695 *(Hello *)new() | start_msg
696 | *(StrMsg *)new( "hello" ) | stop_msg;
697 *(Hello *)new() | start_msg
698 | *(StrMsg *)new( "bonjour" ) | stop_msg;
699 @actor_stop();@ // wait for actors to terminate
700}
701\end{cfa}
702\end{tabular}
703\end{cquote}
704
705
706\section{Thread}
707
708\begin{cquote}
709\begin{tabular}{@{}l|ll@{}}
710\begin{uC++}
711
712@_Task@ T {
713 // private task fields
714 void main() {
715 ... @_Resume E( ... ) _At partner@;
716 ... @uThisTask();@ ...
717 }
718 public:
719};
720\end{uC++}
721&
722\begin{cfa}
723#include <$thread$.hfa>
724@thread@ T {
725 // private task fields
726
727};
728void main( @T & t@ ) {
729 ... @resumeAt( partner, ExceptionInst( E, ... )@ );
730 ... @active_thread();@ ...
731}
732\end{cfa}
733\\
734\multicolumn{2}{@{}l@{}}{\lstinline{T t; // start thread in main routine}}
735\end{tabular}
736\end{cquote}
737
738
739\section{Locks}
740
741\begin{cquote}
742\begin{tabular}{@{}l|ll@{}}
743\begin{uC++}
744
745uOwnerLock m;
746uCondLock s;
747m.acquire();
748if ( ! s.empty() ) s.wait( m );
749else {
750 m.release();
751}
752@osacquire( cout )@ << i << endl;
753\end{uC++}
754&
755\begin{cfa}
756#include <locks.hfa>
757owner_lock m;
758condition_variable( owner_lock ) s; // generic type on mutex lock
759lock( m );
760if ( ! empty( s ) ) wait( s, m );
761else {
762 unlock( m );
763}
764@mutex( sout )@ sout | i; // thread safe I/O
765\end{cfa}
766\end{tabular}
767\end{cquote}
768
769
770\section{Barrier}
771
772\begin{cquote}
773\begin{tabular}{@{}l|ll@{}}
774\begin{uC++}
775#include <iostream>
776using namespace std;
777#include <uBarrier.h>
778
779@_Cormonitor@ Barrier
780 : @public uBarrier@ { // inheritance
781 int total;
782 void @last@() { cout << total << endl; }
783 public:
784 Barrier( unsigned int group ) :
785 @uBarrier( group )@ {
786 total = 0;
787 }
788 void @block@( int subtotal ) {
789
790
791 total += subtotal;
792 @uBarrier::block();@
793 }
794};
795enum { N = 3 };
796Barrier b{ N };
797
798_Task T {
799 void main() {
800 for ( int i = 0; i < 10; i += 1 ) {
801 b.block( 1 );
802 }
803 }
804};
805int main() {
806 uProcessor p[N - 1];
807 T t[N];
808}
809\end{uC++}
810&
811\begin{cfa}
812#include <fstream.hfa>
813#include <$thread$.hfa>
814#include <barrier.hfa>
815#include <mutex_stmt.hfa>
816struct Barrier {
817 @barrier b;@ // containment
818 int total;
819
820};
821void ?{}( Barrier & B, unsigned int group ) with(B) {
822 @?{}( b, group );@ // initialize barrier
823 total = 0;
824}
825unsigned int block( Barrier & B, int subtotal ) with(B) {
826 void @last@() { sout | total; } // called by Gth arriving thread
827 @mutex( b )@ { // use barrier's mutual exclusion
828 total += subtotal;
829 return @block@( b, last ); // wait for barrier trigger
830 }
831}
832enum { N = 3 };
833Barrier b{ N };
834
835thread T {};
836void main( T & ) {
837 for ( 10 ) {
838 block( b, 1 );
839 }
840}
841
842int main() {
843 processor p[N - 1];
844 T t[N];
845}
846\end{cfa}
847\end{tabular}
848\end{cquote}
849
850\newpage
851
852\section{Monitor}
853
854Internal Scheduling
855\begin{cquote}
856\begin{tabular}{@{}l|ll@{}}
857\begin{uC++}
858
859@_Monitor@ BoundedBufferI {
860 @uCondition@ full, empty;
861 int front = 0, back = 0, count = 0;
862 int elements[20];
863 public:
864
865
866
867 @_Nomutex@ int query() const { return count; }
868
869 void insert( int elem ) {
870 if ( count == 20 ) @empty.wait();@
871 elements[back] = elem;
872 back = ( back + 1 ) % 20;
873 count += 1;
874 @full.signal();@
875 }
876 int remove() {
877 if ( count == 0 ) @full.wait();@
878 int elem = elements[front];
879 front = ( front + 1 ) % 20;
880 count -= 1;
881 @empty.signal();@
882 return elem;
883 }
884};
885\end{uC++}
886&
887\begin{cfa}
888#include <$monitor$.hfa>
889@monitor@ BoundedBufferI {
890 @condition@ full, empty;
891 int front, back, count;
892 int elements[20];
893};
894void ?{}( BoundedBufferI & buf ) with( buf ) {
895 front = back = count = 0;
896}
897int query( BoundedBufferI & buf ) { return buf.count; }
898int remove( BoundedBufferI & @mutex@ buf ); // forward
899void insert( BoundedBufferI & @mutex@ buf, int elem ) with( buf ) {
900 if ( count == 20 ) @wait( empty );@
901 elements[back] = elem;
902 back = ( back + 1 ) % 20;
903 count += 1
904 @signal( full );@
905}
906int remove( BoundedBufferI & @mutex@ buf ) with( buf ) {
907 if ( count == 0 ) @wait( full );@
908 int elem = elements[front];
909 front = ( front + 1 ) % 20;
910 count -= 1;
911 @signal( empty );@
912 return elem;
913}
914
915\end{cfa}
916\end{tabular}
917\end{cquote}
918
919\enlargethispage{1000pt}
920
921\noindent
922External Scheduling
923\begin{cquote}
924\begin{tabular}{@{}l|ll@{}}
925\begin{uC++}
926
927_Monitor BoundedBuffer {
928 int front = 0, back = 0, count = 0;
929 int elements[20];
930 public:
931 _Nomutex int query() const { return count; }
932 void insert( int elem );
933 int remove();
934};
935
936void BoundedBuffer::insert( int elem ) {
937 if ( count == 20 ) @_Accept( remove );@
938 elements[back] = elem;
939 back = ( back + 1 ) % 20;
940 count += 1;
941}
942int BoundedBuffer::remove() {
943 if ( count == 0 ) @_Accept( insert );@
944 int elem = elements[front];
945 front = ( front + 1 ) % 20;
946 count -= 1;
947 return elem;
948}
949\end{uC++}
950&
951\begin{cfa}
952#include <$monitor$.hfa>
953monitor BoundedBuffer {
954 int front, back, count;
955 int elements[20];
956};
957void ?{}( BoundedBuffer & buf ) with( buf ) {
958 front = back = count = 0;
959}
960int query( BoundedBuffer & buf ) { return buf.count; }
961int remove( BoundedBuffer & @mutex@ buf ); // forward
962void insert( BoundedBuffer & @mutex@ buf, int elem ) with( buf ) {
963 if ( count == 20 ) @waitfor( remove : buf );@
964 elements[back] = elem;
965 back = ( back + 1 ) % 20;
966 count += 1;
967}
968int remove( BoundedBuffer & @mutex@ buf ) with( buf ) {
969 if ( count == 0 ) @waitfor( insert : buf );@
970 int elem = elements[front];
971 front = ( front + 1 ) % 20;
972 count -= 1;
973 return elem;
974}
975\end{cfa}
976\end{tabular}
977\end{cquote}
978
979
980\input{uC++toCFA.ind}
981
982% \bibliographystyle{plain}
983% \bibliography{pl}
984
985\end{document}
986
987% Local Variables: %
988% tab-width: 4 %
989% fill-column: 100 %
990% compile-command: "make" %
991% End: %
Note: See TracBrowser for help on using the repository browser.