source: src/tests/sched-ext-barge.c @ daacf82

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 daacf82 was daacf82, checked in by Thierry Delisle <tdelisle@…>, 7 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
9static const unsigned long N = 5_000ul;
10
11enum state_t { WAITFOR, CALL, BARGE };
12
13monitor global_t {
14        bool done;
15        bool started;
16        state_t state;
17};
18
19void ?{} ( global_t & this ) {
20        this.done = false;
21        this.started = false;
22        this.state = BARGE;
23}
24
25void ^?{} ( global_t & mutex this ) {}
26
27global_t global;
28
29bool barge( global_t & mutex this ) {
30        this.state = BARGE;
31        return !this.done;
32}
33
34thread barger_t {};
35void main( barger_t & this ) {
36        yield();
37        while( barge( global ) ) { yield(((unsigned)rand48()) % 10); }
38}
39
40bool 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
50thread caller_t {};
51void main( caller_t & this ) {
52        while( do_call(global) ) { yield(((unsigned)rand48()) % 10); }
53}
54
55void 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
72thread waiter_t{};
73void main( waiter_t & this ) {
74        do_wait(global);
75}
76
77int 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.