Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/examples/PingPong.c

    ra573c22 rd7a02ae  
    22
    33typedef struct PingPong {
    4         int restart;                                                                            // style 1
     4        const char * name;
    55        int N, i;
    6         const char * name;
    76        struct PingPong * partner;
    8         void * next;                                                                            // style 2
     7        void * next;
    98} PingPong;
    10 #define PPCtor( name, N ) { 0, N, 0, name, NULL, NULL }
    11 
     9#define PPCtor( name, N ) { name, N, 0, NULL, NULL }
    1210void comain( PingPong * pp ) __attribute__(( noinline ));
    1311void comain( PingPong * pp ) {
     12        if ( __builtin_expect(pp->next != 0, 1) ) goto *pp->next;
    1413#if 0
    15         if ( __builtin_expect(pp->next != 0, 1) ) goto *pp->next;
     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
    1633        pp->next = &&cycle;
    1734        for ( ; pp->i < pp->N; pp->i += 1 ) {
     
    3653          cycle: ;
    3754        } // for
    38 #endif // 0
    39 
    40 #if 1
    41         static void * states[] = {&&s0, &&s1};
    42         goto *states[pp->restart];
    43   s0: pp->restart = 1;
    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" );
    63           s1: ;
    64         } // for
    65 #endif // 0
    6655}
    6756
     
    8170// Local Variables: //
    8271// tab-width: 4 //
    83 // compile-command: "gcc-9 -g -DPRINT PingPong.c" //
     72// compile-command: "gcc-8 -g -DPRINT PingPong.c" //
    8473// End: //
Note: See TracChangeset for help on using the changeset viewer.