Ignore:
File:
1 edited

Legend:

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

    ra573c22 r466fa01  
    22
    33typedef struct {
    4         int restart, g, b;
     4        void * next;
    55        char ch;
     6        int g, b;
    67} Fmt;
    78
    89void format( Fmt * f ) {
    9         static void * states[] = {&&s0, &&s1};
    10         goto *states[f->restart];
    11   s0: f->restart = 1;
     10        if ( __builtin_expect(f->next != 0, 1) ) goto *f->next;
     11        f->next = &&s1;
    1212        for ( ;; ) {
    1313                for ( f->g = 0; f->g < 5; f->g += 1 ) {                 // groups
    1414                        for ( f->b = 0; f->b < 4; f->b += 1 ) {         // blocks
    1515                                return;
    16                           s1: if ( f->ch == '\0' ) goto fini;           // EOF ?
     16                          s1: ;
     17                                if ( f->ch == '\0' ) goto fini;                 // EOF ?
    1718                                while ( f->ch == '\n' ) return;                 // ignore
    18 //                              printf( "%c", f->ch );                                  // print character
     19                                printf( "%c", f->ch );                                  // print character
    1920                        }
    20 //                      printf( " " );                                                          // block separator
     21                        printf( " " );                                                          // block separator
    2122                }
    22 //              printf( "\n" );                                                                 // group separator
     23                printf( "\n" );                                                                 // group separator
    2324        }
    24   fini:;
    25 //      if ( f->g != 0 || f->b != 0 ) printf( "\n" );
     25  fini:
     26        if ( f->g != 0 || f->b != 0 ) printf( "\n" );
    2627}
    2728
    2829int main() {
    29         Fmt fmt = { 0 };
     30        Fmt fmt = { NULL };
    3031        format( &fmt );                                                                         // prime
    31         fmt.ch = 'a';
    32         for ( long int i = 0; i < 1000000000; i += 1 ) {
    33 //              scanf( "%c", &fmt.ch );                                                 // direct read into communication variable
    34 //        if ( feof( stdin ) ) break;
     32        for ( ;; ) {
     33                scanf( "%c", &fmt.ch );                                                 // direct read into communication variable
     34          if ( feof( stdin ) ) break;
    3535                format( &fmt );
    3636        }
    37         fmt.ch = '\0';                                                                          // sentential (EOF)
     37        fmt.ch = '\0';
    3838        format( &fmt );
    3939}
Note: See TracChangeset for help on using the changeset viewer.