Ignore:
Timestamp:
Mar 27, 2019, 9:07:47 AM (6 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
86fb8f2
Parents:
0087e0e
Message:

start rewrite of coroutine section

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/Paper.tex

    r0087e0e r1e5d0f0c  
    654654\centering
    655655\newbox\myboxA
     656% \begin{lrbox}{\myboxA}
     657% \begin{cfa}[aboveskip=0pt,belowskip=0pt]
     658% `int fn1, fn2, state = 1;`   // single global variables
     659% int fib() {
     660%       int fn;
     661%       `switch ( state )` {  // explicit execution state
     662%         case 1: fn = 0;  fn1 = fn;  state = 2;  break;
     663%         case 2: fn = 1;  fn2 = fn1;  fn1 = fn;  state = 3;  break;
     664%         case 3: fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;  break;
     665%       }
     666%       return fn;
     667% }
     668% int main() {
     669%
     670%       for ( int i = 0; i < 10; i += 1 ) {
     671%               printf( "%d\n", fib() );
     672%       }
     673% }
     674% \end{cfa}
     675% \end{lrbox}
    656676\begin{lrbox}{\myboxA}
    657677\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    658 `int fn1, fn2, state = 1;`   // single global variables
    659 int fib() {
    660         int fn;
    661         `switch ( state )` {  // explicit execution state
    662           case 1: fn = 0;  fn1 = fn;  state = 2;  break;
    663           case 2: fn = 1;  fn2 = fn1;  fn1 = fn;  state = 3;  break;
    664           case 3: fn = fn1 + fn2;  fn2 = fn1;  fn1 = fn;  break;
    665         }
    666         return fn;
    667 }
     678#define FIB_INIT { 0, 1 }
     679typedef struct { int fn1, fn; } Fib;
     680int fib( Fib * f ) {
     681
     682        int ret = f->fn1;
     683        f->fn1 = f->fn;
     684        f->fn = ret + f->fn;
     685        return ret;
     686}
     687
     688
     689
    668690int main() {
    669 
     691        Fib f1 = FIB_INIT, f2 = FIB_INIT;
    670692        for ( int i = 0; i < 10; i += 1 ) {
    671                 printf( "%d\n", fib() );
     693                printf( "%d %d\n",
     694                                fib( &f1 ), fib( &f2 ) );
    672695        }
    673696}
     
    678701\begin{lrbox}{\myboxB}
    679702\begin{cfa}[aboveskip=0pt,belowskip=0pt]
    680 #define FIB_INIT `{ 0, 1 }`
    681 typedef struct { int fn2, fn1; } Fib;
    682 int fib( Fib * f ) {
    683 
    684         int ret = f->fn2;
    685         int fn = f->fn1 + f->fn2;
    686         f->fn2 = f->fn1; f->fn1 = fn;
    687 
    688         return ret;
    689 }
    690 int main() {
    691         Fib f1 = FIB_INIT, f2 = FIB_INIT;
    692         for ( int i = 0; i < 10; i += 1 ) {
    693                 printf( "%d %d\n", fib( &fn1 ), fib( &f2 ) );
     703`coroutine` Fib { int fn1; };
     704void main( Fib & fib ) with( fib ) {
     705        int fn;
     706        [fn1, fn] = [0, 1];
     707        for () {
     708                `suspend();`
     709                [fn1, fn] = [fn, fn1 + fn];
    694710        }
    695711}
    696 \end{cfa}
    697 \end{lrbox}
    698 
    699 \subfloat[3 states: global variables]{\label{f:GlobalVariables}\usebox\myboxA}
    700 \qquad
    701 \subfloat[1 state: encapsulated variables]{\label{f:ExternalState}\usebox\myboxB}
    702 \caption{C fibonacci fsm}
    703 \label{f:C-fibonacci}
    704 
    705 \bigskip
    706 
    707 \newbox\myboxA
    708 \begin{lrbox}{\myboxA}
    709 \begin{cfa}[aboveskip=0pt,belowskip=0pt]
    710 `coroutine` Fib { int fn; };
    711 void main( Fib & fib ) with( fib ) {
    712         fn = 0;  int fn1 = fn; `suspend()`;
    713         fn = 1;  int fn2 = fn1;  fn1 = fn; `suspend()`;
    714         for () {
    715                 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; `suspend()`; }
    716 }
    717 int next( Fib & fib ) with( fib ) { `resume( fib );` return fn; }
     712int ?()( Fib & fib ) with( fib ) {
     713        `resume( fib );`  return fn1;
     714}
    718715int main() {
    719716        Fib f1, f2;
    720         for ( 10 )
    721                 sout | next( f1 ) | next( f2 );
    722 }
     717        for ( 10 ) {
     718                sout | f1() | f2();
     719}
     720
     721
    723722\end{cfa}
    724723\end{lrbox}
    725 \newbox\myboxB
    726 \begin{lrbox}{\myboxB}
     724
     725\newbox\myboxC
     726\begin{lrbox}{\myboxC}
    727727\begin{python}[aboveskip=0pt,belowskip=0pt]
    728728
    729 def Fibonacci():
    730         fn = 0; fn1 = fn; `yield fn`  # suspend
    731         fn = 1; fn2 = fn1; fn1 = fn; `yield fn`
    732         while True:
    733                 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; `yield fn`
    734 
    735 
    736 f1 = Fibonacci()
    737 f2 = Fibonacci()
     729def Fib():
     730
     731    fn1, fn = 0, 1
     732    while True:
     733        `yield fn1`
     734        fn1, fn = fn, fn1 + fn
     735
     736
     737// next prewritten
     738
     739
     740f1 = Fib()
     741f2 = Fib()
    738742for i in range( 10 ):
    739         print( `next( f1 )`, `next( f2 )` ) # resume
     743        print( next( f1 ), next( f2 ) )
     744
     745
    740746
    741747\end{python}
    742748\end{lrbox}
    743 \subfloat[\CFA]{\label{f:Coroutine3States}\usebox\myboxA}
    744 \qquad
    745 \subfloat[Python]{\label{f:Coroutine1State}\usebox\myboxB}
    746 \caption{Fibonacci input coroutine, 3 states, internal variables}
    747 \label{f:cfa-fibonacci}
     749
     750\subfloat[C]{\label{f:GlobalVariables}\usebox\myboxA}
     751\hspace{3pt}
     752\vrule
     753\hspace{3pt}
     754\subfloat[\CFA]{\label{f:ExternalState}\usebox\myboxB}
     755\hspace{3pt}
     756\vrule
     757\hspace{3pt}
     758\subfloat[Python]{\label{f:ExternalState}\usebox\myboxC}
     759\caption{Fibonacci Generator}
     760\label{f:C-fibonacci}
     761
     762% \bigskip
     763%
     764% \newbox\myboxA
     765% \begin{lrbox}{\myboxA}
     766% \begin{cfa}[aboveskip=0pt,belowskip=0pt]
     767% `coroutine` Fib { int fn; };
     768% void main( Fib & fib ) with( fib ) {
     769%       fn = 0;  int fn1 = fn; `suspend()`;
     770%       fn = 1;  int fn2 = fn1;  fn1 = fn; `suspend()`;
     771%       for () {
     772%               fn = fn1 + fn2; fn2 = fn1; fn1 = fn; `suspend()`; }
     773% }
     774% int next( Fib & fib ) with( fib ) { `resume( fib );` return fn; }
     775% int main() {
     776%       Fib f1, f2;
     777%       for ( 10 )
     778%               sout | next( f1 ) | next( f2 );
     779% }
     780% \end{cfa}
     781% \end{lrbox}
     782% \newbox\myboxB
     783% \begin{lrbox}{\myboxB}
     784% \begin{python}[aboveskip=0pt,belowskip=0pt]
     785%
     786% def Fibonacci():
     787%       fn = 0; fn1 = fn; `yield fn`  # suspend
     788%       fn = 1; fn2 = fn1; fn1 = fn; `yield fn`
     789%       while True:
     790%               fn = fn1 + fn2; fn2 = fn1; fn1 = fn; `yield fn`
     791%
     792%
     793% f1 = Fibonacci()
     794% f2 = Fibonacci()
     795% for i in range( 10 ):
     796%       print( `next( f1 )`, `next( f2 )` ) # resume
     797%
     798% \end{python}
     799% \end{lrbox}
     800% \subfloat[\CFA]{\label{f:Coroutine3States}\usebox\myboxA}
     801% \qquad
     802% \subfloat[Python]{\label{f:Coroutine1State}\usebox\myboxB}
     803% \caption{Fibonacci input coroutine, 3 states, internal variables}
     804% \label{f:cfa-fibonacci}
    748805\end{figure}
    749806
Note: See TracChangeset for help on using the changeset viewer.