ast-experimental
Last change
on this file since 2314aac was cca568e, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago |
change all example waitfor statements from comma separate to colon
|
-
Property mode
set to
100644
|
File size:
1.7 KB
|
Rev | Line | |
---|
[9fe39530] | 1 | //---------------------------------------------------------
|
---|
| 2 | // Barging test
|
---|
| 3 | // Ensures that no barging can occur between :
|
---|
| 4 | // - the frontend of the waitfor and the waited call
|
---|
| 5 | // - the waited call and the backend of the waitfor
|
---|
| 6 | //---------------------------------------------------------
|
---|
| 7 |
|
---|
[73abe95] | 8 | #include <fstream.hfa>
|
---|
| 9 | #include <kernel.hfa>
|
---|
| 10 | #include <monitor.hfa>
|
---|
| 11 | #include <stdlib.hfa>
|
---|
| 12 | #include <thread.hfa>
|
---|
[daacf82] | 13 |
|
---|
| 14 | #include <stdbool.h>
|
---|
| 15 |
|
---|
| 16 | static const unsigned long N = 5_000ul;
|
---|
| 17 |
|
---|
| 18 | enum state_t { WAITFOR, CALL, BARGE };
|
---|
| 19 |
|
---|
| 20 | monitor global_t {
|
---|
| 21 | bool done;
|
---|
| 22 | bool started;
|
---|
| 23 | state_t state;
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | void ?{} ( global_t & this ) {
|
---|
| 27 | this.done = false;
|
---|
| 28 | this.started = false;
|
---|
| 29 | this.state = BARGE;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | void ^?{} ( global_t & mutex this ) {}
|
---|
| 33 |
|
---|
| 34 | global_t global;
|
---|
| 35 |
|
---|
| 36 | bool barge( global_t & mutex this ) {
|
---|
| 37 | this.state = BARGE;
|
---|
| 38 | return !this.done;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | thread barger_t {};
|
---|
| 42 | void main( barger_t & this ) {
|
---|
| 43 | yield();
|
---|
[6c7b1e7] | 44 | while( barge( global ) ) { yield(random( 10 )); }
|
---|
[daacf82] | 45 | }
|
---|
| 46 |
|
---|
| 47 | bool do_call( global_t & mutex this ) {
|
---|
[6c7b1e7] | 48 | yield(random( 10 ));
|
---|
[daacf82] | 49 | if( this.state != WAITFOR && !this.done && this.started ) {
|
---|
[200fcb3] | 50 | serr | "Barging before caller detected";
|
---|
[daacf82] | 51 | }
|
---|
| 52 |
|
---|
| 53 | this.state = CALL;
|
---|
| 54 | return !this.done;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | thread caller_t {};
|
---|
| 58 | void main( caller_t & this ) {
|
---|
[6c7b1e7] | 59 | while( do_call(global) ) { yield(random( 10 )); }
|
---|
[daacf82] | 60 | }
|
---|
| 61 |
|
---|
| 62 | void do_wait( global_t & mutex this ) {
|
---|
| 63 | this.started = true;
|
---|
| 64 | for( int i = 0; i < N; i++) {
|
---|
[6c7b1e7] | 65 | yield(random( 10 ));
|
---|
[daacf82] | 66 | this.state = WAITFOR;
|
---|
[cca568e] | 67 | waitfor(do_call : this) {
|
---|
[200fcb3] | 68 | sout | i;
|
---|
[daacf82] | 69 | }
|
---|
| 70 |
|
---|
| 71 | if( this.state != CALL ) {
|
---|
[200fcb3] | 72 | serr | "Barging after caller detected";
|
---|
[daacf82] | 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | this.done = true;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | thread waiter_t{};
|
---|
| 80 | void main( waiter_t & this ) {
|
---|
| 81 | do_wait(global);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | int main() {
|
---|
[200fcb3] | 85 | sout | "Starting";
|
---|
[daacf82] | 86 | {
|
---|
| 87 | barger_t bargers[17];
|
---|
| 88 | caller_t callers[7];
|
---|
| 89 | waiter_t waiters;
|
---|
| 90 | }
|
---|
[200fcb3] | 91 | sout | "Stopping";
|
---|
[6c7b1e7] | 92 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.