source: src/tests/sched-int-block.c @ f6b70e5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f6b70e5 was 2c9ebab, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

concurrent test for signal block

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#include <fstream>
2#include <kernel>
3#include <monitor>
4#include <stdlib>
5#include <thread>
6
7static const unsigned N = 100_000;
8
9enum state_t { WAITED, SIGNAL, BARGE };
10
11monitor global_t {};
12
13monitor global_data_t {
14        state_t state;
15        bool ran;
16};
17
18void ?{} ( global_data_t * this ) {
19        this->state = BARGE;
20}
21
22void ^?{} ( global_data_t * this ) {}
23
24global_data_t globalA, globalB;
25
26condition cond;
27
28volatile bool done;
29
30//------------------------------------------------------------------------------
31void wait_op( global_data_t * mutex a, global_data_t * mutex b, unsigned i ) {
32        wait( &cond );
33        a->ran = b->ran = true;
34
35        yield( ((unsigned)rand48()) % 10 );
36
37        if(a->state != SIGNAL || b->state != SIGNAL) {
38                sout | "ERROR Barging detected" | a->state | b->state | endl;
39                abort();
40        }
41
42        a->state = b->state = WAITED;
43
44        yield( ((unsigned)rand48()) % 10 );
45}
46
47thread Waiter {};
48void main( Waiter* this ) {
49        for( int i = 0; i < N; i++ ) {
50                wait_op( &globalA, &globalB, i );
51        }
52}
53
54//------------------------------------------------------------------------------
55void signal_op( global_data_t * mutex a, global_data_t * mutex b ) {
56        yield( ((unsigned)rand48()) % 10 );
57
58        a->ran = b->ran = false;
59        a->state = b->state = SIGNAL;
60
61        signal_block( &cond );
62
63        yield( ((unsigned)rand48()) % 10 );
64
65        assert(a->ran == b->ran);
66        if(a->ran)
67        {
68                if(a->state != WAITED || b->state != WAITED) {
69                        sout | "ERROR Barging detected" | a->state | b->state | endl;
70                        abort();
71                }
72        }
73
74}
75
76thread Signaller {};
77void main( Signaller* this ) {
78        while( !done ) {
79                signal_op( &globalA, &globalB );
80        }
81}
82
83//------------------------------------------------------------------------------
84void barge_op( global_data_t * mutex a ) {
85        a->state = BARGE;
86}
87
88thread Barger {};
89void main( Barger* this ) {
90        for( unsigned i = 0; !done; i++ ) {
91                //Choose some monitor to barge into with some irregular pattern
92                bool choose_a = (i % 13) > (i % 17);
93                barge_op( choose_a ? &globalA : &globalB );
94        }
95}
96
97//------------------------------------------------------------------------------
98
99int main(int argc, char* argv[]) {
100        rand48seed(0);
101        done = false;
102        processor p;
103        {
104                Signaller s[4];
105                Barger b[13];
106                sout | "Starting waiters" | endl;
107                {
108                        Waiter w[3];
109                }
110                sout | "Waiters done" | endl;
111                done = true;
112        }
113}
Note: See TracBrowser for help on using the repository browser.