source: src/tests/sched-int-disjoint.c @ c850687

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 c850687 was 9737ffe, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Renamed internal scheduling tests to be more evocative

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[f80ab45]1#include <fstream>
[5ea06d6]2#include <kernel>
3#include <monitor>
4#include <thread>
5
[102a58b]6#define N 100_000
[5ea06d6]7
[102a58b]8enum state_t { WAIT, SIGNAL, BARGE };
9
10monitor global_t {};
11global_t mut;
12
13monitor global_data_t;
14void ?{}( global_data_t * this );
15void ^?{} ( global_data_t * this );
16
17monitor global_data_t {
18        int counter;
19        state_t state;
20} data;
[5ea06d6]21
22condition cond;
23
[102a58b]24volatile bool all_done;
[5ea06d6]25
[102a58b]26void ?{}( global_data_t * this ) {
27        this->counter == 0;
28        this->state = BARGE;
[5ea06d6]29}
30
[102a58b]31void ^?{} ( global_data_t * this ) {}
[5ea06d6]32
[102a58b]33//------------------------------------------------------------------------------
34// Barging logic
35void barge( global_data_t * mutex d ) {
36        d->state = BARGE;
[5ea06d6]37}
38
[102a58b]39thread Barger {};
[5ea06d6]40
[102a58b]41void main( Barger * this ) {
42        while( !all_done ) { 
43                barge( &data );
44                yield(); 
45        }
[5ea06d6]46}
47
[102a58b]48//------------------------------------------------------------------------------
49// Waiting logic
50bool wait( global_t * mutex m, global_data_t * mutex d ) {
51        wait( &cond );
52        if( d->state != SIGNAL ) {
53                sout | "ERROR barging!" | endl; 
54        }
55
56        d->counter++;
57
58        if( (d->counter % 1000) == 0 ) sout | d->counter | endl;
59
60        return d->counter < N;
[5ea06d6]61}
62
[102a58b]63thread Waiter {};
64
65void main( Waiter * this ) {
66        while( wait( &mut, &data ) ) { yield(); }
67}
68
69
70//------------------------------------------------------------------------------
71// Signalling logic
72void signal( condition * cond, global_t * mutex a, global_data_t * mutex b ) {
73        b->state = SIGNAL;
74        signal( cond );
75}
76
77void logic( global_t * mutex a ) {
78        signal( &cond, a, &data );
79
80        int pauses = (unsigned)rand48() % 10;
81        for(int i = 0; i < pauses; i++) {
82                yield();
[5ea06d6]83        }
84
[102a58b]85        //This is technically a mutual exclusion violation but the mutex monitor protects us
86        bool running = data.counter < N && data.counter > 0;
87        if( data.state != SIGNAL && running ) {
88                sout | "ERROR Eager signal" | data.state | endl; 
89        }
[5ea06d6]90}
91
[102a58b]92thread Signaller {};
93
94void main( Signaller * this ) {
95        while( !all_done ) { 
96                logic( &mut );
97                yield(); 
98        }
99}
100
101//------------------------------------------------------------------------------
102// Main loop
[5ea06d6]103int main(int argc, char* argv[]) {
[102a58b]104        all_done = false;
[5ea06d6]105        processor p;
106        {
[102a58b]107                Signaller s;
108                Barger b[17];
109                {
110                        Waiter w[4];
111                }
112                sout | "All waiter done" | endl;
113                all_done = true;
114        }       
[5ea06d6]115}
Note: See TracBrowser for help on using the repository browser.