source: doc/papers/concurrency/examples/PingPong.c @ aaeacf4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since aaeacf4 was 466fa01, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

add diagrams and example programs

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <stdio.h>
2
3typedef struct PingPong {
4        const char * name;
5        struct PingPong * partner;
6        int N, i;
7        void * next;
8} PingPong;
9#define PPCtor( name, N ) { name, NULL, N, 0, NULL }
10void comain( PingPong * pp ) __attribute__(( noinline ));
11void comain( PingPong * pp ) {
12        if ( __builtin_expect(pp->next != 0, 1) ) goto *pp->next;
13#if 0
14        pp->next = &&here;
15                asm( "mov  %0,%%rdi" : "=m" (pp) );
16                asm( "mov  %rdi,%rax" );
17#ifndef OPT
18#ifdef PRINT
19                asm( "add  $16, %rsp" );
20#endif // PRINT
21                asm( "popq %rbp" );
22#endif // ! OPT
23
24#ifdef OPT
25#ifdef PRINT
26                asm( "popq %rbx" );
27#endif // PRINT
28#endif // OPT
29                asm( "jmp  comain" );
30  here: ;
31#endif // 0
32
33        pp->next = &&cycle;
34        for ( ; pp->i < pp->N; pp->i += 1 ) {
35#ifdef PRINT
36                printf( "%s %d\n", pp->name, pp->i );
37#endif // PRINT
38                asm( "mov  %0,%%rdi" : "=m" (pp->partner) );
39                asm( "mov  %rdi,%rax" );
40#ifndef OPT
41#ifdef PRINT
42                asm( "add  $16, %rsp" );
43#endif // PRINT
44                asm( "popq %rbp" );
45#endif // ! OPT
46
47#ifdef OPT
48#ifdef PRINT
49                asm( "popq %rbx" );
50#endif // PRINT
51#endif // OPT
52                asm( "jmp  comain" );
53          cycle: ;
54        } // for
55}
56
57int main() {
58        enum { N =
59#ifdef PRINT
60                   5
61#else
62                   1000000000
63#endif // PRINT
64        };
65        PingPong ping = PPCtor( "ping", N ), pong = PPCtor( "pong", N );
66        ping.partner = &pong;  pong.partner = &ping;
67        comain( &ping );
68}
69
70// Local Variables: //
71// tab-width: 4 //
72// compile-command: "gcc-8 -g -DPRINT PingPong.c" //
73// End: //
Note: See TracBrowser for help on using the repository browser.