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