Changes in / [b5563e1:a9b1b0c]


Ignore:
Files:
1 deleted
11 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/Makefile

    rb5563e1 ra9b1b0c  
    44Figures = figures
    55Macros = AMA/AMA-stix/ama
    6 TeXLIB = .:annex:../../LaTeXmacros:${Macros}:${Build}:../../bibliography:
     6TeXLIB = .:style:annex:../../LaTeXmacros:${Macros}:${Build}:../../bibliography:
    77LaTeX  = TEXINPUTS=${TeXLIB} && export TEXINPUTS && latex -halt-on-error -output-directory=${Build}
    88BibTeX = BIBINPUTS=${TeXLIB} && export BIBINPUTS && bibtex
  • doc/papers/concurrency/Paper.tex

    rb5563e1 ra9b1b0c  
     1% inline code ©...© (copyright symbol) emacs: C-q M-)
     2% red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
     3% blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_
     4% green highlighting ¢...¢ (cent symbol) emacs: C-q M-"
     5% LaTex escape §...§ (section symbol) emacs: C-q M-'
     6% keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
     7% math escape $...$ (dollar symbol)
     8
    19\documentclass[AMA,STIX1COL]{WileyNJD-v2}
    210
     
    1220
    1321% Latex packages used in the document.
     22
     23\usepackage[T1]{fontenc}                                        % allow Latin1 (extended ASCII) characters
     24\usepackage{textcomp}
     25\usepackage[latin1]{inputenc}
     26
    1427\usepackage{epic,eepic}
    1528\usepackage{xspace}
     
    2134\usepackage{siunitx}
    2235\sisetup{ binary-units=true }
    23 %\input{style}                                                          % bespoke macros used in the document
     36\input{style}                                                           % bespoke macros used in the document
    2437
    2538\hypersetup{breaklinks=true}
     
    3851% Names used in the document.
    3952
    40 \newcommand{\CFAIcon}{\textsf{C}\raisebox{\depth}{\rotatebox{180}{\textsf{A}}}\xspace} % Cforall symbolic name
    41 \newcommand{\CFA}{\protect\CFAIcon}             % safe for section/caption
    42 \newcommand{\CFL}{\textrm{Cforall}\xspace}      % Cforall symbolic name
    43 \newcommand{\Celeven}{\textrm{C11}\xspace}      % C11 symbolic name
    44 \newcommand{\CC}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}\xspace} % C++ symbolic name
    45 \newcommand{\CCeleven}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}11\xspace} % C++11 symbolic name
    46 \newcommand{\CCfourteen}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}14\xspace} % C++14 symbolic name
    47 \newcommand{\CCseventeen}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}17\xspace} % C++17 symbolic name
    48 \newcommand{\CCtwenty}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}20\xspace} % C++20 symbolic name
    49 \newcommand{\Csharp}{C\raisebox{-0.7ex}{\Large$^\sharp$}\xspace} % C# symbolic name
    50 
    51 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
     53\newcommand{\CS}{C\raisebox{-0.9ex}{\large$^\sharp$}\xspace}
    5254
    5355\newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}}
     
    6062\newcommand{\TODO}{{\Textbf{TODO}}}
    6163
     64
     65\newsavebox{\LstBox}
     66
    6267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    63 
    64 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
    65 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
    66 % AFTER HYPERREF.
    67 %\DeclareTextCommandDefault{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}
    68 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
    69 
    70 \makeatletter
    71 % parindent is relative, i.e., toggled on/off in environments like itemize, so store the value for
    72 % use rather than use \parident directly.
    73 \newlength{\parindentlnth}
    74 \setlength{\parindentlnth}{\parindent}
    75 
    76 \newcommand{\LstBasicStyle}[1]{{\lst@basicstyle{\lst@basicstyle{#1}}}}
    77 \newcommand{\LstKeywordStyle}[1]{{\lst@basicstyle{\lst@keywordstyle{#1}}}}
    78 \newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}}
    79 
    80 \newlength{\gcolumnposn}                                        % temporary hack because lstlisting does not handle tabs correctly
    81 \newlength{\columnposn}
    82 \setlength{\gcolumnposn}{3.5in}
    83 \setlength{\columnposn}{\gcolumnposn}
    84 \newcommand{\C}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\lst@basicstyle{\LstCommentStyle{#2}}}}
    85 \newcommand{\CRT}{\global\columnposn=\gcolumnposn}
    86 
    87 % Denote newterms in particular font and index them without particular font and in lowercase, e.g., \newterm{abc}.
    88 % The option parameter provides an index term different from the new term, e.g., \newterm[\texttt{abc}]{abc}
    89 % The star version does not lowercase the index information, e.g., \newterm*{IBM}.
    90 \newcommand{\newtermFontInline}{\emph}
    91 \newcommand{\newterm}{\@ifstar\@snewterm\@newterm}
    92 \newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
    93 \newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi}
    94 
    95 % Latin abbreviation
    96 \newcommand{\abbrevFont}{\textit}                       % set empty for no italics
    97 \newcommand{\EG}{\abbrevFont{e}.\abbrevFont{g}.}
    98 \newcommand*{\eg}{%
    99         \@ifnextchar{,}{\EG}%
    100                 {\@ifnextchar{:}{\EG}%
    101                         {\EG,\xspace}}%
    102 }%
    103 \newcommand{\IE}{\abbrevFont{i}.\abbrevFont{e}.}
    104 \newcommand*{\ie}{%
    105         \@ifnextchar{,}{\IE}%
    106                 {\@ifnextchar{:}{\IE}%
    107                         {\IE,\xspace}}%
    108 }%
    109 \newcommand{\ETC}{\abbrevFont{etc}}
    110 \newcommand*{\etc}{%
    111         \@ifnextchar{.}{\ETC}%
    112         {\ETC.\xspace}%
    113 }%
    114 \newcommand{\ETAL}{\abbrevFont{et}~\abbrevFont{al}}
    115 \renewcommand*{\etal}{%
    116         \@ifnextchar{.}{\protect\ETAL}%
    117                 {\protect\ETAL.\xspace}%
    118 }%
    119 \newcommand{\VIZ}{\abbrevFont{viz}}
    120 \newcommand*{\viz}{%
    121         \@ifnextchar{.}{\VIZ}%
    122                 {\VIZ.\xspace}%
    123 }%
    124 \makeatother
    125 
    126 \newenvironment{cquote}{%
    127         \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=3pt\parsep=0pt\leftmargin=\parindentlnth\rightmargin\leftmargin}%
    128         \item\relax
    129 }{%
    130         \endlist
    131 }% cquote
    132 
    133 % CFA programming language, based on ANSI C (with some gcc additions)
    134 \lstdefinelanguage{CFA}[ANSI]{C}{
    135         morekeywords={
    136                 _Alignas, _Alignof, __alignof, __alignof__, asm, __asm, __asm__, _At, __attribute,
    137                 __attribute__, auto, _Bool, catch, catchResume, choose, _Complex, __complex, __complex__,
    138                 __const, __const__, disable, dtype, enable, exception, __extension__, fallthrough, fallthru,
    139                 finally, forall, ftype, _Generic, _Imaginary, inline, __label__, lvalue, _Noreturn, one_t,
    140                 otype, restrict, _Static_assert, throw, throwResume, trait, try, ttype, typeof, __typeof,
    141                 __typeof__, virtual, with, zero_t},
    142         morekeywords=[2]{
    143                 _Atomic, coroutine, is_coroutine, is_monitor, is_thread, monitor, mutex, nomutex, or,
    144                 resume, suspend, thread, _Thread_local, waitfor, when, yield},
    145         moredirectives={defined,include_next}%
    146 }
    147 
    148 \lstset{
    149 language=CFA,
    150 columns=fullflexible,
    151 basicstyle=\linespread{0.9}\sf,                                                 % reduce line spacing and use sanserif font
    152 stringstyle=\tt,                                                                                % use typewriter font
    153 tabsize=5,                                                                                              % N space tabbing
    154 xleftmargin=\parindentlnth,                                                             % indent code to paragraph indentation
    155 %mathescape=true,                                                                               % LaTeX math escape in CFA code $...$
    156 escapechar=\$,                                                                                  % LaTeX escape in CFA code
    157 keepspaces=true,                                                                                %
    158 showstringspaces=false,                                                                 % do not show spaces with cup
    159 showlines=true,                                                                                 % show blank lines at end of code
    160 aboveskip=4pt,                                                                                  % spacing above/below code block
    161 belowskip=3pt,
    162 % replace/adjust listing characters that look bad in sanserif
    163 literate={-}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.1ex}}}}1 {^}{\raisebox{0.6ex}{$\scriptstyle\land\,$}}1
    164         {~}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}}1 % {`}{\ttfamily\upshape\hspace*{-0.1ex}`}1
    165         {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2 {->}{\makebox[1ex][c]{\raisebox{0.5ex}{\rule{0.8ex}{0.075ex}}}\kern-0.2ex{\textgreater}}2,
    166 moredelim=**[is][\color{red}]{`}{`},
    167 }% lstset
    168 
    169 % uC++ programming language, based on ANSI C++
    170 \lstdefinelanguage{uC++}[ANSI]{C++}{
    171         morekeywords={
    172                 _Accept, _AcceptReturn, _AcceptWait, _Actor, _At, _CatchResume, _Cormonitor, _Coroutine, _Disable,
    173                 _Else, _Enable, _Event, _Finally, _Monitor, _Mutex, _Nomutex, _PeriodicTask, _RealTimeTask,
    174                 _Resume, _Select, _SporadicTask, _Task, _Timeout, _When, _With, _Throw},
    175 }
    176 \lstdefinelanguage{Golang}{
    177         morekeywords=[1]{package,import,func,type,struct,return,defer,panic,recover,select,var,const,iota,},
    178         morekeywords=[2]{string,uint,uint8,uint16,uint32,uint64,int,int8,int16,int32,int64,
    179                 bool,float32,float64,complex64,complex128,byte,rune,uintptr, error,interface},
    180         morekeywords=[3]{map,slice,make,new,nil,len,cap,copy,close,true,false,delete,append,real,imag,complex,chan,},
    181         morekeywords=[4]{for,break,continue,range,goto,switch,case,fallthrough,if,else,default,},
    182         morekeywords=[5]{Println,Printf,Error,},
    183         sensitive=true,
    184         morecomment=[l]{//},
    185         morecomment=[s]{/*}{*/},
    186         morestring=[b]',
    187         morestring=[b]",
    188         morestring=[s]{`}{`},
    189 }
    190 
    191 \lstnewenvironment{cfa}[1][]
    192 {\lstset{#1}}
    193 {}
    194 \lstnewenvironment{C++}[1][]                            % use C++ style
    195 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{`}{`},#1}\lstset{#1}}
    196 {}
    197 \lstnewenvironment{uC++}[1][]
    198 {\lstset{#1}}
    199 {}
    200 \lstnewenvironment{Go}[1][]
    201 {\lstset{#1}}
    202 {}
    203 
    204 % inline code @...@
    205 \lstMakeShortInline@%
    206 
    20768
    20869\title{\texorpdfstring{Concurrency in \protect\CFA}{Concurrency in Cforall}}
     
    22081\abstract[Summary]{
    22182\CFA is a modern, polymorphic, \emph{non-object-oriented} extension of the C programming language.
    222 This paper discusses the design of the concurrency and parallelism features in \CFA, and the concurrent runtime-system.
     83This paper discusses the design of the concurrency and parallelism features in \CFA, and the concurrent runtime-system that supports them.
    22384These features are created from scratch as ISO C lacks concurrency, relying largely on pthreads.
    22485Coroutines and lightweight (user) threads are introduced into the language.
     
    22687A unique contribution is allowing multiple monitors to be safely acquired simultaneously.
    22788All features respect the expectations of C programmers, while being fully integrate with the \CFA polymorphic type-system and other language features.
    228 Finally, experimental results are presented to compare the performance of the new features with similar mechanisms in other concurrent programming-languages.
     89Finally, experimental results are presented to validate several of the new features with other concurrent programming-languages.
    22990}%
    23091
     
    243104% ======================================================================
    244105
    245 This paper provides a minimal concurrency \newterm{API} that is simple, efficient and can be used to build other concurrency features.
    246 While the simplest concurrency system is a thread and a lock, this low-level approach is hard to master.
    247 An easier approach for programmers is to support higher-level constructs as the basis of concurrency.
    248 Indeed, for highly productive concurrent programming, high-level approaches are much more popular~\cite{Hochstein05}.
    249 Examples of high-level approaches are task based~\cite{TBB}, message passing~\cite{Erlang,MPI}, and implicit threading~\cite{OpenMP}.
    250 
    251 The terminology used in this paper is as follows.
    252 A \newterm{thread} is a fundamental unit of execution that runs a sequence of code and requires a stack to maintain state.
    253 Multiple simultaneous threads gives rise to \newterm{concurrency}, which requires locking to ensure safe access to shared data.
    254 % Correspondingly, concurrency is defined as the concepts and challenges that occur when multiple independent (sharing memory, timing dependencies, etc.) concurrent threads are introduced.
    255 \newterm{Locking}, and by extension locks, are defined as a mechanism to prevent progress of threads to provide safety.
    256 \newterm{Parallelism} is running multiple threads simultaneously.
    257 Parallelism implies \emph{actual} simultaneous execution, where concurrency only requires \emph{apparent} simultaneous execution.
    258 As such, parallelism is only observable in differences in performance, which is observed through differences in timing.
    259 
    260 Hence, there are two problems to be solved in the design of concurrency for a programming language: concurrency and parallelism.
     106This paper provides a minimal concurrency \textbf{api} that is simple, efficient and can be reused to build higher-level features.
     107The simplest possible concurrency system is a thread and a lock but this low-level approach is hard to master.
     108An easier approach for users is to support higher-level constructs as the basis of concurrency.
     109Indeed, for highly productive concurrent programming, high-level approaches are much more popular~\cite{HPP:Study}.
     110Examples are task based, message passing and implicit threading.
     111The high-level approach and its minimal \textbf{api} are tested in a dialect of C, called \CFA.
     112Furthermore, the proposed \textbf{api} doubles as an early definition of the \CFA language and library.
     113This paper also provides an implementation of the concurrency library for \CFA as well as all the required language features added to the source-to-source translator.
     114
     115There are actually two problems that need to be solved in the design of concurrency for a programming language: which concurrency and which parallelism tools are available to the programmer.
    261116While these two concepts are often combined, they are in fact distinct, requiring different tools~\cite{Buhr05a}.
    262 Concurrency tools handle mutual exclusion and synchronization, while parallelism tools handle performance, cost and resource utilization.
    263 
    264 The proposed concurrency API is implemented in a dialect of C, called \CFA.
    265 The paper discusses how the language features are added to the \CFA translator with respect to parsing, semantic, and type checking, and the corresponding high-perforamnce runtime-library to implement the concurrency features.
     117Concurrency tools need to handle mutual exclusion and synchronization, while parallelism tools are about performance, cost and resource utilization.
     118
     119In the context of this paper, a \textbf{thread} is a fundamental unit of execution that runs a sequence of code, generally on a program stack.
     120Having multiple simultaneous threads gives rise to concurrency and generally requires some kind of locking mechanism to ensure proper execution.
     121Correspondingly, \textbf{concurrency} is defined as the concepts and challenges that occur when multiple independent (sharing memory, timing dependencies, etc.) concurrent threads are introduced.
     122Accordingly, \textbf{locking} (and by extension locks) are defined as a mechanism that prevents the progress of certain threads in order to avoid problems due to concurrency.
     123Finally, in this paper \textbf{parallelism} is distinct from concurrency and is defined as running multiple threads simultaneously.
     124More precisely, parallelism implies \emph{actual} simultaneous execution as opposed to concurrency which only requires \emph{apparent} simultaneous execution.
     125As such, parallelism is only observable in the differences in performance or, more generally, differences in timing.
    266126
    267127% ======================================================================
     
    279139Interestingly, while \CFA is not an object-oriented language, lacking the concept of a receiver (e.g., {\tt this}), it does have some notion of objects\footnote{C defines the term objects as : ``region of data storage in the execution environment, the contents of which can represent
    280140values''~\cite[3.15]{C11}}, most importantly construction and destruction of objects.
    281 Most of the following code examples can be found on the \CFA website~\cite{Cforall}.
    282 
    283 
     141Most of the following code examples can be found on the \CFA website~\cite{www-cfa}.
     142
     143% ======================================================================
    284144\subsection{References}
    285145
    286146Like \CC, \CFA introduces rebind-able references providing multiple dereferencing as an alternative to pointers.
    287147In regards to concurrency, the semantic difference between pointers and references are not particularly relevant, but since this document uses mostly references, here is a quick overview of the semantics:
    288 \begin{cfa}
     148\begin{cfacode}
    289149int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
    290150        &r1 = x,    &&r2 = r1,   &&&r3 = r2;
    291 ***p3 = 3;                                                      $\C{// change x}$
    292 r3    = 3;                                                      $\C{// change x, ***r3}$
    293 **p3  = ...;                                            $\C{// change p1}$
    294 *p3   = ...;                                            $\C{// change p2}$
    295 int y, z, & ar[3] = {x, y, z};          $\C{// initialize array of references}$
    296 typeof( ar[1]) p;                                       $\C{// is int, referenced object type}$
    297 typeof(&ar[1]) q;                                       $\C{// is int \&, reference type}$
    298 sizeof( ar[1]) == sizeof(int);          $\C{// is true, referenced object size}$
    299 sizeof(&ar[1]) == sizeof(int *);        $\C{// is true, reference size}$
    300 \end{cfa}
     151***p3 = 3;                                                      //change x
     152r3    = 3;                                                      //change x, ***r3
     153**p3  = ...;                                            //change p1
     154*p3   = ...;                                            //change p2
     155int y, z, & ar[3] = {x, y, z};          //initialize array of references
     156typeof( ar[1]) p;                                       //is int, referenced object type
     157typeof(&ar[1]) q;                                       //is int &, reference type
     158sizeof( ar[1]) == sizeof(int);          //is true, referenced object size
     159sizeof(&ar[1]) == sizeof(int *);        //is true, reference size
     160\end{cfacode}
    301161The important take away from this code example is that a reference offers a handle to an object, much like a pointer, but which is automatically dereferenced for convenience.
    302162
     
    307167As well, \CFA uses the return type as part of the selection criteria, as in Ada~\cite{Ada}.
    308168For routines with multiple parameters and returns, the selection is complex.
    309 \begin{cfa}
    310 // selection based on type and number of parameters
    311 void f(void);                   $\C{// (1)}$
    312 void f(char);                   $\C{// (2)}$
    313 void f(int, double);    $\C{// (3)}$
    314 f();                                    $\C{// select (1)}$
    315 f('a');                                 $\C{// select (2)}$
    316 f(3, 5.2);                              $\C{// select (3)}$
    317 
    318 // selection based on  type and number of returns
    319 char   f(int);                  $\C{// (1)}$
    320 double f(int);                  $\C{// (2)}$
    321 char   c = f(3);                $\C{// select (1)}$
    322 double d = f(4);                $\C{// select (2)}$
    323 \end{cfa}
     169\begin{cfacode}
     170//selection based on type and number of parameters
     171void f(void);                   //(1)
     172void f(char);                   //(2)
     173void f(int, double);    //(3)
     174f();                                    //select (1)
     175f('a');                                 //select (2)
     176f(3, 5.2);                              //select (3)
     177
     178//selection based on  type and number of returns
     179char   f(int);                  //(1)
     180double f(int);                  //(2)
     181char   c = f(3);                //select (1)
     182double d = f(4);                //select (2)
     183\end{cfacode}
    324184This feature is particularly important for concurrency since the runtime system relies on creating different types to represent concurrency objects.
    325185Therefore, overloading is necessary to prevent the need for long prefixes and other naming conventions that prevent name clashes.
    326 As seen in section \ref{basics}, routine @main@ is an example that benefits from overloading.
     186As seen in section \ref{basics}, routine \code{main} is an example that benefits from overloading.
    327187
    328188% ======================================================================
     
    330190Overloading also extends to operators.
    331191The syntax for denoting operator-overloading is to name a routine with the symbol of the operator and question marks where the arguments of the operation appear, e.g.:
    332 \begin{cfa}
    333 int ++? (int op);                       $\C{// unary prefix increment}$
    334 int ?++ (int op);                       $\C{// unary postfix increment}$
    335 int ?+? (int op1, int op2);             $\C{// binary plus}$
    336 int ?<=?(int op1, int op2);             $\C{// binary less than}$
    337 int ?=? (int & op1, int op2);           $\C{// binary assignment}$
    338 int ?+=?(int & op1, int op2);           $\C{// binary plus-assignment}$
     192\begin{cfacode}
     193int ++? (int op);                       //unary prefix increment
     194int ?++ (int op);                       //unary postfix increment
     195int ?+? (int op1, int op2);             //binary plus
     196int ?<=?(int op1, int op2);             //binary less than
     197int ?=? (int & op1, int op2);           //binary assignment
     198int ?+=?(int & op1, int op2);           //binary plus-assignment
    339199
    340200struct S {int i, j;};
    341 S ?+?(S op1, S op2) {                           $\C{// add two structures}$
     201S ?+?(S op1, S op2) {                           //add two structures
    342202        return (S){op1.i + op2.i, op1.j + op2.j};
    343203}
    344204S s1 = {1, 2}, s2 = {2, 3}, s3;
    345 s3 = s1 + s2;                                           $\C{// compute sum: s3 == {2, 5}}$
    346 \end{cfa}
     205s3 = s1 + s2;                                           //compute sum: s3 == {2, 5}
     206\end{cfacode}
    347207While concurrency does not use operator overloading directly, this feature is more important as an introduction for the syntax of constructors.
    348208
     
    351211Object lifetime is often a challenge in concurrency. \CFA uses the approach of giving concurrent meaning to object lifetime as a means of synchronization and/or mutual exclusion.
    352212Since \CFA relies heavily on the lifetime of objects, constructors and destructors is a core feature required for concurrency and parallelism. \CFA uses the following syntax for constructors and destructors:
    353 \begin{cfa}
     213\begin{cfacode}
    354214struct S {
    355215        size_t size;
    356216        int * ia;
    357217};
    358 void ?{}(S & s, int asize) {    $\C{// constructor operator}$
    359         s.size = asize;                         $\C{// initialize fields}$
     218void ?{}(S & s, int asize) {    //constructor operator
     219        s.size = asize;                         //initialize fields
    360220        s.ia = calloc(size, sizeof(S));
    361221}
    362 void ^?{}(S & s) {                              $\C{// destructor operator}$
    363         free(ia);                                       $\C{// de-initialization fields}$
     222void ^?{}(S & s) {                              //destructor operator
     223        free(ia);                                       //de-initialization fields
    364224}
    365225int main() {
    366         S x = {10}, y = {100};          $\C{// implicit calls: ?\{\}(x, 10), ?\{\}(y, 100)}$
    367         ...                                                     $\C{// use x and y}$
    368         ^x{};  ^y{};                            $\C{// explicit calls to de-initialize}$
    369         x{20};  y{200};                         $\C{// explicit calls to reinitialize}$
    370         ...                                                     $\C{// reuse x and y}$
    371 }                                                               $\C{// implicit calls: \^?\{\}(y), \^?\{\}(x)}$
    372 \end{cfa}
     226        S x = {10}, y = {100};          //implicit calls: ?{}(x, 10), ?{}(y, 100)
     227        ...                                                     //use x and y
     228        ^x{};  ^y{};                            //explicit calls to de-initialize
     229        x{20};  y{200};                         //explicit calls to reinitialize
     230        ...                                                     //reuse x and y
     231}                                                               //implicit calls: ^?{}(y), ^?{}(x)
     232\end{cfacode}
    373233The language guarantees that every object and all their fields are constructed.
    374234Like \CC, construction of an object is automatically done on allocation and destruction of the object is done on deallocation.
    375235Allocation and deallocation can occur on the stack or on the heap.
    376 \begin{cfa}
     236\begin{cfacode}
    377237{
    378         struct S s = {10};      $\C{// allocation, call constructor}$
     238        struct S s = {10};      //allocation, call constructor
    379239        ...
    380 }                                               $\C{// deallocation, call destructor}$
    381 struct S * s = new();   $\C{// allocation, call constructor}$
     240}                                               //deallocation, call destructor
     241struct S * s = new();   //allocation, call constructor
    382242...
    383 delete(s);                              $\C{// deallocation, call destructor}$
    384 \end{cfa}
    385 Note that like \CC, \CFA introduces @new@ and @delete@, which behave like @malloc@ and @free@ in addition to constructing and destructing objects, after calling @malloc@ and before calling @free@, respectively.
     243delete(s);                              //deallocation, call destructor
     244\end{cfacode}
     245Note that like \CC, \CFA introduces \code{new} and \code{delete}, which behave like \code{malloc} and \code{free} in addition to constructing and destructing objects, after calling \code{malloc} and before calling \code{free}, respectively.
    386246
    387247% ======================================================================
     
    389249\label{s:ParametricPolymorphism}
    390250Routines in \CFA can also be reused for multiple types.
    391 This capability is done using the @forall@ clauses, which allow separately compiled routines to support generic usage over multiple types.
     251This capability is done using the \code{forall} clauses, which allow separately compiled routines to support generic usage over multiple types.
    392252For example, the following sum function works for any type that supports construction from 0 and addition:
    393 \begin{cfa}
    394 // constraint type, 0 and +
     253\begin{cfacode}
     254//constraint type, 0 and +
    395255forall(otype T | { void ?{}(T *, zero_t); T ?+?(T, T); })
    396256T sum(T a[ ], size_t size) {
    397         T total = 0;                            $\C{// construct T from 0}$
     257        T total = 0;                            //construct T from 0
    398258        for(size_t i = 0; i < size; i++)
    399                 total = total + a[i];   $\C{// select appropriate +}$
     259                total = total + a[i];   //select appropriate +
    400260        return total;
    401261}
    402262
    403263S sa[5];
    404 int i = sum(sa, 5);                             $\C{// use S's 0 construction and +}$
    405 \end{cfa}
     264int i = sum(sa, 5);                             //use S's 0 construction and +
     265\end{cfacode}
    406266
    407267Since writing constraints on types can become cumbersome for more constrained functions, \CFA also has the concept of traits.
    408268Traits are named collection of constraints that can be used both instead and in addition to regular constraints:
    409 \begin{cfa}
     269\begin{cfacode}
    410270trait summable( otype T ) {
    411         void ?{}(T *, zero_t);          $\C{// constructor from 0 literal}$
    412         T ?+?(T, T);                            $\C{// assortment of additions}$
     271        void ?{}(T *, zero_t);          //constructor from 0 literal
     272        T ?+?(T, T);                            //assortment of additions
    413273        T ?+=?(T *, T);
    414274        T ++?(T *);
    415275        T ?++(T *);
    416276};
    417 forall( otype T | summable(T) ) $\C{// use trait}$
     277forall( otype T | summable(T) ) //use trait
    418278T sum(T a[], size_t size);
    419 \end{cfa}
    420 
    421 Note that the type use for assertions can be either an @otype@ or a @dtype@.
    422 Types declared as @otype@ refer to ``complete'' objects, i.e., objects with a size, a default constructor, a copy constructor, a destructor and an assignment operator.
    423 Using @dtype@, on the other hand, has none of these assumptions but is extremely restrictive, it only guarantees the object is addressable.
     279\end{cfacode}
     280
     281Note that the type use for assertions can be either an \code{otype} or a \code{dtype}.
     282Types declared as \code{otype} refer to ``complete'' objects, i.e., objects with a size, a default constructor, a copy constructor, a destructor and an assignment operator.
     283Using \code{dtype,} on the other hand, has none of these assumptions but is extremely restrictive, it only guarantees the object is addressable.
    424284
    425285% ======================================================================
    426286\subsection{with Clause/Statement}
    427287Since \CFA lacks the concept of a receiver, certain functions end up needing to repeat variable names often.
    428 To remove this inconvenience, \CFA provides the @with@ statement, which opens an aggregate scope making its fields directly accessible (like Pascal).
    429 \begin{cfa}
     288To remove this inconvenience, \CFA provides the \code{with} statement, which opens an aggregate scope making its fields directly accessible (like Pascal).
     289\begin{cfacode}
    430290struct S { int i, j; };
    431 int mem(S & this) with (this)           $\C{// with clause}$
    432         i = 1;                                                  $\C{// this->i}$
    433         j = 2;                                                  $\C{// this->j}$
     291int mem(S & this) with (this)           //with clause
     292        i = 1;                                                  //this->i
     293        j = 2;                                                  //this->j
    434294}
    435295int foo() {
    436296        struct S1 { ... } s1;
    437297        struct S2 { ... } s2;
    438         with (s1)                                               $\C{// with statement}$
     298        with (s1)                                               //with statement
    439299        {
    440                 // access fields of s1 without qualification
    441                 with (s2)                                       $\C{// nesting}$
     300                //access fields of s1 without qualification
     301                with (s2)                                       //nesting
    442302                {
    443                         // access fields of s1 and s2 without qualification
     303                        //access fields of s1 and s2 without qualification
    444304                }
    445305        }
    446         with (s1, s2)                                   $\C{// scopes open in parallel}$
     306        with (s1, s2)                                   //scopes open in parallel
    447307        {
    448                 // access fields of s1 and s2 without qualification
    449         }
    450 }
    451 \end{cfa}
    452 
    453 For more information on \CFA see \cite{cforall-ug,Schluntz17,www-cfa}.
     308                //access fields of s1 and s2 without qualification
     309        }
     310}
     311\end{cfacode}
     312
     313For more information on \CFA see \cite{cforall-ug,rob-thesis,www-cfa}.
    454314
    455315% ======================================================================
     
    479339Optimal performance in concurrent applications is often obtained by having as much non-determinism as correctness allows.
    480340
    481 
    482341\section{\protect\CFA's Thread Building Blocks}
    483 
    484342One of the important features that are missing in C is threading\footnote{While the C11 standard defines a ``threads.h'' header, it is minimal and defined as optional.
    485343As such, library support for threading is far from widespread.
    486 At the time of writing the paper, neither \protect\lstinline|gcc| nor \protect\lstinline|clang| support ``threads.h'' in their standard libraries.}.
     344At the time of writing the paper, neither \texttt{gcc} nor \texttt{clang} support ``threads.h'' in their respective standard libraries.}.
    487345On modern architectures, a lack of threading is unacceptable~\cite{Sutter05, Sutter05b}, and therefore modern programming languages must have the proper tools to allow users to write efficient concurrent programs to take advantage of parallelism.
    488346As an extension of C, \CFA needs to express these concepts in a way that is as natural as possible to programmers familiar with imperative languages.
    489347And being a system-level language means programmers expect to choose precisely which features they need and which cost they are willing to pay.
    490348
    491 
    492349\section{Coroutines: A Stepping Stone}\label{coroutine}
    493 
    494350While the main focus of this proposal is concurrency and parallelism, it is important to address coroutines, which are actually a significant building block of a concurrency system. \textbf{Coroutine}s are generalized routines which have predefined points where execution is suspended and can be resumed at a later time.
    495351Therefore, they need to deal with context switches and other context-management operations.
    496352This proposal includes coroutines both as an intermediate step for the implementation of threads, and a first-class feature of \CFA.
    497353Furthermore, many design challenges of threads are at least partially present in designing coroutines, which makes the design effort that much more relevant.
    498 The core \textbf{api} of coroutines revolves around two features: independent call-stacks and @suspend@/@resume@.
    499 
    500 \begin{figure}
     354The core \textbf{api} of coroutines revolves around two features: independent call-stacks and \code{suspend}/\code{resume}.
     355
     356\begin{table}
    501357\begin{center}
    502 \begin{tabular}{@{}lll@{}}
    503 \multicolumn{1}{c}{\textbf{callback}} & \multicolumn{1}{c}{\textbf{output array}} & \multicolumn{1}{c}{\textbf{external state}} \\
    504 \begin{cfa}
    505 void fib_func(
    506         int n, void (* callback)( int )
     358\begin{tabular}{c @{\hskip 0.025in}|@{\hskip 0.025in} c @{\hskip 0.025in}|@{\hskip 0.025in} c}
     359\begin{ccode}[tabsize=2]
     360//Using callbacks
     361void fibonacci_func(
     362        int n,
     363        void (*callback)(int)
    507364) {
    508         int fn, f1 = 0, f2 = 1;
    509         for ( int i = 0; i < n; i++ ) {
    510                 callback( f1 );
    511                 fn = f1 + f2;
    512                 f1 = f2;  f2 = fn;
    513         }
    514 }
     365        int first = 0;
     366        int second = 1;
     367        int next, i;
     368        for(i = 0; i < n; i++)
     369        {
     370                if(i <= 1)
     371                        next = i;
     372                else {
     373                        next = f1 + f2;
     374                        f1 = f2;
     375                        f2 = next;
     376                }
     377                callback(next);
     378        }
     379}
     380
    515381int main() {
    516         void print_fib( int n ) {
    517                 printf( "%d\n", n );
    518         }
    519         fib_func( 10, print_fib );
    520 }
    521 
    522 \end{cfa}
    523 &
    524 \begin{cfa}
    525 void fib_array(
    526         int n, int * array
     382        void print_fib(int n) {
     383                printf("%d\n", n);
     384        }
     385
     386        fibonacci_func(
     387                10, print_fib
     388        );
     389
     390
     391
     392}
     393\end{ccode}&\begin{ccode}[tabsize=2]
     394//Using output array
     395void fibonacci_array(
     396        int n,
     397        int* array
    527398) {
    528         int fn, f1 = 0, f2 = 1;
    529         for ( int i = 0; i < n; i++ ) {
    530                 array[i] = f1;
    531                 fn = f1 + f2;
    532                 f1 = f2;  f2 = fn;
    533         }
    534 }
     399        int f1 = 0; int f2 = 1;
     400        int next, i;
     401        for(i = 0; i < n; i++)
     402        {
     403                if(i <= 1)
     404                        next = i;
     405                else {
     406                        next = f1 + f2;
     407                        f1 = f2;
     408                        f2 = next;
     409                }
     410                array[i] = next;
     411        }
     412}
     413
     414
    535415int main() {
    536416        int a[10];
    537         fib_array( 10, a );
    538         for ( int i = 0; i < 10; i++ ) {
    539                 printf( "%d\n", a[i] );
    540         }
    541 }
    542 \end{cfa}
    543 &
    544 \begin{cfa}
    545 
    546 typedef struct { int f1, f2; } Fib;
    547 int fib_state(
    548         Fib * fib
     417
     418        fibonacci_func(
     419                10, a
     420        );
     421
     422        for(int i=0;i<10;i++){
     423                printf("%d\n", a[i]);
     424        }
     425
     426}
     427\end{ccode}&\begin{ccode}[tabsize=2]
     428//Using external state
     429typedef struct {
     430        int f1, f2;
     431} Iterator_t;
     432
     433int fibonacci_state(
     434        Iterator_t* it
    549435) {
    550         int ret = fib->f1;
    551         int fn = fib->f1 + fib->f2;
    552         fib->f2 = fib->f1; fib->f1 = fn;
    553         return ret;
    554 }
     436        int f;
     437        f = it->f1 + it->f2;
     438        it->f2 = it->f1;
     439        it->f1 = max(f,1);
     440        return f;
     441}
     442
     443
     444
     445
     446
     447
     448
    555449int main() {
    556         Fib fib = { 0, 1 };
    557 
    558         for ( int i = 0; i < 10; i++ ) {
    559                 printf( "%d\n", fib_state( &fib ) );
    560         }
    561 }
    562 \end{cfa}
     450        Iterator_t it={0,0};
     451
     452        for(int i=0;i<10;i++){
     453                printf("%d\n",
     454                        fibonacci_state(
     455                                &it
     456                        );
     457                );
     458        }
     459
     460}
     461\end{ccode}
    563462\end{tabular}
    564463\end{center}
    565 \caption{Fibonacci Implementations in C}
    566 \label{lst:fib-c}
    567 \end{figure}
     464\caption{Different implementations of a Fibonacci sequence generator in C.}
     465\label{lst:fibonacci-c}
     466\end{table}
    568467
    569468A good example of a problem made easier with coroutines is generators, e.g., generating the Fibonacci sequence.
     
    575474Listing \ref{lst:fibonacci-cfa} is an example of a solution to the Fibonacci problem using \CFA coroutines, where the coroutine stack holds sufficient state for the next generation.
    576475This solution has the advantage of having very strong decoupling between how the sequence is generated and how it is used.
    577 Indeed, this version is as easy to use as the @fibonacci_state@ solution, while the implementation is very similar to the @fibonacci_func@ example.
     476Indeed, this version is as easy to use as the \code{fibonacci_state} solution, while the implementation is very similar to the \code{fibonacci_func} example.
    578477
    579478\begin{figure}
    580 \begin{cfa}
    581 coroutine Fibonacci { int fn; };                                $\C{// used for communication}$
    582 
    583 void ?{}( Fibonacci & fib ) with( fib ) { fn = 0; } $\C{// constructor}$
    584 
    585 void main( Fibonacci & fib ) with( fib ) {              $\C{// main called on first resume}$
    586         int fn1, fn2;                                                           $\C{// retained between resumes}$
    587         fn = 0;  fn1 = fn;                                                      $\C{// 1st case}$
    588         suspend();                                                                      $\C{// restart last resume}$
    589         fn = 1;  fn2 = fn1;  fn1 = fn;                          $\C{// 2nd case}$
    590         suspend();                                                                      $\C{// restart last resume}$
     479\begin{cfacode}[caption={Implementation of Fibonacci using coroutines},label={lst:fibonacci-cfa}]
     480coroutine Fibonacci {
     481        int fn; //used for communication
     482};
     483
     484void ?{}(Fibonacci& this) { //constructor
     485        this.fn = 0;
     486}
     487
     488//main automatically called on first resume
     489void main(Fibonacci& this) with (this) {
     490        int fn1, fn2;           //retained between resumes
     491        fn  = 0;
     492        fn1 = fn;
     493        suspend(this);          //return to last resume
     494
     495        fn  = 1;
     496        fn2 = fn1;
     497        fn1 = fn;
     498        suspend(this);          //return to last resume
     499
    591500        for ( ;; ) {
    592                 fn = fn1 + fn2; fn2 = fn1;  fn1 = fn;   $\C{// general case}$
    593                 suspend();                                                              $\C{// restart last resume}$
    594         }
    595 }
    596 int next( Fibonacci & fib ) with( fib ) {
    597         resume( fib );                                                          $\C{// restart last suspend}$
    598         return fn;
    599 }
    600 int main() {
     501                fn  = fn1 + fn2;
     502                fn2 = fn1;
     503                fn1 = fn;
     504                suspend(this);  //return to last resume
     505        }
     506}
     507
     508int next(Fibonacci& this) {
     509        resume(this); //transfer to last suspend
     510        return this.fn;
     511}
     512
     513void main() { //regular program main
    601514        Fibonacci f1, f2;
    602         for ( int i = 1; i <= 10; i++ ) {
     515        for ( int i = 1; i <= 10; i += 1 ) {
    603516                sout | next( f1 ) | next( f2 ) | endl;
    604517        }
    605518}
    606 \end{cfa}
    607 \caption{Coroutine Fibonacci }
    608 \label{lst:fibonacci-cfa}
     519\end{cfacode}
    609520\end{figure}
    610521
    611 Listing \ref{lst:fmt-line} shows the @Format@ coroutine for restructuring text into groups of character blocks of fixed size.
     522Listing \ref{lst:fmt-line} shows the \code{Format} coroutine for restructuring text into groups of character blocks of fixed size.
    612523The example takes advantage of resuming coroutines in the constructor to simplify the code and highlights the idea that interesting control flow can occur in the constructor.
    613524
    614525\begin{figure}
    615 \begin{cfa}[tabsize=3,caption={Formatting text into lines of 5 blocks of 4 characters.},label={lst:fmt-line}]
    616 // format characters into blocks of 4 and groups of 5 blocks per line
     526\begin{cfacode}[tabsize=3,caption={Formatting text into lines of 5 blocks of 4 characters.},label={lst:fmt-line}]
     527//format characters into blocks of 4 and groups of 5 blocks per line
    617528coroutine Format {
    618         char ch;                                                                        // used for communication
    619         int g, b;                                                               // global because used in destructor
     529        char ch;                                                                        //used for communication
     530        int g, b;                                                               //global because used in destructor
    620531};
    621532
    622533void  ?{}(Format& fmt) {
    623         resume( fmt );                                                  // prime (start) coroutine
     534        resume( fmt );                                                  //prime (start) coroutine
    624535}
    625536
     
    630541
    631542void main(Format& fmt) with fmt {
    632         for ( ;; ) {                                                    // for as many characters
    633                 for(g = 0; g < 5; g++) {                // groups of 5 blocks
    634                         for(b = 0; b < 4; fb++) {       // blocks of 4 characters
     543        for ( ;; ) {                                                    //for as many characters
     544                for(g = 0; g < 5; g++) {                //groups of 5 blocks
     545                        for(b = 0; b < 4; fb++) {       //blocks of 4 characters
    635546                                suspend();
    636                                 sout | ch;                                      // print character
     547                                sout | ch;                                      //print character
    637548                        }
    638                         sout | "  ";                                    // print block separator
     549                        sout | "  ";                                    //print block separator
    639550                }
    640                 sout | endl;                                            // print group separator
     551                sout | endl;                                            //print group separator
    641552        }
    642553}
     
    650561        Format fmt;
    651562        char ch;
    652         Eof: for ( ;; ) {                                               // read until end of file
    653                 sin | ch;                                                       // read one character
    654                 if(eof(sin)) break Eof;                 // eof ?
    655                 prt(fmt, ch);                                           // push character for formatting
    656         }
    657 }
    658 \end{cfa}
     563        Eof: for ( ;; ) {                                               //read until end of file
     564                sin | ch;                                                       //read one character
     565                if(eof(sin)) break Eof;                 //eof ?
     566                prt(fmt, ch);                                           //push character for formatting
     567        }
     568}
     569\end{cfacode}
    659570\end{figure}
    660571
     
    671582For example, the following code, while looking benign, can run into undefined behaviour because of thunks:
    672583
    673 \begin{cfa}
    674 // async: Runs function asynchronously on another thread
     584\begin{cfacode}
     585//async: Runs function asynchronously on another thread
    675586forall(otype T)
    676587extern void async(void (*func)(T*), T* obj);
     
    681592void bar() {
    682593        int a;
    683         async(noop, &a); // start thread running noop with argument a
    684 }
    685 \end{cfa}
     594        async(noop, &a); //start thread running noop with argument a
     595}
     596\end{cfacode}
    686597
    687598The generated C code\footnote{Code trimmed down for brevity} creates a local thunk to hold type information:
    688599
    689 \begin{cfa}
     600\begin{ccode}
    690601extern void async(/* omitted */, void (*func)(void*), void* obj);
    691602
     
    701612        async(/* omitted */, ((void (*)(void*))(&_thunk0)), (&a));
    702613}
    703 \end{cfa}
    704 The problem in this example is a storage management issue, the function pointer @_thunk0@ is only valid until the end of the block, which limits the viable solutions because storing the function pointer for too long causes undefined behaviour; i.e., the stack-based thunk being destroyed before it can be used.
     614\end{ccode}
     615The problem in this example is a storage management issue, the function pointer \code{_thunk0} is only valid until the end of the block, which limits the viable solutions because storing the function pointer for too long causes undefined behaviour; i.e., the stack-based thunk being destroyed before it can be used.
    705616This challenge is an extension of challenges that come with second-class routines.
    706617Indeed, GCC nested routines also have the limitation that nested routine cannot be passed outside of the declaration scope.
     
    710621One solution to this challenge is to use composition/containment, where coroutine fields are added to manage the coroutine.
    711622
    712 \begin{cfa}
     623\begin{cfacode}
    713624struct Fibonacci {
    714         int fn; // used for communication
    715         coroutine c; // composition
     625        int fn; //used for communication
     626        coroutine c; //composition
    716627};
    717628
     
    722633void ?{}(Fibonacci& this) {
    723634        this.fn = 0;
    724         // Call constructor to initialize coroutine
     635        //Call constructor to initialize coroutine
    725636        (this.c){myMain};
    726637}
    727 \end{cfa}
     638\end{cfacode}
    728639The downside of this approach is that users need to correctly construct the coroutine handle before using it.
    729640Like any other objects, the user must carefully choose construction order to prevent usage of objects not yet constructed.
     
    734645The next alternative is to use language support to annotate coroutines as follows:
    735646
    736 \begin{cfa}
     647\begin{cfacode}
    737648coroutine Fibonacci {
    738         int fn; // used for communication
     649        int fn; //used for communication
    739650};
    740 \end{cfa}
    741 The @coroutine@ keyword means the compiler can find and inject code where needed.
     651\end{cfacode}
     652The \code{coroutine} keyword means the compiler can find and inject code where needed.
    742653The downside of this approach is that it makes coroutine a special case in the language.
    743654Users wanting to extend coroutines or build their own for various reasons can only do so in ways offered by the language.
     
    750661For coroutines as for threads, many implementations are based on routine pointers or function objects~\cite{Butenhof97, C++14, MS:VisualC++, BoostCoroutines15}.
    751662For example, Boost implements coroutines in terms of four functor object types:
    752 \begin{cfa}
     663\begin{cfacode}
    753664asymmetric_coroutine<>::pull_type
    754665asymmetric_coroutine<>::push_type
    755666symmetric_coroutine<>::call_type
    756667symmetric_coroutine<>::yield_type
    757 \end{cfa}
    758 Often, the canonical threading paradigm in languages is based on function pointers, @pthread@ being one of the most well-known examples.
     668\end{cfacode}
     669Often, the canonical threading paradigm in languages is based on function pointers, \texttt{pthread} being one of the most well-known examples.
    759670The main problem of this approach is that the thread usage is limited to a generic handle that must otherwise be wrapped in a custom type.
    760671Since the custom type is simple to write in \CFA and solves several issues, added support for routine/lambda based coroutines adds very little.
    761672
    762 A variation of this would be to use a simple function pointer in the same way @pthread@ does for threads:
    763 \begin{cfa}
     673A variation of this would be to use a simple function pointer in the same way \texttt{pthread} does for threads:
     674\begin{cfacode}
    764675void foo( coroutine_t cid, void* arg ) {
    765676        int* value = (int*)arg;
    766         // Coroutine body
     677        //Coroutine body
    767678}
    768679
     
    772683        coroutine_resume( &cid );
    773684}
    774 \end{cfa}
     685\end{cfacode}
    775686This semantics is more common for thread interfaces but coroutines work equally well.
    776687As discussed in section \ref{threads}, this approach is superseded by static approaches in terms of expressivity.
     
    779690
    780691Finally, the underlying approach, which is the one closest to \CFA idioms, is to use trait-based lazy coroutines.
    781 This approach defines a coroutine as anything that satisfies the trait @is_coroutine@ (as defined below) and is used as a coroutine.
    782 
    783 \begin{cfa}
     692This approach defines a coroutine as anything that satisfies the trait \code{is_coroutine} (as defined below) and is used as a coroutine.
     693
     694\begin{cfacode}
    784695trait is_coroutine(dtype T) {
    785696      void main(T& this);
     
    789700forall( dtype T | is_coroutine(T) ) void suspend(T&);
    790701forall( dtype T | is_coroutine(T) ) void resume (T&);
    791 \end{cfa}
    792 This ensures that an object is not a coroutine until @resume@ is called on the object.
    793 Correspondingly, any object that is passed to @resume@ is a coroutine since it must satisfy the @is_coroutine@ trait to compile.
    794 The advantage of this approach is that users can easily create different types of coroutines, for example, changing the memory layout of a coroutine is trivial when implementing the @get_coroutine@ routine.
    795 The \CFA keyword @coroutine@ simply has the effect of implementing the getter and forward declarations required for users to implement the main routine.
     702\end{cfacode}
     703This ensures that an object is not a coroutine until \code{resume} is called on the object.
     704Correspondingly, any object that is passed to \code{resume} is a coroutine since it must satisfy the \code{is_coroutine} trait to compile.
     705The advantage of this approach is that users can easily create different types of coroutines, for example, changing the memory layout of a coroutine is trivial when implementing the \code{get_coroutine} routine.
     706The \CFA keyword \code{coroutine} simply has the effect of implementing the getter and forward declarations required for users to implement the main routine.
    796707
    797708\begin{center}
    798709\begin{tabular}{c c c}
    799 \begin{cfa}[tabsize=3]
     710\begin{cfacode}[tabsize=3]
    800711coroutine MyCoroutine {
    801712        int someValue;
    802713};
    803 \end{cfa} & == & \begin{cfa}[tabsize=3]
     714\end{cfacode} & == & \begin{cfacode}[tabsize=3]
    804715struct MyCoroutine {
    805716        int someValue;
     
    815726
    816727void main(struct MyCoroutine* this);
    817 \end{cfa}
     728\end{cfacode}
    818729\end{tabular}
    819730\end{center}
     
    825736Both user and kernel threads are supported, where user threads are the concurrency mechanism and kernel threads are the parallel mechanism.
    826737User threads offer a flexible and lightweight interface.
    827 A thread can be declared using a struct declaration @thread@ as follows:
    828 
    829 \begin{cfa}
     738A thread can be declared using a struct declaration \code{thread} as follows:
     739
     740\begin{cfacode}
    830741thread foo {};
    831 \end{cfa}
     742\end{cfacode}
    832743
    833744As for coroutines, the keyword is a thin wrapper around a \CFA trait:
    834745
    835 \begin{cfa}
     746\begin{cfacode}
    836747trait is_thread(dtype T) {
    837748      void ^?{}(T & mutex this);
     
    839750      thread_desc* get_thread(T & this);
    840751};
    841 \end{cfa}
     752\end{cfacode}
    842753
    843754Obviously, for this thread implementation to be useful it must run some user code.
    844755Several other threading interfaces use a function-pointer representation as the interface of threads (for example \Csharp~\cite{Csharp} and Scala~\cite{Scala}).
    845 However, this proposal considers that statically tying a @main@ routine to a thread supersedes this approach.
    846 Since the @main@ routine is already a special routine in \CFA (where the program begins), it is a natural extension of the semantics to use overloading to declare mains for different threads (the normal main being the main of the initial thread).
    847 As such the @main@ routine of a thread can be defined as
    848 \begin{cfa}
     756However, this proposal considers that statically tying a \code{main} routine to a thread supersedes this approach.
     757Since the \code{main} routine is already a special routine in \CFA (where the program begins), it is a natural extension of the semantics to use overloading to declare mains for different threads (the normal main being the main of the initial thread).
     758As such the \code{main} routine of a thread can be defined as
     759\begin{cfacode}
    849760thread foo {};
    850761
     
    852763        sout | "Hello World!" | endl;
    853764}
    854 \end{cfa}
    855 
    856 In this example, threads of type @foo@ start execution in the @void main(foo &)@ routine, which prints @"Hello World!".@ While this paper encourages this approach to enforce strongly typed programming, users may prefer to use the routine-based thread semantics for the sake of simplicity.
     765\end{cfacode}
     766
     767In this example, threads of type \code{foo} start execution in the \code{void main(foo &)} routine, which prints \code{"Hello World!".} While this paper encourages this approach to enforce strongly typed programming, users may prefer to use the routine-based thread semantics for the sake of simplicity.
    857768With the static semantics it is trivial to write a thread type that takes a function pointer as a parameter and executes it on its stack asynchronously.
    858 \begin{cfa}
     769\begin{cfacode}
    859770typedef void (*voidFunc)(int);
    860771
     
    870781
    871782void main(FuncRunner & this) {
    872         // thread starts here and runs the function
     783        //thread starts here and runs the function
    873784        this.func( this.arg );
    874785}
     
    882793        return 0?
    883794}
    884 \end{cfa}
     795\end{cfacode}
    885796
    886797A consequence of the strongly typed approach to main is that memory layout of parameters and return values to/from a thread are now explicitly specified in the \textbf{api}.
    887798
    888799Of course, for threads to be useful, it must be possible to start and stop threads and wait for them to complete execution.
    889 While using an \textbf{api} such as @fork@ and @join@ is relatively common in the literature, such an interface is unnecessary.
    890 Indeed, the simplest approach is to use \textbf{raii} principles and have threads @fork@ after the constructor has completed and @join@ before the destructor runs.
    891 \begin{cfa}
     800While using an \textbf{api} such as \code{fork} and \code{join} is relatively common in the literature, such an interface is unnecessary.
     801Indeed, the simplest approach is to use \textbf{raii} principles and have threads \code{fork} after the constructor has completed and \code{join} before the destructor runs.
     802\begin{cfacode}
    892803thread World;
    893804
     
    898809void main() {
    899810        World w;
    900         // Thread forks here
    901 
    902         // Printing "Hello " and "World!" are run concurrently
     811        //Thread forks here
     812
     813        //Printing "Hello " and "World!" are run concurrently
    903814        sout | "Hello " | endl;
    904815
    905         // Implicit join at end of scope
    906 }
    907 \end{cfa}
     816        //Implicit join at end of scope
     817}
     818\end{cfacode}
    908819
    909820This semantic has several advantages over explicit semantics: a thread is always started and stopped exactly once, users cannot make any programming errors, and it naturally scales to multiple threads meaning basic synchronization is very simple.
    910821
    911 \begin{cfa}
     822\begin{cfacode}
    912823thread MyThread {
    913824        //...
    914825};
    915826
    916 // main
     827//main
    917828void main(MyThread& this) {
    918829        //...
     
    921832void foo() {
    922833        MyThread thrds[10];
    923         // Start 10 threads at the beginning of the scope
     834        //Start 10 threads at the beginning of the scope
    924835
    925836        DoStuff();
    926837
    927         // Wait for the 10 threads to finish
    928 }
    929 \end{cfa}
     838        //Wait for the 10 threads to finish
     839}
     840\end{cfacode}
    930841
    931842However, one of the drawbacks of this approach is that threads always form a tree where nodes must always outlive their children, i.e., they are always destroyed in the opposite order of construction because of C scoping rules.
    932843This restriction is relaxed by using dynamic allocation, so threads can outlive the scope in which they are created, much like dynamically allocating memory lets objects outlive the scope in which they are created.
    933844
    934 \begin{cfa}
     845\begin{cfacode}
    935846thread MyThread {
    936847        //...
     
    944855        MyThread* long_lived;
    945856        {
    946                 // Start a thread at the beginning of the scope
     857                //Start a thread at the beginning of the scope
    947858                MyThread short_lived;
    948859
    949                 // create another thread that will outlive the thread in this scope
     860                //create another thread that will outlive the thread in this scope
    950861                long_lived = new MyThread;
    951862
    952863                DoStuff();
    953864
    954                 // Wait for the thread short_lived to finish
     865                //Wait for the thread short_lived to finish
    955866        }
    956867        DoMoreStuff();
    957868
    958         // Now wait for the long_lived to finish
     869        //Now wait for the long_lived to finish
    959870        delete long_lived;
    960871}
    961 \end{cfa}
     872\end{cfacode}
    962873
    963874
     
    977888At the lowest level, concurrent paradigms are implemented as atomic operations and locks.
    978889Many such mechanisms have been proposed, including semaphores~\cite{Dijkstra68b} and path expressions~\cite{Campbell74}.
    979 However, for productivity reasons it is desirable to have a higher-level construct be the core concurrency paradigm~\cite{Hochstein05}.
     890However, for productivity reasons it is desirable to have a higher-level construct be the core concurrency paradigm~\cite{HPP:Study}.
    980891
    981892An approach that is worth mentioning because it is gaining in popularity is transactional memory~\cite{Herlihy93}.
     
    998909Methods range from low-level locks, which are fast and flexible but require significant attention to be correct, to  higher-level concurrency techniques, which sacrifice some performance in order to improve ease of use.
    999910Ease of use comes by either guaranteeing some problems cannot occur (e.g., being deadlock free) or by offering a more explicit coupling between data and corresponding critical section.
    1000 For example, the \CC @std::atomic<T>@ offers an easy way to express mutual-exclusion on a restricted set of operations (e.g., reading/writing large types atomically).
     911For example, the \CC \code{std::atomic<T>} offers an easy way to express mutual-exclusion on a restricted set of operations (e.g., reading/writing large types atomically).
    1001912Another challenge with low-level locks is composability.
    1002913Locks have restricted composability because it takes careful organizing for multiple locks to be used while preventing deadlocks.
     
    1027938This concept is generally associated with object-oriented languages like Java~\cite{Java} or \uC~\cite{uC++book} but does not strictly require OO semantics.
    1028939The only requirement is the ability to declare a handle to a shared object and a set of routines that act on it:
    1029 \begin{cfa}
     940\begin{cfacode}
    1030941typedef /*some monitor type*/ monitor;
    1031942int f(monitor & m);
    1032943
    1033944int main() {
    1034         monitor m;  // Handle m
    1035         f(m);       // Routine using handle
    1036 }
    1037 \end{cfa}
     945        monitor m;  //Handle m
     946        f(m);       //Routine using handle
     947}
     948\end{cfacode}
    1038949
    1039950% ======================================================================
     
    1045956First, it is necessary to use pass-by-reference over pass-by-value for monitor routines.
    1046957This semantics is important, because at their core, monitors are implicit mutual-exclusion objects (locks), and these objects cannot be copied.
    1047 Therefore, monitors are non-copy-able objects (@dtype@).
     958Therefore, monitors are non-copy-able objects (\code{dtype}).
    1048959
    1049960Another aspect to consider is when a monitor acquires its mutual exclusion.
    1050961For example, a monitor may need to be passed through multiple helper routines that do not acquire the monitor mutual-exclusion on entry.
    1051 Passthrough can occur for generic helper routines (@swap@, @sort@, etc.) or specific helper routines like the following to implement an atomic counter:
    1052 
    1053 \begin{cfa}
     962Passthrough can occur for generic helper routines (\code{swap}, \code{sort}, etc.) or specific helper routines like the following to implement an atomic counter:
     963
     964\begin{cfacode}
    1054965monitor counter_t { /*...see section $\ref{data}$...*/ };
    1055966
    1056 void ?{}(counter_t & nomutex this); // constructor
    1057 size_t ++?(counter_t & mutex this); // increment
    1058 
    1059 // need for mutex is platform dependent
    1060 void ?{}(size_t * this, counter_t & mutex cnt); // conversion
    1061 \end{cfa}
     967void ?{}(counter_t & nomutex this); //constructor
     968size_t ++?(counter_t & mutex this); //increment
     969
     970//need for mutex is platform dependent
     971void ?{}(size_t * this, counter_t & mutex cnt); //conversion
     972\end{cfacode}
    1062973This counter is used as follows:
    1063974\begin{center}
    1064975\begin{tabular}{c @{\hskip 0.35in} c @{\hskip 0.35in} c}
    1065 \begin{cfa}
    1066 // shared counter
     976\begin{cfacode}
     977//shared counter
    1067978counter_t cnt1, cnt2;
    1068979
    1069 // multiple threads access counter
     980//multiple threads access counter
    1070981thread 1 : cnt1++; cnt2++;
    1071982thread 2 : cnt1++; cnt2++;
     
    1073984        ...
    1074985thread N : cnt1++; cnt2++;
    1075 \end{cfa}
     986\end{cfacode}
    1076987\end{tabular}
    1077988\end{center}
    1078 Notice how the counter is used without any explicit synchronization and yet supports thread-safe semantics for both reading and writing, which is similar in usage to the \CC template @std::atomic@.
    1079 
    1080 Here, the constructor (@?{}@) uses the @nomutex@ keyword to signify that it does not acquire the monitor mutual-exclusion when constructing.
    1081 This semantics is because an object not yet constructed should never be shared and therefore does not require mutual exclusion.
     989Notice how the counter is used without any explicit synchronization and yet supports thread-safe semantics for both reading and writing, which is similar in usage to the \CC template \code{std::atomic}.
     990
     991Here, the constructor (\code{?\{\}}) uses the \code{nomutex} keyword to signify that it does not acquire the monitor mutual-exclusion when constructing.
     992This semantics is because an object not yet con\-structed should never be shared and therefore does not require mutual exclusion.
    1082993Furthermore, it allows the implementation greater freedom when it initializes the monitor locking.
    1083 The prefix increment operator uses @mutex@ to protect the incrementing process from race conditions.
    1084 Finally, there is a conversion operator from @counter_t@ to @size_t@.
    1085 This conversion may or may not require the @mutex@ keyword depending on whether or not reading a @size_t@ is an atomic operation.
     994The prefix increment operator uses \code{mutex} to protect the incrementing process from race conditions.
     995Finally, there is a conversion operator from \code{counter_t} to \code{size_t}.
     996This conversion may or may not require the \code{mutex} keyword depending on whether or not reading a \code{size_t} is an atomic operation.
    1086997
    1087998For maximum usability, monitors use \textbf{multi-acq} semantics, which means a single thread can acquire the same monitor multiple times without deadlock.
    1088999For example, listing \ref{fig:search} uses recursion and \textbf{multi-acq} to print values inside a binary tree.
    10891000\begin{figure}
    1090 \begin{cfa}[caption={Recursive printing algorithm using \textbf{multi-acq}.},label={fig:search}]
     1001\begin{cfacode}[caption={Recursive printing algorithm using \textbf{multi-acq}.},label={fig:search}]
    10911002monitor printer { ... };
    10921003struct tree {
     
    11011012        print(p, t->right);
    11021013}
    1103 \end{cfa}
     1014\end{cfacode}
    11041015\end{figure}
    11051016
    1106 Having both @mutex@ and @nomutex@ keywords can be redundant, depending on the meaning of a routine having neither of these keywords.
    1107 For example, it is reasonable that it should default to the safest option (@mutex@) when given a routine without qualifiers @void foo(counter_t & this)@, whereas assuming @nomutex@ is unsafe and may cause subtle errors.
    1108 On the other hand, @nomutex@ is the ``normal'' parameter behaviour, it effectively states explicitly that ``this routine is not special''.
     1017Having both \code{mutex} and \code{nomutex} keywords can be redundant, depending on the meaning of a routine having neither of these keywords.
     1018For example, it is reasonable that it should default to the safest option (\code{mutex}) when given a routine without qualifiers \code{void foo(counter_t & this)}, whereas assuming \code{nomutex} is unsafe and may cause subtle errors.
     1019On the other hand, \code{nomutex} is the ``normal'' parameter behaviour, it effectively states explicitly that ``this routine is not special''.
    11091020Another alternative is making exactly one of these keywords mandatory, which provides the same semantics but without the ambiguity of supporting routines with neither keyword.
    11101021Mandatory keywords would also have the added benefit of being self-documented but at the cost of extra typing.
     
    11121023Mandatory keywords in \CFA would imply that the compiler must know without doubt whether or not a parameter is a monitor or not.
    11131024Since \CFA relies heavily on traits as an abstraction mechanism, the distinction between a type that is a monitor and a type that looks like a monitor can become blurred.
    1114 For this reason, \CFA only has the @mutex@ keyword and uses no keyword to mean @nomutex@.
    1115 
    1116 The next semantic decision is to establish when @mutex@ may be used as a type qualifier.
     1025For this reason, \CFA only has the \code{mutex} keyword and uses no keyword to mean \code{nomutex}.
     1026
     1027The next semantic decision is to establish when \code{mutex} may be used as a type qualifier.
    11171028Consider the following declarations:
    1118 \begin{cfa}
     1029\begin{cfacode}
    11191030int f1(monitor & mutex m);
    11201031int f2(const monitor & mutex m);
     
    11221033int f4(monitor * mutex m []);
    11231034int f5(graph(monitor *) & mutex m);
    1124 \end{cfa}
     1035\end{cfacode}
    11251036The problem is to identify which object(s) should be acquired.
    11261037Furthermore, each object needs to be acquired only once.
    1127 In the case of simple routines like @f1@ and @f2@ it is easy to identify an exhaustive list of objects to acquire on entry.
    1128 Adding indirections (@f3@) still allows the compiler and programmer to identify which object is acquired.
    1129 However, adding in arrays (@f4@) makes it much harder.
     1038In the case of simple routines like \code{f1} and \code{f2} it is easy to identify an exhaustive list of objects to acquire on entry.
     1039Adding indirections (\code{f3}) still allows the compiler and programmer to identify which object is acquired.
     1040However, adding in arrays (\code{f4}) makes it much harder.
    11301041Array lengths are not necessarily known in C, and even then, making sure objects are only acquired once becomes none-trivial.
    1131 This problem can be extended to absurd limits like @f5@, which uses a graph of monitors.
     1042This problem can be extended to absurd limits like \code{f5}, which uses a graph of monitors.
    11321043To make the issue tractable, this project imposes the requirement that a routine may only acquire one monitor per parameter and it must be the type of the parameter with at most one level of indirection (ignoring potential qualifiers).
    1133 Also note that while routine @f3@ can be supported, meaning that monitor @**m@ is acquired, passing an array to this routine would be type-safe and yet result in undefined behaviour because only the first element of the array is acquired.
     1044Also note that while routine \code{f3} can be supported, meaning that monitor \code{**m} is acquired, passing an array to this routine would be type-safe and yet result in undefined behaviour because only the first element of the array is acquired.
    11341045However, this ambiguity is part of the C type-system with respects to arrays.
    1135 For this reason, @mutex@ is disallowed in the context where arrays may be passed:
    1136 \begin{cfa}
    1137 int f1(monitor & mutex m);    // Okay : recommended case
    1138 int f2(monitor * mutex m);    // Not Okay : Could be an array
    1139 int f3(monitor mutex m []);  // Not Okay : Array of unknown length
    1140 int f4(monitor ** mutex m);   // Not Okay : Could be an array
    1141 int f5(monitor * mutex m []); // Not Okay : Array of unknown length
    1142 \end{cfa}
     1046For this reason, \code{mutex} is disallowed in the context where arrays may be passed:
     1047\begin{cfacode}
     1048int f1(monitor & mutex m);    //Okay : recommended case
     1049int f2(monitor * mutex m);    //Not Okay : Could be an array
     1050int f3(monitor mutex m []);  //Not Okay : Array of unknown length
     1051int f4(monitor ** mutex m);   //Not Okay : Could be an array
     1052int f5(monitor * mutex m []); //Not Okay : Array of unknown length
     1053\end{cfacode}
    11431054Note that not all array functions are actually distinct in the type system.
    11441055However, even if the code generation could tell the difference, the extra information is still not sufficient to extend meaningfully the monitor call semantic.
     
    11461057Unlike object-oriented monitors, where calling a mutex member \emph{implicitly} acquires mutual-exclusion of the receiver object, \CFA uses an explicit mechanism to specify the object that acquires mutual-exclusion.
    11471058A consequence of this approach is that it extends naturally to multi-monitor calls.
    1148 \begin{cfa}
     1059\begin{cfacode}
    11491060int f(MonitorA & mutex a, MonitorB & mutex b);
    11501061
     
    11521063MonitorB b;
    11531064f(a,b);
    1154 \end{cfa}
     1065\end{cfacode}
    11551066While OO monitors could be extended with a mutex qualifier for multiple-monitor calls, no example of this feature could be found.
    11561067The capability to acquire multiple locks before entering a critical section is called \emph{\textbf{bulk-acq}}.
     
    11601071This consistent ordering means acquiring multiple monitors is safe from deadlock when using \textbf{bulk-acq}.
    11611072However, users can still force the acquiring order.
    1162 For example, notice which routines use @mutex@/@nomutex@ and how this affects acquiring order:
    1163 \begin{cfa}
    1164 void foo(A& mutex a, B& mutex b) { // acquire a & b
     1073For example, notice which routines use \code{mutex}/\code{nomutex} and how this affects acquiring order:
     1074\begin{cfacode}
     1075void foo(A& mutex a, B& mutex b) { //acquire a & b
    11651076        ...
    11661077}
    11671078
    1168 void bar(A& mutex a, B& /*nomutex*/ b) { // acquire a
    1169         ... foo(a, b); ... // acquire b
    1170 }
    1171 
    1172 void baz(A& /*nomutex*/ a, B& mutex b) { // acquire b
    1173         ... foo(a, b); ... // acquire a
    1174 }
    1175 \end{cfa}
    1176 The \textbf{multi-acq} monitor lock allows a monitor lock to be acquired by both @bar@ or @baz@ and acquired again in @foo@.
    1177 In the calls to @bar@ and @baz@ the monitors are acquired in opposite order.
     1079void bar(A& mutex a, B& /*nomutex*/ b) { //acquire a
     1080        ... foo(a, b); ... //acquire b
     1081}
     1082
     1083void baz(A& /*nomutex*/ a, B& mutex b) { //acquire b
     1084        ... foo(a, b); ... //acquire a
     1085}
     1086\end{cfacode}
     1087The \textbf{multi-acq} monitor lock allows a monitor lock to be acquired by both \code{bar} or \code{baz} and acquired again in \code{foo}.
     1088In the calls to \code{bar} and \code{baz} the monitors are acquired in opposite order.
    11781089
    11791090However, such use leads to lock acquiring order problems.
    1180 In the example above, the user uses implicit ordering in the case of function @foo@ but explicit ordering in the case of @bar@ and @baz@.
     1091In the example above, the user uses implicit ordering in the case of function \code{foo} but explicit ordering in the case of \code{bar} and \code{baz}.
    11811092This subtle difference means that calling these routines concurrently may lead to deadlock and is therefore undefined behaviour.
    11821093As shown~\cite{Lister77}, solving this problem requires:
     
    11901101
    11911102For example, \textbf{multi-acq} and \textbf{bulk-acq} can be used together in interesting ways:
    1192 \begin{cfa}
     1103\begin{cfacode}
    11931104monitor bank { ... };
    11941105
     
    11991110        deposit( yourbank, me2you );
    12001111}
    1201 \end{cfa}
     1112\end{cfacode}
    12021113This example shows a trivial solution to the bank-account transfer problem~\cite{BankTransfer}.
    12031114Without \textbf{multi-acq} and \textbf{bulk-acq}, the solution to this problem is much more involved and requires careful engineering.
    12041115
    1205 
    1206 \subsection{\protect\lstinline|mutex| statement} \label{mutex-stmt}
    1207 
    1208 The call semantics discussed above have one software engineering issue: only a routine can acquire the mutual-exclusion of a set of monitor. \CFA offers the @mutex@ statement to work around the need for unnecessary names, avoiding a major software engineering problem~\cite{2FTwoHardThings}.
    1209 Table \ref{lst:mutex-stmt} shows an example of the @mutex@ statement, which introduces a new scope in which the mutual-exclusion of a set of monitor is acquired.
    1210 Beyond naming, the @mutex@ statement has no semantic difference from a routine call with @mutex@ parameters.
     1116\subsection{\code{mutex} statement} \label{mutex-stmt}
     1117
     1118The call semantics discussed above have one software engineering issue: only a routine can acquire the mutual-exclusion of a set of monitor. \CFA offers the \code{mutex} statement to work around the need for unnecessary names, avoiding a major software engineering problem~\cite{2FTwoHardThings}.
     1119Table \ref{lst:mutex-stmt} shows an example of the \code{mutex} statement, which introduces a new scope in which the mutual-exclusion of a set of monitor is acquired.
     1120Beyond naming, the \code{mutex} statement has no semantic difference from a routine call with \code{mutex} parameters.
    12111121
    12121122\begin{table}
    12131123\begin{center}
    12141124\begin{tabular}{|c|c|}
    1215 function call & @mutex@ statement \\
     1125function call & \code{mutex} statement \\
    12161126\hline
    1217 \begin{cfa}[tabsize=3]
     1127\begin{cfacode}[tabsize=3]
    12181128monitor M {};
    12191129void foo( M & mutex m1, M & mutex m2 ) {
    1220         // critical section
     1130        //critical section
    12211131}
    12221132
     
    12241134        foo( m1, m2 );
    12251135}
    1226 \end{cfa}&\begin{cfa}[tabsize=3]
     1136\end{cfacode}&\begin{cfacode}[tabsize=3]
    12271137monitor M {};
    12281138void bar( M & m1, M & m2 ) {
    12291139        mutex(m1, m2) {
    1230                 // critical section
    1231         }
    1232 }
    1233 
    1234 
    1235 \end{cfa}
     1140                //critical section
     1141        }
     1142}
     1143
     1144
     1145\end{cfacode}
    12361146\end{tabular}
    12371147\end{center}
    1238 \caption{Regular call semantics vs. \protect\lstinline|mutex| statement}
     1148\caption{Regular call semantics vs. \code{mutex} statement}
    12391149\label{lst:mutex-stmt}
    12401150\end{table}
     
    12491159This data should be intrinsic to the monitor declaration to prevent any accidental use of data without its appropriate protection.
    12501160For example, here is a complete version of the counter shown in section \ref{call}:
    1251 \begin{cfa}
     1161\begin{cfacode}
    12521162monitor counter_t {
    12531163        int value;
     
    12621172}
    12631173
    1264 // need for mutex is platform dependent here
     1174//need for mutex is platform dependent here
    12651175void ?{}(int * this, counter_t & mutex cnt) {
    12661176        *this = (int)cnt;
    12671177}
    1268 \end{cfa}
    1269 
    1270 Like threads and coroutines, monitors are defined in terms of traits with some additional language support in the form of the @monitor@ keyword.
     1178\end{cfacode}
     1179
     1180Like threads and coroutines, monitors are defined in terms of traits with some additional language support in the form of the \code{monitor} keyword.
    12711181The monitor trait is:
    1272 \begin{cfa}
     1182\begin{cfacode}
    12731183trait is_monitor(dtype T) {
    12741184        monitor_desc * get_monitor( T & );
    12751185        void ^?{}( T & mutex );
    12761186};
    1277 \end{cfa}
    1278 Note that the destructor of a monitor must be a @mutex@ routine to prevent deallocation while a thread is accessing the monitor.
    1279 As with any object, calls to a monitor, using @mutex@ or otherwise, is undefined behaviour after the destructor has run.
     1187\end{cfacode}
     1188Note that the destructor of a monitor must be a \code{mutex} routine to prevent deallocation while a thread is accessing the monitor.
     1189As with any object, calls to a monitor, using \code{mutex} or otherwise, is undefined behaviour after the destructor has run.
    12801190
    12811191% ======================================================================
     
    12921202First, here is a simple example of internal scheduling:
    12931203
    1294 \begin{cfa}
     1204\begin{cfacode}
    12951205monitor A {
    12961206        condition e;
     
    12991209void foo(A& mutex a1, A& mutex a2) {
    13001210        ...
    1301         // Wait for cooperation from bar()
     1211        //Wait for cooperation from bar()
    13021212        wait(a1.e);
    13031213        ...
     
    13051215
    13061216void bar(A& mutex a1, A& mutex a2) {
    1307         // Provide cooperation for foo()
     1217        //Provide cooperation for foo()
    13081218        ...
    1309         // Unblock foo
     1219        //Unblock foo
    13101220        signal(a1.e);
    13111221}
    1312 \end{cfa}
     1222\end{cfacode}
    13131223There are two details to note here.
    1314 First, @signal@ is a delayed operation; it only unblocks the waiting thread when it reaches the end of the critical section.
     1224First, \code{signal} is a delayed operation; it only unblocks the waiting thread when it reaches the end of the critical section.
    13151225This semantics is needed to respect mutual-exclusion, i.e., the signaller and signalled thread cannot be in the monitor simultaneously.
    1316 The alternative is to return immediately after the call to @signal@, which is significantly more restrictive.
    1317 Second, in \CFA, while it is common to store a @condition@ as a field of the monitor, a @condition@ variable can be stored/created independently of a monitor.
    1318 Here routine @foo@ waits for the @signal@ from @bar@ before making further progress, ensuring a basic ordering.
    1319 
    1320 An important aspect of the implementation is that \CFA does not allow barging, which means that once function @bar@ releases the monitor, @foo@ is guaranteed to be the next thread to acquire the monitor (unless some other thread waited on the same condition).
     1226The alternative is to return immediately after the call to \code{signal}, which is significantly more restrictive.
     1227Second, in \CFA, while it is common to store a \code{condition} as a field of the monitor, a \code{condition} variable can be stored/created independently of a monitor.
     1228Here routine \code{foo} waits for the \code{signal} from \code{bar} before making further progress, ensuring a basic ordering.
     1229
     1230An important aspect of the implementation is that \CFA does not allow barging, which means that once function \code{bar} releases the monitor, \code{foo} is guaranteed to be the next thread to acquire the monitor (unless some other thread waited on the same condition).
    13211231This guarantee offers the benefit of not having to loop around waits to recheck that a condition is met.
    13221232The main reason \CFA offers this guarantee is that users can easily introduce barging if it becomes a necessity but adding barging prevention or barging avoidance is more involved without language support.
     
    13301240It is easy to understand the problem of multi-monitor scheduling using a series of pseudo-code examples.
    13311241Note that for simplicity in the following snippets of pseudo-code, waiting and signalling is done using an implicit condition variable, like Java built-in monitors.
    1332 Indeed, @wait@ statements always use the implicit condition variable as parameters and explicitly name the monitors (A and B) associated with the condition.
     1242Indeed, \code{wait} statements always use the implicit condition variable as parameters and explicitly name the monitors (A and B) associated with the condition.
    13331243Note that in \CFA, condition variables are tied to a \emph{group} of monitors on first use (called branding), which means that using internal scheduling with distinct sets of monitors requires one condition variable per set of monitors.
    13341244The example below shows the simple case of having two threads (one for each column) and a single monitor A.
     
    13361246\begin{multicols}{2}
    13371247thread 1
    1338 \begin{cfa}
     1248\begin{pseudo}
    13391249acquire A
    13401250        wait A
    13411251release A
    1342 \end{cfa}
     1252\end{pseudo}
    13431253
    13441254\columnbreak
    13451255
    13461256thread 2
    1347 \begin{cfa}
     1257\begin{pseudo}
    13481258acquire A
    13491259        signal A
    13501260release A
    1351 \end{cfa}
     1261\end{pseudo}
    13521262\end{multicols}
    13531263One thread acquires before waiting (atomically blocking and releasing A) and the other acquires before signalling.
    1354 It is important to note here that both @wait@ and @signal@ must be called with the proper monitor(s) already acquired.
     1264It is important to note here that both \code{wait} and \code{signal} must be called with the proper monitor(s) already acquired.
    13551265This semantic is a logical requirement for barging prevention.
    13561266
    13571267A direct extension of the previous example is a \textbf{bulk-acq} version:
    13581268\begin{multicols}{2}
    1359 \begin{cfa}
     1269\begin{pseudo}
    13601270acquire A & B
    13611271        wait A & B
    13621272release A & B
    1363 \end{cfa}
     1273\end{pseudo}
    13641274\columnbreak
    1365 \begin{cfa}
     1275\begin{pseudo}
    13661276acquire A & B
    13671277        signal A & B
    13681278release A & B
    1369 \end{cfa}
     1279\end{pseudo}
    13701280\end{multicols}
    13711281\noindent This version uses \textbf{bulk-acq} (denoted using the {\sf\&} symbol), but the presence of multiple monitors does not add a particularly new meaning.
     
    13751285
    13761286While deadlock issues can occur when nesting monitors, these issues are only a symptom of the fact that locks, and by extension monitors, are not perfectly composable.
    1377 For monitors, a well-known deadlock problem is the Nested Monitor Problem~\cite{Lister77}, which occurs when a @wait@ is made by a thread that holds more than one monitor.
    1378 For example, the following cfa-code runs into the nested-monitor problem:
     1287For monitors, a well-known deadlock problem is the Nested Monitor Problem~\cite{Lister77}, which occurs when a \code{wait} is made by a thread that holds more than one monitor.
     1288For example, the following pseudo-code runs into the nested-monitor problem:
    13791289\begin{multicols}{2}
    1380 \begin{cfa}
     1290\begin{pseudo}
    13811291acquire A
    13821292        acquire B
     
    13841294        release B
    13851295release A
    1386 \end{cfa}
     1296\end{pseudo}
    13871297
    13881298\columnbreak
    13891299
    1390 \begin{cfa}
     1300\begin{pseudo}
    13911301acquire A
    13921302        acquire B
     
    13941304        release B
    13951305release A
    1396 \end{cfa}
     1306\end{pseudo}
    13971307\end{multicols}
    1398 \noindent The @wait@ only releases monitor @B@ so the signalling thread cannot acquire monitor @A@ to get to the @signal@.
    1399 Attempting release of all acquired monitors at the @wait@ introduces a different set of problems, such as releasing monitor @C@, which has nothing to do with the @signal@.
     1308\noindent The \code{wait} only releases monitor \code{B} so the signalling thread cannot acquire monitor \code{A} to get to the \code{signal}.
     1309Attempting release of all acquired monitors at the \code{wait} introduces a different set of problems, such as releasing monitor \code{C}, which has nothing to do with the \code{signal}.
    14001310
    14011311However, for monitors as for locks, it is possible to write a program using nesting without encountering any problems if nesting is done correctly.
    1402 For example, the next cfa-code snippet acquires monitors {\sf A} then {\sf B} before waiting, while only acquiring {\sf B} when signalling, effectively avoiding the Nested Monitor Problem~\cite{Lister77}.
     1312For example, the next pseudo-code snippet acquires monitors {\sf A} then {\sf B} before waiting, while only acquiring {\sf B} when signalling, effectively avoiding the Nested Monitor Problem~\cite{Lister77}.
    14031313
    14041314\begin{multicols}{2}
    1405 \begin{cfa}
     1315\begin{pseudo}
    14061316acquire A
    14071317        acquire B
     
    14091319        release B
    14101320release A
    1411 \end{cfa}
     1321\end{pseudo}
    14121322
    14131323\columnbreak
    14141324
    1415 \begin{cfa}
     1325\begin{pseudo}
    14161326
    14171327acquire B
     
    14191329release B
    14201330
    1421 \end{cfa}
     1331\end{pseudo}
    14221332\end{multicols}
    14231333
     
    14311341
    14321342A larger example is presented to show complex issues for \textbf{bulk-acq} and its implementation options are analyzed.
    1433 Listing \ref{lst:int-bulk-cfa} shows an example where \textbf{bulk-acq} adds a significant layer of complexity to the internal signalling semantics, and listing \ref{lst:int-bulk-cfa} shows the corresponding \CFA code to implement the cfa-code in listing \ref{lst:int-bulk-cfa}.
    1434 For the purpose of translating the given cfa-code into \CFA-code, any method of introducing a monitor is acceptable, e.g., @mutex@ parameters, global variables, pointer parameters, or using locals with the @mutex@ statement.
    1435 
    1436 \begin{figure}
     1343Listing \ref{lst:int-bulk-pseudo} shows an example where \textbf{bulk-acq} adds a significant layer of complexity to the internal signalling semantics, and listing \ref{lst:int-bulk-cfa} shows the corresponding \CFA code to implement the pseudo-code in listing \ref{lst:int-bulk-pseudo}.
     1344For the purpose of translating the given pseudo-code into \CFA-code, any method of introducing a monitor is acceptable, e.g., \code{mutex} parameters, global variables, pointer parameters, or using locals with the \code{mutex} statement.
     1345
     1346\begin{figure}[!t]
    14371347\begin{multicols}{2}
    14381348Waiting thread
    1439 \begin{cfa}[numbers=left]
     1349\begin{pseudo}[numbers=left]
    14401350acquire A
    1441         // Code Section 1
     1351        //Code Section 1
    14421352        acquire A & B
    1443                 // Code Section 2
     1353                //Code Section 2
    14441354                wait A & B
    1445                 // Code Section 3
     1355                //Code Section 3
    14461356        release A & B
    1447         // Code Section 4
     1357        //Code Section 4
    14481358release A
    1449 \end{cfa}
     1359\end{pseudo}
    14501360\columnbreak
    14511361Signalling thread
    1452 \begin{cfa}[numbers=left, firstnumber=10,escapechar=|]
     1362\begin{pseudo}[numbers=left, firstnumber=10,escapechar=|]
    14531363acquire A
    1454         // Code Section 5
     1364        //Code Section 5
    14551365        acquire A & B
    1456                 // Code Section 6
     1366                //Code Section 6
    14571367                |\label{line:signal1}|signal A & B
    1458                 // Code Section 7
     1368                //Code Section 7
    14591369        |\label{line:releaseFirst}|release A & B
    1460         // Code Section 8
     1370        //Code Section 8
    14611371|\label{line:lastRelease}|release A
    1462 \end{cfa}
     1372\end{pseudo}
    14631373\end{multicols}
    1464 \begin{cfa}[caption={Internal scheduling with \textbf{bulk-acq}},label={lst:int-bulk-cfa}]
    1465 \end{cfa}
     1374\begin{cfacode}[caption={Internal scheduling with \textbf{bulk-acq}},label={lst:int-bulk-pseudo}]
     1375\end{cfacode}
    14661376\begin{center}
    1467 \begin{cfa}[xleftmargin=.4\textwidth]
     1377\begin{cfacode}[xleftmargin=.4\textwidth]
    14681378monitor A a;
    14691379monitor B b;
    14701380condition c;
    1471 \end{cfa}
     1381\end{cfacode}
    14721382\end{center}
    14731383\begin{multicols}{2}
    14741384Waiting thread
    1475 \begin{cfa}
     1385\begin{cfacode}
    14761386mutex(a) {
    1477         // Code Section 1
     1387        //Code Section 1
    14781388        mutex(a, b) {
    1479                 // Code Section 2
     1389                //Code Section 2
    14801390                wait(c);
    1481                 // Code Section 3
    1482         }
    1483         // Code Section 4
    1484 }
    1485 \end{cfa}
     1391                //Code Section 3
     1392        }
     1393        //Code Section 4
     1394}
     1395\end{cfacode}
    14861396\columnbreak
    14871397Signalling thread
    1488 \begin{cfa}
     1398\begin{cfacode}
    14891399mutex(a) {
    1490         // Code Section 5
     1400        //Code Section 5
    14911401        mutex(a, b) {
    1492                 // Code Section 6
     1402                //Code Section 6
    14931403                signal(c);
    1494                 // Code Section 7
    1495         }
    1496         // Code Section 8
    1497 }
    1498 \end{cfa}
     1404                //Code Section 7
     1405        }
     1406        //Code Section 8
     1407}
     1408\end{cfacode}
    14991409\end{multicols}
    1500 \begin{cfa}[caption={Equivalent \CFA code for listing \ref{lst:int-bulk-cfa}},label={lst:int-bulk-cfa}]
    1501 \end{cfa}
     1410\begin{cfacode}[caption={Equivalent \CFA code for listing \ref{lst:int-bulk-pseudo}},label={lst:int-bulk-cfa}]
     1411\end{cfacode}
    15021412\begin{multicols}{2}
    15031413Waiter
    1504 \begin{cfa}[numbers=left]
     1414\begin{pseudo}[numbers=left]
    15051415acquire A
    15061416        acquire A & B
     
    15081418        release A & B
    15091419release A
    1510 \end{cfa}
     1420\end{pseudo}
    15111421
    15121422\columnbreak
    15131423
    15141424Signaller
    1515 \begin{cfa}[numbers=left, firstnumber=6,escapechar=|]
     1425\begin{pseudo}[numbers=left, firstnumber=6,escapechar=|]
    15161426acquire A
    15171427        acquire A & B
    15181428                signal A & B
    15191429        release A & B
    1520         |\label{line:secret}|// Secretly keep B here
     1430        |\label{line:secret}|//Secretly keep B here
    15211431release A
    1522 // Wakeup waiter and transfer A & B
    1523 \end{cfa}
     1432//Wakeup waiter and transfer A & B
     1433\end{pseudo}
    15241434\end{multicols}
    1525 \begin{cfa}[caption={Listing \ref{lst:int-bulk-cfa}, with delayed signalling comments},label={lst:int-secret}]
    1526 \end{cfa}
     1435\begin{cfacode}[caption={Listing \ref{lst:int-bulk-pseudo}, with delayed signalling comments},label={lst:int-secret}]
     1436\end{cfacode}
    15271437\end{figure}
    15281438
    1529 The complexity begins at code sections 4 and 8 in listing \ref{lst:int-bulk-cfa}, which are where the existing semantics of internal scheduling needs to be extended for multiple monitors.
    1530 The root of the problem is that \textbf{bulk-acq} is used in a context where one of the monitors is already acquired, which is why it is important to define the behaviour of the previous cfa-code.
    1531 When the signaller thread reaches the location where it should ``release @A & B@'' (listing \ref{lst:int-bulk-cfa} line \ref{line:releaseFirst}), it must actually transfer ownership of monitor @B@ to the waiting thread.
    1532 This ownership transfer is required in order to prevent barging into @B@ by another thread, since both the signalling and signalled threads still need monitor @A@.
     1439The complexity begins at code sections 4 and 8 in listing \ref{lst:int-bulk-pseudo}, which are where the existing semantics of internal scheduling needs to be extended for multiple monitors.
     1440The root of the problem is that \textbf{bulk-acq} is used in a context where one of the monitors is already acquired, which is why it is important to define the behaviour of the previous pseudo-code.
     1441When the signaller thread reaches the location where it should ``release \code{A & B}'' (listing \ref{lst:int-bulk-pseudo} line \ref{line:releaseFirst}), it must actually transfer ownership of monitor \code{B} to the waiting thread.
     1442This ownership transfer is required in order to prevent barging into \code{B} by another thread, since both the signalling and signalled threads still need monitor \code{A}.
    15331443There are three options:
    15341444
     
    15421452
    15431453However, listing \ref{lst:int-secret} shows this solution can become much more complicated depending on what is executed while secretly holding B at line \ref{line:secret}, while avoiding the need to transfer ownership of a subset of the condition monitors.
    1544 Listing \ref{lst:dependency} shows a slightly different example where a third thread is waiting on monitor @A@, using a different condition variable.
    1545 Because the third thread is signalled when secretly holding @B@, the goal  becomes unreachable.
     1454Listing \ref{lst:dependency} shows a slightly different example where a third thread is waiting on monitor \code{A}, using a different condition variable.
     1455Because the third thread is signalled when secretly holding \code{B}, the goal  becomes unreachable.
    15461456Depending on the order of signals (listing \ref{lst:dependency} line \ref{line:signal-ab} and \ref{line:signal-a}) two cases can happen:
    15471457
    1548 \paragraph{Case 1: thread $\alpha$ goes first.} In this case, the problem is that monitor @A@ needs to be passed to thread $\beta$ when thread $\alpha$ is done with it.
    1549 \paragraph{Case 2: thread $\beta$ goes first.} In this case, the problem is that monitor @B@ needs to be retained and passed to thread $\alpha$ along with monitor @A@, which can be done directly or possibly using thread $\beta$ as an intermediate.
     1458\paragraph{Case 1: thread $\alpha$ goes first.} In this case, the problem is that monitor \code{A} needs to be passed to thread $\beta$ when thread $\alpha$ is done with it.
     1459\paragraph{Case 2: thread $\beta$ goes first.} In this case, the problem is that monitor \code{B} needs to be retained and passed to thread $\alpha$ along with monitor \code{A}, which can be done directly or possibly using thread $\beta$ as an intermediate.
    15501460\\
    15511461
     
    15611471\begin{multicols}{3}
    15621472Thread $\alpha$
    1563 \begin{cfa}[numbers=left, firstnumber=1]
     1473\begin{pseudo}[numbers=left, firstnumber=1]
    15641474acquire A
    15651475        acquire A & B
     
    15671477        release A & B
    15681478release A
    1569 \end{cfa}
     1479\end{pseudo}
    15701480\columnbreak
    15711481Thread $\gamma$
    1572 \begin{cfa}[numbers=left, firstnumber=6, escapechar=|]
     1482\begin{pseudo}[numbers=left, firstnumber=6, escapechar=|]
    15731483acquire A
    15741484        acquire A & B
     
    15771487        |\label{line:signal-a}|signal A
    15781488|\label{line:release-a}|release A
    1579 \end{cfa}
     1489\end{pseudo}
    15801490\columnbreak
    15811491Thread $\beta$
    1582 \begin{cfa}[numbers=left, firstnumber=12, escapechar=|]
     1492\begin{pseudo}[numbers=left, firstnumber=12, escapechar=|]
    15831493acquire A
    15841494        wait A
    15851495|\label{line:release-aa}|release A
    1586 \end{cfa}
     1496\end{pseudo}
    15871497\end{multicols}
    1588 \begin{cfa}[caption={Pseudo-code for the three thread example.},label={lst:dependency}]
    1589 \end{cfa}
     1498\begin{cfacode}[caption={Pseudo-code for the three thread example.},label={lst:dependency}]
     1499\end{cfacode}
    15901500\begin{center}
    15911501\input{dependency}
     
    15951505\end{figure}
    15961506
    1597 In listing \ref{lst:int-bulk-cfa}, there is a solution that satisfies both barging prevention and mutual exclusion.
    1598 If ownership of both monitors is transferred to the waiter when the signaller releases @A & B@ and then the waiter transfers back ownership of @A@ back to the signaller when it releases it, then the problem is solved (@B@ is no longer in use at this point).
     1507In listing \ref{lst:int-bulk-pseudo}, there is a solution that satisfies both barging prevention and mutual exclusion.
     1508If ownership of both monitors is transferred to the waiter when the signaller releases \code{A & B} and then the waiter transfers back ownership of \code{A} back to the signaller when it releases it, then the problem is solved (\code{B} is no longer in use at this point).
    15991509Dynamically finding the correct order is therefore the second possible solution.
    16001510The problem is effectively resolving a dependency graph of ownership requirements.
     
    16041514\begin{figure}
    16051515\begin{multicols}{2}
    1606 \begin{cfa}
     1516\begin{pseudo}
    16071517acquire A
    16081518        acquire B
     
    16121522        release B
    16131523release A
    1614 \end{cfa}
     1524\end{pseudo}
    16151525
    16161526\columnbreak
    16171527
    1618 \begin{cfa}
     1528\begin{pseudo}
    16191529acquire A
    16201530        acquire B
     
    16241534        release B
    16251535release A
    1626 \end{cfa}
     1536\end{pseudo}
    16271537\end{multicols}
    1628 \begin{cfa}[caption={Extension to three monitors of listing \ref{lst:int-bulk-cfa}},label={lst:explosion}]
    1629 \end{cfa}
     1538\begin{cfacode}[caption={Extension to three monitors of listing \ref{lst:int-bulk-pseudo}},label={lst:explosion}]
     1539\end{cfacode}
    16301540\end{figure}
    16311541
     
    16361546\subsubsection{Partial Signalling} \label{partial-sig}
    16371547Finally, the solution that is chosen for \CFA is to use partial signalling.
    1638 Again using listing \ref{lst:int-bulk-cfa}, the partial signalling solution transfers ownership of monitor @B@ at lines \ref{line:signal1} to the waiter but does not wake the waiting thread since it is still using monitor @A@.
     1548Again using listing \ref{lst:int-bulk-pseudo}, the partial signalling solution transfers ownership of monitor \code{B} at lines \ref{line:signal1} to the waiter but does not wake the waiting thread since it is still using monitor \code{A}.
    16391549Only when it reaches line \ref{line:lastRelease} does it actually wake up the waiting thread.
    16401550This solution has the benefit that complexity is encapsulated into only two actions: passing monitors to the next owner when they should be released and conditionally waking threads if all conditions are met.
     
    16441554Using partial signalling, listing \ref{lst:dependency} can be solved easily:
    16451555\begin{itemize}
    1646         \item When thread $\gamma$ reaches line \ref{line:release-ab} it transfers monitor @B@ to thread $\alpha$ and continues to hold monitor @A@.
    1647         \item When thread $\gamma$ reaches line \ref{line:release-a}  it transfers monitor @A@ to thread $\beta$  and wakes it up.
    1648         \item When thread $\beta$  reaches line \ref{line:release-aa} it transfers monitor @A@ to thread $\alpha$ and wakes it up.
     1556        \item When thread $\gamma$ reaches line \ref{line:release-ab} it transfers monitor \code{B} to thread $\alpha$ and continues to hold monitor \code{A}.
     1557        \item When thread $\gamma$ reaches line \ref{line:release-a}  it transfers monitor \code{A} to thread $\beta$  and wakes it up.
     1558        \item When thread $\beta$  reaches line \ref{line:release-aa} it transfers monitor \code{A} to thread $\alpha$ and wakes it up.
    16491559\end{itemize}
    16501560
     
    16561566\begin{table}
    16571567\begin{tabular}{|c|c|}
    1658 @signal@ & @signal_block@ \\
     1568\code{signal} & \code{signal_block} \\
    16591569\hline
    1660 \begin{cfa}[tabsize=3]
    1661 monitor DatingService {
    1662         // compatibility codes
     1570\begin{cfacode}[tabsize=3]
     1571monitor DatingService
     1572{
     1573        //compatibility codes
    16631574        enum{ CCodes = 20 };
    16641575
     
    16711582condition exchange;
    16721583
    1673 int girl(int phoneNo, int cfa) {
    1674         // no compatible boy ?
    1675         if(empty(boys[cfa])) {
    1676                 wait(girls[cfa]);               // wait for boy
    1677                 girlPhoneNo = phoneNo;          // make phone number available
    1678                 signal(exchange);               // wake boy from chair
    1679         } else {
    1680                 girlPhoneNo = phoneNo;          // make phone number available
    1681                 signal(boys[cfa]);              // wake boy
    1682                 wait(exchange);         // sit in chair
     1584int girl(int phoneNo, int ccode)
     1585{
     1586        //no compatible boy ?
     1587        if(empty(boys[ccode]))
     1588        {
     1589                //wait for boy
     1590                wait(girls[ccode]);
     1591
     1592                //make phone number available
     1593                girlPhoneNo = phoneNo;
     1594
     1595                //wake boy from chair
     1596                signal(exchange);
     1597        }
     1598        else
     1599        {
     1600                //make phone number available
     1601                girlPhoneNo = phoneNo;
     1602
     1603                //wake boy
     1604                signal(boys[ccode]);
     1605
     1606                //sit in chair
     1607                wait(exchange);
    16831608        }
    16841609        return boyPhoneNo;
    16851610}
    1686 int boy(int phoneNo, int cfa) {
    1687         // same as above
    1688         // with boy/girl interchanged
    1689 }
    1690 \end{cfa}&\begin{cfa}[tabsize=3]
    1691 monitor DatingService {
    1692 
    1693         enum{ CCodes = 20 };    // compatibility codes
     1611
     1612int boy(int phoneNo, int ccode)
     1613{
     1614        //same as above
     1615        //with boy/girl interchanged
     1616}
     1617\end{cfacode}&\begin{cfacode}[tabsize=3]
     1618monitor DatingService
     1619{
     1620        //compatibility codes
     1621        enum{ CCodes = 20 };
    16941622
    16951623        int girlPhoneNo;
     
    16991627condition girls[CCodes];
    17001628condition boys [CCodes];
    1701 // exchange is not needed
    1702 
    1703 int girl(int phoneNo, int cfa) {
    1704         // no compatible boy ?
    1705         if(empty(boys[cfa])) {
    1706                 wait(girls[cfa]);               // wait for boy
    1707                 girlPhoneNo = phoneNo;          // make phone number available
    1708                 signal(exchange);               // wake boy from chair
    1709         } else {
    1710                 girlPhoneNo = phoneNo;          // make phone number available
    1711                 signal_block(boys[cfa]);                // wake boy
    1712 
    1713                 // second handshake unnecessary
     1629//exchange is not needed
     1630
     1631int girl(int phoneNo, int ccode)
     1632{
     1633        //no compatible boy ?
     1634        if(empty(boys[ccode]))
     1635        {
     1636                //wait for boy
     1637                wait(girls[ccode]);
     1638
     1639                //make phone number available
     1640                girlPhoneNo = phoneNo;
     1641
     1642                //wake boy from chair
     1643                signal(exchange);
     1644        }
     1645        else
     1646        {
     1647                //make phone number available
     1648                girlPhoneNo = phoneNo;
     1649
     1650                //wake boy
     1651                signal_block(boys[ccode]);
     1652
     1653                //second handshake unnecessary
    17141654
    17151655        }
     
    17171657}
    17181658
    1719 int boy(int phoneNo, int cfa) {
    1720         // same as above
    1721         // with boy/girl interchanged
    1722 }
    1723 \end{cfa}
     1659int boy(int phoneNo, int ccode)
     1660{
     1661        //same as above
     1662        //with boy/girl interchanged
     1663}
     1664\end{cfacode}
    17241665\end{tabular}
    1725 \caption{Dating service example using \protect\lstinline|signal| and \protect\lstinline|signal_block|. }
     1666\caption{Dating service example using \code{signal} and \code{signal_block}. }
    17261667\label{tbl:datingservice}
    17271668\end{table}
    17281669An important note is that, until now, signalling a monitor was a delayed operation.
    1729 The ownership of the monitor is transferred only when the monitor would have otherwise been released, not at the point of the @signal@ statement.
    1730 However, in some cases, it may be more convenient for users to immediately transfer ownership to the thread that is waiting for cooperation, which is achieved using the @signal_block@ routine.
     1670The ownership of the monitor is transferred only when the monitor would have otherwise been released, not at the point of the \code{signal} statement.
     1671However, in some cases, it may be more convenient for users to immediately transfer ownership to the thread that is waiting for cooperation, which is achieved using the \code{signal_block} routine.
    17311672
    17321673The example in table \ref{tbl:datingservice} highlights the difference in behaviour.
    1733 As mentioned, @signal@ only transfers ownership once the current critical section exits; this behaviour requires additional synchronization when a two-way handshake is needed.
    1734 To avoid this explicit synchronization, the @condition@ type offers the @signal_block@ routine, which handles the two-way handshake as shown in the example.
     1674As mentioned, \code{signal} only transfers ownership once the current critical section exits; this behaviour requires additional synchronization when a two-way handshake is needed.
     1675To avoid this explicit synchronization, the \code{condition} type offers the \code{signal_block} routine, which handles the two-way handshake as shown in the example.
    17351676This feature removes the need for a second condition variables and simplifies programming.
    1736 Like every other monitor semantic, @signal_block@ uses barging prevention, which means mutual-exclusion is baton-passed both on the front end and the back end of the call to @signal_block@, meaning no other thread can acquire the monitor either before or after the call.
     1677Like every other monitor semantic, \code{signal_block} uses barging prevention, which means mutual-exclusion is baton-passed both on the front end and the back end of the call to \code{signal_block}, meaning no other thread can acquire the monitor either before or after the call.
    17371678
    17381679% ======================================================================
     
    17461687Internal Scheduling & External Scheduling & Go\\
    17471688\hline
    1748 \begin{uC++}[tabsize=3]
     1689\begin{ucppcode}[tabsize=3]
    17491690_Monitor Semaphore {
    17501691        condition c;
     
    17611702        }
    17621703}
    1763 \end{uC++}&\begin{uC++}[tabsize=3]
     1704\end{ucppcode}&\begin{ucppcode}[tabsize=3]
    17641705_Monitor Semaphore {
    17651706
     
    17761717        }
    17771718}
    1778 \end{uC++}&\begin{Go}[tabsize=3]
     1719\end{ucppcode}&\begin{gocode}[tabsize=3]
    17791720type MySem struct {
    17801721        inUse bool
     
    17961737        s.inUse = false
    17971738
    1798         // This actually deadlocks
    1799         // when single thread
     1739        //This actually deadlocks
     1740        //when single thread
    18001741        s.c <- false
    18011742}
    1802 \end{Go}
     1743\end{gocode}
    18031744\end{tabular}
    18041745\caption{Different forms of scheduling.}
     
    18071748This method is more constrained and explicit, which helps users reduce the non-deterministic nature of concurrency.
    18081749Indeed, as the following examples demonstrate, external scheduling allows users to wait for events from other threads without the concern of unrelated events occurring.
    1809 External scheduling can generally be done either in terms of control flow (e.g., Ada with @accept@, \uC with @_Accept@) or in terms of data (e.g., Go with channels).
     1750External scheduling can generally be done either in terms of control flow (e.g., Ada with \code{accept}, \uC with \code{_Accept}) or in terms of data (e.g., Go with channels).
    18101751Of course, both of these paradigms have their own strengths and weaknesses, but for this project, control-flow semantics was chosen to stay consistent with the rest of the languages semantics.
    18111752Two challenges specific to \CFA arise when trying to add external scheduling with loose object definitions and multiple-monitor routines.
    1812 The previous example shows a simple use @_Accept@ versus @wait@/@signal@ and its advantages.
    1813 Note that while other languages often use @accept@/@select@ as the core external scheduling keyword, \CFA uses @waitfor@ to prevent name collisions with existing socket \textbf{api}s.
    1814 
    1815 For the @P@ member above using internal scheduling, the call to @wait@ only guarantees that @V@ is the last routine to access the monitor, allowing a third routine, say @isInUse()@, acquire mutual exclusion several times while routine @P@ is waiting.
    1816 On the other hand, external scheduling guarantees that while routine @P@ is waiting, no other routine than @V@ can acquire the monitor.
     1753The previous example shows a simple use \code{_Accept} versus \code{wait}/\code{signal} and its advantages.
     1754Note that while other languages often use \code{accept}/\code{select} as the core external scheduling keyword, \CFA uses \code{waitfor} to prevent name collisions with existing socket \textbf{api}s.
     1755
     1756For the \code{P} member above using internal scheduling, the call to \code{wait} only guarantees that \code{V} is the last routine to access the monitor, allowing a third routine, say \code{isInUse()}, acquire mutual exclusion several times while routine \code{P} is waiting.
     1757On the other hand, external scheduling guarantees that while routine \code{P} is waiting, no other routine than \code{V} can acquire the monitor.
    18171758
    18181759% ======================================================================
     
    18241765Since \CFA is not object oriented, monitors become both more difficult to implement and less clear for a user:
    18251766
    1826 \begin{cfa}
     1767\begin{cfacode}
    18271768monitor A {};
    18281769
    18291770void f(A & mutex a);
    18301771void g(A & mutex a) {
    1831         waitfor(f); // Obvious which f() to wait for
    1832 }
    1833 
    1834 void f(A & mutex a, int); // New different F added in scope
     1772        waitfor(f); //Obvious which f() to wait for
     1773}
     1774
     1775void f(A & mutex a, int); //New different F added in scope
    18351776void h(A & mutex a) {
    1836         waitfor(f); // Less obvious which f() to wait for
    1837 }
    1838 \end{cfa}
     1777        waitfor(f); //Less obvious which f() to wait for
     1778}
     1779\end{cfacode}
    18391780
    18401781Furthermore, external scheduling is an example where implementation constraints become visible from the interface.
    1841 Here is the cfa-code for the entering phase of a monitor:
     1782Here is the pseudo-code for the entering phase of a monitor:
    18421783\begin{center}
    18431784\begin{tabular}{l}
    1844 \begin{cfa}
     1785\begin{pseudo}
    18451786        if monitor is free
    18461787                enter
     
    18511792        else
    18521793                block
    1853 \end{cfa}
     1794\end{pseudo}
    18541795\end{tabular}
    18551796\end{center}
    18561797For the first two conditions, it is easy to implement a check that can evaluate the condition in a few instructions.
    1857 However, a fast check for @monitor accepts me@ is much harder to implement depending on the constraints put on the monitors.
     1798However, a fast check for \pscode{monitor accepts me} is much harder to implement depending on the constraints put on the monitors.
    18581799Indeed, monitors are often expressed as an entry queue and some acceptor queue as in Figure~\ref{fig:ClassicalMonitor}.
    18591800
     
    18811822The alternative is to alter the implementation as in Figure~\ref{fig:BulkMonitor}.
    18821823Here, the mutex routine called is associated with a thread on the entry queue while a list of acceptable routines is kept separate.
    1883 Generating a mask dynamically means that the storage for the mask information can vary between calls to @waitfor@, allowing for more flexibility and extensions.
     1824Generating a mask dynamically means that the storage for the mask information can vary between calls to \code{waitfor}, allowing for more flexibility and extensions.
    18841825Storing an array of accepted function pointers replaces the single instruction bitmask comparison with dereferencing a pointer followed by a linear search.
    1885 Furthermore, supporting nested external scheduling (e.g., listing \ref{lst:nest-ext}) may now require additional searches for the @waitfor@ statement to check if a routine is already queued.
     1826Furthermore, supporting nested external scheduling (e.g., listing \ref{lst:nest-ext}) may now require additional searches for the \code{waitfor} statement to check if a routine is already queued.
    18861827
    18871828\begin{figure}
    1888 \begin{cfa}[caption={Example of nested external scheduling},label={lst:nest-ext}]
     1829\begin{cfacode}[caption={Example of nested external scheduling},label={lst:nest-ext}]
    18891830monitor M {};
    18901831void foo( M & mutex a ) {}
    18911832void bar( M & mutex b ) {
    1892         // Nested in the waitfor(bar, c) call
     1833        //Nested in the waitfor(bar, c) call
    18931834        waitfor(foo, b);
    18941835}
     
    18971838}
    18981839
    1899 \end{cfa}
     1840\end{cfacode}
    19001841\end{figure}
    19011842
     
    19171858External scheduling, like internal scheduling, becomes significantly more complex when introducing multi-monitor syntax.
    19181859Even in the simplest possible case, some new semantics needs to be established:
    1919 \begin{cfa}
     1860\begin{cfacode}
    19201861monitor M {};
    19211862
     
    19231864
    19241865void g(M & mutex b, M & mutex c) {
    1925         waitfor(f); // two monitors M => unknown which to pass to f(M & mutex)
    1926 }
    1927 \end{cfa}
     1866        waitfor(f); //two monitors M => unknown which to pass to f(M & mutex)
     1867}
     1868\end{cfacode}
    19281869The obvious solution is to specify the correct monitor as follows:
    19291870
    1930 \begin{cfa}
     1871\begin{cfacode}
    19311872monitor M {};
    19321873
     
    19341875
    19351876void g(M & mutex a, M & mutex b) {
    1936         // wait for call to f with argument b
     1877        //wait for call to f with argument b
    19371878        waitfor(f, b);
    19381879}
    1939 \end{cfa}
     1880\end{cfacode}
    19401881This syntax is unambiguous.
    1941 Both locks are acquired and kept by @g@.
    1942 When routine @f@ is called, the lock for monitor @b@ is temporarily transferred from @g@ to @f@ (while @g@ still holds lock @a@).
    1943 This behaviour can be extended to the multi-monitor @waitfor@ statement as follows.
    1944 
    1945 \begin{cfa}
     1882Both locks are acquired and kept by \code{g}.
     1883When routine \code{f} is called, the lock for monitor \code{b} is temporarily transferred from \code{g} to \code{f} (while \code{g} still holds lock \code{a}).
     1884This behaviour can be extended to the multi-monitor \code{waitfor} statement as follows.
     1885
     1886\begin{cfacode}
    19461887monitor M {};
    19471888
     
    19491890
    19501891void g(M & mutex a, M & mutex b) {
    1951         // wait for call to f with arguments a and b
     1892        //wait for call to f with arguments a and b
    19521893        waitfor(f, a, b);
    19531894}
    1954 \end{cfa}
    1955 
    1956 Note that the set of monitors passed to the @waitfor@ statement must be entirely contained in the set of monitors already acquired in the routine. @waitfor@ used in any other context is undefined behaviour.
     1895\end{cfacode}
     1896
     1897Note that the set of monitors passed to the \code{waitfor} statement must be entirely contained in the set of monitors already acquired in the routine. \code{waitfor} used in any other context is undefined behaviour.
    19571898
    19581899An important behaviour to note is when a set of monitors only match partially:
    19591900
    1960 \begin{cfa}
     1901\begin{cfacode}
    19611902mutex struct A {};
    19621903
     
    19711912
    19721913void foo() {
    1973         g(a1, b); // block on accept
     1914        g(a1, b); //block on accept
    19741915}
    19751916
    19761917void bar() {
    1977         f(a2, b); // fulfill cooperation
    1978 }
    1979 \end{cfa}
     1918        f(a2, b); //fulfill cooperation
     1919}
     1920\end{cfacode}
    19801921While the equivalent can happen when using internal scheduling, the fact that conditions are specific to a set of monitors means that users have to use two different condition variables.
    19811922In both cases, partially matching monitor sets does not wakeup the waiting thread.
    1982 It is also important to note that in the case of external scheduling the order of parameters is irrelevant; @waitfor(f,a,b)@ and @waitfor(f,b,a)@ are indistinguishable waiting condition.
    1983 
    1984 % ======================================================================
    1985 % ======================================================================
    1986 \subsection{\protect\lstinline|waitfor| Semantics}
    1987 % ======================================================================
    1988 % ======================================================================
    1989 
    1990 Syntactically, the @waitfor@ statement takes a function identifier and a set of monitors.
    1991 While the set of monitors can be any list of expressions, the function name is more restricted because the compiler validates at compile time the validity of the function type and the parameters used with the @waitfor@ statement.
     1923It is also important to note that in the case of external scheduling the order of parameters is irrelevant; \code{waitfor(f,a,b)} and \code{waitfor(f,b,a)} are indistinguishable waiting condition.
     1924
     1925% ======================================================================
     1926% ======================================================================
     1927\subsection{\code{waitfor} Semantics}
     1928% ======================================================================
     1929% ======================================================================
     1930
     1931Syntactically, the \code{waitfor} statement takes a function identifier and a set of monitors.
     1932While the set of monitors can be any list of expressions, the function name is more restricted because the compiler validates at compile time the validity of the function type and the parameters used with the \code{waitfor} statement.
    19921933It checks that the set of monitors passed in matches the requirements for a function call.
    19931934Listing \ref{lst:waitfor} shows various usages of the waitfor statement and which are acceptable.
    1994 The choice of the function type is made ignoring any non-@mutex@ parameter.
     1935The choice of the function type is made ignoring any non-\code{mutex} parameter.
    19951936One limitation of the current implementation is that it does not handle overloading, but overloading is possible.
    19961937\begin{figure}
    1997 \begin{cfa}[caption={Various correct and incorrect uses of the waitfor statement},label={lst:waitfor}]
     1938\begin{cfacode}[caption={Various correct and incorrect uses of the waitfor statement},label={lst:waitfor}]
    19981939monitor A{};
    19991940monitor B{};
     
    20091950        void (*fp)( A & mutex ) = f1;
    20101951
    2011         waitfor(f1, a1);     // Correct : 1 monitor case
    2012         waitfor(f2, a1, b1); // Correct : 2 monitor case
    2013         waitfor(f3, a1);     // Correct : non-mutex arguments are ignored
    2014         waitfor(f1, *ap);    // Correct : expression as argument
    2015 
    2016         waitfor(f1, a1, b1); // Incorrect : Too many mutex arguments
    2017         waitfor(f2, a1);     // Incorrect : Too few mutex arguments
    2018         waitfor(f2, a1, a2); // Incorrect : Mutex arguments don't match
    2019         waitfor(f1, 1);      // Incorrect : 1 not a mutex argument
    2020         waitfor(f9, a1);     // Incorrect : f9 function does not exist
    2021         waitfor(*fp, a1 );   // Incorrect : fp not an identifier
    2022         waitfor(f4, a1);     // Incorrect : f4 ambiguous
    2023 
    2024         waitfor(f2, a1, b2); // Undefined behaviour : b2 not mutex
    2025 }
    2026 \end{cfa}
     1952        waitfor(f1, a1);     //Correct : 1 monitor case
     1953        waitfor(f2, a1, b1); //Correct : 2 monitor case
     1954        waitfor(f3, a1);     //Correct : non-mutex arguments are ignored
     1955        waitfor(f1, *ap);    //Correct : expression as argument
     1956
     1957        waitfor(f1, a1, b1); //Incorrect : Too many mutex arguments
     1958        waitfor(f2, a1);     //Incorrect : Too few mutex arguments
     1959        waitfor(f2, a1, a2); //Incorrect : Mutex arguments don't match
     1960        waitfor(f1, 1);      //Incorrect : 1 not a mutex argument
     1961        waitfor(f9, a1);     //Incorrect : f9 function does not exist
     1962        waitfor(*fp, a1 );   //Incorrect : fp not an identifier
     1963        waitfor(f4, a1);     //Incorrect : f4 ambiguous
     1964
     1965        waitfor(f2, a1, b2); //Undefined behaviour : b2 not mutex
     1966}
     1967\end{cfacode}
    20271968\end{figure}
    20281969
    2029 Finally, for added flexibility, \CFA supports constructing a complex @waitfor@ statement using the @or@, @timeout@ and @else@.
    2030 Indeed, multiple @waitfor@ clauses can be chained together using @or@; this chain forms a single statement that uses baton pass to any function that fits one of the function+monitor set passed in.
    2031 To enable users to tell which accepted function executed, @waitfor@s are followed by a statement (including the null statement @;@) or a compound statement, which is executed after the clause is triggered.
    2032 A @waitfor@ chain can also be followed by a @timeout@, to signify an upper bound on the wait, or an @else@, to signify that the call should be non-blocking, which checks for a matching function call already arrived and otherwise continues.
    2033 Any and all of these clauses can be preceded by a @when@ condition to dynamically toggle the accept clauses on or off based on some current state.
     1970Finally, for added flexibility, \CFA supports constructing a complex \code{waitfor} statement using the \code{or}, \code{timeout} and \code{else}.
     1971Indeed, multiple \code{waitfor} clauses can be chained together using \code{or}; this chain forms a single statement that uses baton pass to any function that fits one of the function+monitor set passed in.
     1972To enable users to tell which accepted function executed, \code{waitfor}s are followed by a statement (including the null statement \code{;}) or a compound statement, which is executed after the clause is triggered.
     1973A \code{waitfor} chain can also be followed by a \code{timeout}, to signify an upper bound on the wait, or an \code{else}, to signify that the call should be non-blocking, which checks for a matching function call already arrived and otherwise continues.
     1974Any and all of these clauses can be preceded by a \code{when} condition to dynamically toggle the accept clauses on or off based on some current state.
    20341975Listing \ref{lst:waitfor2} demonstrates several complex masks and some incorrect ones.
    20351976
    20361977\begin{figure}
    2037 \lstset{language=CFA,deletedelim=**[is][]{`}{`}}
    2038 \begin{cfa}
     1978\begin{cfacode}[caption={Various correct and incorrect uses of the or, else, and timeout clause around a waitfor statement},label={lst:waitfor2}]
    20391979monitor A{};
    20401980
     
    20431983
    20441984void foo( A & mutex a, bool b, int t ) {
    2045         waitfor(f1, a);                                                 $\C{// Correct : blocking case}$
    2046 
    2047         waitfor(f1, a) {                                                $\C{// Correct : block with statement}$
     1985        //Correct : blocking case
     1986        waitfor(f1, a);
     1987
     1988        //Correct : block with statement
     1989        waitfor(f1, a) {
    20481990                sout | "f1" | endl;
    20491991        }
    2050         waitfor(f1, a) {                                                $\C{// Correct : block waiting for f1 or f2}$
     1992
     1993        //Correct : block waiting for f1 or f2
     1994        waitfor(f1, a) {
    20511995                sout | "f1" | endl;
    20521996        } or waitfor(f2, a) {
    20531997                sout | "f2" | endl;
    20541998        }
    2055         waitfor(f1, a); or else;                                $\C{// Correct : non-blocking case}$
    2056 
    2057         waitfor(f1, a) {                                                $\C{// Correct : non-blocking case}$
     1999
     2000        //Correct : non-blocking case
     2001        waitfor(f1, a); or else;
     2002
     2003        //Correct : non-blocking case
     2004        waitfor(f1, a) {
    20582005                sout | "blocked" | endl;
    20592006        } or else {
    20602007                sout | "didn't block" | endl;
    20612008        }
    2062         waitfor(f1, a) {                                                $\C{// Correct : block at most 10 seconds}$
     2009
     2010        //Correct : block at most 10 seconds
     2011        waitfor(f1, a) {
    20632012                sout | "blocked" | endl;
    20642013        } or timeout( 10`s) {
    20652014                sout | "didn't block" | endl;
    20662015        }
    2067         // Correct : block only if b == true if b == false, don't even make the call
     2016
     2017        //Correct : block only if b == true
     2018        //if b == false, don't even make the call
    20682019        when(b) waitfor(f1, a);
    20692020
    2070         // Correct : block only if b == true if b == false, make non-blocking call
     2021        //Correct : block only if b == true
     2022        //if b == false, make non-blocking call
    20712023        waitfor(f1, a); or when(!b) else;
    20722024
    2073         // Correct : block only of t > 1
     2025        //Correct : block only of t > 1
    20742026        waitfor(f1, a); or when(t > 1) timeout(t); or else;
    20752027
    2076         // Incorrect : timeout clause is dead code
     2028        //Incorrect : timeout clause is dead code
    20772029        waitfor(f1, a); or timeout(t); or else;
    20782030
    2079         // Incorrect : order must be waitfor [or waitfor... [or timeout] [or else]]
     2031        //Incorrect : order must be
     2032        //waitfor [or waitfor... [or timeout] [or else]]
    20802033        timeout(t); or waitfor(f1, a); or else;
    20812034}
    2082 \end{cfa}
    2083 \caption{Correct and incorrect uses of the or, else, and timeout clause around a waitfor statement}
    2084 \label{lst:waitfor2}
     2035\end{cfacode}
    20852036\end{figure}
    20862037
     
    20902041% ======================================================================
    20912042% ======================================================================
    2092 An interesting use for the @waitfor@ statement is destructor semantics.
    2093 Indeed, the @waitfor@ statement can accept any @mutex@ routine, which includes the destructor (see section \ref{data}).
     2043An interesting use for the \code{waitfor} statement is destructor semantics.
     2044Indeed, the \code{waitfor} statement can accept any \code{mutex} routine, which includes the destructor (see section \ref{data}).
    20942045However, with the semantics discussed until now, waiting for the destructor does not make any sense, since using an object after its destructor is called is undefined behaviour.
    2095 The simplest approach is to disallow @waitfor@ on a destructor.
    2096 However, a more expressive approach is to flip ordering of execution when waiting for the destructor, meaning that waiting for the destructor allows the destructor to run after the current @mutex@ routine, similarly to how a condition is signalled.
     2046The simplest approach is to disallow \code{waitfor} on a destructor.
     2047However, a more expressive approach is to flip ordering of execution when waiting for the destructor, meaning that waiting for the destructor allows the destructor to run after the current \code{mutex} routine, similarly to how a condition is signalled.
    20972048\begin{figure}
    2098 \begin{cfa}[caption={Example of an executor which executes action in series until the destructor is called.},label={lst:dtor-order}]
     2049\begin{cfacode}[caption={Example of an executor which executes action in series until the destructor is called.},label={lst:dtor-order}]
    20992050monitor Executer {};
    21002051struct  Action;
     
    21102061        }
    21112062}
    2112 \end{cfa}
     2063\end{cfacode}
    21132064\end{figure}
    21142065For example, listing \ref{lst:dtor-order} shows an example of an executor with an infinite loop, which waits for the destructor to break out of this loop.
     
    21282079In this decade, it is no longer reasonable to create a high-performance application without caring about parallelism.
    21292080Indeed, parallelism is an important aspect of performance and more specifically throughput and hardware utilization.
    2130 The lowest-level approach of parallelism is to use \textbf{kthread} in combination with semantics like @fork@, @join@, etc.
     2081The lowest-level approach of parallelism is to use \textbf{kthread} in combination with semantics like \code{fork}, \code{join}, etc.
    21312082However, since these have significant costs and limitations, \textbf{kthread} are now mostly used as an implementation tool rather than a user oriented one.
    21322083There are several alternatives to solve these issues that all have strengths and weaknesses.
     
    22072158Conveniently, the call stack fits that description and is easy to use, which is why it is used heavily in the implementation of internal scheduling, particularly variable-length arrays.
    22082159Since stack allocation is based on scopes, the first step of the implementation is to identify the scopes that are available to store the information, and which of these can have a variable-length array.
    2209 The threads and the condition both have a fixed amount of memory, while @mutex@ routines and blocking calls allow for an unbound amount, within the stack size.
     2160The threads and the condition both have a fixed amount of memory, while \code{mutex} routines and blocking calls allow for an unbound amount, within the stack size.
    22102161
    22112162Note that since the major contributions of this paper are extending monitor semantics to \textbf{bulk-acq} and loose object definitions, any challenges that are not resulting of these characteristics of \CFA are considered as solved problems and therefore not discussed.
     
    22172168% ======================================================================
    22182169
    2219 The first step towards the monitor implementation is simple @mutex@ routines.
     2170The first step towards the monitor implementation is simple \code{mutex} routines.
    22202171In the single monitor case, mutual-exclusion is done using the entry/exit procedure in listing \ref{lst:entry1}.
    22212172The entry/exit procedures do not have to be extended to support multiple monitors.
     
    22282179\begin{multicols}{2}
    22292180Entry
    2230 \begin{cfa}
     2181\begin{pseudo}
    22312182if monitor is free
    22322183        enter
     
    22362187        block
    22372188increment recursions
    2238 \end{cfa}
     2189\end{pseudo}
    22392190\columnbreak
    22402191Exit
    2241 \begin{cfa}
     2192\begin{pseudo}
    22422193decrement recursion
    22432194if recursion == 0
    22442195        if entry queue not empty
    22452196                wake-up thread
    2246 \end{cfa}
     2197\end{pseudo}
    22472198\end{multicols}
    2248 \begin{cfa}[caption={Initial entry and exit routine for monitors},label={lst:entry1}]
    2249 \end{cfa}
     2199\begin{pseudo}[caption={Initial entry and exit routine for monitors},label={lst:entry1}]
     2200\end{pseudo}
    22502201\end{figure}
    22512202
     
    22542205However, it is shown that entry-point locking solves most of the issues.
    22552206
    2256 First of all, interaction between @otype@ polymorphism (see Section~\ref{s:ParametricPolymorphism}) and monitors is impossible since monitors do not support copying.
    2257 Therefore, the main question is how to support @dtype@ polymorphism.
     2207First of all, interaction between \code{otype} polymorphism (see Section~\ref{s:ParametricPolymorphism}) and monitors is impossible since monitors do not support copying.
     2208Therefore, the main question is how to support \code{dtype} polymorphism.
    22582209It is important to present the difference between the two acquiring options: \textbf{callsite-locking} and entry-point locking, i.e., acquiring the monitors before making a mutex routine-call or as the first operation of the mutex routine-call.
    22592210For example:
    2260 \begin{table}
     2211\begin{table}[H]
    22612212\begin{center}
    22622213\begin{tabular}{|c|c|c|}
    22632214Mutex & \textbf{callsite-locking} & \textbf{entry-point-locking} \\
    2264 call & cfa-code & cfa-code \\
     2215call & pseudo-code & pseudo-code \\
    22652216\hline
    2266 \begin{cfa}[tabsize=3]
     2217\begin{cfacode}[tabsize=3]
    22672218void foo(monitor& mutex a){
    22682219
    2269         // Do Work
     2220        //Do Work
    22702221        //...
    22712222
     
    22782229
    22792230}
    2280 \end{cfa} & \begin{cfa}[tabsize=3]
     2231\end{cfacode} & \begin{pseudo}[tabsize=3]
    22812232foo(& a) {
    22822233
    2283         // Do Work
     2234        //Do Work
    22842235        //...
    22852236
     
    22922243        release(a);
    22932244}
    2294 \end{cfa} & \begin{cfa}[tabsize=3]
     2245\end{pseudo} & \begin{pseudo}[tabsize=3]
    22952246foo(& a) {
    22962247        acquire(a);
    2297         // Do Work
     2248        //Do Work
    22982249        //...
    22992250        release(a);
     
    23062257
    23072258}
    2308 \end{cfa}
     2259\end{pseudo}
    23092260\end{tabular}
    23102261\end{center}
     
    23132264\end{table}
    23142265
    2315 Note the @mutex@ keyword relies on the type system, which means that in cases where a generic monitor-routine is desired, writing the mutex routine is possible with the proper trait, e.g.:
    2316 \begin{cfa}
    2317 // Incorrect: T may not be monitor
     2266Note the \code{mutex} keyword relies on the type system, which means that in cases where a generic monitor-routine is desired, writing the mutex routine is possible with the proper trait, e.g.:
     2267\begin{cfacode}
     2268//Incorrect: T may not be monitor
    23182269forall(dtype T)
    23192270void foo(T * mutex t);
    23202271
    2321 // Correct: this function only works on monitors (any monitor)
     2272//Correct: this function only works on monitors (any monitor)
    23222273forall(dtype T | is_monitor(T))
    23232274void bar(T * mutex t));
    2324 \end{cfa}
     2275\end{cfacode}
    23252276
    23262277Both entry point and \textbf{callsite-locking} are feasible implementations.
     
    23482299
    23492300\subsection{Processors}
    2350 Parallelism in \CFA is built around using processors to specify how much parallelism is desired. \CFA processors are object wrappers around kernel threads, specifically @pthread@s in the current implementation of \CFA.
     2301Parallelism in \CFA is built around using processors to specify how much parallelism is desired. \CFA processors are object wrappers around kernel threads, specifically \texttt{pthread}s in the current implementation of \CFA.
    23512302Indeed, any parallelism must go through operating-system libraries.
    23522303However, \textbf{uthread} are still the main source of concurrency, processors are simply the underlying source of parallelism.
     
    23572308\subsection{Stack Management}
    23582309One of the challenges of this system is to reduce the footprint as much as possible.
    2359 Specifically, all @pthread@s created also have a stack created with them, which should be used as much as possible.
     2310Specifically, all \texttt{pthread}s created also have a stack created with them, which should be used as much as possible.
    23602311Normally, coroutines also create their own stack to run on, however, in the case of the coroutines used for processors, these coroutines run directly on the \textbf{kthread} stack, effectively stealing the processor stack.
    23612312The exception to this rule is the Main Processor, i.e., the initial \textbf{kthread} that is given to any program.
     
    23722323Obviously, this doubles the context-switch cost because threads must context-switch to an intermediate stack.
    23732324The alternative 1-step context-switch uses the stack of the ``from'' thread to schedule and then context-switches directly to the ``to'' thread.
    2374 However, the performance of the 2-step context-switch is still superior to a @pthread_yield@ (see section \ref{results}).
    2375 Additionally, for users in need for optimal performance, it is important to note that having a 2-step context-switch as the default does not prevent \CFA from offering a 1-step context-switch (akin to the Microsoft @SwitchToFiber@~\cite{switchToWindows} routine).
     2325However, the performance of the 2-step context-switch is still superior to a \code{pthread_yield} (see section \ref{results}).
     2326Additionally, for users in need for optimal performance, it is important to note that having a 2-step context-switch as the default does not prevent \CFA from offering a 1-step context-switch (akin to the Microsoft \code{SwitchToFiber}~\cite{switchToWindows} routine).
    23762327This option is not currently present in \CFA, but the changes required to add it are strictly additive.
    23772328
     
    24052356This behaviour is only a problem if all kernel threads, among which a user thread can migrate, differ in terms of signal masks\footnote{Sadly, official POSIX documentation is silent on what distinguishes ``async-signal-safe'' functions from other functions.}.
    24062357However, since the kernel thread handling preemption requires a different signal mask, executing user threads on the kernel-alarm thread can cause deadlocks.
    2407 For this reason, the alarm thread is in a tight loop around a system call to @sigwaitinfo@, requiring very little CPU time for preemption.
     2358For this reason, the alarm thread is in a tight loop around a system call to \code{sigwaitinfo}, requiring very little CPU time for preemption.
    24082359One final detail about the alarm thread is how to wake it when additional communication is required (e.g., on thread termination).
    2409 This unblocking is also done using {\tt SIGALRM}, but sent through the @pthread_sigqueue@.
    2410 Indeed, @sigwait@ can differentiate signals sent from @pthread_sigqueue@ from signals sent from alarms or the kernel.
     2360This unblocking is also done using {\tt SIGALRM}, but sent through the \code{pthread_sigqueue}.
     2361Indeed, \code{sigwait} can differentiate signals sent from \code{pthread_sigqueue} from signals sent from alarms or the kernel.
    24112362
    24122363\subsection{Scheduler}
     
    24222373The following figure is the traditional illustration of a monitor (repeated from page~\pageref{fig:ClassicalMonitor} for convenience):
    24232374
    2424 \begin{figure}
     2375\begin{figure}[H]
    24252376\begin{center}
    24262377{\resizebox{0.4\textwidth}{!}{\input{monitor}}}
     
    24372388Secondly, the thread waiting on the condition has to be separated across multiple monitors, seen in figure \ref{fig:monitor_cfa}.
    24382389
    2439 \begin{figure}
     2390\begin{figure}[H]
    24402391\begin{center}
    24412392{\resizebox{0.8\textwidth}{!}{\input{int_monitor}}}
     
    24512402For a specific signalling operation every monitor needs a piece of thread on its AS-stack.
    24522403
    2453 \begin{figure}
     2404\begin{figure}[b]
    24542405\begin{multicols}{2}
    24552406Entry
    2456 \begin{cfa}
     2407\begin{pseudo}
    24572408if monitor is free
    24582409        enter
     
    24632414increment recursion
    24642415
    2465 \end{cfa}
     2416\end{pseudo}
    24662417\columnbreak
    24672418Exit
    2468 \begin{cfa}
     2419\begin{pseudo}
    24692420decrement recursion
    24702421if recursion == 0
     
    24762427        if entry queue not empty
    24772428                wake-up thread
    2478 \end{cfa}
     2429\end{pseudo}
    24792430\end{multicols}
    2480 \begin{cfa}[caption={Entry and exit routine for monitors with internal scheduling},label={lst:entry2}]
    2481 \end{cfa}
     2431\begin{pseudo}[caption={Entry and exit routine for monitors with internal scheduling},label={lst:entry2}]
     2432\end{pseudo}
    24822433\end{figure}
    24832434
     
    24852436Basically, the solution boils down to having a separate data structure for the condition queue and the AS-stack, and unconditionally transferring ownership of the monitors but only unblocking the thread when the last monitor has transferred ownership.
    24862437This solution is deadlock safe as well as preventing any potential barging.
    2487 The data structures used for the AS-stack are reused extensively for external scheduling, but in the case of internal scheduling, the data is allocated using variable-length arrays on the call stack of the @wait@ and @signal_block@ routines.
    2488 
    2489 \begin{figure}
     2438The data structures used for the AS-stack are reused extensively for external scheduling, but in the case of internal scheduling, the data is allocated using variable-length arrays on the call stack of the \code{wait} and \code{signal_block} routines.
     2439
     2440\begin{figure}[H]
    24902441\begin{center}
    24912442{\resizebox{0.8\textwidth}{!}{\input{monitor_structs.pstex_t}}}
     
    24972448Figure \ref{fig:structs} shows a high-level representation of these data structures.
    24982449The main idea behind them is that, a thread cannot contain an arbitrary number of intrusive ``next'' pointers for linking onto monitors.
    2499 The @condition node@ is the data structure that is queued onto a condition variable and, when signalled, the condition queue is popped and each @condition criterion@ is moved to the AS-stack.
     2450The \code{condition node} is the data structure that is queued onto a condition variable and, when signalled, the condition queue is popped and each \code{condition criterion} is moved to the AS-stack.
    25002451Once all the criteria have been popped from their respective AS-stacks, the thread is woken up, which is what is shown in listing \ref{lst:entry2}.
    25012452
     
    25072458Similarly to internal scheduling, external scheduling for multiple monitors relies on the idea that waiting-thread queues are no longer specific to a single monitor, as mentioned in section \ref{extsched}.
    25082459For internal scheduling, these queues are part of condition variables, which are still unique for a given scheduling operation (i.e., no signal statement uses multiple conditions).
    2509 However, in the case of external scheduling, there is no equivalent object which is associated with @waitfor@ statements.
     2460However, in the case of external scheduling, there is no equivalent object which is associated with \code{waitfor} statements.
    25102461This absence means the queues holding the waiting threads must be stored inside at least one of the monitors that is acquired.
    2511 These monitors being the only objects that have sufficient lifetime and are available on both sides of the @waitfor@ statement.
     2462These monitors being the only objects that have sufficient lifetime and are available on both sides of the \code{waitfor} statement.
    25122463This requires an algorithm to choose which monitor holds the relevant queue.
    25132464It is also important that said algorithm be independent of the order in which users list parameters.
     
    25262477\begin{itemize}
    25272478        \item The threads waiting on the entry queue need to keep track of which routine they are trying to enter, and using which set of monitors.
    2528 The @mutex@ routine already has all the required information on its stack, so the thread only needs to keep a pointer to that information.
     2479The \code{mutex} routine already has all the required information on its stack, so the thread only needs to keep a pointer to that information.
    25292480        \item The monitors need to keep a mask of acceptable routines.
    25302481This mask contains for each acceptable routine, a routine pointer and an array of monitors to go with it.
    25312482It also needs storage to keep track of which routine was accepted.
    25322483Since this information is not specific to any monitor, the monitors actually contain a pointer to an integer on the stack of the waiting thread.
    2533 Note that if a thread has acquired two monitors but executes a @waitfor@ with only one monitor as a parameter, setting the mask of acceptable routines to both monitors will not cause any problems since the extra monitor will not change ownership regardless.
    2534 This becomes relevant when @when@ clauses affect the number of monitors passed to a @waitfor@ statement.
     2484Note that if a thread has acquired two monitors but executes a \code{waitfor} with only one monitor as a parameter, setting the mask of acceptable routines to both monitors will not cause any problems since the extra monitor will not change ownership regardless.
     2485This becomes relevant when \code{when} clauses affect the number of monitors passed to a \code{waitfor} statement.
    25352486        \item The entry/exit routines need to be updated as shown in listing \ref{lst:entry3}.
    25362487\end{itemize}
     
    25402491This routine is needed because of the storage requirements of the call order inversion.
    25412492Indeed, when waiting for the destructors, storage is needed for the waiting context and the lifetime of said storage needs to outlive the waiting operation it is needed for.
    2542 For regular @waitfor@ statements, the call stack of the routine itself matches this requirement but it is no longer the case when waiting for the destructor since it is pushed on to the AS-stack for later.
    2543 The @waitfor@ semantics can then be adjusted correspondingly, as seen in listing \ref{lst:entry-dtor}
     2493For regular \code{waitfor} statements, the call stack of the routine itself matches this requirement but it is no longer the case when waiting for the destructor since it is pushed on to the AS-stack for later.
     2494The \code{waitfor} semantics can then be adjusted correspondingly, as seen in listing \ref{lst:entry-dtor}
    25442495
    25452496\begin{figure}
    25462497\begin{multicols}{2}
    25472498Entry
    2548 \begin{cfa}
     2499\begin{pseudo}
    25492500if monitor is free
    25502501        enter
     
    25572508        block
    25582509increment recursion
    2559 \end{cfa}
     2510\end{pseudo}
    25602511\columnbreak
    25612512Exit
    2562 \begin{cfa}
     2513\begin{pseudo}
    25632514decrement recursion
    25642515if recursion == 0
     
    25732524                wake-up thread
    25742525        endif
    2575 \end{cfa}
     2526\end{pseudo}
    25762527\end{multicols}
    2577 \begin{cfa}[caption={Entry and exit routine for monitors with internal scheduling and external scheduling},label={lst:entry3}]
    2578 \end{cfa}
     2528\begin{pseudo}[caption={Entry and exit routine for monitors with internal scheduling and external scheduling},label={lst:entry3}]
     2529\end{pseudo}
    25792530\end{figure}
    25802531
     
    25822533\begin{multicols}{2}
    25832534Destructor Entry
    2584 \begin{cfa}
     2535\begin{pseudo}
    25852536if monitor is free
    25862537        enter
     
    25962547        wait
    25972548increment recursion
    2598 \end{cfa}
     2549\end{pseudo}
    25992550\columnbreak
    26002551Waitfor
    2601 \begin{cfa}
     2552\begin{pseudo}
    26022553if matching thread is already there
    26032554        if found destructor
     
    26192570block
    26202571return
    2621 \end{cfa}
     2572\end{pseudo}
    26222573\end{multicols}
    2623 \begin{cfa}[caption={Pseudo code for the \protect\lstinline|waitfor| routine and the \protect\lstinline|mutex| entry routine for destructors},label={lst:entry-dtor}]
    2624 \end{cfa}
     2574\begin{pseudo}[caption={Pseudo code for the \code{waitfor} routine and the \code{mutex} entry routine for destructors},label={lst:entry-dtor}]
     2575\end{pseudo}
    26252576\end{figure}
    26262577
     
    26342585
    26352586\section{Threads As Monitors}
    2636 As it was subtly alluded in section \ref{threads}, @thread@s in \CFA are in fact monitors, which means that all monitor features are available when using threads.
     2587As it was subtly alluded in section \ref{threads}, \code{thread}s in \CFA are in fact monitors, which means that all monitor features are available when using threads.
    26372588For example, here is a very simple two thread pipeline that could be used for a simulator of a game engine:
    2638 \begin{figure}
    2639 \begin{cfa}[caption={Toy simulator using \protect\lstinline|thread|s and \protect\lstinline|monitor|s.},label={lst:engine-v1}]
     2589\begin{figure}[H]
     2590\begin{cfacode}[caption={Toy simulator using \code{thread}s and \code{monitor}s.},label={lst:engine-v1}]
    26402591// Visualization declaration
    26412592thread Renderer {} renderer;
     
    26642615        }
    26652616}
    2666 \end{cfa}
     2617\end{cfacode}
    26672618\end{figure}
    26682619One of the obvious complaints of the previous code snippet (other than its toy-like simplicity) is that it does not handle exit conditions and just goes on forever.
    26692620Luckily, the monitor semantics can also be used to clearly enforce a shutdown order in a concise manner:
    2670 \begin{figure}
    2671 \begin{cfa}[caption={Same toy simulator with proper termination condition.},label={lst:engine-v2}]
     2621\begin{figure}[H]
     2622\begin{cfacode}[caption={Same toy simulator with proper termination condition.},label={lst:engine-v2}]
    26722623// Visualization declaration
    26732624thread Renderer {} renderer;
     
    27072658// Call destructor for simulator once simulator finishes
    27082659// Call destructor for renderer to signify shutdown
    2709 \end{cfa}
     2660\end{cfacode}
    27102661\end{figure}
    27112662
     
    27132664As mentioned in section \ref{preemption}, \CFA uses preemptive threads by default but can use fibers on demand.
    27142665Currently, using fibers is done by adding the following line of code to the program~:
    2715 \begin{cfa}
     2666\begin{cfacode}
    27162667unsigned int default_preemption() {
    27172668        return 0;
    27182669}
    2719 \end{cfa}
     2670\end{cfacode}
    27202671This function is called by the kernel to fetch the default preemption rate, where 0 signifies an infinite time-slice, i.e., no preemption.
    27212672However, once clusters are fully implemented, it will be possible to create fibers and \textbf{uthread} in the same system, as in listing \ref{lst:fiber-uthread}
    27222673\begin{figure}
    2723 \lstset{language=CFA,deletedelim=**[is][]{`}{`}}
    2724 \begin{cfa}[caption={Using fibers and \textbf{uthread} side-by-side in \CFA},label={lst:fiber-uthread}]
    2725 // Cluster forward declaration
     2674\begin{cfacode}[caption={Using fibers and \textbf{uthread} side-by-side in \CFA},label={lst:fiber-uthread}]
     2675//Cluster forward declaration
    27262676struct cluster;
    27272677
    2728 // Processor forward declaration
     2678//Processor forward declaration
    27292679struct processor;
    27302680
    2731 // Construct clusters with a preemption rate
     2681//Construct clusters with a preemption rate
    27322682void ?{}(cluster& this, unsigned int rate);
    2733 // Construct processor and add it to cluster
     2683//Construct processor and add it to cluster
    27342684void ?{}(processor& this, cluster& cluster);
    2735 // Construct thread and schedule it on cluster
     2685//Construct thread and schedule it on cluster
    27362686void ?{}(thread& this, cluster& cluster);
    27372687
    2738 // Declare two clusters
    2739 cluster thread_cluster = { 10`ms };                     // Preempt every 10 ms
    2740 cluster fibers_cluster = { 0 };                         // Never preempt
    2741 
    2742 // Construct 4 processors
     2688//Declare two clusters
     2689cluster thread_cluster = { 10`ms };                     //Preempt every 10 ms
     2690cluster fibers_cluster = { 0 };                         //Never preempt
     2691
     2692//Construct 4 processors
    27432693processor processors[4] = {
    27442694        //2 for the thread cluster
     
    27502700};
    27512701
    2752 // Declares thread
     2702//Declares thread
    27532703thread UThread {};
    27542704void ?{}(UThread& this) {
    2755         // Construct underlying thread to automatically
    2756         // be scheduled on the thread cluster
     2705        //Construct underlying thread to automatically
     2706        //be scheduled on the thread cluster
    27572707        (this){ thread_cluster }
    27582708}
     
    27602710void main(UThread & this);
    27612711
    2762 // Declares fibers
     2712//Declares fibers
    27632713thread Fiber {};
    27642714void ?{}(Fiber& this) {
    2765         // Construct underlying thread to automatically
    2766         // be scheduled on the fiber cluster
     2715        //Construct underlying thread to automatically
     2716        //be scheduled on the fiber cluster
    27672717        (this.__thread){ fibers_cluster }
    27682718}
    27692719
    27702720void main(Fiber & this);
    2771 \end{cfa}
     2721\end{cfacode}
    27722722\end{figure}
    27732723
     
    27812731Table \ref{tab:machine} shows the characteristics of the machine used to run the benchmarks.
    27822732All tests were made on this machine.
    2783 \begin{table}
     2733\begin{table}[H]
    27842734\begin{center}
    27852735\begin{tabular}{| l | r | l | r |}
     
    28132763
    28142764\section{Micro Benchmarks}
    2815 All benchmarks are run using the same harness to produce the results, seen as the @BENCH()@ macro in the following examples.
     2765All benchmarks are run using the same harness to produce the results, seen as the \code{BENCH()} macro in the following examples.
    28162766This macro uses the following logic to benchmark the code:
    2817 \begin{cfa}
     2767\begin{pseudo}
    28182768#define BENCH(run, result) \
    28192769        before = gettime(); \
     
    28212771        after  = gettime(); \
    28222772        result = (after - before) / N;
    2823 \end{cfa}
    2824 The method used to get time is @clock_gettime(CLOCK_THREAD_CPUTIME_ID);@.
     2773\end{pseudo}
     2774The method used to get time is \code{clock_gettime(CLOCK_THREAD_CPUTIME_ID);}.
    28252775Each benchmark is using many iterations of a simple call to measure the cost of the call.
    28262776The specific number of iterations depends on the specific benchmark.
     
    28372787\begin{multicols}{2}
    28382788\CFA Coroutines
    2839 \begin{cfa}
     2789\begin{cfacode}
    28402790coroutine GreatSuspender {};
    28412791void main(GreatSuspender& this) {
     
    28532803        printf("%llu\n", result);
    28542804}
    2855 \end{cfa}
     2805\end{cfacode}
    28562806\columnbreak
    28572807\CFA Threads
    2858 \begin{cfa}
     2808\begin{cfacode}
    28592809
    28602810
     
    28722822        printf("%llu\n", result);
    28732823}
    2874 \end{cfa}
     2824\end{cfacode}
    28752825\end{multicols}
    2876 \begin{cfa}[caption={\CFA benchmark code used to measure context-switches for coroutines and threads.},label={lst:ctx-switch}]
    2877 \end{cfa}
     2826\begin{cfacode}[caption={\CFA benchmark code used to measure context-switches for coroutines and threads.},label={lst:ctx-switch}]
     2827\end{cfacode}
    28782828\end{figure}
    28792829
     
    29032853For monitors, the simplest approach is to measure how long it takes to enter and leave a monitor routine.
    29042854Listing \ref{lst:mutex} shows the code for \CFA.
    2905 To put the results in context, the cost of entering a non-inline function and the cost of acquiring and releasing a @pthread_mutex@ lock is also measured.
     2855To put the results in context, the cost of entering a non-inline function and the cost of acquiring and releasing a \code{pthread_mutex} lock is also measured.
    29062856The results can be shown in table \ref{tab:mutex}.
    29072857
    29082858\begin{figure}
    2909 \begin{cfa}[caption={\CFA benchmark code used to measure mutex routines.},label={lst:mutex}]
     2859\begin{cfacode}[caption={\CFA benchmark code used to measure mutex routines.},label={lst:mutex}]
    29102860monitor M {};
    29112861void __attribute__((noinline)) call( M & mutex m /*, m2, m3, m4*/ ) {}
     
    29212871        printf("%llu\n", result);
    29222872}
    2923 \end{cfa}
     2873\end{cfacode}
    29242874\end{figure}
    29252875
     
    29332883FetchAdd + FetchSub                             & 26            & 26            & 0    \\
    29342884Pthreads Mutex Lock                             & 31            & 31.86 & 0.99 \\
    2935 \uC @monitor@ member routine            & 30            & 30            & 0    \\
    2936 \CFA @mutex@ routine, 1 argument        & 41            & 41.57 & 0.9  \\
    2937 \CFA @mutex@ routine, 2 argument        & 76            & 76.96 & 1.57 \\
    2938 \CFA @mutex@ routine, 4 argument        & 145           & 146.68        & 3.85 \\
     2885\uC \code{monitor} member routine               & 30            & 30            & 0    \\
     2886\CFA \code{mutex} routine, 1 argument   & 41            & 41.57 & 0.9  \\
     2887\CFA \code{mutex} routine, 2 argument   & 76            & 76.96 & 1.57 \\
     2888\CFA \code{mutex} routine, 4 argument   & 145           & 146.68        & 3.85 \\
    29392889Java synchronized routine                       & 27            & 28.57 & 2.6  \\
    29402890\hline
     
    29522902
    29532903\begin{figure}
    2954 \begin{cfa}[caption={Benchmark code for internal scheduling},label={lst:int-sched}]
     2904\begin{cfacode}[caption={Benchmark code for internal scheduling},label={lst:int-sched}]
    29552905volatile int go = 0;
    29562906condition c;
     
    29822932        return do_wait(m1);
    29832933}
    2984 \end{cfa}
     2934\end{cfacode}
    29852935\end{figure}
    29862936
     
    29922942\hline
    29932943Pthreads Condition Variable                     & 5902.5        & 6093.29       & 714.78 \\
    2994 \uC @signal@                                    & 322           & 323   & 3.36   \\
    2995 \CFA @signal@, 1 @monitor@      & 352.5 & 353.11        & 3.66   \\
    2996 \CFA @signal@, 2 @monitor@      & 430           & 430.29        & 8.97   \\
    2997 \CFA @signal@, 4 @monitor@      & 594.5 & 606.57        & 18.33  \\
    2998 Java @notify@                           & 13831.5       & 15698.21      & 4782.3 \\
     2944\uC \code{signal}                                       & 322           & 323   & 3.36   \\
     2945\CFA \code{signal}, 1 \code{monitor}    & 352.5 & 353.11        & 3.66   \\
     2946\CFA \code{signal}, 2 \code{monitor}    & 430           & 430.29        & 8.97   \\
     2947\CFA \code{signal}, 4 \code{monitor}    & 594.5 & 606.57        & 18.33  \\
     2948Java \code{notify}                              & 13831.5       & 15698.21      & 4782.3 \\
    29992949\hline
    30002950\end{tabular}
     
    30062956
    30072957\subsection{External Scheduling}
    3008 The Internal scheduling benchmark measures the cost of the @waitfor@ statement (@_Accept@ in \uC).
     2958The Internal scheduling benchmark measures the cost of the \code{waitfor} statement (\code{_Accept} in \uC).
    30092959Listing \ref{lst:ext-sched} shows the code for \CFA, with results in table \ref{tab:ext-sched}.
    30102960As with all other benchmarks, all omitted tests are functionally identical to one of these tests.
    30112961
    30122962\begin{figure}
    3013 \begin{cfa}[caption={Benchmark code for external scheduling},label={lst:ext-sched}]
     2963\begin{cfacode}[caption={Benchmark code for external scheduling},label={lst:ext-sched}]
    30142964volatile int go = 0;
    30152965monitor M {};
     
    30402990        return do_wait(m1);
    30412991}
    3042 \end{cfa}
     2992\end{cfacode}
    30432993\end{figure}
    30442994
     
    30492999\multicolumn{1}{c |}{} & \multicolumn{1}{c |}{ Median } &\multicolumn{1}{c |}{ Average } & \multicolumn{1}{c |}{ Standard Deviation} \\
    30503000\hline
    3051 \uC @Accept@                                    & 350           & 350.61        & 3.11  \\
    3052 \CFA @waitfor@, 1 @monitor@     & 358.5 & 358.36        & 3.82  \\
    3053 \CFA @waitfor@, 2 @monitor@     & 422           & 426.79        & 7.95  \\
    3054 \CFA @waitfor@, 4 @monitor@     & 579.5 & 585.46        & 11.25 \\
     3001\uC \code{Accept}                                       & 350           & 350.61        & 3.11  \\
     3002\CFA \code{waitfor}, 1 \code{monitor}   & 358.5 & 358.36        & 3.82  \\
     3003\CFA \code{waitfor}, 2 \code{monitor}   & 422           & 426.79        & 7.95  \\
     3004\CFA \code{waitfor}, 4 \code{monitor}   & 579.5 & 585.46        & 11.25 \\
    30553005\hline
    30563006\end{tabular}
     
    30633013\subsection{Object Creation}
    30643014Finally, the last benchmark measures the cost of creation for concurrent objects.
    3065 Listing \ref{lst:creation} shows the code for @pthread@s and \CFA threads, with results shown in table \ref{tab:creation}.
     3015Listing \ref{lst:creation} shows the code for \texttt{pthread}s and \CFA threads, with results shown in table \ref{tab:creation}.
    30663016As with all other benchmarks, all omitted tests are functionally identical to one of these tests.
    30673017The only note here is that the call stacks of \CFA coroutines are lazily created, therefore without priming the coroutine, the creation cost is very low.
     
    30693019\begin{figure}
    30703020\begin{center}
    3071 @pthread@
    3072 \begin{cfa}
     3021\texttt{pthread}
     3022\begin{ccode}
    30733023int main() {
    30743024        BENCH(
     
    30893039        printf("%llu\n", result);
    30903040}
    3091 \end{cfa}
     3041\end{ccode}
    30923042
    30933043
    30943044
    30953045\CFA Threads
    3096 \begin{cfa}
     3046\begin{cfacode}
    30973047int main() {
    30983048        BENCH(
     
    31043054        printf("%llu\n", result);
    31053055}
    3106 \end{cfa}
     3056\end{cfacode}
    31073057\end{center}
    3108 \caption{Benchmark code for \protect\lstinline|pthread|s and \CFA to measure object creation}
    3109 \label{lst:creation}
     3058\begin{cfacode}[caption={Benchmark code for \texttt{pthread}s and \CFA to measure object creation},label={lst:creation}]
     3059\end{cfacode}
    31103060\end{figure}
    31113061
     
    31913141\begin{tabular}[t]{|c|c|c|}
    31923142Sequential & Library Parallel & Language Parallel \\
    3193 \begin{cfa}[tabsize=3]
     3143\begin{cfacode}[tabsize=3]
    31943144void big_sum(
    31953145        int* a, int* b,
     
    32153165//... fill in a & b
    32163166big_sum(a,b,c,10000);
    3217 \end{cfa} &\begin{cfa}[tabsize=3]
     3167\end{cfacode} &\begin{cfacode}[tabsize=3]
    32183168void big_sum(
    32193169        int* a, int* b,
     
    32393189//... fill in a & b
    32403190big_sum(a,b,c,10000);
    3241 \end{cfa}&\begin{cfa}[tabsize=3]
     3191\end{cfacode}&\begin{cfacode}[tabsize=3]
    32423192void big_sum(
    32433193        int* a, int* b,
     
    32633213//... fill in a & b
    32643214big_sum(a,b,c,10000);
    3265 \end{cfa}
     3215\end{cfacode}
    32663216\end{tabular}
    32673217\end{center}
  • doc/papers/concurrency/annex/local.bib

    rb5563e1 ra9b1b0c  
    2121@string{pldi="Programming Language Design and Implementation"}
    2222
    23 @inproceedings{Hochstein05,
    24     keywords    = {Application software; Computer aided software engineering; Concurrent computing; Educational
    25                   institutions; High performance computing; Humans; Instruments; Productivity; Programming profession;
    26                   Software engineering},
    27     author      = {Lorin Hochstein and Jeff Carver and Forrest Shull and Sima Asgari and Victor Basili and Jeffrey K. Hollingsworth and Marvin V. Zelkowitz},
    28     title       = {Parallel Programmer Productivity: A Case Study of Novice Parallel Programmers},
    29     booktitle   = {Supercomputing, 2005. Proceedings of the ACM/IEEE SC 2005 Conference},
    30     publisher   = {IEEE},
    31     year        = {2005},
    32     pages       = {35-35},
    33     month       = nov,
     23
     24@article{HPP:Study,
     25        keywords        = {Parallel, Productivity},
     26        author  = {Lorin Hochstein and Jeff Carver and Forrest Shull and Sima Asgari and Victor Basili and Jeffrey K. Hollingsworth and Marvin V. Zelkowitz },
     27        title   = {Parallel Programmer Productivity: A Case Study of Novice Parallel Programmers},
    3428}
    3529
     
    4135}
    4236
    43 @misc{TBB,
    44     keywords    = {Intel, TBB},
    45     key         = {TBB},
    46     title       = {Thread Building Blocks},
    47     howpublished= {Intel, \url{https://www.threadingbuildingblocks.org}},
    48     note        = {Accessed: 2018-3},
     37@article{TBB,
     38        key     = {TBB},
     39        keywords        = {Intel, TBB},
     40        title   = {Intel Thread Building Blocks},
     41        note            = "\url{https://www.threadingbuildingblocks.org/}"
    4942}
    5043
     
    5548        title   = {C$\forall$ Programmming Language},
    5649        note    = {\url{https://plg.uwaterloo.ca/~cforall}},
     50}
     51
     52@mastersthesis{rob-thesis,
     53        keywords        = {Constructors, Destructors, Tuples},
     54        author  = {Rob Schluntz},
     55        title   = {Resource Management and Tuples in Cforall},
     56        year            = 2017,
     57        school  = {University of Waterloo},
     58        note    = {\url{https://uwspace.uwaterloo.ca/handle/10012/11830}},
    5759}
    5860
  • doc/papers/concurrency/style/cfa-format.tex

    rb5563e1 ra9b1b0c  
    1111% from https://gist.github.com/nikolajquorning/92bbbeef32e1dd80105c9bf2daceb89a
    1212\lstdefinelanguage{sml} {
    13         morekeywords= {
    14                 EQUAL, GREATER, LESS, NONE, SOME, abstraction, abstype, and, andalso, array, as, before,
    15                 bool, case, char, datatype, do, else, end, eqtype, exception, exn, false, fn, fun, functor,
    16                 handle, if, in, include, infix, infixr, int, let, list, local, nil, nonfix, not, o, of, op,
    17                 open, option, orelse, overload, print, raise, real, rec, ref, sharing, sig, signature,
    18                 string, struct, structure, substring, then, true, type, unit, val, vector, where, while,
    19                 with, withtype, word
    20     },
    21     morestring=[b]",
    22     morecomment=[s]{(*}{*)},
     13  morekeywords= {
     14    EQUAL, GREATER, LESS, NONE, SOME, abstraction, abstype, and, andalso, array, as, before, bool, case, char, datatype, do, else, end, eqtype, exception, exn, false, fn, fun, functor, handle, if, in, include, infix, infixr, int, let, list, local, nil, nonfix, not, o, of, op, open, option, orelse, overload, print, raise, real, rec, ref, sharing, sig, signature, string, struct, structure, substring, then, true, type, unit, val, vector, where, while, with, withtype, word
     15  },
     16  morestring=[b]",
     17  morecomment=[s]{(*}{*)},
    2318}
    2419
    2520\lstdefinelanguage{D}{
    26         % Keywords
    27         morekeywords=[1]{
    28                 abstract, alias, align, auto, body, break, cast, catch, class, const, continue, debug,
    29                 delegate, delete, deprecated, do, else, enum, export, false, final, finally, for, foreach,
    30                 foreach_reverse, function, goto, if, immutable, import, in, inout, interface, invariant, is,
    31                 lazy, macro, mixin, module, new, nothrow, null, out, override, package, pragma, private,
    32                 protected, public, pure, ref, return, shared, static, struct, super, switch, synchronized,
    33                 template, this, throw, true, try, typedef, typeid, typeof, union, unittest, volatile, while,
    34                 with
    35         },
    36         % Special identifiers, common functions
    37         morekeywords=[2]{enforce},
    38         % Ugly identifiers
    39         morekeywords=[3]{
    40                 __DATE__, __EOF__, __FILE__, __LINE__, __TIMESTAMP__, __TIME__, __VENDOR__,
    41                 __VERSION__, __ctfe, __gshared, __monitor, __thread, __vptr, _argptr,
    42                 _arguments, _ctor, _dtor
    43         },
    44         % Basic types
    45         morekeywords=[4]{
    46                 byte, ubyte, short, ushort, int, uint, long, ulong, cent, ucent, void, bool, bit, float,
    47                 double, real, ushort, int, uint, long, ulong, float, char, wchar, dchar, string, wstring,
    48                 dstring, ireal, ifloat, idouble, creal, cfloat, cdouble, size_t, ptrdiff_t, sizediff_t,
    49                 equals_t, hash_t
    50         },
    51         % Strings
    52         morestring=[b]{"},
    53         morestring=[b]{'},
    54         morestring=[b]{`},
    55         % Comments
    56         comment=[l]{//},
    57         morecomment=[s]{/*}{*/},
    58         morecomment=[s][\color{blue}]{/**}{*/},
    59         morecomment=[n]{/+}{+/},
    60         morecomment=[n][\color{blue}]{/++}{+/},
    61         % Options
    62         sensitive=true
     21  % Keywords
     22  morekeywords=[1]{
     23    abstract, alias, align, auto, body, break, cast, catch, class, const,
     24    continue, debug, delegate, delete, deprecated, do, else, enum, export,
     25    false, final, finally, for, foreach, foreach_reverse, function, goto, if,
     26    immutable, import, in, inout, interface, invariant, is, lazy, macro, mixin,
     27    module, new, nothrow, null, out, override, package, pragma, private,
     28    protected, public, pure, ref, return, shared, static, struct, super,
     29    switch, synchronized, template, this, throw, true, try, typedef, typeid,
     30    typeof, union, unittest, volatile, while, with
     31  },
     32  % Special identifiers, common functions
     33  morekeywords=[2]{enforce},
     34  % Ugly identifiers
     35  morekeywords=[3]{
     36    __DATE__, __EOF__, __FILE__, __LINE__, __TIMESTAMP__, __TIME__, __VENDOR__,
     37    __VERSION__, __ctfe, __gshared, __monitor, __thread, __vptr, _argptr,
     38    _arguments, _ctor, _dtor
     39  },
     40  % Basic types
     41  morekeywords=[4]{
     42     byte, ubyte, short, ushort, int, uint, long, ulong, cent, ucent, void,
     43     bool, bit, float, double, real, ushort, int, uint, long, ulong, float,
     44     char, wchar, dchar, string, wstring, dstring, ireal, ifloat, idouble,
     45     creal, cfloat, cdouble, size_t, ptrdiff_t, sizediff_t, equals_t, hash_t
     46  },
     47  % Strings
     48  morestring=[b]{"},
     49  morestring=[b]{'},
     50  morestring=[b]{`},
     51  % Comments
     52  comment=[l]{//},
     53  morecomment=[s]{/*}{*/},
     54  morecomment=[s][\color{blue}]{/**}{*/},
     55  morecomment=[n]{/+}{+/},
     56  morecomment=[n][\color{blue}]{/++}{+/},
     57  % Options
     58  sensitive=true
    6359}
    6460
    6561\lstdefinelanguage{rust}{
    66         % Keywords
    67         morekeywords=[1]{
    68                 abstract, alignof, as, become, box, break, const, continue, crate, do, else, enum, extern,
    69                 false, final, fn, for, if, impl, in, let, loop, macro, match, mod, move, mut, offsetof,
    70                 override, priv, proc, pub, pure, ref, return, Self, self, sizeof, static, struct, super,
    71                 trait, true, type, typeof, unsafe, unsized, use, virtual, where, while, yield
    72         },
    73         % Strings
    74         morestring=[b]{"},
    75         % Comments
    76         comment=[l]{//},
    77         morecomment=[s]{/*}{*/},
    78         % Options
    79         sensitive=true
     62  % Keywords
     63  morekeywords=[1]{
     64    abstract, alignof, as, become, box,
     65    break, const, continue, crate, do,
     66    else, enum, extern, false, final,
     67    fn, for, if, impl, in,
     68    let, loop, macro, match, mod,
     69    move, mut, offsetof, override, priv,
     70    proc, pub, pure, ref, return,
     71    Self, self, sizeof, static, struct,
     72    super, trait, true,  type, typeof,
     73    unsafe, unsized, use, virtual, where,
     74    while, yield
     75  },
     76  % Strings
     77  morestring=[b]{"},
     78  % Comments
     79  comment=[l]{//},
     80  morecomment=[s]{/*}{*/},
     81  % Options
     82  sensitive=true
    8083}
    8184
    8285\lstdefinelanguage{pseudo}{
    83         morekeywords={string,uint,int,bool,float},
    84         sensitive=true,
    85         morecomment=[l]{//},
    86         morecomment=[s]{/*}{*/},
    87         morestring=[b]',
    88         morestring=[b]",
    89         morestring=[s]{`}{`},
    90 }
     86        morekeywords={string,uint,int,bool,float},%
     87        sensitive=true,%
     88        morecomment=[l]{//},%
     89        morecomment=[s]{/*}{*/},%
     90        morestring=[b]',%
     91        morestring=[b]",%
     92        morestring=[s]{`}{`},%
     93}%
    9194
    9295\newcommand{\KWC}{K-W C\xspace}
    9396
    9497\lstdefinestyle{pseudoStyle}{
    95         escapeinside={@@},
    96         basicstyle=\linespread{0.9}\sf\footnotesize,            % reduce line spacing and use typewriter font
    97         keywordstyle=\bfseries\color{blue},
    98         keywordstyle=[2]\bfseries\color{Plum},
    99         commentstyle=\itshape\color{OliveGreen},                    % green and italic comments
    100         identifierstyle=\color{identifierCol},
    101         stringstyle=\sf\color{Mahogany},                                          % use sanserif font
    102         mathescape=true,
    103         columns=fixed,
    104         aboveskip=4pt,                                                            % spacing above/below code block
    105         belowskip=3pt,
    106         keepspaces=true,
    107         tabsize=4,
    108         % frame=lines,
    109         literate=,
    110         showlines=true,                                                          % show blank lines at end of code
    111         showspaces=false,
    112         showstringspaces=false,
    113         escapechar=\$,
    114         xleftmargin=\parindentlnth,                                  % indent code to paragraph indentation
    115         moredelim=[is][\color{red}\bfseries]{**R**}{**R**},    % red highlighting
    116         % moredelim=* detects keywords, comments, strings, and other delimiters and applies their formatting
    117         % moredelim=** allows cumulative application
     98  escapeinside={@@},
     99  basicstyle=\linespread{0.9}\sf\footnotesize,          % reduce line spacing and use typewriter font
     100  keywordstyle=\bfseries\color{blue},
     101  keywordstyle=[2]\bfseries\color{Plum},
     102  commentstyle=\itshape\color{OliveGreen},                  % green and italic comments
     103  identifierstyle=\color{identifierCol},
     104  stringstyle=\sf\color{Mahogany},                                % use sanserif font
     105  mathescape=true,
     106  columns=fixed,
     107  aboveskip=4pt,                                  % spacing above/below code block
     108  belowskip=3pt,
     109  keepspaces=true,
     110  tabsize=4,
     111  % frame=lines,
     112  literate=,
     113  showlines=true,                                % show blank lines at end of code
     114  showspaces=false,
     115  showstringspaces=false,
     116  escapechar=\$,
     117  xleftmargin=\parindentlnth,                     % indent code to paragraph indentation
     118  moredelim=[is][\color{red}\bfseries]{**R**}{**R**},    % red highlighting
     119  % moredelim=* detects keywords, comments, strings, and other delimiters and applies their formatting
     120  % moredelim=** allows cumulative application
    118121}
    119122
    120123\lstdefinestyle{defaultStyle}{
    121         escapeinside={@@},
    122         basicstyle=\linespread{0.9}\tt\footnotesize,            % reduce line spacing and use typewriter font
    123         keywordstyle=\bfseries\color{blue},
    124         keywordstyle=[2]\bfseries\color{Plum},
    125         commentstyle=\itshape\color{OliveGreen},                    % green and italic comments
    126         identifierstyle=\color{identifierCol},
    127         stringstyle=\sf\color{Mahogany},                                          % use sanserif font
    128         mathescape=true,
    129         columns=fixed,
    130         aboveskip=4pt,                                                            % spacing above/below code block
    131         belowskip=3pt,
    132         keepspaces=true,
    133         tabsize=4,
    134         % frame=lines,
    135         literate=,
    136         showlines=true,                                                          % show blank lines at end of code
    137         showspaces=false,
    138         showstringspaces=false,
    139         escapechar=\$,
    140         xleftmargin=\parindentlnth,                                  % indent code to paragraph indentation
    141         moredelim=[is][\color{red}\bfseries]{**R**}{**R**},    % red highlighting
    142         % moredelim=* detects keywords, comments, strings, and other delimiters and applies their formatting
    143         % moredelim=** allows cumulative application
     124  escapeinside={@@},
     125  basicstyle=\linespread{0.9}\tt\footnotesize,          % reduce line spacing and use typewriter font
     126  keywordstyle=\bfseries\color{blue},
     127  keywordstyle=[2]\bfseries\color{Plum},
     128  commentstyle=\itshape\color{OliveGreen},                  % green and italic comments
     129  identifierstyle=\color{identifierCol},
     130  stringstyle=\sf\color{Mahogany},                                % use sanserif font
     131  mathescape=true,
     132  columns=fixed,
     133  aboveskip=4pt,                                  % spacing above/below code block
     134  belowskip=3pt,
     135  keepspaces=true,
     136  tabsize=4,
     137  % frame=lines,
     138  literate=,
     139  showlines=true,                                % show blank lines at end of code
     140  showspaces=false,
     141  showstringspaces=false,
     142  escapechar=\$,
     143  xleftmargin=\parindentlnth,                     % indent code to paragraph indentation
     144  moredelim=[is][\color{red}\bfseries]{**R**}{**R**},    % red highlighting
     145  % moredelim=* detects keywords, comments, strings, and other delimiters and applies their formatting
     146  % moredelim=** allows cumulative application
    144147}
    145148
    146149\lstdefinestyle{cfaStyle}{
    147         escapeinside={@@},
    148         basicstyle=\linespread{0.9}\sf,         % reduce line spacing and use typewriter font
     150  escapeinside={@@},
     151  basicstyle=\linespread{0.9}\sf,               % reduce line spacing and use typewriter font
    149152%  keywordstyle=\bfseries\color{blue},
    150         keywordstyle=[2]\bfseries\color{red},
     153  keywordstyle=[2]\bfseries\color{red},
    151154%  commentstyle=\sf\itshape\color{OliveGreen},            % green and italic comments
    152         identifierstyle=\color{identifierCol},
    153 %  stringstyle=\sf\color{Mahogany},                                       % use sanserif font
    154         stringstyle=\tt,                                                                                % use typewriter font
    155         mathescape=true,
    156         columns=fixed,
    157         aboveskip=4pt,                                                            % spacing above/below code block
    158         belowskip=3pt,
    159         keepspaces=true,
    160         tabsize=4,
    161         % frame=lines,
    162         literate=,
    163         showlines=true,                                                          % show blank lines at end of code
    164         showspaces=false,
    165         showstringspaces=false,
    166         escapechar=\$,
    167         xleftmargin=\parindentlnth,                                  % indent code to paragraph indentation
    168         moredelim=[is][\color{red}\bfseries]{**R**}{**R**},    % red highlighting
    169         morekeywords=[2]{accept, signal, signal_block, wait, waitfor},
     155  identifierstyle=\color{identifierCol},
     156%  stringstyle=\sf\color{Mahogany},                               % use sanserif font
     157  stringstyle=\tt,                                                                              % use typewriter font
     158  mathescape=true,
     159  columns=fixed,
     160  aboveskip=4pt,                                  % spacing above/below code block
     161  belowskip=3pt,
     162  keepspaces=true,
     163  tabsize=4,
     164  % frame=lines,
     165  literate=,
     166  showlines=true,                                % show blank lines at end of code
     167  showspaces=false,
     168  showstringspaces=false,
     169  escapechar=\$,
     170  xleftmargin=\parindentlnth,                     % indent code to paragraph indentation
     171  moredelim=[is][\color{red}\bfseries]{**R**}{**R**},    % red highlighting
     172  morekeywords=[2]{accept, signal, signal_block, wait, waitfor},
    170173}
    171174
     
    173176
    174177\lstnewenvironment{ccode}[1][]{
    175         \lstset{
    176                 language = C,
    177                 style=defaultStyle,
    178                 captionpos=b,
    179                 #1
    180         }
     178  \lstset{
     179    language = C,
     180    style=defaultStyle,
     181    captionpos=b,
     182    #1
     183  }
    181184}{}
    182185
    183186\lstnewenvironment{cfacode}[1][]{
    184         \lstset{
    185                 language = CFA,
    186                 style=cfaStyle,
    187                 captionpos=b,
    188                 #1
    189         }
     187  \lstset{
     188    language = CFA,
     189    style=cfaStyle,
     190    captionpos=b,
     191    #1
     192  }
    190193}{}
    191194
    192195\lstnewenvironment{pseudo}[1][]{
    193         \lstset{
    194                 language = pseudo,
    195                 style=pseudoStyle,
    196                 captionpos=b,
    197                 #1
    198         }
     196  \lstset{
     197    language = pseudo,
     198    style=pseudoStyle,
     199    captionpos=b,
     200    #1
     201  }
    199202}{}
    200203
    201204\lstnewenvironment{cppcode}[1][]{
    202         \lstset{
    203                 language = c++,
    204                 style=defaultStyle,
    205                 captionpos=b,
    206                 #1
    207         }
     205  \lstset{
     206    language = c++,
     207    style=defaultStyle,
     208    captionpos=b,
     209    #1
     210  }
    208211}{}
    209212
    210213\lstnewenvironment{ucppcode}[1][]{
    211         \lstset{
    212                 language = c++,
    213                 style=defaultStyle,
    214                 captionpos=b,
    215                 #1
    216         }
     214  \lstset{
     215    language = c++,
     216    style=defaultStyle,
     217    captionpos=b,
     218    #1
     219  }
    217220}{}
    218221
    219222\lstnewenvironment{javacode}[1][]{
    220         \lstset{
    221                 language = java,
    222                 style=defaultStyle,
    223                 captionpos=b,
    224                 #1
    225         }
     223  \lstset{
     224    language = java,
     225    style=defaultStyle,
     226    captionpos=b,
     227    #1
     228  }
    226229}{}
    227230
    228231\lstnewenvironment{scalacode}[1][]{
    229         \lstset{
    230                 language = scala,
    231                 style=defaultStyle,
    232                 captionpos=b,
    233                 #1
    234         }
     232  \lstset{
     233    language = scala,
     234    style=defaultStyle,
     235    captionpos=b,
     236    #1
     237  }
    235238}{}
    236239
    237240\lstnewenvironment{smlcode}[1][]{
    238         \lstset{
    239                 language = sml,
    240                 style=defaultStyle,
    241                 captionpos=b,
    242                 #1
    243         }
     241  \lstset{
     242    language = sml,
     243    style=defaultStyle,
     244    captionpos=b,
     245    #1
     246  }
    244247}{}
    245248
    246249\lstnewenvironment{dcode}[1][]{
    247         \lstset{
    248                 language = D,
    249                 style=defaultStyle,
    250                 captionpos=b,
    251                 #1
    252         }
     250  \lstset{
     251    language = D,
     252    style=defaultStyle,
     253    captionpos=b,
     254    #1
     255  }
    253256}{}
    254257
    255258\lstnewenvironment{rustcode}[1][]{
    256         \lstset{
    257                 language = rust,
    258                 style=defaultStyle,
    259                 captionpos=b,
    260                 #1
    261         }
     259  \lstset{
     260    language = rust,
     261    style=defaultStyle,
     262    captionpos=b,
     263    #1
     264  }
    262265}{}
    263266
    264267\lstnewenvironment{gocode}[1][]{
    265         \lstset{
    266                 language = Golang,
    267                 style=defaultStyle,
    268                 captionpos=b,
    269                 #1
    270         }
     268  \lstset{
     269    language = Golang,
     270    style=defaultStyle,
     271    captionpos=b,
     272    #1
     273  }
    271274}{}
    272275
     
    276279\newcommand{\code}[1]{\lstinline[language=CFA,style=cfaStyle]{#1}}
    277280\newcommand{\pscode}[1]{\lstinline[language=pseudo,style=pseudoStyle]{#1}}
    278 
    279 % Local Variables: %
    280 % tab-width: 4 %
    281 % fill-column: 100 %
    282 % End: %
  • doc/papers/general/Paper.tex

    rb5563e1 ra9b1b0c  
    671671\begin{cfa}
    672672int f( int, int );
    673 [int] g( [int, int] );
    674 [int] h( int, [int, int] );
     673int g( [int, int] );
     674int h( int, [int, int] );
    675675[int, int] x;
    676676int y;
     
    706706This example shows mass, multiple, and cascading assignment used in one expression:
    707707\begin{cfa}
    708 [void] f( [int, int] );
     708void f( [int, int] );
    709709f( [x, y] = z = 1.5 );                                          $\C{// assignments in parameter list}$
    710710\end{cfa}
     
    814814Flattening and restructuring conversions are also applied to tuple types in polymorphic type assertions.
    815815\begin{cfa}
    816 [int] f( [int, double], double );
     816int f( [int, double], double );
    817817forall( otype T, otype U | { T f( T, U, U ); } ) void g( T, U );
    818818g( 5, 10.21 );
     
    11521152  case 4:
    11531153        ... `fallthrough common;`
    1154   `common`: // below fallthrough at same level as case clauses
     1154  common: // below fallthrough at same level as case clauses
    11551155        ...      // common code for cases 3 and 4
    11561156        // implicit break
     
    18571857\begin{cfa}
    18581858struct S { double x, y; };
    1859 int x, y;
     1859int i, j;
    18601860void f( int & i, int & j, S & s, int v[] );
    1861 f( 3, x + y, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); $\C{// pass rvalue to lvalue \(\Rightarrow\) implicit temporary}$
     1861f( 3, i + j, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } );   $\C{// pass rvalue to lvalue \(\Rightarrow\) implicit temporary}$
    18621862\end{cfa}
    18631863This allows complex values to be succinctly and efficiently passed to functions, without the syntactic overhead of explicit definition of a temporary variable or the runtime cost of pass-by-value.
     
    19461946
    19471947One of the strengths (and weaknesses) of C is memory-management control, allowing resource release to be precisely specified versus unknown release with garbage-collected memory-management.
    1948 However, this manual approach is verbose, and it is useful to manage resources other than memory (\eg file handles) using the same mechanism as memory.
     1948However, this manual approach is often verbose, and it is useful to manage resources other than memory (\eg file handles) using the same mechanism as memory.
    19491949\CC addresses these issues using Resource Aquisition Is Initialization (RAII), implemented by means of \newterm{constructor} and \newterm{destructor} functions;
    19501950\CFA adopts constructors and destructors (and @finally@) to facilitate RAII.
     
    19981998{
    19991999        VLA  x,            y = { 20, 0x01 },     z = y; $\C{// z points to y}$
    2000         //    ?{}( x );   ?{}( y, 20, 0x01 );    ?{}( z, y );
     2000        //      ?{}( x );  ?{}( y, 20, 0x01 );  ?{}( z, y );
    20012001        ^x{};                                                                   $\C{// deallocate x}$
    20022002        x{};                                                                    $\C{// reallocate x}$
     
    20992099
    21002100For readability, it is useful to associate units to scale literals, \eg weight (stone, pound, kilogram) or time (seconds, minutes, hours).
    2101 The left of Figure~\ref{f:UserLiteral} shows the \CFA alternative call-syntax (postfix: literal argument before function name), using the backquote, to convert basic literals into user literals.
     2101The left of Figure~\ref{f:UserLiteral} shows the \CFA alternative call-syntax (literal argument before function name), using the backquote, to convert basic literals into user literals.
    21022102The backquote is a small character, making the unit (function name) predominate.
    21032103For examples, the multi-precision integer-type in Section~\ref{s:MultiPrecisionIntegers} has user literals:
     
    21072107y = "12345678901234567890123456789"|`mp| + "12345678901234567890123456789"|`mp|;
    21082108\end{cfa}
    2109 Because \CFA uses a standard function, all types and literals are applicable, as well as overloading and conversions, where @?`@ denotes a postfix-function name and @`@ denotes a postfix-function call.
     2109Because \CFA uses a standard function, all types and literals are applicable, as well as overloading and conversions.
    21102110}%
    2111 \begin{cquote}
    2112 \lstset{language=CFA,moredelim=**[is][\color{red}]{|}{|},deletedelim=**[is][]{`}{`}}
    2113 \lstDeleteShortInline@%
    2114 \begin{tabular}{@{}l@{\hspace{2\parindentlnth}}l@{\hspace{2\parindentlnth}}l@{\hspace{2\parindentlnth}}l@{}}
    2115 \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{postfix function}}        & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{constant}}      & \multicolumn{1}{c@{\hspace{2\parindentlnth}}}{\textbf{variable/expression}}   & \multicolumn{1}{c}{\textbf{postfix pointer}}  \\
    2116 \begin{cfa}
    2117 int ?`h( int s );
    2118 int ?`h( double s );
    2119 int ?`m( char c );
    2120 int ?`m( const char * s );
    2121 int ?`t( int a, int b, int c );
    2122 \end{cfa}
    2123 &
    2124 \begin{cfa}
    2125 0 `h;
    2126 3.5`h;
    2127 '1'`m;
    2128 "123" "456"`m;
    2129 [1,2,3]`t;
    2130 \end{cfa}
    2131 &
    2132 \begin{cfa}
    2133 int i = 7;
    2134 i`h;
    2135 (i + 3)`h;
    2136 (i + 3.5)`h;
    2137 
    2138 \end{cfa}
    2139 &
    2140 \begin{cfa}
    2141 int (* ?`p)( int i );
    2142 ?`p = ?`h;
    2143 3`p;
    2144 i`p;
    2145 (i + 3)`p;
    2146 \end{cfa}
    2147 \end{tabular}
    2148 \lstMakeShortInline@%
    2149 \end{cquote}
    21502111
    21512112The right of Figure~\ref{f:UserLiteral} shows the equivalent \CC version using the underscore for the call-syntax.
  • src/Parser/ExpressionNode.cc

    rb5563e1 ra9b1b0c  
    1010// Created On       : Sat May 16 13:17:07 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar 22 11:57:39 2018
    13 // Update Count     : 801
     12// Last Modified On : Sat Mar  3 18:22:33 2018
     13// Update Count     : 796
    1414//
    1515
     
    9494} // checkLNInt
    9595
     96static void sepNumeric( string & str, string & units ) {
     97        string::size_type posn = str.find_first_of( "`" );
     98        if ( posn != string::npos ) {
     99                units = "?" + str.substr( posn );                               // extract units
     100                str.erase( posn );                                                              // remove units
     101        } // if
     102} // sepNumeric
     103
    96104Expression * build_constantInteger( string & str ) {
    97105        static const BasicType::Kind kind[2][6] = {
     
    100108                { BasicType::ShortUnsignedInt, BasicType::UnsignedChar, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::UnsignedInt128, },
    101109        };
     110
     111        string units;
     112        sepNumeric( str, units );                                                       // separate constant from units
    102113
    103114        bool dec = true, Unsigned = false;                                      // decimal, unsigned constant
     
    221232        } // if
    222233  CLEANUP:
     234        if ( units.length() != 0 ) {
     235                ret = new UntypedExpr( new NameExpr( units ), { ret } );
     236        } // if
    223237
    224238        delete &str;                                                                            // created by lex
     
    254268        };
    255269
     270        string units;
     271        sepNumeric( str, units );                                                       // separate constant from units
     272
    256273        bool complx = false;                                                            // real, complex
    257274        int size = 1;                                                                           // 0 => float, 1 => double, 2 => long double
     
    286303        if ( lnth != -1 ) {                                                                     // explicit length ?
    287304                ret = new CastExpr( ret, new BasicType( Type::Qualifiers(), kind[complx][size] ) );
     305        } // if
     306        if ( units.length() != 0 ) {
     307                ret = new UntypedExpr( new NameExpr( units ), { ret } );
    288308        } // if
    289309
  • src/Parser/lex.ll

    rb5563e1 ra9b1b0c  
    1010 * Created On       : Sat Sep 22 08:58:10 2001
    1111 * Last Modified By : Peter A. Buhr
    12  * Last Modified On : Thu Mar 22 16:47:06 2018
    13  * Update Count     : 668
     12 * Last Modified On : Sat Mar  3 18:38:16 2018
     13 * Update Count     : 640
    1414 */
    1515
     
    5454
    5555void rm_underscore() {
    56         // SKULLDUGGERY: remove underscores (ok to shorten?)
     56        // Remove underscores in numeric constant by copying the non-underscore characters to the front of the string.
    5757        yyleng = 0;
    58         for ( int i = 0; yytext[i] != '\0'; i += 1 ) {          // copying non-underscore characters to front of string
     58        for ( int i = 0; yytext[i] != '\0'; i += 1 ) {
     59                if ( yytext[i] == '`' ) {
     60                        // copy user suffix
     61                        for ( ; yytext[i] != '\0'; i += 1 ) {
     62                                yytext[yyleng] = yytext[i];
     63                                yyleng += 1;
     64                        } // for
     65                        break;
     66                } // if
    5967                if ( yytext[i] != '_' ) {
    6068                        yytext[yyleng] = yytext[i];
     
    6371        } // for
    6472        yytext[yyleng] = '\0';
    65 } // rm_underscore
     73}
    6674
    6775// Stop warning due to incorrectly generated flex code.
     
    8290attr_identifier "@"{identifier}
    8391
     92user_suffix_opt ("`"{identifier})?
     93
    8494                                // numeric constants, CFA: '_' in constant
    8595hex_quad {hex}("_"?{hex}){3}
    8696size_opt (8|16|32|64|128)?
    8797length ("ll"|"LL"|[lL]{size_opt})|("hh"|"HH"|[hH])
    88 integer_suffix_opt ("_"?(([uU]({length}?[iI]?)|([iI]{length}))|([iI]({length}?[uU]?)|([uU]{length}))|({length}([iI]?[uU]?)|([uU][iI]))|[zZ]))?
     98integer_suffix_opt ("_"?(([uU]({length}?[iI]?)|([iI]{length}))|([iI]({length}?[uU]?)|([uU]{length}))|({length}([iI]?[uU]?)|([uU][iI]))|[zZ]))?{user_suffix_opt}
    8999
    90100octal_digits ({octal})|({octal}({octal}|"_")*{octal})
     
    108118floating_length ([fFdDlL]|[lL]{floating_size})
    109119floating_suffix ({floating_length}?[iI]?)|([iI]{floating_length})
    110 floating_suffix_opt ("_"?({floating_suffix}|"DL"))?
     120floating_suffix_opt ("_"?({floating_suffix}|"DL"))?{user_suffix_opt}
    111121decimal_digits ({decimal})|({decimal}({decimal}|"_")*{decimal})
    112122floating_decimal {decimal_digits}"."{exponent}?{floating_suffix_opt}
     
    115125
    116126binary_exponent "_"?[pP]"_"?[+-]?{decimal_digits}
    117 hex_floating_suffix_opt ("_"?({floating_suffix}))?
     127hex_floating_suffix_opt ("_"?({floating_suffix}))?{user_suffix_opt}
    118128hex_floating_fraction ({hex_digits}?"."{hex_digits})|({hex_digits}".")
    119129hex_floating_constant {hex_prefix}(({hex_floating_fraction}{binary_exponent})|({hex_digits}{binary_exponent})){hex_floating_suffix_opt}
     
    304314                                /* identifier */
    305315{identifier}    { IDENTIFIER_RETURN(); }
    306 "`"{identifier}"`" {                                                                    // CFA
    307         yytext[yyleng - 1] = '\0'; yytext += 1;                         // SKULLDUGGERY: remove backquotes (ok to shorten?)
    308         IDENTIFIER_RETURN();
    309 }
    310316{attr_identifier} { ATTRIBUTE_RETURN(); }
     317"`"                             { BEGIN BKQUOTE; }
     318<BKQUOTE>{identifier} { IDENTIFIER_RETURN(); }
     319<BKQUOTE>"`"    { BEGIN 0; }
    311320
    312321                                /* numeric constants */
     
    323332({cwide_prefix}[_]?)?['] { BEGIN QUOTE; rm_underscore(); strtext = new string( yytext, yyleng ); }
    324333<QUOTE>[^'\\\n]* { strtext->append( yytext, yyleng ); }
    325 <QUOTE>['\n]    { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(CHARACTERconstant); }
     334<QUOTE>['\n]{user_suffix_opt}   { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(CHARACTERconstant); }
    326335                                /* ' stop editor highlighting */
    327336
     
    329338({swide_prefix}[_]?)?["] { BEGIN STRING; rm_underscore(); strtext = new string( yytext, yyleng ); }
    330339<STRING>[^"\\\n]* { strtext->append( yytext, yyleng ); }
    331 <STRING>["\n]   { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(STRINGliteral); }
     340<STRING>["\n]{user_suffix_opt}  { BEGIN 0; strtext->append( yytext, yyleng ); RETURN_STR(STRINGliteral); }
    332341                                /* " stop editor highlighting */
    333342
     
    339348                                /* punctuation */
    340349"@"                             { ASCIIOP_RETURN(); }
    341 "`"                             { ASCIIOP_RETURN(); }
    342350"["                             { ASCIIOP_RETURN(); }
    343351"]"                             { ASCIIOP_RETURN(); }
     
    404412"?"({op_unary_pre_post}|"()"|"[?]"|"{}") { IDENTIFIER_RETURN(); }
    405413"^?{}"                  { IDENTIFIER_RETURN(); }
    406 "?`"{identifier} { IDENTIFIER_RETURN(); }                               // postfix operator
     414"?`"{identifier} { IDENTIFIER_RETURN(); }                               // unit operator
    407415"?"{op_binary_over}"?"  { IDENTIFIER_RETURN(); }                // binary
    408416        /*
  • src/Parser/parser.yy

    rb5563e1 ra9b1b0c  
    1010// Created On       : Sat Sep  1 20:22:55 2001
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar 22 16:56:21 2018
    13 // Update Count     : 3125
     12// Last Modified On : Fri Mar 16 17:24:44 2018
     13// Update Count     : 3081
    1414//
    1515
     
    126126        } // if
    127127} // rebindForall
    128 
    129 NameExpr * build_postfix_name( const string * name ) {
    130         NameExpr * new_name = build_varref( new string( "?`" + *name ) );
    131         delete name;
    132         return new_name;
    133 } // build_postfix_name
    134128
    135129bool forall = false;                                                                    // aggregate have one or more forall qualifiers ?
     
    486480        | '(' compound_statement ')'                                            // GCC, lambda expression
    487481                { $$ = new ExpressionNode( new StmtExpr( dynamic_cast< CompoundStmt * >(maybeMoveBuild< Statement >($2) ) ) ); }
    488         | constant '`' IDENTIFIER                                                       // CFA, postfix call
    489                 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), $1 ) ); }
    490         | string_literal '`' IDENTIFIER                                         // CFA, postfix call
    491                 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), new ExpressionNode( $1 ) ) ); }
    492         | IDENTIFIER '`' IDENTIFIER                                                     // CFA, postfix call
    493                 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), new ExpressionNode( build_varref( $1 ) ) ) ); }
    494         | tuple '`' IDENTIFIER                                                          // CFA, postfix call
    495                 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $3 ) ), $1 ) ); }
    496         | '(' comma_expression ')' '`' IDENTIFIER                       // CFA, postfix call
    497                 { $$ = new ExpressionNode( build_func( new ExpressionNode( build_postfix_name( $5 ) ), $2 ) ); }
    498482        | type_name '.' no_attr_identifier                                      // CFA, nested type
    499483                { SemanticError( yylloc, "Qualified names are currently unimplemented." ); $$ = nullptr; } // FIX ME
  • src/libcfa/Makefile.am

    rb5563e1 ra9b1b0c  
    1111## Created On       : Sun May 31 08:54:01 2015
    1212## Last Modified By : Peter A. Buhr
    13 ## Last Modified On : Thu Mar 22 17:14:15 2018
    14 ## Update Count     : 224
     13## Last Modified On : Fri Feb  9 15:51:24 2018
     14## Update Count     : 223
    1515###############################################################################
    1616
     
    9999        ${stdhdr}                       \
    100100        math                            \
    101         time                            \
    102101        gmp                             \
    103102        bits/align.h            \
  • src/libcfa/Makefile.in

    rb5563e1 ra9b1b0c  
    263263        containers/result containers/vector concurrency/coroutine \
    264264        concurrency/thread concurrency/kernel concurrency/monitor \
    265         ${shell find stdhdr -type f -printf "%p "} math time gmp \
     265        ${shell find stdhdr -type f -printf "%p "} math gmp \
    266266        bits/align.h bits/cfatime.h bits/containers.h bits/defs.h \
    267267        bits/debug.h bits/locks.h concurrency/invoke.h
     
    435435        ${stdhdr}                       \
    436436        math                            \
    437         time                            \
    438437        gmp                             \
    439438        bits/align.h            \
  • src/tests/coroutine/fibonacci.c

    rb5563e1 ra9b1b0c  
    1010// Created On       : Thu Jun  8 07:29:37 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar 22 22:45:44 2018
    13 // Update Count     : 15
     12// Last Modified On : Tue Dec  5 22:27:54 2017
     13// Update Count     : 14
    1414//
    1515
     
    2121void ?{}( Fibonacci & fib ) with( fib ) { fn = 0; }
    2222
    23 // main automatically called on first resume
    2423void main( Fibonacci & fib ) with( fib ) {
    2524        int fn1, fn2;                                                                           // retained between resumes
    26         fn = 0;  fn1 = fn;                                                                      // 1st case
     25
     26        fn = 0; fn1 = fn;                                                                       // 1st case
    2727        suspend();                                                                                      // restart last resume
    28         fn = 1;  fn2 = fn1;  fn1 = fn;                                          // 2nd case
     28
     29        fn = 1; fn2 = fn1;  fn1 = fn;                                           // 2nd case
    2930        suspend();                                                                                      // restart last resume
     31
    3032        for ( ;; ) {
    3133                fn = fn1 + fn2; fn2 = fn1;  fn1 = fn;                   // general case
Note: See TracChangeset for help on using the changeset viewer.