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

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

preemption works for threads

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