source: tests/concurrency/channels/hot_potato.cfa @ d923fca

Last change on this file since d923fca was d923fca, checked in by Andrew Beach <ajbeach@…>, 6 weeks ago

Clean-up the warnings of the concurrency tests. A lot of little test level fixes, the most interesting repeated one is some formally redundent fallthough statements. pthread_attr_test had to be rewritten because of library restrictions. Changed some types so they would always be pointer sized. There was a library change, there was a function that could not be implemented; I trust that it is included for a reason so I just put it in a comment. There is a change to the compiler, wait-until now uses goto. The labelled breaks were code generated as unlabelled breaks and although it worked out slipped through some checks. Finally, there is one warning that I cannot solve at this time so tests that produce it have been put in their own lax group.

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