source: src/tests/concurrent/signal/disjoint.c@ 682dcae

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 682dcae was 0f56058, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

divide "time" into type and functions

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