source: tests/concurrency/suspend_then.cfa @ 503c350

Last change on this file since 503c350 was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 18 months ago

first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names

  • Property mode set to 100644
File size: 1.5 KB
Line 
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
11Duration default_preemption() {
12        return 0;
13}
14
15#ifdef TEST_LONG
16static const unsigned long N = 600_000ul;
17#else
18static 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
29generator Coroutine { int i; };
30
31volatile bool done = false;
32Coroutine * volatile the_cor = 0p;
33
34void store(Coroutine & cor) {
35        __atomic_store_n(&the_cor, &cor, __ATOMIC_SEQ_CST);
36}
37
38Coroutine * 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
45void 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
65thread Thread {};
66void 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
80int 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}
Note: See TracBrowser for help on using the repository browser.