Changeset 600d7be
- Timestamp:
- Jun 4, 2019, 6:28:55 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 7261e3c
- Parents:
- 466fa01
- Location:
- doc/papers/concurrency/examples
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/examples/Fib.c
r466fa01 r600d7be 1 1 #include <stdio.h> 2 2 3 #define FIB_INIT { 0, 1 } 4 typedef struct { int fn1, fn; } Fib; 3 typedef struct { 4 int fn1, fn; 5 } Fib; 6 7 #define FibCtor { 1, 0 } 8 5 9 int fib( Fib * f ) { 6 7 int ret = f->fn1; 8 f->fn1 = f->fn; 9 f->fn = ret + f->fn; 10 11 return ret; 10 int fn = f->fn; f->fn = f->fn1; 11 f->fn1 = f->fn + fn; 12 return fn; 12 13 } 13 14 int main() { 14 Fib f1 = F IB_INIT, f2 = FIB_INIT;15 Fib f1 = FibCtor, f2 = FibCtor; 15 16 for ( int i = 0; i < 10; i += 1 ) { 16 17 printf( "%d %d\n", fib( &f1 ), fib( &f2 ) ); -
doc/papers/concurrency/examples/Fib.cfa
r466fa01 r600d7be 13 13 } 14 14 15 #define F IB_INIT{ 0, 1 }16 typedef struct { int fn 1, fn2; } Fib;15 #define FibCtor { 0, 1 } 16 typedef struct { int fn, fn1; } Fib; 17 17 int fib_state( Fib & f ) with( f ) { 18 int ret = fn2; 19 int fn = fn1 + fn2; 20 fn2 = fn1; fn1 = fn; 21 return ret; 18 int fn0 = fn1 + fn2; fn2 = fn1; fn = fn0; 19 return fn1; 22 20 } 23 21 … … 30 28 } 31 29 } 32 int next( Fib1 & fib ) with( fib ) { resume( fib ); returnfn; }30 int ?()( Fib1 & fib ) with( fib ) { return resume( fib ).fn; } 33 31 34 coroutine Fib2 { int ret; };// used for communication32 coroutine Fib2 { int fn; }; // used for communication 35 33 void main( Fib2 & fib ) with( fib ) { // called on first resume 36 int fn , fn1 = 1, fn2 = 0;// precompute first two states34 int fn1 = 1, fn2 = 0; // precompute first two states 37 35 for () { 38 ret = fn2;39 36 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case 40 37 suspend(); // restart last resume 41 38 } 42 39 } 43 int next( Fib2 & fib ) with( fib ) { 44 resume( fib ); // restart last suspend 45 return ret; 40 int ?()( Fib2 & fib ) with( fib ) { 41 return resume( fib ).fn; // restart last suspend 42 } 43 int ?()( Fib2 & fib, int N ) with( fib ) { 44 for ( N - 1 ) fib(); 45 return fib(); 46 } 47 double ?()( Fib2 & fib ) with( fib ) { 48 return (int)(fib()) / 3.14159; // restart last suspend 46 49 } 47 50 … … 51 54 sout | nl; 52 55 53 Fib f1 = F IB_INIT, f2 = FIB_INIT;56 Fib f1 = FibCtor, f2 = FibCtor; 54 57 for ( 10 ) 55 58 sout | fib_state( f1 ) | fib_state( f2 ); … … 58 61 Fib1 f1, f2; 59 62 for ( 10 ) 60 sout | next( f1 ) | next( f2);63 sout | f1() | f2(); 61 64 sout | nl; 62 65 63 Fib2 f1 , f2;66 Fib2 f12, f22; 64 67 for ( 10 ) 65 sout | next( (Fib2 &)f1 ) | next( (Fib2 &)f2 );68 sout | (int)f12() | (double)f12() | f22( 2 ); 66 69 } 67 70 -
doc/papers/concurrency/examples/Fib2.cfa
r466fa01 r600d7be 10 10 // Created On : Thu Apr 26 23:20:08 2018 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Mar 22 17:26:41201913 // Update Count : 2812 // Last Modified On : Sat May 18 08:55:59 2019 13 // Update Count : 36 14 14 // 15 15 … … 17 17 #include <coroutine.hfa> 18 18 19 coroutine Fibonacci { int fn1 ; };// used for communication19 coroutine Fibonacci { int fn1, fn; }; // used for communication 20 20 21 21 void main( Fibonacci & fib ) with( fib ) { // called on first resume 22 int fn; 23 [fn1, fn] = [0, 1]; // precompute first two states 22 [fn1, fn] = [1, 0]; // precompute first two states 24 23 for () { 25 24 suspend(); // restart last resume … … 29 28 30 29 int ?()( Fibonacci & fib ) with( fib ) { // function call operator 31 resume( fib ); // restart last suspend 32 return fn1; 30 return resume( fib ).fn1; // restart last suspend 33 31 } 34 32 … … 36 34 Fibonacci f1, f2; 37 35 for ( 10 ) { // print N Fibonacci values 38 sout | f1() | f2();36 sout | resume( f1 ).fn | resume( f2 ).fn; 39 37 } // for 40 38 } … … 42 40 // Local Variables: // 43 41 // tab-width: 4 // 44 // compile-command: "cfa fibonacci_1.cfa" //42 // compile-command: "cfa Fib2.cfa" // 45 43 // End: //
Note: See TracChangeset
for help on using the changeset viewer.