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
resolv-new
with_gc
Last change
on this file since 74bba15 was daacf82, checked in by Thierry Delisle <tdelisle@…>, 8 years ago |
Added test for validate single monitor barging avoidance for waitfor statments
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Line | |
---|
1 | #include <fstream>
|
---|
2 | #include <kernel>
|
---|
3 | #include <monitor>
|
---|
4 | #include <stdlib>
|
---|
5 | #include <thread>
|
---|
6 |
|
---|
7 | #include <stdbool.h>
|
---|
8 |
|
---|
9 | static const unsigned long N = 5_000ul;
|
---|
10 |
|
---|
11 | enum state_t { WAITFOR, CALL, BARGE };
|
---|
12 |
|
---|
13 | monitor global_t {
|
---|
14 | bool done;
|
---|
15 | bool started;
|
---|
16 | state_t state;
|
---|
17 | };
|
---|
18 |
|
---|
19 | void ?{} ( global_t & this ) {
|
---|
20 | this.done = false;
|
---|
21 | this.started = false;
|
---|
22 | this.state = BARGE;
|
---|
23 | }
|
---|
24 |
|
---|
25 | void ^?{} ( global_t & mutex this ) {}
|
---|
26 |
|
---|
27 | global_t global;
|
---|
28 |
|
---|
29 | bool barge( global_t & mutex this ) {
|
---|
30 | this.state = BARGE;
|
---|
31 | return !this.done;
|
---|
32 | }
|
---|
33 |
|
---|
34 | thread barger_t {};
|
---|
35 | void main( barger_t & this ) {
|
---|
36 | yield();
|
---|
37 | while( barge( global ) ) { yield(((unsigned)rand48()) % 10); }
|
---|
38 | }
|
---|
39 |
|
---|
40 | bool do_call( global_t & mutex this ) {
|
---|
41 | yield(((unsigned)rand48()) % 10);
|
---|
42 | if( this.state != WAITFOR && !this.done && this.started ) {
|
---|
43 | serr | "Barging before caller detected" | endl;
|
---|
44 | }
|
---|
45 |
|
---|
46 | this.state = CALL;
|
---|
47 | return !this.done;
|
---|
48 | }
|
---|
49 |
|
---|
50 | thread caller_t {};
|
---|
51 | void main( caller_t & this ) {
|
---|
52 | while( do_call(global) ) { yield(((unsigned)rand48()) % 10); }
|
---|
53 | }
|
---|
54 |
|
---|
55 | void do_wait( global_t & mutex this ) {
|
---|
56 | this.started = true;
|
---|
57 | for( int i = 0; i < N; i++) {
|
---|
58 | yield(((unsigned)rand48()) % 10);
|
---|
59 | this.state = WAITFOR;
|
---|
60 | waitfor(do_call, this) {
|
---|
61 | sout | i | endl;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if( this.state != CALL ) {
|
---|
65 | serr | "Barging after caller detected" | endl;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | this.done = true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | thread waiter_t{};
|
---|
73 | void main( waiter_t & this ) {
|
---|
74 | do_wait(global);
|
---|
75 | }
|
---|
76 |
|
---|
77 | int main() {
|
---|
78 | sout | "Starting" | endl;
|
---|
79 | {
|
---|
80 | barger_t bargers[17];
|
---|
81 | caller_t callers[7];
|
---|
82 | waiter_t waiters;
|
---|
83 | }
|
---|
84 | sout | "Stopping" | endl;
|
---|
85 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.