source: tests/concurrency/channels/pub_sub.cfa @ ad47ec4

Last change on this file since ad47ec4 was 50be8af, checked in by Peter A. Buhr <pabuhr@…>, 14 months ago

clean up command-line handling and I/O

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[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#include <mutex_stmt.hfa>
9
10size_t total_operations = 0;
11size_t Processors = 1, Tasks = 4;
12
13typedef channel( size_t ) Channel;
14
15channel( int ) * barWait;
16channel( int ) * entryWait;
17int BarrierSize = 4;
18static inline void closeBarrier() {
19    close(*entryWait);
20    close(*barWait);
21}
22
23static inline void initBarrier() {
24    barWait = malloc();
25    entryWait = malloc();
26    (*barWait){ BarrierSize };
27    (*entryWait){ BarrierSize };
28    for ( j; BarrierSize )
29        insert( *entryWait, j );
30}
31
32static inline void deleteBarrier() {
33    delete(barWait);
34    delete(entryWait);
35}
36
37static inline void barrier() {
38    int ticket = remove( *entryWait );
39    if ( ticket == BarrierSize - 1 ) {
40                for ( j; BarrierSize - 1 )
41            insert( *barWait, j );
42        return;
43        }
44    ticket = remove( *barWait );
45
46        // last one out
47        if ( BarrierSize == 1 || ticket == BarrierSize - 2 ) {
48                for ( j; BarrierSize )
49            insert( *entryWait, j );
50        }
51}
52
53Channel * chans;
54owner_lock o;
55
56thread Task { size_t id; };
57static inline void ?{}( Task & p, size_t i, cluster & clu ) {
58    ((thread &)p){ clu };
59    p.id = i;
60}
61void main(Task & this) with(this) {
62    size_t runs = 0;
63    try {
64        for ( ;; ) {
65            // publish
66            for ( i; Tasks ) {
67                insert(chans[id], i);
68            }
69
70            // subscribe
71            for ( i; Tasks ) {
72                remove( chans[i] );
73            }
74            barrier();
75            runs++;
76        }
77    } catch ( channel_closed * e ) { }
78    lock(o);
79    total_operations += runs;
80    // sout | runs;
81    unlock(o);
82}
83
84
85int main( int argc, char * argv[] ) {
86    switch ( argc ) {
87          case 3:
88                if ( strcmp( argv[2], "d" ) != 0 ) {                    // default ?
[50be8af]89                        Tasks = ato( argv[2] );
90            if ( Tasks < 1 ) fallthru default;
[9319a23]91                } // if
92          case 2:
93                if ( strcmp( argv[1], "d" ) != 0 ) {                    // default ?
[50be8af]94                        Processors = ato( argv[1] );
95                        if ( Processors < 1 ) fallthru default;
[9319a23]96                } // if
97          case 1:                                                                                       // use defaults
98                break;
99          default:
[50be8af]100                exit | "Usage: " | argv[0]
[9319a23]101             | " [ processors (> 0) | 'd' (default " | Processors
102                         | ") ] [ Tasks (> 0) | 'd' (default " | Tasks
103                         | ") ]" ;
104        } // switch
105    BarrierSize = Tasks;
106
107    size_t Clusters = 1;
108    // create a cluster
109    cluster clus[Clusters];
110    processor * proc[Processors];
111
112    // setup processors
113    for ( i; Processors )
114        (*(proc[i] = malloc())){clus[i % Clusters]};
115
116    // setup pub/sub chans
117    chans = aalloc( Tasks );
118    for ( i; Tasks )
119        chans[i]{ Tasks };
120
121    // setup barrier
122    initBarrier();
123
124    // sout | "Processors: " | Processors | " ProdsPerChan: " | Producers | " ConsPerChan: " | Consumers | "Channels: " | Channels | " Channel Size: " | ChannelSize;
125
126    sout | "start";
127    Task * t[Tasks];
128
129    // create tasks
130    for ( i; Tasks )
131        (*(t[i] = malloc())){ i, clus[i % Clusters] };
132
133    sleep(10`s);
134
135    closeBarrier();
136    for ( i; Tasks )
137        close( chans[i] );
138
139    for ( i; Tasks ) {
140        delete(t[i]);
141    }
142
143    deleteBarrier();
144   
145    // sout | total_operations;
146
147    for ( i; Processors ) {
148        delete(proc[i]);
149    }
150    adelete( chans );
151    sout | "done";
152    return 0;
[50be8af]153}
Note: See TracBrowser for help on using the repository browser.