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

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 6d267ca was 0322865c, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Cleaned long tests to be more consistent and offer more debug information

  • 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
[e1c1829]6#ifndef N
[0322865c]7#define N 10_000
[e1c1829]8#endif
[5ea06d6]9
[102a58b]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;
[5ea06d6]23
24condition cond;
25
[102a58b]26volatile bool all_done;
[5ea06d6]27
[102a58b]28void ?{}( global_data_t * this ) {
29 this->counter == 0;
30 this->state = BARGE;
[5ea06d6]31}
32
[102a58b]33void ^?{} ( global_data_t * this ) {}
[5ea06d6]34
[102a58b]35//------------------------------------------------------------------------------
36// Barging logic
37void barge( global_data_t * mutex d ) {
38 d->state = BARGE;
[5ea06d6]39}
40
[102a58b]41thread Barger {};
[5ea06d6]42
[102a58b]43void main( Barger * this ) {
[0322865c]44 while( !all_done ) {
[102a58b]45 barge( &data );
[0322865c]46 yield();
[102a58b]47 }
[5ea06d6]48}
49
[102a58b]50//------------------------------------------------------------------------------
51// Waiting logic
52bool wait( global_t * mutex m, global_data_t * mutex d ) {
53 wait( &cond );
54 if( d->state != SIGNAL ) {
[0322865c]55 sout | "ERROR barging!" | endl;
[102a58b]56 }
57
58 d->counter++;
59
60 if( (d->counter % 1000) == 0 ) sout | d->counter | endl;
61
62 return d->counter < N;
[5ea06d6]63}
64
[102a58b]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
[4c5b972]82 yield( (unsigned)rand48() % 10 );
[5ea06d6]83
[102a58b]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 ) {
[0322865c]87 sout | "ERROR Eager signal" | data.state | endl;
[102a58b]88 }
[5ea06d6]89}
90
[102a58b]91thread Signaller {};
92
93void main( Signaller * this ) {
[0322865c]94 while( !all_done ) {
[102a58b]95 logic( &mut );
[0322865c]96 yield();
[102a58b]97 }
98}
99
100//------------------------------------------------------------------------------
101// Main loop
[5ea06d6]102int main(int argc, char* argv[]) {
[102a58b]103 all_done = false;
[5ea06d6]104 processor p;
105 {
[102a58b]106 Signaller s;
107 Barger b[17];
108 {
109 Waiter w[4];
110 }
111 sout | "All waiter done" | endl;
112 all_done = true;
[0322865c]113 }
[5ea06d6]114}
Note: See TracBrowser for help on using the repository browser.