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

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 83a071f9 was 83a071f9, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Fix concurrency library, tests, and keywords for references

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