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

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 0720e049 was cd99ef1, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Clean-up longrunning tests to be more consistent

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