[5b11c25] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <kernel.hfa> |
---|
| 3 | #include <stdlib.hfa> |
---|
| 4 | #include <thread.hfa> |
---|
| 5 | #include <monitor.hfa> |
---|
| 6 | #include <time.hfa> |
---|
| 7 | |
---|
| 8 | #define __kick_rate 150000ul |
---|
| 9 | #include "long_tests.hfa" |
---|
| 10 | |
---|
| 11 | #ifndef PREEMPTION_RATE |
---|
| 12 | #define PREEMPTION_RATE 10`ms |
---|
| 13 | #endif |
---|
| 14 | |
---|
| 15 | Duration default_preemption() { |
---|
| 16 | return PREEMPTION_RATE; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | #ifdef TEST_LONG |
---|
| 20 | static const unsigned long N = 600_000ul; |
---|
| 21 | #else |
---|
| 22 | static const unsigned long N = 1_000ul; |
---|
| 23 | #endif |
---|
| 24 | |
---|
| 25 | monitor Printer {}; |
---|
| 26 | #if !defined(TEST_FOREVER) |
---|
| 27 | static inline void print(Printer & mutex this, const char * const text ) { |
---|
| 28 | sout | text; |
---|
| 29 | } |
---|
[b9696a8] | 30 | |
---|
| 31 | static inline void print(Printer & mutex this, const char * const text, int i ) { |
---|
| 32 | sout | text | i; |
---|
| 33 | } |
---|
[5b11c25] | 34 | #else |
---|
| 35 | static inline void print(Printer & this, const char * const text ) {} |
---|
[b9696a8] | 36 | static inline void print(Printer & this, const char * const text, int i ) {} |
---|
[5b11c25] | 37 | #endif |
---|
| 38 | Printer printer; |
---|
| 39 | |
---|
| 40 | coroutine Coroutine {}; |
---|
| 41 | |
---|
| 42 | volatile bool done = false; |
---|
| 43 | Coroutine * volatile the_cor = 0p; |
---|
| 44 | |
---|
[b9696a8] | 45 | void store(Coroutine & cor) { |
---|
| 46 | __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST); |
---|
[5b11c25] | 47 | } |
---|
| 48 | |
---|
[b9696a8] | 49 | Coroutine * take(void) { |
---|
| 50 | Coroutine * val = 0p; |
---|
| 51 | Coroutine * ret = __atomic_exchange_n(&the_cor, val, __ATOMIC_SEQ_CST); |
---|
| 52 | assert(!ret || !the_cor); |
---|
| 53 | return ret; |
---|
[5b11c25] | 54 | } |
---|
| 55 | |
---|
| 56 | void main(Coroutine& this) { |
---|
[b9696a8] | 57 | suspend(); |
---|
[5b11c25] | 58 | for(int i = 0; TEST(i < N); i++) { |
---|
| 59 | |
---|
[b9696a8] | 60 | print(printer, "Coroutine 1", i); |
---|
[5b11c25] | 61 | void publish() { |
---|
| 62 | assert(!the_cor); |
---|
[b9696a8] | 63 | store( this ); |
---|
[5b11c25] | 64 | } |
---|
| 65 | suspend_then(publish); |
---|
[b9696a8] | 66 | assert(!the_cor); |
---|
[5b11c25] | 67 | print(printer, "Coroutine 2"); |
---|
| 68 | KICK_WATCHDOG; |
---|
| 69 | yield(); |
---|
| 70 | } |
---|
| 71 | done = true; |
---|
[b9696a8] | 72 | suspend(); |
---|
[5b11c25] | 73 | } |
---|
| 74 | |
---|
| 75 | thread Thread {}; |
---|
| 76 | void main(Thread & this) { |
---|
| 77 | Coroutine * mine = 0p; |
---|
| 78 | while(!done) { |
---|
| 79 | yield(); |
---|
| 80 | |
---|
[b9696a8] | 81 | mine = take(); |
---|
[5b11c25] | 82 | if(!mine) continue; |
---|
| 83 | |
---|
| 84 | print(printer, "Thread 1"); |
---|
| 85 | resume(*mine); |
---|
| 86 | print(printer, "Thread 2"); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | int main(int argc, char* argv[]) { |
---|
| 92 | processor p[2]; |
---|
| 93 | Coroutine c; |
---|
[b9696a8] | 94 | resume(c); // Prime the coroutine to avoid one of the threads being its starter |
---|
[5b11c25] | 95 | the_cor = &c; |
---|
| 96 | { |
---|
| 97 | Thread t[2]; |
---|
| 98 | } |
---|
| 99 | } |
---|