| 1 | #include <fstream.hfa>
 | 
|---|
| 2 | #include <kernel.hfa>
 | 
|---|
| 3 | #include <thread.hfa>
 | 
|---|
| 4 | #include <time.hfa>
 | 
|---|
| 5 | #include <stdlib.hfa>
 | 
|---|
| 6 | #include <string.h>
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #define __kick_rate 150000ul
 | 
|---|
| 9 | #include "long_tests.hfa"
 | 
|---|
| 10 | 
 | 
|---|
| 11 | Duration default_preemption() {
 | 
|---|
| 12 |         return 0;
 | 
|---|
| 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)
 | 
|---|
| 22 |         static inline void print(const char * const text ) {
 | 
|---|
| 23 |                 write( STDOUT_FILENO, text, strlen(text) );
 | 
|---|
| 24 |         }
 | 
|---|
| 25 | #else
 | 
|---|
| 26 |         static inline void print(Printer & this, const char * const text ) {}
 | 
|---|
| 27 | #endif
 | 
|---|
| 28 | 
 | 
|---|
| 29 | generator Coroutine { int i; };
 | 
|---|
| 30 | 
 | 
|---|
| 31 | volatile bool done = false;
 | 
|---|
| 32 | Coroutine * volatile the_cor = 0p;
 | 
|---|
| 33 | 
 | 
|---|
| 34 | void store(Coroutine & cor) {
 | 
|---|
| 35 |         __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST);
 | 
|---|
| 36 | }
 | 
|---|
| 37 | 
 | 
|---|
| 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;
 | 
|---|
| 43 | }
 | 
|---|
| 44 | 
 | 
|---|
| 45 | void main(Coroutine& this) {
 | 
|---|
| 46 |         this.i = 0;
 | 
|---|
| 47 |         suspend;
 | 
|---|
| 48 |         for(;TEST(this.i < N); this.i++) {
 | 
|---|
| 49 | 
 | 
|---|
| 50 |                 print("C - Suspending\n");
 | 
|---|
| 51 |                 suspend{
 | 
|---|
| 52 |                         print("C - Publishing\n");
 | 
|---|
| 53 |                         assert(!the_cor);
 | 
|---|
| 54 |                         store( this );
 | 
|---|
| 55 |                 }
 | 
|---|
| 56 |                 assert(!the_cor);
 | 
|---|
| 57 |                 print("C - Back\n");
 | 
|---|
| 58 |                 KICK_WATCHDOG;
 | 
|---|
| 59 |                 yield();
 | 
|---|
| 60 |         }
 | 
|---|
| 61 |         done = true;
 | 
|---|
| 62 |         suspend;
 | 
|---|
| 63 | }
 | 
|---|
| 64 | 
 | 
|---|
| 65 | thread Thread {};
 | 
|---|
| 66 | void main(Thread & this) {
 | 
|---|
| 67 |         Coroutine * mine = 0p;
 | 
|---|
| 68 |         while(!done) {
 | 
|---|
| 69 |                 yield();
 | 
|---|
| 70 | 
 | 
|---|
| 71 |                 mine = take();
 | 
|---|
| 72 |                 if(!mine) continue;
 | 
|---|
| 73 | 
 | 
|---|
| 74 |                 print("T - took\n");
 | 
|---|
| 75 |                 resume(*mine);
 | 
|---|
| 76 |         }
 | 
|---|
| 77 | }
 | 
|---|
| 78 | 
 | 
|---|
| 79 | 
 | 
|---|
| 80 | int main(int argc, char* argv[]) {
 | 
|---|
| 81 |         processor p[2];
 | 
|---|
| 82 |         Coroutine c;
 | 
|---|
| 83 |         resume(c); // Prime the coroutine to avoid one of the threads being its starter
 | 
|---|
| 84 |         the_cor = &c;
 | 
|---|
| 85 |         {
 | 
|---|
| 86 |                 Thread t[2];
 | 
|---|
| 87 |         }
 | 
|---|
| 88 | }
 | 
|---|