Ignore:
Timestamp:
May 1, 2019, 3:23:01 PM (3 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
arm-eh, cleanup-dtors, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr
Children:
f019069
Parents:
8278abf
Message:

Improved test for suspend_then feature

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/concurrent/coroutineThen.cfa

    r8278abf rb9696a8  
    2828                sout | text;
    2929        }
     30
     31        static inline void print(Printer & mutex this, const char * const text, int i ) {
     32                sout | text | i;
     33        }
    3034#else
    3135        static inline void print(Printer & this, const char * const text ) {}
     36        static inline void print(Printer & this, const char * const text, int i ) {}
    3237#endif
    3338Printer printer;
     
    3843Coroutine * volatile the_cor = 0p;
    3944
    40 void store(Coroutine * volatile * target, Coroutine * value) {
    41         long long int val = value;
    42         volatile long long int * ptr = target;
    43         __atomic_store(ptr, &val, __ATOMIC_SEQ_CST);
     45void store(Coroutine & cor) {
     46        __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST);
    4447}
    4548
    46 Coroutine * exchange(Coroutine * volatile * target) {
    47         long long int ret;
    48         volatile long long int * ptr = target;
    49         long long int val = 0;
    50         ret = __atomic_exchange_n(ptr, &val, __ATOMIC_SEQ_CST);
    51         assert(ret == 0 || *ptr == 0);
    52         return (Coroutine *)ret;
     49Coroutine * 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;
    5354}
    5455
    5556void main(Coroutine& this) {
     57        suspend();
    5658        for(int i = 0; TEST(i < N); i++) {
    5759
    58                 print(printer, "Coroutine 1");
     60                print(printer, "Coroutine 1", i);
    5961                void publish() {
    6062                        assert(!the_cor);
    61                         store( &the_cor, &this );
     63                        store( this );
    6264                }
    6365                suspend_then(publish);
     66                assert(!the_cor);
    6467                print(printer, "Coroutine 2");
    6568                KICK_WATCHDOG;
     
    6770        }
    6871        done = true;
     72        suspend();
    6973}
    7074
     
    7579                yield();
    7680
    77                 mine = exchange( &the_cor );
     81                mine = take();
    7882                if(!mine) continue;
    7983
     
    8892        processor p[2];
    8993        Coroutine c;
     94        resume(c); // Prime the coroutine to avoid one of the threads being its starter
    9095        the_cor = &c;
    9196        {
Note: See TracChangeset for help on using the changeset viewer.