source: tests/concurrency/channels/daisy_chain.cfa @ 66ac416

Last change on this file since 66ac416 was 66ac416, checked in by caparsons <caparson@…>, 11 months ago

revert channel test change

  • Property mode set to 100644
File size: 1.6 KB
Line 
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
9size_t total_operations = 0;
10size_t Processors = 1, Tasks = 4;
11
12owner_lock o;
13
14typedef channel( int ) Channel;
15
16Channel * chain;
17
18thread Task {};
19void main(Task & this) {
20    size_t runs = 0;
21    // int token = 0;
22    try{
23        for ( ;; ) {
24            // token << *chain;
25            remove( *chain );
26            // token >> *chain;
27            insert( *chain, 0 );
28            runs++;
29        }
30    } catch ( channel_closed * e ) {}
31    lock( o );
32    total_operations += runs;
33    unlock( o );
34}
35
36
37int main( int argc, char * argv[] ) {
38    switch ( argc ) {
39          case 3:
40                if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ?
41                        Tasks = atoi( argv[2] );
42            if ( Tasks < 1 ) goto Usage;
43                } // if
44          case 2:
45                if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ?
46                        Processors = atoi( argv[1] );
47                        if ( Processors < 1 ) goto Usage;
48                } // if
49          case 1:                                                                                       // use defaults
50                break;
51          default:
52          Usage:
53                sout | "Usage: " | argv[0]
54             | " [ processors (> 0) | 'd' (default " | Processors
55                         | ") ] [ channel size (>= 0) | 'd' (default " | Tasks
56                         | ") ]" ;
57                exit( EXIT_FAILURE );
58        } // switch
59    processor proc[Processors - 1];
60
61    sout | "start";
62    Channel chainChan{ 1 };
63
64    // ((int)0) >> chainChan;
65    insert( chainChan, 0 );
66
67    chain = &chainChan;   
68    {
69        Task t[Tasks];
70        sleep(10`s);
71        close( chainChan );
72    }
73   
74    // sout | total_operations;
75    sout | "done";
76
77    return 0;
78}
Note: See TracBrowser for help on using the repository browser.