source: src/tests/concurrent/signal/block.c @ 8ad6533

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 8ad6533 was 8ad6533, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove cfatime.h, move itimerval constructor to "time", update concurrent examples to use Duration

  • Property mode set to 100644
File size: 3.1 KB
Line 
1//---------------------------------------------------------
2// Barging test
3// Ensures that no barging can occur between :
4//   - the frontend of the signal_block and the signaled thread
5//   - the signaled  threadand the backend of the signal_block
6//---------------------------------------------------------
7
8
9#include <fstream>
10#include <kernel>
11#include <monitor>
12#include <stdlib>
13#include <thread>
14
15#include <time.h>
16
17#ifdef LONG_TEST
18static const unsigned long N = 150_000ul;
19#else
20static const unsigned long N = 5_000ul;
21#endif
22
23#ifndef PREEMPTION_RATE
24#define PREEMPTION_RATE 10`ms
25#endif
26
27Duration default_preemption() {
28        return PREEMPTION_RATE;
29}
30
31enum state_t { WAITED, SIGNAL, BARGE };
32
33monitor global_data_t {
34        thread_desc * last_thread;
35        thread_desc * last_signaller;
36};
37
38void ?{} ( global_data_t & this ) {
39        this.last_thread = NULL;
40        this.last_signaller = NULL;
41}
42
43void ^?{} ( global_data_t & this ) {}
44
45global_data_t globalA, globalB;
46
47condition cond;
48
49volatile bool done;
50
51//------------------------------------------------------------------------------
52void wait_op( global_data_t & mutex a, global_data_t & mutex b, unsigned i ) {
53    wait( cond, (uintptr_t)active_thread() );
54
55        yield( random( 10 ) );
56
57        if(a.last_thread != a.last_signaller || b.last_thread != b.last_signaller ) {
58                sout | "ERROR Barging detected, expected" | a.last_signaller | b.last_signaller | "got" | a.last_thread | b.last_thread | endl;
59                abort();
60        }
61
62        a.last_thread = b.last_thread = active_thread();
63
64        yield( random( 10 ) );
65}
66
67thread Waiter {};
68void main( Waiter & this ) {
69        for( int i = 0; i < N; i++ ) {
70                wait_op( globalA, globalB, i );
71        }
72}
73
74//------------------------------------------------------------------------------
75void signal_op( global_data_t & mutex a, global_data_t & mutex b ) {
76        yield( random( 10 ) );
77
78        [a.last_thread, b.last_thread, a.last_signaller, b.last_signaller] = active_thread();
79
80        if( !is_empty( cond ) ) {
81
82                thread_desc * next = front( cond );
83
84                if( ! signal_block( cond ) ) {
85                        sout | "ERROR expected to be able to signal" | endl;
86                        abort();
87                }
88
89                yield( random( 10 ) );
90
91                if(a.last_thread != next || b.last_thread != next) {
92                        sout | "ERROR Barging detected, expected" | next | "got" | a.last_thread | b.last_thread | endl;
93                        abort();
94                }
95        }
96
97}
98
99thread Signaller {};
100void main( Signaller & this ) {
101        while( !done ) {
102                signal_op( globalA, globalB );
103        }
104}
105
106//------------------------------------------------------------------------------
107void barge_op( global_data_t & mutex a ) {
108        a.last_thread = active_thread();
109}
110
111thread Barger {};
112void main( Barger & this ) {
113        for( unsigned i = 0; !done; i++ ) {
114                //Choose some monitor to barge into with some irregular pattern
115                bool choose_a = (i % 13) > (i % 17);
116                if ( choose_a ) barge_op( globalA );
117                else barge_op( globalB );
118        }
119}
120
121//------------------------------------------------------------------------------
122
123int main(int argc, char* argv[]) {
124        srandom( time( NULL ) );
125        done = false;
126        processor p;
127        {
128                Signaller s[4];
129                Barger b[13];
130                sout | "Starting waiters" | endl;
131                {
132                        Waiter w[3];
133                }
134                sout | "Waiters done" | endl;
135                done = true;
136        }
137}
Note: See TracBrowser for help on using the repository browser.