source: tests/concurrent/preempt.cfa

Last change on this file was 7b2c8c3c, checked in by Thierry Delisle <tdelisle@…>, 10 months ago

Moved globals to a struct so maybe gcc won't just optimize out my debug information.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1#include <clock.hfa>
2#include <fstream.hfa>
3#include <kernel.hfa>
4#include <thread.hfa>
5#include <time.hfa>
6
7#include "long_tests.hfa"
8
9#ifndef PREEMPTION_RATE
10#define PREEMPTION_RATE 10`ms
11#endif
12
13Duration default_preemption() {
14        return PREEMPTION_RATE;
15}
16
17#ifdef TEST_LONG
18static const unsigned long N = 30_000ul;
19#else
20static const unsigned long N = 500ul;
21#endif
22
23extern void __cfaabi_check_preemption();
24
25static struct {
26        volatile int counter;
27        volatile Time prev;
28        Duration durations[6];
29} globals;
30
31thread worker_t {
32        int value;
33        unsigned long long spin;
34};
35
36void ?{}( worker_t & this, int value ) {
37        this.value = value;
38        this.spin = 0;
39}
40
41void main(worker_t & this) {
42        while(TEST(globals.counter < N)) {
43                if(this.spin > 50_000_000_000) abort | "Worker" | this.value | "has been spinning too long! (" | this.spin | ")";
44                __cfaabi_check_preemption();
45                if( (globals.counter % 7) == this.value ) {
46                        __cfaabi_check_preemption();
47                        #if !defined(TEST_LONG)
48                                Time now = timeHiRes();
49                                Duration diff = now - globals.prev;
50                                globals.prev = now;
51                        #endif
52                        int next = __atomic_add_fetch( &globals.counter, 1, __ATOMIC_SEQ_CST );
53                        __cfaabi_check_preemption();
54                        if( (next % 100) == 0 ) {
55                                #if !defined(TEST_LONG)
56                                        unsigned idx = next / 100;
57                                        if (idx >= 6) abort | "Idx from next is invalid: " | idx | "vs" | next;
58                                        globals.durations[idx] = diff;
59                                        if(diff > 12`s) serr | "Duration suspiciously large:" | diff;
60                                #endif
61                                printf("%d\n", (int)next);
62
63                        }
64                        __cfaabi_check_preemption();
65                        this.spin = 0;
66                }
67                __cfaabi_check_preemption();
68                KICK_WATCHDOG;
69                this.spin++;
70        }
71}
72
73int main(int argc, char* argv[]) {
74        processor p;
75        globals.counter = 0;
76        globals.durations[0] = 0;
77        globals.durations[1] = 0;
78        globals.durations[2] = 0;
79        globals.durations[3] = 0;
80        globals.durations[4] = 0;
81        globals.durations[5] = 0;
82        {
83                globals.prev = timeHiRes();
84                worker_t w0 = 0;
85                worker_t w1 = 1;
86                worker_t w2 = 2;
87                worker_t w3 = 3;
88                worker_t w4 = 4;
89                worker_t w5 = 5;
90                worker_t w6 = 6;
91        }
92}
Note: See TracBrowser for help on using the repository browser.