ADTast-experimental
Last change
on this file since 9319a23 was
a573c22,
checked in by Peter A. Buhr <pabuhr@…>, 5 years ago
|
example programs updated for concurrency paper
|
-
Property mode set to
100644
|
File size:
1.6 KB
|
Rev | Line | |
---|
[466fa01] | 1 | #include <stdio.h> |
---|
| 2 | |
---|
| 3 | typedef struct PingPong { |
---|
[a573c22] | 4 | int restart; // style 1 |
---|
[466fa01] | 5 | int N, i; |
---|
[a573c22] | 6 | const char * name; |
---|
[d7a02ae] | 7 | struct PingPong * partner; |
---|
[a573c22] | 8 | void * next; // style 2 |
---|
[466fa01] | 9 | } PingPong; |
---|
[a573c22] | 10 | #define PPCtor( name, N ) { 0, N, 0, name, NULL, NULL } |
---|
| 11 | |
---|
[466fa01] | 12 | void comain( PingPong * pp ) __attribute__(( noinline )); |
---|
| 13 | void comain( PingPong * pp ) { |
---|
| 14 | #if 0 |
---|
[a573c22] | 15 | if ( __builtin_expect(pp->next != 0, 1) ) goto *pp->next; |
---|
| 16 | pp->next = &&cycle; |
---|
| 17 | for ( ; pp->i < pp->N; pp->i += 1 ) { |
---|
| 18 | #ifdef PRINT |
---|
| 19 | printf( "%s %d\n", pp->name, pp->i ); |
---|
| 20 | #endif // PRINT |
---|
| 21 | asm( "mov %0,%%rdi" : "=m" (pp->partner) ); |
---|
[466fa01] | 22 | asm( "mov %rdi,%rax" ); |
---|
| 23 | #ifndef OPT |
---|
| 24 | #ifdef PRINT |
---|
| 25 | asm( "add $16, %rsp" ); |
---|
| 26 | #endif // PRINT |
---|
| 27 | asm( "popq %rbp" ); |
---|
| 28 | #endif // ! OPT |
---|
| 29 | |
---|
| 30 | #ifdef OPT |
---|
| 31 | #ifdef PRINT |
---|
| 32 | asm( "popq %rbx" ); |
---|
| 33 | #endif // PRINT |
---|
| 34 | #endif // OPT |
---|
| 35 | asm( "jmp comain" ); |
---|
[a573c22] | 36 | cycle: ; |
---|
| 37 | } // for |
---|
[466fa01] | 38 | #endif // 0 |
---|
| 39 | |
---|
[a573c22] | 40 | #if 1 |
---|
| 41 | static void * states[] = {&&s0, &&s1}; |
---|
| 42 | goto *states[pp->restart]; |
---|
| 43 | s0: pp->restart = 1; |
---|
[466fa01] | 44 | for ( ; pp->i < pp->N; pp->i += 1 ) { |
---|
| 45 | #ifdef PRINT |
---|
| 46 | printf( "%s %d\n", pp->name, pp->i ); |
---|
| 47 | #endif // PRINT |
---|
| 48 | asm( "mov %0,%%rdi" : "=m" (pp->partner) ); |
---|
| 49 | asm( "mov %rdi,%rax" ); |
---|
| 50 | #ifndef OPT |
---|
| 51 | #ifdef PRINT |
---|
| 52 | asm( "add $16, %rsp" ); |
---|
| 53 | #endif // PRINT |
---|
| 54 | asm( "popq %rbp" ); |
---|
| 55 | #endif // ! OPT |
---|
| 56 | |
---|
| 57 | #ifdef OPT |
---|
| 58 | #ifdef PRINT |
---|
| 59 | asm( "popq %rbx" ); |
---|
| 60 | #endif // PRINT |
---|
| 61 | #endif // OPT |
---|
| 62 | asm( "jmp comain" ); |
---|
[a573c22] | 63 | s1: ; |
---|
[466fa01] | 64 | } // for |
---|
[a573c22] | 65 | #endif // 0 |
---|
[466fa01] | 66 | } |
---|
| 67 | |
---|
| 68 | int main() { |
---|
| 69 | enum { N = |
---|
| 70 | #ifdef PRINT |
---|
| 71 | 5 |
---|
| 72 | #else |
---|
| 73 | 1000000000 |
---|
| 74 | #endif // PRINT |
---|
| 75 | }; |
---|
| 76 | PingPong ping = PPCtor( "ping", N ), pong = PPCtor( "pong", N ); |
---|
| 77 | ping.partner = &pong; pong.partner = &ping; |
---|
| 78 | comain( &ping ); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | // Local Variables: // |
---|
| 82 | // tab-width: 4 // |
---|
[a573c22] | 83 | // compile-command: "gcc-9 -g -DPRINT PingPong.c" // |
---|
[466fa01] | 84 | // End: // |
---|
Note: See
TracBrowser
for help on using the repository browser.