source: tests/concurrency/waitfor/when.cfa @ 314c9d8

Last change on this file since 314c9d8 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.8 KB
RevLine 
[9fe39530]1//----------------------------------------------------------------
2// When test
3// Ensures that when clauses on waitfor are respected
4//-----------------------------------------------------------------
5
[73abe95]6#include <fstream.hfa>
7#include <kernel.hfa>
8#include <monitor.hfa>
9#include <stdlib.hfa>
10#include <thread.hfa>
[9fe39530]11
12#include <stdbool.h>
13#include <time.h>
14
15static const unsigned long N = 4_998ul;
16
[6c7b1e7]17static inline void rand_yield() { yield(random( 10 )); }
[9fe39530]18
19monitor global_t {
20        int last_call;
21        bool done;
22};
23
24void ?{} ( global_t & this ) {
25        this.last_call = 6;
26        this.done = false;
27}
28
29void ^?{} ( global_t & mutex this ) {}
30
31global_t global;
32
33bool call1( global_t & mutex this ) { this.last_call = 1; return this.done; }
34bool call2( global_t & mutex this ) { this.last_call = 2; return this.done; }
35bool call3( global_t & mutex this ) { this.last_call = 3; return this.done; }
36bool call4( global_t & mutex this ) { this.last_call = 4; return this.done; }
37bool call5( global_t & mutex this ) { this.last_call = 5; return this.done; }
38bool call6( global_t & mutex this ) { this.last_call = 6; return this.done; }
39
40thread caller_t{};
41void main( caller_t & this ) {
42        while( true ) {
43                rand_yield();
44                if( call1( global ) ) return;
45                rand_yield();
46                if( call2( global ) ) return;
47                rand_yield();
48                if( call3( global ) ) return;
49                rand_yield();
50                if( call4( global ) ) return;
51                rand_yield();
52                if( call5( global ) ) return;
53                rand_yield();
54                if( call6( global ) ) return;
55        }
56}
57
58void arbiter( global_t & mutex this ) {
[75ca7f4]59        // There is a race at start where callers can get in before the arbiter.
60        // It doesn't really matter here so just restart the loop correctly and move on
61        this.last_call = 6;
62
[9fe39530]63        for( int i = 0; i < N; i++ ) {
[cca568e]64                   when( this.last_call == 6 ) waitfor( call1 : this ) { if( this.last_call != 1) { serr | "Expected last_call to be 1 got" | this.last_call; } }
65                or when( this.last_call == 1 ) waitfor( call2 : this ) { if( this.last_call != 2) { serr | "Expected last_call to be 2 got" | this.last_call; } }
66                or when( this.last_call == 2 ) waitfor( call3 : this ) { if( this.last_call != 3) { serr | "Expected last_call to be 3 got" | this.last_call; } }
67                or when( this.last_call == 3 ) waitfor( call4 : this ) { if( this.last_call != 4) { serr | "Expected last_call to be 4 got" | this.last_call; } }
68                or when( this.last_call == 4 ) waitfor( call5 : this ) { if( this.last_call != 5) { serr | "Expected last_call to be 5 got" | this.last_call; } }
69                or when( this.last_call == 5 ) waitfor( call6 : this ) { if( this.last_call != 6) { serr | "Expected last_call to be 6 got" | this.last_call; } }
[200fcb3]70
71                sout | this.last_call;
[9fe39530]72        }
73
74        this.done = true;
75}
76
77thread arbiter_t{};
78void main( arbiter_t & this ) {
79        arbiter( global );
80}
81
82int main() {
[54aba8d]83        srandom( time(NULL) );
[200fcb3]84        sout | "Starting";
[9fe39530]85        {
86                arbiter_t arbiter;
87                caller_t callers[7];
88
89        }
[200fcb3]90        sout | "Stopping";
[6c7b1e7]91}
Note: See TracBrowser for help on using the repository browser.