source: tests/concurrency/channels/barrier.cfa @ 7e4bd9b6

Last change on this file since 7e4bd9b6 was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 18 months ago

first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names

  • Property mode set to 100644
File size: 2.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;
10int Processors = 1, Tasks = 5, BarrierSize = 2;
11
12typedef channel( int ) Channel;
13
14Channel * barWait;
15Channel * entryWait;
16owner_lock o;
17
18bool done = false;
19size_t tasks_done = 0;
20
21static inline void initBarrier() {
22    for ( j; BarrierSize )
23        insert( *entryWait, j );
24}
25
26static inline void barrier() {
27    int ticket = remove( *entryWait );
28
29    if ( ticket == BarrierSize - 1 ) {
30                for ( j; BarrierSize - 1 )
31            insert( *barWait, j );
32        return;
33        }
34    ticket = remove( *barWait );
35
36        // last one out
37        if ( BarrierSize == 1 || ticket == BarrierSize - 2 ) {
38                for ( j; BarrierSize )
39            insert( *entryWait, j );
40        }
41}
42
43thread Task {};
44static inline void ?{}( Task & p, cluster & clu ) {
45    ((thread &)p){ clu };
46}
47void main(Task & this) {
48    size_t runs = 0;
49    try {
50        for ( ;; ) {
51            if ( done ) break;
52            barrier();
53            runs++;
54        }
55    } catch ( channel_closed * e ) {}
56    lock(o);
57    total_operations += runs;
58    // sout | "P: " | runs;
59    unlock(o);
60}
61
62
63int main( int argc, char * argv[] ) {
64    switch ( argc ) {
65          case 3:
66                if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ?
67                        BarrierSize = atoi( argv[2] );
68            if ( Processors < 1 ) goto Usage;
69                } // if
70          case 2:
71                if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ?
72                        Processors = atoi( argv[1] );
73                        if ( Processors < 1 ) goto Usage;
74                } // if
75          case 1:                                                                                       // use defaults
76                break;
77          default:
78          Usage:
79                sout | "Usage: " | argv[0]
80             | " [ processors (> 0) | 'd' (default " | Processors
81                         | ") ] [ BarrierSize (> 0) | 'd' (default " | BarrierSize
82                         | ") ]" ;
83                exit( EXIT_FAILURE );
84        } // switch
85    if ( Tasks < BarrierSize )
86        Tasks = BarrierSize;
87
88    size_t Clusters = 1;
89    // create a cluster
90    cluster clus[Clusters];
91    processor * proc[Processors];
92    for ( i; Processors ) {
93        (*(proc[i] = malloc())){clus[i % Clusters]};
94    }
95
96    Channel entry{ BarrierSize };
97    Channel wait{ BarrierSize };
98
99    entryWait = &entry;
100    barWait = &wait;
101
102    initBarrier();
103
104    sout | "start";
105    Task * t[Tasks];
106
107    for ( i; Tasks ) {
108        (*(t[i] = malloc())){ clus[i % Clusters] };
109    }
110
111    sleep(10`s);
112    done = true;
113
114    sout | "sleep";
115
116    close( entry );
117    close( wait );
118
119    for ( i; Tasks ) {
120        delete(t[i]);
121    }
122   
123    // sout | total_operations;
124
125    for ( i; Processors ) {
126        delete(proc[i]);
127    }
128    sout | "done";
129    return 0;
130}
Note: See TracBrowser for help on using the repository browser.