source: src/tests/concurrent/signal/disjoint.c@ 4d7fb9e

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 with_gc
Last change on this file since 4d7fb9e was 8638cef, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Added missing mutex on some destructors

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