source: src/tests/concurrent/signal/disjoint.c@ 934d200

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 934d200 was 7bdcac1, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Added the option to make longrun tests run until failure

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