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

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f673c13c was d7a02ae, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

first complete draft of new concurrency paper

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <stdio.h>
2
3typedef struct PingPong {
4 const char * name;
5 int N, i;
6 struct PingPong * partner;
7 void * next;
8} PingPong;
9#define PPCtor( name, N ) { name, N, 0, NULL, 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.