| Last change
 on this file since 454aab2 was             50be8af5, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago | 
        
          | 
clean up command-line handling and I/O
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1.5 KB | 
      
      
| Rev | Line |  | 
|---|
| [9319a23] | 1 | #include <locks.hfa> | 
|---|
|  | 2 | #include <fstream.hfa> | 
|---|
|  | 3 | #include <stdio.h> | 
|---|
|  | 4 | #include <channel.hfa> | 
|---|
|  | 5 | #include <thread.hfa> | 
|---|
|  | 6 | #include <time.hfa> | 
|---|
|  | 7 | #include <string.h> | 
|---|
|  | 8 |  | 
|---|
|  | 9 | size_t total_operations = 0; | 
|---|
| [50be8af5] | 10 | ssize_t Processors = 1, Tasks = 4;                                              // must be signed | 
|---|
| [9319a23] | 11 |  | 
|---|
|  | 12 | owner_lock o; | 
|---|
|  | 13 |  | 
|---|
|  | 14 | typedef channel( int ) Channel; | 
|---|
|  | 15 |  | 
|---|
|  | 16 | Channel * chain; | 
|---|
|  | 17 |  | 
|---|
|  | 18 | thread Task {}; | 
|---|
|  | 19 | void main(Task & this) { | 
|---|
|  | 20 | size_t runs = 0; | 
|---|
| [5adb277] | 21 | int token = 0; | 
|---|
| [9319a23] | 22 | try{ | 
|---|
|  | 23 | for ( ;; ) { | 
|---|
| [5adb277] | 24 | token << *chain; | 
|---|
| [cb344f7] | 25 | *chain << token; | 
|---|
| [9319a23] | 26 | runs++; | 
|---|
|  | 27 | } | 
|---|
|  | 28 | } catch ( channel_closed * e ) {} | 
|---|
|  | 29 | lock( o ); | 
|---|
|  | 30 | total_operations += runs; | 
|---|
|  | 31 | unlock( o ); | 
|---|
|  | 32 | } | 
|---|
|  | 33 |  | 
|---|
|  | 34 |  | 
|---|
|  | 35 | int main( int argc, char * argv[] ) { | 
|---|
|  | 36 | switch ( argc ) { | 
|---|
|  | 37 | case 3: | 
|---|
|  | 38 | if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ? | 
|---|
| [50be8af5] | 39 | Tasks = ato( argv[2] ); | 
|---|
|  | 40 | if ( Tasks < 1 ) fallthru default; | 
|---|
| [9319a23] | 41 | } // if | 
|---|
|  | 42 | case 2: | 
|---|
|  | 43 | if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ? | 
|---|
| [50be8af5] | 44 | Processors = ato( argv[1] ); | 
|---|
|  | 45 | if ( Processors < 1 ) fallthru default; | 
|---|
| [9319a23] | 46 | } // if | 
|---|
|  | 47 | case 1:                                                                                       // use defaults | 
|---|
|  | 48 | break; | 
|---|
|  | 49 | default: | 
|---|
| [50be8af5] | 50 | exit | "Usage: " | argv[0] | 
|---|
| [9319a23] | 51 | | " [ processors (> 0) | 'd' (default " | Processors | 
|---|
|  | 52 | | ") ] [ channel size (>= 0) | 'd' (default " | Tasks | 
|---|
|  | 53 | | ") ]" ; | 
|---|
|  | 54 | } // switch | 
|---|
|  | 55 | processor proc[Processors - 1]; | 
|---|
|  | 56 |  | 
|---|
|  | 57 | sout | "start"; | 
|---|
|  | 58 | Channel chainChan{ 1 }; | 
|---|
|  | 59 |  | 
|---|
| [cb344f7] | 60 | chainChan << ((int)0); | 
|---|
| [9319a23] | 61 |  | 
|---|
|  | 62 | chain = &chainChan; | 
|---|
|  | 63 | { | 
|---|
|  | 64 | Task t[Tasks]; | 
|---|
|  | 65 | sleep(10`s); | 
|---|
|  | 66 | close( chainChan ); | 
|---|
|  | 67 | } | 
|---|
|  | 68 |  | 
|---|
|  | 69 | // sout | total_operations; | 
|---|
|  | 70 | sout | "done"; | 
|---|
| [50be8af5] | 71 | } | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.