ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change
on this file since 3381ed7 was
3381ed7,
checked in by Thierry Delisle <tdelisle@…>, 5 years ago
|
Added park/unpark primitives thread and removed BlockInternal?.
Converted monitors to use park unpark.
Intrusive Queue now mark next field when thread is inside queue.
Added several asserts to kernel and monitor.
Added a few tests for park and unpark.
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | #include <thread.hfa> |
---|
2 | #include <fstream.hfa> |
---|
3 | |
---|
4 | volatile int global = 0; |
---|
5 | |
---|
6 | thread Poller {}; |
---|
7 | void 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 | |
---|
18 | thread Waiter; |
---|
19 | thread Waiter {}; |
---|
20 | |
---|
21 | |
---|
22 | void park_loop(Waiter & this, int id, bool force) { |
---|
23 | for(int i = 0; i < 5; i++) { |
---|
24 | // Unpark this thread, don't force a yield |
---|
25 | sout | id | "Calling unpark" | (force ? "(Force)" : "(No Force)") | i; |
---|
26 | unpark(this, force); |
---|
27 | |
---|
28 | // Force a preemption before the call to park |
---|
29 | int prev = global; |
---|
30 | while(prev == global) {} |
---|
31 | |
---|
32 | // Park this thread, |
---|
33 | sout | id | "Parking" | (force ? "(Force)" : "(No Force)") | i; |
---|
34 | park(); |
---|
35 | sout | id | "Unparked" | (force ? "(Force)" : "(No Force)") | i; |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | volatile int count = 0; |
---|
40 | |
---|
41 | void main(Waiter & this) { |
---|
42 | // Get a unique id |
---|
43 | int id = __atomic_fetch_add(&count, 1, __ATOMIC_SEQ_CST); |
---|
44 | |
---|
45 | // First without forcing yield |
---|
46 | park_loop( this, id, false ); |
---|
47 | |
---|
48 | // First with forcing yield |
---|
49 | park_loop( this, id, true ); |
---|
50 | } |
---|
51 | |
---|
52 | int main() { |
---|
53 | Poller p; |
---|
54 | { |
---|
55 | Waiter waiters[5]; |
---|
56 | } |
---|
57 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.