source: doc/papers/concurrency/examples/PingPong.cc @ 9fb8f01

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9fb8f01 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.2 KB
Line 
1#include <cstdio>
2
3struct Obj {
4        Obj() { printf( "Obj constructor\n" ); }
5        ~Obj() { printf( "Obj destructor\n" ); }
6        int i = 4;
7};
8
9struct PingPong {
10        const char * name;
11        const int N;
12        PingPong * partner = nullptr;
13        int i = 0;
14        void * next = nullptr;
15
16        PingPong( const char * name, int N ) : name(name), N(N) {}
17
18        void operator()() {
19                if ( __builtin_expect(next != 0, 1) ) goto *next;
20                next = &&cycle;
21                for ( ; i < N; i += 1 ) {
22                        Obj obj;
23                        printf( "X %s %d\n", name, obj.i );
24                        obj.i = 7;
25#ifdef PRINT
26                        printf( "%s %d\n", name, i );
27#endif // PRINT
28                        asm( "mov  %0,%%rdi" : "=m" (partner) );
29                        asm( "mov  %rdi,%rax" );
30#ifndef OPT
31#ifdef PRINT
32                        asm( "add  $40, %rsp" );
33#endif // PRINT
34                        asm( "popq %rbp" );
35#endif // ! OPT
36
37#ifdef OPT
38#ifdef PRINT
39                        asm( "popq %rbx" );
40#endif // PRINT
41#endif // OPT
42                        asm( "jmp  _ZN8PingPongclEv" );
43                  cycle: ;
44                        printf( "Y %s %d\n", name, obj.i );
45                } // for
46        }
47};
48int main() {
49        enum { N =
50#ifdef PRINT
51                   5
52#else
53                   1000000000
54#endif // PRINT
55        };
56        PingPong ping = { "ping", N }, pong = { "pong", N };
57        ping.partner = &pong; pong.partner = &ping;
58        ping();
59}
60
61// Local Variables: //
62// tab-width: 4 //
63// compile-command: "g++-8 -g -DPRINT PingPong.cc" //
64// End: //
Note: See TracBrowser for help on using the repository browser.