source: tests/concurrent/park/force_preempt.cfa @ 50b8885

arm-ehjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 50b8885 was 50b8885, checked in by Thierry Delisle <tdelisle@…>, 22 months ago

Removed owner reason from monitors which was only for debug and did not prove very helpful

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <thread.hfa>
2#include <fstream.hfa>
3
4volatile int global = 0;
5
6thread Poller {};
7void main(Poller & this) {
8        while(true) {
9                waitfor( ^?{} : this ) {
10                        break;
11                } or else {
12                        global = (global + 1) % 10;
13                        yield();
14                }
15        }
16}
17
18thread Waiter;
19thread Waiter {};
20
21
22void park_loop(Waiter & this, int id, bool force) {
23
24        for(int i = 0; i < 5; i++) {
25                // Unpark this thread, don't force a yield
26                sout | id | "Calling unpark" | (force ? "(Force)" : "(No Force)") | i;
27                unpark(this, force);
28
29                // Force a preemption before the call to park
30                int prev = global;
31                while(prev == global) {}
32
33                // Park this thread,
34                sout | id | "Parking"  | (force ? "(Force)" : "(No Force)") | i;
35                park();
36                sout | id | "Unparked" | (force ? "(Force)" : "(No Force)") | i;
37        }
38}
39
40volatile int count = 0;
41
42void main(Waiter & this) {
43        // Get a unique id
44        int id = __atomic_fetch_add(&count, 1, __ATOMIC_SEQ_CST);
45
46        // First without forcing yield
47        park_loop( this, id, false );
48
49        // First with forcing yield
50        park_loop( this, id, true  );
51}
52
53int main() {
54        Poller p;
55        {
56                Waiter waiters[5];
57        }
58}
Note: See TracBrowser for help on using the repository browser.