source:
tests/concurrency/sleep.cfa
@
5f210c0
Last change on this file since 5f210c0 was c26bea2a, checked in by , 18 months ago | |
---|---|
|
|
File size: 717 bytes |
Rev | Line | |
---|---|---|
[c457dc41] | 1 | #include <fstream.hfa> |
2 | #include <thread.hfa> | |
[237df76] | 3 | #include <mutex_stmt.hfa> |
[c457dc41] | 4 | #include <time.hfa> |
5 | ||
6 | Duration default_preemption() { | |
7 | return 0; | |
8 | } | |
9 | ||
[7bd23ce] | 10 | volatile int counter = 0; |
11 | ||
[c457dc41] | 12 | thread fast_sleeper {}; |
13 | void main(fast_sleeper &) { | |
14 | Duration wait = 47`ms; | |
15 | for(i; 20) { | |
16 | sleep(wait); | |
[7bd23ce] | 17 | __atomic_fetch_add( &counter, 1, __ATOMIC_RELAXED ); |
[c457dc41] | 18 | } |
19 | } | |
20 | ||
21 | thread slow_sleeper {}; | |
22 | void main(slow_sleeper &) { | |
23 | Duration wait = 277`ms; | |
24 | for(i; 4) { | |
25 | sleep(wait); | |
[7bd23ce] | 26 | int val = __atomic_add_fetch( &counter, -5, __ATOMIC_RELAXED ); |
27 | if(val < -9) abort | "Value reached negative value:" | val; | |
[c457dc41] | 28 | } |
29 | } | |
30 | ||
31 | int main() { | |
[237df76] | 32 | mutex( sout ) sout | "start"; |
[c457dc41] | 33 | { |
34 | slow_sleeper slow; | |
35 | yield(); | |
36 | fast_sleeper fast; | |
37 | yield(); | |
38 | } | |
[237df76] | 39 | mutex( sout ) sout | "done"; |
[c457dc41] | 40 | } |
41 |
Note: See TracBrowser
for help on using the repository browser.