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

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since e1c1829 was e1c1829, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added monitor tests to preempt longrun tests

  • 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 100_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                barge_op( choose_a ? &globalA : &globalB );
97        }
98}
99
100//------------------------------------------------------------------------------
101
102int main(int argc, char* argv[]) {
103        rand48seed(0);
104        done = false;
105        processor p;
106        {
107                Signaller s[4];
108                Barger b[13];
109                sout | "Starting waiters" | endl;
110                {
111                        Waiter w[3];
112                }
113                sout | "Waiters done" | endl;
114                done = true;
115        }
116}
Note: See TracBrowser for help on using the repository browser.