source: src/tests/concurrent/signal/block.c @ e61de5b

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

change name of random_seed to srandom, and make all random calls through rand48

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