source:
tests/concurrency/suspend_then.cfa
@
b7b3e41
Last change on this file since b7b3e41 was c26bea2a, checked in by , 18 months ago | |
---|---|
|
|
File size: 1.5 KB |
Rev | Line | |
---|---|---|
[5b11c25] | 1 | #include <fstream.hfa> |
2 | #include <kernel.hfa> | |
[f019069] | 3 | #include <thread.hfa> |
[5b11c25] | 4 | #include <time.hfa> |
[f019069] | 5 | #include <stdlib.hfa> |
6 | #include <string.h> | |
[5b11c25] | 7 | |
8 | #define __kick_rate 150000ul | |
9 | #include "long_tests.hfa" | |
10 | ||
11 | Duration default_preemption() { | |
[dfa4360] | 12 | return 0; |
[5b11c25] | 13 | } |
14 | ||
15 | #ifdef TEST_LONG | |
16 | static const unsigned long N = 600_000ul; | |
17 | #else | |
18 | static const unsigned long N = 1_000ul; | |
19 | #endif | |
20 | ||
21 | #if !defined(TEST_FOREVER) | |
[f019069] | 22 | static inline void print(const char * const text ) { |
[dfa4360] | 23 | write( STDOUT_FILENO, text, strlen(text) ); |
[b9696a8] | 24 | } |
[5b11c25] | 25 | #else |
26 | static inline void print(Printer & this, const char * const text ) {} | |
27 | #endif | |
28 | ||
[dfa4360] | 29 | generator Coroutine { int i; }; |
[5b11c25] | 30 | |
31 | volatile bool done = false; | |
32 | Coroutine * volatile the_cor = 0p; | |
33 | ||
[b9696a8] | 34 | void store(Coroutine & cor) { |
35 | __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST); | |
[5b11c25] | 36 | } |
37 | ||
[b9696a8] | 38 | Coroutine * take(void) { |
39 | Coroutine * val = 0p; | |
40 | Coroutine * ret = __atomic_exchange_n(&the_cor, val, __ATOMIC_SEQ_CST); | |
41 | assert(!ret || !the_cor); | |
42 | return ret; | |
[5b11c25] | 43 | } |
44 | ||
45 | void main(Coroutine& this) { | |
[dfa4360] | 46 | this.i = 0; |
[427854b] | 47 | suspend; |
[dfa4360] | 48 | for(;TEST(this.i < N); this.i++) { |
[5b11c25] | 49 | |
[dfa4360] | 50 | print("C - Suspending\n"); |
[427854b] | 51 | suspend{ |
[dfa4360] | 52 | print("C - Publishing\n"); |
[5b11c25] | 53 | assert(!the_cor); |
[b9696a8] | 54 | store( this ); |
[5b11c25] | 55 | } |
[b9696a8] | 56 | assert(!the_cor); |
[dfa4360] | 57 | print("C - Back\n"); |
[5b11c25] | 58 | KICK_WATCHDOG; |
59 | yield(); | |
60 | } | |
61 | done = true; | |
[427854b] | 62 | suspend; |
[5b11c25] | 63 | } |
64 | ||
65 | thread Thread {}; | |
66 | void main(Thread & this) { | |
67 | Coroutine * mine = 0p; | |
68 | while(!done) { | |
69 | yield(); | |
70 | ||
[b9696a8] | 71 | mine = take(); |
[5b11c25] | 72 | if(!mine) continue; |
73 | ||
[dfa4360] | 74 | print("T - took\n"); |
[5b11c25] | 75 | resume(*mine); |
76 | } | |
77 | } | |
78 | ||
79 | ||
80 | int main(int argc, char* argv[]) { | |
81 | processor p[2]; | |
82 | Coroutine c; | |
[b9696a8] | 83 | resume(c); // Prime the coroutine to avoid one of the threads being its starter |
[5b11c25] | 84 | the_cor = &c; |
85 | { | |
86 | Thread t[2]; | |
87 | } | |
88 | } |
Note: See TracBrowser
for help on using the repository browser.