| 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 ) with( this ) {
|
|---|
| 46 | suspend;
|
|---|
| 47 | for( i = 0; TEST( i < N ); i++ ) {
|
|---|
| 48 | print( "C - Suspending\n" );
|
|---|
| 49 | suspend{
|
|---|
| 50 | print( "C - Publishing\n" );
|
|---|
| 51 | assert( ! the_cor );
|
|---|
| 52 | store( this );
|
|---|
| 53 | }
|
|---|
| 54 | assert( ! the_cor );
|
|---|
| 55 | print( "C - Back\n" );
|
|---|
| 56 | KICK_WATCHDOG;
|
|---|
| 57 | yield();
|
|---|
| 58 | }
|
|---|
| 59 | done = true;
|
|---|
| 60 | suspend;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | thread Thread {};
|
|---|
| 64 | void main( Thread & ) {
|
|---|
| 65 | Coroutine * mine = 0p;
|
|---|
| 66 | while ( ! done ) {
|
|---|
| 67 | yield();
|
|---|
| 68 | mine = take();
|
|---|
| 69 | if ( ! mine ) continue;
|
|---|
| 70 |
|
|---|
| 71 | print( "T - took\n" );
|
|---|
| 72 | resume( *mine );
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | int main() {
|
|---|
| 78 | processor p[2];
|
|---|
| 79 | Coroutine c;
|
|---|
| 80 | resume( c ); // Prime the coroutine to avoid one of the threads being its starter
|
|---|
| 81 |
|
|---|
| 82 | #pragma GCC diagnostic push
|
|---|
| 83 | #pragma GCC diagnostic ignored "-Wdangling-pointer" // this assignment is ok
|
|---|
| 84 | the_cor = &c;
|
|---|
| 85 | #pragma GCC diagnostic pop
|
|---|
| 86 |
|
|---|
| 87 | {
|
|---|
| 88 | Thread t[2];
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|