source: src/tests/concurrent/signal/block.c @ 300d75b

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 300d75b was 54aba8d, checked in by Peter A. Buhr <pabuhr@…>, 6 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
RevLine 
[9fe39530]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
[2c9ebab]9#include <fstream>
10#include <kernel>
11#include <monitor>
12#include <stdlib>
13#include <thread>
14
[cd99ef1]15#include <time.h>
16
17static const unsigned long N = 5_000ul;
18
19#ifndef PREEMPTION_RATE
20#define PREEMPTION_RATE 10_000ul
[e1c1829]21#endif
[2c9ebab]22
[cd99ef1]23unsigned int default_preemption() {
24        return PREEMPTION_RATE;
25}
26
[2c9ebab]27enum state_t { WAITED, SIGNAL, BARGE };
28
29monitor global_data_t {
[ccd349d]30        thread_desc * last_thread;
31        thread_desc * last_signaller;
[2c9ebab]32};
33
[00e80b6]34void ?{} ( global_data_t & this ) {
35        this.last_thread = NULL;
36        this.last_signaller = NULL;
[2c9ebab]37}
38
[00e80b6]39void ^?{} ( global_data_t & this ) {}
[2c9ebab]40
41global_data_t globalA, globalB;
42
43condition cond;
44
45volatile bool done;
46
47//------------------------------------------------------------------------------
[83a071f9]48void wait_op( global_data_t & mutex a, global_data_t & mutex b, unsigned i ) {
[4cedd9f]49        wait( cond, (uintptr_t)this_thread );
[2c9ebab]50
[6c7b1e7]51        yield( random( 10 ) );
[2c9ebab]52
[83a071f9]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;
[2c9ebab]55                abort();
56        }
57
[83a071f9]58        a.last_thread = b.last_thread = this_thread;
[2c9ebab]59
[6c7b1e7]60        yield( random( 10 ) );
[2c9ebab]61}
62
63thread Waiter {};
[83a071f9]64void main( Waiter & this ) {
[2c9ebab]65        for( int i = 0; i < N; i++ ) {
[83a071f9]66                wait_op( globalA, globalB, i );
[2c9ebab]67        }
68}
69
70//------------------------------------------------------------------------------
[83a071f9]71void signal_op( global_data_t & mutex a, global_data_t & mutex b ) {
[6c7b1e7]72        yield( random( 10 ) );
[2c9ebab]73
[83a071f9]74        [a.last_thread, b.last_thread, a.last_signaller, b.last_signaller] = this_thread;
[2c9ebab]75
[4cedd9f]76        if( !is_empty( cond ) ) {
[2c9ebab]77
[4cedd9f]78                thread_desc * next = front( cond );
[2c9ebab]79
[4cedd9f]80                if( ! signal_block( cond ) ) {
[ccd349d]81                        sout | "ERROR expected to be able to signal" | endl;
82                        abort();
83                }
84
[6c7b1e7]85                yield( random( 10 ) );
[ccd349d]86
[83a071f9]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;
[2c9ebab]89                        abort();
90                }
91        }
92
93}
94
95thread Signaller {};
[83a071f9]96void main( Signaller & this ) {
[2c9ebab]97        while( !done ) {
[83a071f9]98                signal_op( globalA, globalB );
[2c9ebab]99        }
100}
101
102//------------------------------------------------------------------------------
[83a071f9]103void barge_op( global_data_t & mutex a ) {
104        a.last_thread = this_thread;
[2c9ebab]105}
106
107thread Barger {};
[83a071f9]108void main( Barger & this ) {
[2c9ebab]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);
[83a071f9]112                if ( choose_a ) barge_op( globalA );
113                else barge_op( globalB );
[2c9ebab]114        }
115}
116
117//------------------------------------------------------------------------------
118
119int main(int argc, char* argv[]) {
[54aba8d]120        srandom( time( NULL ) );
[2c9ebab]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        }
[736fe25]133}
Note: See TracBrowser for help on using the repository browser.