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

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

Merge branch 'master' into references

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