Ignore:
File:
1 edited

Legend:

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

    re255902b rbf91d1d  
    1111%% Created On       : Wed Apr  6 14:53:29 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Fri Nov 15 09:55:34 2024
    14 %% Update Count     : 6249
     13%% Last Modified On : Tue Oct 22 17:45:48 2024
     14%% Update Count     : 6068
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    357357
    358358
    359 \section{Constructor / Destructor}
    360 
    361 \begin{cquote}
    362 \begin{tabular}{@{}l|l@{}}
    363 \begin{uC++}
    364 
    365 struct S {
    366         int i, j;
    367 
    368         @S@( int i, int j ) { S::i = i; S::j = j; }
    369         @~S@() {}
    370 };
    371 S s1 = { 1, 2 };
    372 
    373 S * s2 = new S{ 1, 2 };
    374 delete s2;
    375 s2 = new S{ 1, 2 };
    376 delete s2;
    377 S & s3 = *new S{ 1, 2 };
    378 delete &s3;
    379 s3 = *new S{ 1, 2 };
    380 delete &s3;
    381 \end{uC++}
    382 &
    383 \begin{cfa}
    384 #include <stdlib.hfa> // new (malloc)
    385 struct S {
    386         int i, j;
    387 };
    388 void @?{}@( S & s, int i, int j ) { s.i = i; s.j = j; }
    389 void @^?{}@( S & s ) { s.i = 0; s.j = 0; }     
    390 
    391 S s1 = { 1, 2 };
    392 // cannot use 0/1 (zero_t/one_t) with "new"
    393 S * s2 = new( 1@n@, 2 ); // n => (int)
    394 delete( s2 );
    395 s2 = new( 1n, 2 );
    396 delete( s2 );
    397 S & s3 = *new( 1n, 2 );
    398 delete( s3 );
    399 &s3 = &*new( 1n, 2 );
    400 delete( s3 );
    401 \end{cfa}
    402 \end{tabular}
    403 \end{cquote}
    404 
    405 
    406359\section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}}
    407360
     
    428381setter( @s,@ 3 );  // normal calls
    429382int 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
     394struct S {
     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
     406struct 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
    430414\end{cfa}
    431415\end{tabular}
     
    514498
    515499
    516 \section{Coroutine}
     500\section{Coroutines}
    517501
    518502\begin{cquote}
     
    520504\begin{uC++}
    521505
    522 @_Coroutine@ C {
     506_Coroutine C {
    523507        // private coroutine fields
    524508        void main() {
    525                 ... @suspend();@ ...
    526                 ... @_Resume E( ... ) _At partner;@
    527                 ... @uThisCoroutine();@ ...
     509                ... suspend(); ...
     510                ... _Resume E( ... ) _At partner;
     511                ... uThisCoroutine(); ...
    528512
    529513        }
    530514  public:
    531515        void mem( ... ) {
    532                 ... @resume();@ ...
     516                ... resume() ...
    533517        }
    534518};
     
    537521\begin{cfa}
    538522#include <$coroutine$.hfa>
    539 @coroutine@ C {
     523coroutine C {
    540524        // private coroutine fields
    541525
    542526};
    543527void main( C & c ) {
    544         ... @suspend;@ ... // keyword not routine
    545         ... @resumeAt( partner, ExceptionInst( E, ... ) );@
    546         ... @active_coroutine();@ ...
     528        ... suspend; ... // keyword not routine
     529        ... resumeAt( partner, ExceptionInst( E, ... ) );
     530        ... active_coroutine(); ...
    547531}
    548532void mem( C & c, ... ) {
    549         ... @resume( c );@ ...
     533        ... resume( c ); ...
    550534}
    551535\end{cfa}
     
    556540
    557541
    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 };
    580 void 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 
    591542\section{\lstinline{COBEGIN}/\lstinline{COFOR}}
    592543
     
    597548#include <uCobegin.h>
    598549int main() {
    599         @COBEGIN@
     550        COBEGIN
    600551                BEGIN osacquire( cout ) << "A" << endl; END
    601552                BEGIN osacquire( cout ) << "B" << endl; END
     
    603554                BEGIN osacquire( cout ) << "D" << endl; END
    604555                BEGIN osacquire( cout ) << "E" << endl; END
    605         @COEND@
    606         @COFOR@( i, 1, 10,
     556        COEND
     557        COFOR( i, 1, 10,
    607558                osacquire( cout ) << i << endl;
    608559        )
     
    615566int main() {
    616567        {
    617                 @corun@ { mutex( sout ) sout | "A"; }
     568                corun { mutex( sout ) sout | "A"; }
    618569                corun { mutex( sout ) sout | "B"; }
    619570                corun { mutex( sout ) sout | "C"; }
     
    621572                corun { mutex( sout ) sout | "E"; }
    622573        }
    623         @cofor@( i; 10 ) {
     574        cofor( i; 10 ) {
    624575                mutex( sout ) sout | i;
    625576    }
     
    640591
    641592struct StrMsg : @public uActor::Message@ {
    642 
    643593        const char * val; // string message
     594
    644595
    645596        StrMsg( const char * val ) :
     
    649600_Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$
    650601        Allocation receive( Message & msg ) {
    651                 Case( @StartMsg@, msg ) { // discriminate
    652 
    653                 } else Case( StrMsg, msg ) {
     602                Case( StrMsg, msg ) { // discriminate
    654603                        osacquire( cout ) << msg_d->val << endl;
    655 
    656                 } else Case( @StopMsg@, msg )
    657                         return Delete;  // delete actor
    658                 return Nodelete;  // reuse actor
     604                };
     605                return Delete;  // delete after use
    659606        }
    660607};
    661608int main() {
    662609        @uActor::start();@ // start actor system
    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
     610        *new Hello() | *new StrMsg( "hello" );
     611        *new Hello() | *new StrMsg( "bonjour" );
     612        @uActor::stop();@  // wait for all actors to terminate
    668613}
    669614\end{uC++}
     
    678623        const char * val; // string message
    679624};
    680 void ?{}( StrMsg & msg, const char * str ) {
     625void ?{}( StrMsg & msg, char * str ) {
     626        msg.val = str;
    681627        @set_allocation( msg, Delete );@ // delete after use
    682         msg.val = str;
    683 }
    684 struct Hello { @inline actor;@ }; // derived actor
    685 allocation receive( Hello & receiver, @start_msg_t@ & ) {
    686         return Nodelete;
    687 }
     628}
     629struct Hello {
     630        @inline actor;@ // derived actor
     631};
    688632allocation receive( Hello & receiver, StrMsg & msg ) {
    689633        mutex( sout ) sout | msg.val;
    690         return Nodelete;  // reuse actor
    691 }
    692 allocation receive( Hello & receiver, @stop_msg_t@ & ) {
    693         return Delete;  // delete actor
     634        return Delete;  // delete after use
    694635}
    695636
    696637int main() {
    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}
     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}}
    705677\end{tabular}
    706678\end{cquote}
     
    738710
    739711
    740 \section{Barrier}
     712\section{Monitors}
    741713
    742714\begin{cquote}
    743715\begin{tabular}{@{}l|ll@{}}
    744716\begin{uC++}
    745 #include <iostream>
    746 using namespace std;
    747 #include <uBarrier.h>
    748 
    749 @_Cormonitor@ Barrier
    750                 : @public uBarrier@ { // inheritance
    751         int total;
    752         void @last@() { cout << total << endl; }
     717
     718@_Monitor@ M {
     719        @uCondition@ c;
     720        bool avail = true;
    753721  public:
    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 };
    765 enum { N = 3 };
    766 Barrier 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 };
    775 int 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>
    786 struct Barrier {
    787         @barrier b;@                    // containment
    788         int total;
    789 
    790 };
    791 void ?{}( Barrier & B, unsigned int group ) with(B) {
    792         @?{}( b, group );@              // initialize barrier
    793         total = 0;
    794 }
    795 unsigned 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 }
    802 enum { N = 3 };
    803 Barrier b{ N };
    804 
    805 thread T {};
    806 void main( T & ) {
    807         for ( 10 ) {
    808                 block( b, 1 );
    809         }
    810 }
    811 
    812 int 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 
    824 Internal 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;
     722
     723        void rtn() {
     724                if ( ! avail ) c.wait();
     725                else avail = false;
    853726        }
    854727};
     
    857730\begin{cfa}
    858731#include <$monitor$.hfa>
    859 @monitor@ BoundedBufferI {
    860         @condition@ full, empty;
    861         int front, back, count;
    862         int elements[20];
    863 };
    864 void ?{}( BoundedBufferI & buf ) with( buf ) {
    865         front = back = count = 0;
    866 }
    867 int query( BoundedBufferI & buf ) { return buf.count; }
    868 int remove( BoundedBufferI & @mutex@ buf ); // forward
    869 void 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 }
    876 int 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
    892 External 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 
    906 void BoundedBuffer::insert( int elem ) {
    907         if ( count == 20 ) @_Accept( remove );@
    908         elements[back] = elem;
    909         back = ( back + 1 ) % 20;
    910         count += 1;
    911 }
    912 int 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>
    923 monitor BoundedBuffer {
    924         int front, back, count;
    925         int elements[20];
    926 };
    927 void ?{}( BoundedBuffer & buf ) with( buf ) {
    928         front = back = count = 0;
    929 }
    930 int query( BoundedBuffer & buf ) { return buf.count; }
    931 int remove( BoundedBuffer & @mutex@ buf ); // forward
    932 void 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 }
    938 int 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}
     732@monitor@ M {
     733        @condition@ c;
     734        bool avail;
     735};
     736void ?{}( M & m ) { m.avail = true; }
     737void 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;}}
    946745\end{tabular}
    947746\end{cquote}
Note: See TracChangeset for help on using the changeset viewer.