Ignore:
File:
1 edited

Legend:

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

    rbf91d1d re255902b  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Tue Oct 22 17:45:48 2024
    14 %% Update Count     : 6068
     13%% Last Modified On : Fri Nov 15 09:55:34 2024
     14%% Update Count     : 6249
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    357357
    358358
     359\section{Constructor / Destructor}
     360
     361\begin{cquote}
     362\begin{tabular}{@{}l|l@{}}
     363\begin{uC++}
     364
     365struct S {
     366        int i, j;
     367
     368        @S@( int i, int j ) { S::i = i; S::j = j; }
     369        @~S@() {}
     370};
     371S s1 = { 1, 2 };
     372
     373S * s2 = new S{ 1, 2 };
     374delete s2;
     375s2 = new S{ 1, 2 };
     376delete s2;
     377S & s3 = *new S{ 1, 2 };
     378delete &s3;
     379s3 = *new S{ 1, 2 };
     380delete &s3;
     381\end{uC++}
     382&
     383\begin{cfa}
     384#include <stdlib.hfa> // new (malloc)
     385struct S {
     386        int i, j;
     387};
     388void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; }
     389void @^?{}@( S & s ) { s.i = 0; s.j = 0; }     
     390
     391S s1 = { 1, 2 };
     392// cannot use 0/1 (zero_t/one_t) with "new"
     393S * s2 = new( 1@n@, 2 ); // n => (int)
     394delete( s2 );
     395s2 = new( 1n, 2 );
     396delete( s2 );
     397S & s3 = *new( 1n, 2 );
     398delete( s3 );
     399&s3 = &*new( 1n, 2 );
     400delete( s3 );
     401\end{cfa}
     402\end{tabular}
     403\end{cquote}
     404
     405
    359406\section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
    360407
     
    381428setter( @s,@ 3 );  // normal calls
    382429int k = getter( @s@ );
    383 \end{cfa}
    384 \end{tabular}
    385 \end{cquote}
    386 
    387 
    388 \section{Constructor / Destructor}
    389 
    390 \begin{cquote}
    391 \begin{tabular}{@{}l|l@{}}
    392 \begin{uC++}
    393 
    394 struct S {
    395         int i, j;
    396         S( int i, int j ) { S::i = i; S::j = j; }
    397         ~S() {}
    398 };
    399 S s = { 1, 2 }, s2{ 1, 2 };
    400 S * s3 = new S{ 1, 2 };
    401 S & s4 = *new S{ 1, 2 };
    402 \end{uC++}
    403 &
    404 \begin{cfa}
    405 #include <stdlib.hfa> // malloc
    406 struct S {
    407         int i, j;
    408 };
    409 void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }
    410 void ^?{}( S & s ) {}
    411 S s = { 1, 2 }, s2{ 1, 2 };
    412 S * s3 = &(*malloc()){ 1, 2 };
    413 S & s4 = (*malloc()){ 1, 2 }; // fails
    414430\end{cfa}
    415431\end{tabular}
     
    498514
    499515
    500 \section{Coroutines}
     516\section{Coroutine}
    501517
    502518\begin{cquote}
     
    504520\begin{uC++}
    505521
    506 _Coroutine C {
     522@_Coroutine@ C {
    507523        // private coroutine fields
    508524        void main() {
    509                 ... suspend(); ...
    510                 ... _Resume E( ... ) _At partner;
    511                 ... uThisCoroutine(); ...
     525                ... @suspend();@ ...
     526                ... @_Resume E( ... ) _At partner;@
     527                ... @uThisCoroutine();@ ...
    512528
    513529        }
    514530  public:
    515531        void mem( ... ) {
    516                 ... resume() ...
     532                ... @resume();@ ...
    517533        }
    518534};
     
    521537\begin{cfa}
    522538#include <$coroutine$.hfa>
    523 coroutine C {
     539@coroutine@ C {
    524540        // private coroutine fields
    525541
    526542};
    527543void main( C & c ) {
    528         ... suspend; ... // keyword not routine
    529         ... resumeAt( partner, ExceptionInst( E, ... ) );
    530         ... active_coroutine(); ...
     544        ... @suspend;@ ... // keyword not routine
     545        ... @resumeAt( partner, ExceptionInst( E, ... ) );@
     546        ... @active_coroutine();@ ...
    531547}
    532548void mem( C & c, ... ) {
    533         ... resume( c ); ...
     549        ... @resume( c );@ ...
    534550}
    535551\end{cfa}
     
    540556
    541557
     558\section{Thread}
     559
     560\begin{cquote}
     561\begin{tabular}{@{}l|ll@{}}
     562\begin{uC++}
     563
     564@_Task@ T {
     565        // private task fields
     566        void main() {
     567                ... @_Resume E( ... ) _At partner@;
     568                ... @uThisTask();@ ...
     569        }
     570  public:
     571};
     572\end{uC++}
     573&
     574\begin{cfa}
     575#include <$thread$.hfa>
     576@thread@ T {
     577        // private task fields
     578
     579};
     580void main( @T & t@ ) {
     581        ... @resumeAt( partner, ExceptionInst( E, ... )@ );
     582        ... @active_thread();@ ...
     583}
     584\end{cfa}
     585\\
     586\multicolumn{2}{@{}l@{}}{\lstinline{T t; // start thread in main routine}}
     587\end{tabular}
     588\end{cquote}
     589
     590
    542591\section{\lstinline{COBEGIN}/\lstinline{COFOR}}
    543592
     
    548597#include <uCobegin.h>
    549598int main() {
    550         COBEGIN
     599        @COBEGIN@
    551600                BEGIN osacquire( cout ) << "A" << endl; END
    552601                BEGIN osacquire( cout ) << "B" << endl; END
     
    554603                BEGIN osacquire( cout ) << "D" << endl; END
    555604                BEGIN osacquire( cout ) << "E" << endl; END
    556         COEND
    557         COFOR( i, 1, 10,
     605        @COEND@
     606        @COFOR@( i, 1, 10,
    558607                osacquire( cout ) << i << endl;
    559608        )
     
    566615int main() {
    567616        {
    568                 corun { mutex( sout ) sout | "A"; }
     617                @corun@ { mutex( sout ) sout | "A"; }
    569618                corun { mutex( sout ) sout | "B"; }
    570619                corun { mutex( sout ) sout | "C"; }
     
    572621                corun { mutex( sout ) sout | "E"; }
    573622        }
    574         cofor( i; 10 ) {
     623        @cofor@( i; 10 ) {
    575624                mutex( sout ) sout | i;
    576625    }
     
    591640
    592641struct StrMsg : @public uActor::Message@ {
     642
    593643        const char * val; // string message
    594 
    595644
    596645        StrMsg( const char * val ) :
     
    600649_Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
    601650        Allocation receive( Message & msg ) {
    602                 Case( StrMsg, msg ) { // discriminate
     651                Case( @StartMsg@, msg ) { // discriminate
     652
     653                } else Case( StrMsg, msg ) {
    603654                        osacquire( cout ) << msg_d->val << endl;
    604                 };
    605                 return Delete;  // delete after use
     655
     656                } else Case( @StopMsg@, msg )
     657                        return Delete;  // delete actor
     658                return Nodelete;  // reuse actor
    606659        }
    607660};
    608661int main() {
    609662        @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
     663        *new Hello() | uActor::startMsg
     664                | *new StrMsg( "hello" ) | uActor::stopMsg;
     665        *new Hello() | uActor::startMsg
     666                | *new StrMsg( "bonjour" ) | uActor::stopMsg;
     667        @uActor::stop();@  // wait for actors to terminate
    613668}
    614669\end{uC++}
     
    623678        const char * val; // string message
    624679};
    625 void ?{}( StrMsg & msg, char * str ) {
     680void ?{}( StrMsg & msg, const char * str ) {
     681        @set_allocation( msg, Delete );@ // delete after use
    626682        msg.val = str;
    627         @set_allocation( msg, Delete );@ // delete after use
    628 }
    629 struct Hello {
    630         @inline actor;@ // derived actor
    631 };
     683}
     684struct Hello { @inline actor;@ }; // derived actor
     685allocation receive( Hello & receiver, @start_msg_t@ & ) {
     686        return Nodelete;
     687}
    632688allocation receive( Hello & receiver, StrMsg & msg ) {
    633689        mutex( sout ) sout | msg.val;
    634         return Delete;  // delete after use
     690        return Nodelete;  // reuse actor
     691}
     692allocation receive( Hello & receiver, @stop_msg_t@ & ) {
     693        return Delete;  // delete actor
    635694}
    636695
    637696int 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 };
    670 void 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}}
     697        @actor_start();@  // start actor system
     698        *(Hello *)new() | start_msg
     699                | *(StrMsg *)new( "hello" ) | stop_msg;
     700        *(Hello *)new() | start_msg
     701                | *(StrMsg *)new( "bonjour" ) | stop_msg;
     702        @actor_stop();@  // wait for actors to terminate
     703}
     704\end{cfa}
    677705\end{tabular}
    678706\end{cquote}
     
    710738
    711739
    712 \section{Monitors}
     740\section{Barrier}
    713741
    714742\begin{cquote}
    715743\begin{tabular}{@{}l|ll@{}}
    716744\begin{uC++}
    717 
    718 @_Monitor@ M {
    719         @uCondition@ c;
    720         bool avail = true;
     745#include <iostream>
     746using namespace std;
     747#include <uBarrier.h>
     748
     749@_Cormonitor@ Barrier
     750                : @public uBarrier@ { // inheritance
     751        int total;
     752        void @last@() { cout << total << endl; }
    721753  public:
    722 
    723         void rtn() {
    724                 if ( ! avail ) c.wait();
    725                 else avail = false;
     754        Barrier( unsigned int group ) :
     755                        @uBarrier( group )@ {
     756                total = 0;
     757        }
     758        void @block@( int subtotal ) {
     759
     760
     761                total += subtotal;
     762                @uBarrier::block();@
     763        }
     764};
     765enum { N = 3 };
     766Barrier b{ N };
     767
     768_Task T {
     769        void main() {
     770                for ( int i = 0; i < 10; i += 1 ) {
     771                        b.block( 1 );
     772                }
     773        }
     774};
     775int main() {
     776        uProcessor p[N - 1];
     777        T t[N];
     778}
     779\end{uC++}
     780&
     781\begin{cfa}
     782#include <fstream.hfa>
     783#include <$thread$.hfa>
     784#include <barrier.hfa>
     785#include <mutex_stmt.hfa>
     786struct Barrier {
     787        @barrier b;@                    // containment
     788        int total;
     789
     790};
     791void ?{}( Barrier & B, unsigned int group ) with(B) {
     792        @?{}( b, group );@              // initialize barrier
     793        total = 0;
     794}
     795unsigned int block( Barrier & B, int subtotal ) with(B) {
     796        void @last@() { sout | total; } // called by Gth arriving thread
     797        @mutex( b )@ {  // use barrier's mutual exclusion
     798                total += subtotal;
     799                return @block@( b, last ); // wait for barrier trigger
     800        }
     801}
     802enum { N = 3 };
     803Barrier b{ N };
     804
     805thread T {};
     806void main( T & ) {
     807        for ( 10 ) {
     808                block( b, 1 );
     809        }
     810}
     811
     812int main() {
     813        processor p[N - 1];
     814        T t[N];
     815}
     816\end{cfa}
     817\end{tabular}
     818\end{cquote}
     819
     820\newpage
     821
     822\section{Monitor}
     823
     824Internal Scheduling
     825\begin{cquote}
     826\begin{tabular}{@{}l|ll@{}}
     827\begin{uC++}
     828
     829@_Monitor@ BoundedBufferI {
     830        @uCondition@ full, empty;
     831        int front = 0, back = 0, count = 0;
     832        int elements[20];
     833  public:
     834
     835
     836
     837        @_Nomutex@ int query() const { return count; }
     838
     839        void insert( int elem ) {
     840                if ( count == 20 ) @empty.wait();@
     841                elements[back] = elem;
     842                back = ( back + 1 ) % 20;
     843                count += 1;
     844                @full.signal();@
     845        }
     846        int remove() {
     847                if ( count == 0 ) @full.wait();@
     848                int elem = elements[front];
     849                front = ( front + 1 ) % 20;
     850                count -= 1;
     851                @empty.signal();@
     852                return elem;
    726853        }
    727854};
     
    730857\begin{cfa}
    731858#include <$monitor$.hfa>
    732 @monitor@ M {
    733         @condition@ c;
    734         bool avail;
    735 };
    736 void ?{}( M & m ) { m.avail = true; }
    737 void rtn( M & m ) with( m ) {
    738         if ( ! avail ) wait( c );
    739         else avail = false;
    740 }
    741 
    742 \end{cfa}
    743 \\
    744 \multicolumn{2}{@{}l@{}}{\lstinline{M m;}}
     859@monitor@ BoundedBufferI {
     860        @condition@ full, empty;
     861        int front, back, count;
     862        int elements[20];
     863};
     864void ?{}( BoundedBufferI & buf ) with( buf ) {
     865        front = back = count = 0;
     866}
     867int query( BoundedBufferI & buf ) { return buf.count; }
     868int remove( BoundedBufferI & @mutex@ buf ); // forward
     869void insert( BoundedBufferI & @mutex@ buf, int elem ) with( buf ) {
     870        if ( count == 20 ) @wait( empty );@
     871        elements[back] = elem;
     872        back = ( back + 1 ) % 20;
     873        count += 1
     874        @signal( full );@
     875}
     876int remove( BoundedBufferI & @mutex@ buf ) with( buf ) {
     877        if ( count == 0 ) @wait( full );@
     878        int elem = elements[front];
     879        front = ( front + 1 ) % 20;
     880        count -= 1;
     881        @signal( empty );@
     882        return elem;
     883}
     884
     885\end{cfa}
     886\end{tabular}
     887\end{cquote}
     888
     889\enlargethispage{1000pt}
     890
     891\noindent
     892External Scheduling
     893\begin{cquote}
     894\begin{tabular}{@{}l|ll@{}}
     895\begin{uC++}
     896
     897_Monitor BoundedBuffer {
     898        int front = 0, back = 0, count = 0;
     899        int elements[20];
     900  public:
     901        _Nomutex int query() const { return count; }
     902        void insert( int elem );
     903        int remove();
     904};
     905
     906void BoundedBuffer::insert( int elem ) {
     907        if ( count == 20 ) @_Accept( remove );@
     908        elements[back] = elem;
     909        back = ( back + 1 ) % 20;
     910        count += 1;
     911}
     912int BoundedBuffer::remove() {
     913        if ( count == 0 ) @_Accept( insert );@
     914        int elem = elements[front];
     915        front = ( front + 1 ) % 20;
     916        count -= 1;
     917        return elem;
     918}
     919\end{uC++}
     920&
     921\begin{cfa}
     922#include <$monitor$.hfa>
     923monitor BoundedBuffer {
     924        int front, back, count;
     925        int elements[20];
     926};
     927void ?{}( BoundedBuffer & buf ) with( buf ) {
     928        front = back = count = 0;
     929}
     930int query( BoundedBuffer & buf ) { return buf.count; }
     931int remove( BoundedBuffer & @mutex@ buf ); // forward
     932void insert( BoundedBuffer & @mutex@ buf, int elem ) with( buf ) {
     933        if ( count == 20 ) @waitfor( remove : buf );@
     934        elements[back] = elem;
     935        back = ( back + 1 ) % 20;
     936        count += 1;
     937}
     938int remove( BoundedBuffer & @mutex@ buf ) with( buf ) {
     939        if ( count == 0 ) @waitfor( insert : buf );@
     940        int elem = elements[front];
     941        front = ( front + 1 ) % 20;
     942        count -= 1;
     943        return elem;
     944}
     945\end{cfa}
    745946\end{tabular}
    746947\end{cquote}
Note: See TracChangeset for help on using the changeset viewer.