Changeset bf91d1d for doc/uC++toCFA


Ignore:
Timestamp:
Oct 26, 2024, 6:51:04 PM (8 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
33474e6, 63cf80e
Parents:
14c31eb
Message:

numerous changes to the uC++ to CFA cheat sheet

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/uC++toCFA/uC++toCFA.tex

    r14c31eb rbf91d1d  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Wed Sep 18 21:35:47 2024
    14 %% Update Count     : 5999
     13%% Last Modified On : Tue Oct 22 17:45:48 2024
     14%% Update Count     : 6068
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    183183struct S { int i; int j; double m; };  // field i has same type in structures S and T
    184184struct T { int i; int k; int m; };
    185 void foo( S s, T t ) @with(s, t)@ {   // open structure scope s and t in parallel
     185void foo( S s, T t ) @with( s, t )@ {   // open structure scope s and t in parallel
    186186        j + k;                          $\C[1.6in]{// unambiguous, s.j + t.k}$
    187187        m = 5.0;                        $\C{// unambiguous, s.m = 5.0}$
     
    357357
    358358
     359\section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
     360
     361\begin{cquote}
     362\begin{tabular}{@{}l|l@{}}
     363\begin{uC++}
     364struct S {
     365        int i = 0;  // cheat, implicit default constructor
     366        int setter( int j ) { int t = i; i = j; return t; }
     367        int getter() { return i; }
     368};
     369S s;
     370@s.@setter( 3 );  // object calls
     371int k = @s.@getter();
     372\end{uC++}
     373&
     374\begin{cfa}
     375struct S {  int i;  };
     376void ?{}( S & s ) { s.i = 0; } // explicit default constructor
     377int setter( @S & s,@ int j ) @with( s )@ { int t = i; i = j; return t; }
     378int getter( @S & s@ ) @with( s )@ { return i; }
     379
     380S s;
     381setter( @s,@ 3 );  // normal calls
     382int k = getter( @s@ );
     383\end{cfa}
     384\end{tabular}
     385\end{cquote}
     386
     387
    359388\section{Constructor / Destructor}
    360389
     
    362391\begin{tabular}{@{}l|l@{}}
    363392\begin{uC++}
     393
    364394struct S {
    365         ... // fields
    366         @S@( ... ) { ... }
    367         @~S@( ... ) { ... }
    368 };
    369 \end{uC++}
    370 &
    371 \begin{cfa}
     395        int i, j;
     396        S( int i, int j ) { S::i = i; S::j = j; }
     397        ~S() {}
     398};
     399S s = { 1, 2 }, s2{ 1, 2 };
     400S * s3 = new S{ 1, 2 };
     401S & s4 = *new S{ 1, 2 };
     402\end{uC++}
     403&
     404\begin{cfa}
     405#include <stdlib.hfa> // malloc
    372406struct S {
    373         ... // fields
    374 };
    375 @?{}@( @S & s,@ ... ) { ... }
    376 @^?{}@( @S & s@ ) { ... }
     407        int i, j;
     408};
     409void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }
     410void ^?{}( S & s ) {}
     411S s = { 1, 2 }, s2{ 1, 2 };
     412S * s3 = &(*malloc()){ 1, 2 };
     413S & s4 = (*malloc()){ 1, 2 }; // fails
    377414\end{cfa}
    378415\end{tabular}
     
    422459
    423460
    424 \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
    425 
    426 \begin{cquote}
    427 \begin{tabular}{@{}l|l@{}}
    428 \begin{uC++}
    429 struct S {
    430         int i = 0;  // cheat, implicit default constructor
    431         int setter( int j ) { int t = i; i = j; return t; }
    432         int getter() { return i; }
    433 };
    434 
    435 S s;
    436 @s.@setter( 3 );  // object calls
    437 int k = @s.@getter();
    438 \end{uC++}
    439 &
    440 \begin{cfa}
    441 struct S {
    442         int i;
    443 };
    444 void ?{}( S & s ) { s.i = 0; } // explicit default constructor
    445 int setter( @S & s,@ int j ) @with(s)@ { int t = i; i = j; return t; }
    446 int getter( @S & s@ ) @with(s)@ { return i; }
    447 S s;
    448 setter( @s,@ 3 );  // normal calls
    449 int k = getter( @s@ );
    450 \end{cfa}
    451 \end{tabular}
    452 \end{cquote}
    453 
    454 
    455461\section{\texorpdfstring{\lstinline{uArray}}{uArray}}
    456462
     
    503509                ... suspend(); ...
    504510                ... _Resume E( ... ) _At partner;
     511                ... uThisCoroutine(); ...
     512
    505513        }
    506514  public:
     
    520528        ... suspend; ... // keyword not routine
    521529        ... resumeAt( partner, ExceptionInst( E, ... ) );
     530        ... active_coroutine(); ...
    522531}
    523532void mem( C & c, ... ) {
     
    531540
    532541
     542\section{\lstinline{COBEGIN}/\lstinline{COFOR}}
     543
     544\begin{cquote}
     545\begin{tabular}{@{}l|ll@{}}
     546\begin{uC++}
     547
     548#include <uCobegin.h>
     549int main() {
     550        COBEGIN
     551                BEGIN osacquire( cout ) << "A" << endl; END
     552                BEGIN osacquire( cout ) << "B" << endl; END
     553                BEGIN osacquire( cout ) << "C" << endl; END
     554                BEGIN osacquire( cout ) << "D" << endl; END
     555                BEGIN osacquire( cout ) << "E" << endl; END
     556        COEND
     557        COFOR( i, 1, 10,
     558                osacquire( cout ) << i << endl;
     559        )
     560}
     561\end{uC++}
     562&
     563\begin{cfa}
     564#include <mutex_stmt.hfa>
     565#include <$cofor$.hfa>
     566int main() {
     567        {
     568                corun { mutex( sout ) sout | "A"; }
     569                corun { mutex( sout ) sout | "B"; }
     570                corun { mutex( sout ) sout | "C"; }
     571                corun { mutex( sout ) sout | "D"; }
     572                corun { mutex( sout ) sout | "E"; }
     573        }
     574        cofor( i; 10 ) {
     575                mutex( sout ) sout | i;
     576    }
     577}
     578\end{cfa}
     579\end{tabular}
     580\end{cquote}
     581
     582
     583\section{Actor}
     584
     585\begin{cquote}
     586\begin{tabular}{@{}l|ll@{}}
     587\begin{uC++}
     588#include <iostream>
     589using namespace std;
     590#include <uActor.h>
     591
     592struct StrMsg : @public uActor::Message@ {
     593        const char * val; // string message
     594
     595
     596        StrMsg( const char * val ) :
     597                @Message( uActor::Delete )@, // delete after use
     598                val( val ) {}
     599};
     600_Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
     601        Allocation receive( Message & msg ) {
     602                Case( StrMsg, msg ) { // discriminate
     603                        osacquire( cout ) << msg_d->val << endl;
     604                };
     605                return Delete;  // delete after use
     606        }
     607};
     608int main() {
     609        @uActor::start();@ // start actor system
     610        *new Hello() | *new StrMsg( "hello" );
     611        *new Hello() | *new StrMsg( "bonjour" );
     612        @uActor::stop();@  // wait for all actors to terminate
     613}
     614\end{uC++}
     615&
     616\begin{cfa}
     617#include <fstream.hfa>
     618#include <mutex_stmt.hfa>
     619#include <actor.hfa>
     620
     621struct StrMsg {
     622        @inline message;@ // derived message
     623        const char * val; // string message
     624};
     625void ?{}( StrMsg & msg, char * str ) {
     626        msg.val = str;
     627        @set_allocation( msg, Delete );@ // delete after use
     628}
     629struct Hello {
     630        @inline actor;@ // derived actor
     631};
     632allocation receive( Hello & receiver, StrMsg & msg ) {
     633        mutex( sout ) sout | msg.val;
     634        return Delete;  // delete after use
     635}
     636
     637int main() {
     638        @start_actor_system();@  // start actor system
     639        *(Hello *)new() | *(StrMsg *)new( "hello" );
     640        *(Hello *)new() | *(StrMsg *)new( "bonjour" );
     641        @stop_actor_system();@  // wait for all actors to terminate
     642}
     643\end{cfa}
     644\end{tabular}
     645\end{cquote}
     646
     647
     648\section{Threads}
     649
     650\begin{cquote}
     651\begin{tabular}{@{}l|ll@{}}
     652\begin{uC++}
     653
     654@_Task@ T {
     655        // private task fields
     656        void main() {
     657                ... _Resume E( ... ) _At partner;
     658                ... uThisTask(); ...
     659        }
     660  public:
     661};
     662\end{uC++}
     663&
     664\begin{cfa}
     665#include <$thread$.hfa>
     666@thread@ T {
     667        // private task fields
     668
     669};
     670void main( @T & t@ ) {
     671        ... resumeAt( partner, ExceptionInst( E, ... ) );
     672        ... active_thread(); ...
     673}
     674\end{cfa}
     675\\
     676\multicolumn{2}{@{}l@{}}{\lstinline{T t; // start thread in main routine}}
     677\end{tabular}
     678\end{cquote}
     679
     680
    533681\section{Locks}
    534682
     
    539687uOwnerLock m;
    540688uCondLock s;
    541 bool avail = true;
    542689m.acquire();
    543 if ( ! avail ) s.wait( m );
     690if ( ! s.empty() ) s.wait( m );
    544691else {
    545         avail = false;
    546692        m.release();
    547693}
     
    552698#include <locks.hfa>
    553699owner_lock m;
    554 condition_variable( owner_lock ) s;
    555 bool avail = true;
     700condition_variable( owner_lock ) s;  // generic type on mutex lock
    556701lock( m );
    557 if ( ! avail ) wait( s, m );
     702if ( ! empty( s ) ) wait( s, m );
    558703else {
    559         avail = false;
    560704        unlock( m );
    561705}
     
    599743\\
    600744\multicolumn{2}{@{}l@{}}{\lstinline{M m;}}
    601 \end{tabular}
    602 \end{cquote}
    603 
    604 
    605 \section{Threads}
    606 
    607 \begin{cquote}
    608 \begin{tabular}{@{}l|ll@{}}
    609 \begin{uC++}
    610 
    611 @_Task@ T {
    612         // private task fields
    613         void main() {
    614                 ... _Resume E( ... ) _At partner;
    615         }
    616   public:
    617 };
    618 \end{uC++}
    619 &
    620 \begin{cfa}
    621 #include <$thread$.hfa>
    622 @thread@ T {
    623         // private task fields
    624 
    625 };
    626 void main( @T & t@ ) {
    627         ... resumeAt( partner, ExceptionInst( E, ... ) );
    628 }
    629 \end{cfa}
    630 \\
    631 \multicolumn{2}{@{}l@{}}{\lstinline{T t; // start thread in main routine}}
    632745\end{tabular}
    633746\end{cquote}
Note: See TracChangeset for help on using the changeset viewer.