Changes in doc/uC++toCFA/uC++toCFA.tex [bf91d1d:e255902b]
- File:
-
- 1 edited
-
doc/uC++toCFA/uC++toCFA.tex (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/uC++toCFA/uC++toCFA.tex
rbf91d1d re255902b 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Tue Oct 22 17:45:48202414 %% Update Count : 6 06813 %% Last Modified On : Fri Nov 15 09:55:34 2024 14 %% Update Count : 6249 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 357 357 358 358 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 359 406 \section{\texorpdfstring{Structures (object-oriented \protect\vs routine style)}{Structures (object-oriented vs. routine style)}} 360 407 … … 381 428 setter( @s,@ 3 ); // normal calls 382 429 int 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> // malloc406 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 }; // fails414 430 \end{cfa} 415 431 \end{tabular} … … 498 514 499 515 500 \section{Coroutine s}516 \section{Coroutine} 501 517 502 518 \begin{cquote} … … 504 520 \begin{uC++} 505 521 506 _CoroutineC {522 @_Coroutine@ C { 507 523 // private coroutine fields 508 524 void main() { 509 ... suspend();...510 ... _Resume E( ... ) _At partner;511 ... uThisCoroutine();...525 ... @suspend();@ ... 526 ... @_Resume E( ... ) _At partner;@ 527 ... @uThisCoroutine();@ ... 512 528 513 529 } 514 530 public: 515 531 void mem( ... ) { 516 ... resume()...532 ... @resume();@ ... 517 533 } 518 534 }; … … 521 537 \begin{cfa} 522 538 #include <$coroutine$.hfa> 523 coroutineC {539 @coroutine@ C { 524 540 // private coroutine fields 525 541 526 542 }; 527 543 void main( C & c ) { 528 ... suspend;... // keyword not routine529 ... resumeAt( partner, ExceptionInst( E, ... ) );530 ... active_coroutine();...544 ... @suspend;@ ... // keyword not routine 545 ... @resumeAt( partner, ExceptionInst( E, ... ) );@ 546 ... @active_coroutine();@ ... 531 547 } 532 548 void mem( C & c, ... ) { 533 ... resume( c );...549 ... @resume( c );@ ... 534 550 } 535 551 \end{cfa} … … 540 556 541 557 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 542 591 \section{\lstinline{COBEGIN}/\lstinline{COFOR}} 543 592 … … 548 597 #include <uCobegin.h> 549 598 int main() { 550 COBEGIN599 @COBEGIN@ 551 600 BEGIN osacquire( cout ) << "A" << endl; END 552 601 BEGIN osacquire( cout ) << "B" << endl; END … … 554 603 BEGIN osacquire( cout ) << "D" << endl; END 555 604 BEGIN osacquire( cout ) << "E" << endl; END 556 COEND557 COFOR( i, 1, 10,605 @COEND@ 606 @COFOR@( i, 1, 10, 558 607 osacquire( cout ) << i << endl; 559 608 ) … … 566 615 int main() { 567 616 { 568 corun{ mutex( sout ) sout | "A"; }617 @corun@ { mutex( sout ) sout | "A"; } 569 618 corun { mutex( sout ) sout | "B"; } 570 619 corun { mutex( sout ) sout | "C"; } … … 572 621 corun { mutex( sout ) sout | "E"; } 573 622 } 574 cofor( i; 10 ) {623 @cofor@( i; 10 ) { 575 624 mutex( sout ) sout | i; 576 625 } … … 591 640 592 641 struct StrMsg : @public uActor::Message@ { 642 593 643 const char * val; // string message 594 595 644 596 645 StrMsg( const char * val ) : … … 600 649 _Actor Hello { ${\color{red}\LstCommentStyle{// : public uActor}}$ 601 650 Allocation receive( Message & msg ) { 602 Case( StrMsg, msg ) { // discriminate 651 Case( @StartMsg@, msg ) { // discriminate 652 653 } else Case( StrMsg, msg ) { 603 654 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 606 659 } 607 660 }; 608 661 int main() { 609 662 @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 613 668 } 614 669 \end{uC++} … … 623 678 const char * val; // string message 624 679 }; 625 void ?{}( StrMsg & msg, char * str ) { 680 void ?{}( StrMsg & msg, const char * str ) { 681 @set_allocation( msg, Delete );@ // delete after use 626 682 msg.val = str; 627 @set_allocation( msg, Delete );@ // delete after use 628 } 629 struct Hello{630 @inline actor;@ // derived actor631 } ;683 } 684 struct Hello { @inline actor;@ }; // derived actor 685 allocation receive( Hello & receiver, @start_msg_t@ & ) { 686 return Nodelete; 687 } 632 688 allocation receive( Hello & receiver, StrMsg & msg ) { 633 689 mutex( sout ) sout | msg.val; 634 return Delete; // delete after use 690 return Nodelete; // reuse actor 691 } 692 allocation receive( Hello & receiver, @stop_msg_t@ & ) { 693 return Delete; // delete actor 635 694 } 636 695 637 696 int 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} 677 705 \end{tabular} 678 706 \end{cquote} … … 710 738 711 739 712 \section{ Monitors}740 \section{Barrier} 713 741 714 742 \begin{cquote} 715 743 \begin{tabular}{@{}l|ll@{}} 716 744 \begin{uC++} 717 718 @_Monitor@ M { 719 @uCondition@ c; 720 bool avail = true; 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; } 721 753 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 }; 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; 726 853 } 727 854 }; … … 730 857 \begin{cfa} 731 858 #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 }; 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} 745 946 \end{tabular} 746 947 \end{cquote}
Note:
See TracChangeset
for help on using the changeset viewer.