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

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since b1e63ac5 was e1c1829, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Added monitor tests to preempt longrun tests

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