| 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 | }
|
|---|
| 30 |
|
|---|
| 31 | static inline void print(Printer & mutex this, const char * const text, int i ) {
|
|---|
| 32 | sout | text | i;
|
|---|
| 33 | }
|
|---|
| 34 | #else
|
|---|
| 35 | static inline void print(Printer & this, const char * const text ) {}
|
|---|
| 36 | static inline void print(Printer & this, const char * const text, int i ) {}
|
|---|
| 37 | #endif
|
|---|
| 38 | Printer printer;
|
|---|
| 39 |
|
|---|
| 40 | coroutine Coroutine {};
|
|---|
| 41 |
|
|---|
| 42 | volatile bool done = false;
|
|---|
| 43 | Coroutine * volatile the_cor = 0p;
|
|---|
| 44 |
|
|---|
| 45 | void store(Coroutine & cor) {
|
|---|
| 46 | __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 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;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void main(Coroutine& this) {
|
|---|
| 57 | suspend();
|
|---|
| 58 | for(int i = 0; TEST(i < N); i++) {
|
|---|
| 59 |
|
|---|
| 60 | print(printer, "Coroutine 1", i);
|
|---|
| 61 | void publish() {
|
|---|
| 62 | assert(!the_cor);
|
|---|
| 63 | store( this );
|
|---|
| 64 | }
|
|---|
| 65 | suspend_then(publish);
|
|---|
| 66 | assert(!the_cor);
|
|---|
| 67 | print(printer, "Coroutine 2");
|
|---|
| 68 | KICK_WATCHDOG;
|
|---|
| 69 | yield();
|
|---|
| 70 | }
|
|---|
| 71 | done = true;
|
|---|
| 72 | suspend();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | thread Thread {};
|
|---|
| 76 | void main(Thread & this) {
|
|---|
| 77 | Coroutine * mine = 0p;
|
|---|
| 78 | while(!done) {
|
|---|
| 79 | yield();
|
|---|
| 80 |
|
|---|
| 81 | mine = take();
|
|---|
| 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;
|
|---|
| 94 | resume(c); // Prime the coroutine to avoid one of the threads being its starter
|
|---|
| 95 | the_cor = &c;
|
|---|
| 96 | {
|
|---|
| 97 | Thread t[2];
|
|---|
| 98 | }
|
|---|
| 99 | }
|
|---|