source: tests/concurrent/coroutineThen.cfa @ 427854b

arm-ehjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 427854b was 427854b, checked in by Thierry Delisle <tdelisle@…>, 20 months ago

First draft implementation of generators, still missing error checking, testing and clean-up

  • Property mode set to 100644
File size: 1.6 KB
Line 
1#include <fstream.hfa>
2#include <kernel.hfa>
3#include <monitor.hfa>
4#include <thread.hfa>
5#include <time.hfa>
6#include <stdlib.hfa>
7#include <string.h>
8
9#define __kick_rate 150000ul
10#include "long_tests.hfa"
11
12#ifndef PREEMPTION_RATE
13#define PREEMPTION_RATE 10`ms
14#endif
15
16Duration default_preemption() {
17        return PREEMPTION_RATE;
18}
19
20#ifdef TEST_LONG
21static const unsigned long N = 600_000ul;
22#else
23static const unsigned long N = 1_000ul;
24#endif
25
26#if !defined(TEST_FOREVER)
27        static inline void print(const char * const text ) {
28                write( STDERR_FILENO, text, strlen(text) );
29        }
30#else
31        static inline void print(Printer & this, const char * const text ) {}
32#endif
33
34coroutine Coroutine {};
35
36volatile bool done = false;
37Coroutine * volatile the_cor = 0p;
38
39void store(Coroutine & cor) {
40        __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST);
41}
42
43Coroutine * take(void) {
44        Coroutine * val = 0p;
45        Coroutine * ret = __atomic_exchange_n(&the_cor, val, __ATOMIC_SEQ_CST);
46        assert(!ret || !the_cor);
47        return ret;
48}
49
50void main(Coroutine& this) {
51        suspend;
52        for(int i = 0; TEST(i < N); i++) {
53
54                print("C - Suspending");
55                suspend{
56                        print("C - Publishing");
57                        assert(!the_cor);
58                        store( this );
59                }
60                assert(!the_cor);
61                print("Coroutine 2");
62                KICK_WATCHDOG;
63                yield();
64        }
65        done = true;
66        suspend;
67}
68
69thread Thread {};
70void main(Thread & this) {
71        Coroutine * mine = 0p;
72        while(!done) {
73                yield();
74
75                mine = take();
76                if(!mine) continue;
77
78                print("T - took");
79                resume(*mine);
80                print("T - back");
81        }
82}
83
84
85int main(int argc, char* argv[]) {
86        processor p[2];
87        Coroutine c;
88        resume(c); // Prime the coroutine to avoid one of the threads being its starter
89        the_cor = &c;
90        {
91                Thread t[2];
92        }
93}
Note: See TracBrowser for help on using the repository browser.