source: tests/concurrent/coroutineThen.cfa @ b9696a8

arm-ehcleanup-dtorsjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since b9696a8 was b9696a8, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Improved test for suspend_then feature

  • Property mode set to 100644
File size: 1.9 KB
Line 
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
15Duration default_preemption() {
16        return PREEMPTION_RATE;
17}
18
19#ifdef TEST_LONG
20static const unsigned long N = 600_000ul;
21#else
22static const unsigned long N = 1_000ul;
23#endif
24
25monitor 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
38Printer printer;
39
40coroutine Coroutine {};
41
42volatile bool done = false;
43Coroutine * volatile the_cor = 0p;
44
45void store(Coroutine & cor) {
46        __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST);
47}
48
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;
54}
55
56void 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
75thread Thread {};
76void 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
91int 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}
Note: See TracBrowser for help on using the repository browser.