source: tests/collections/atomic_mpsc.cfa @ 68ff3de

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 68ff3de was cd26abf, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added tests for the mpsc lock free queue

  • Property mode set to 100644
File size: 1.4 KB
Line 
1#include <fstream.hfa>
2#include <queueLockFree.hfa>
3#include <thread.hfa>
4
5const unsigned int num_times = 50000;
6
7struct some_node {
8        some_node * volatile next;
9        single_sem sem;
10        unsigned value;
11};
12
13void ?{}(some_node & this) { this.next = 0p; }
14
15static inline some_node * volatile & ?`next ( some_node * node ) {
16        return node->next;
17}
18
19mpsc_queue(some_node) global_queue;
20
21void thrash() {
22        unsigned t[100];
23        for(i; 100) {
24                t[i] = 0xDEADBEEF;
25        }
26}
27
28thread Producer {
29        size_t sum;
30};
31void ?{}( Producer & this ) { this.sum = 0; }
32
33void wait(Producer & this) {
34        some_node node;
35        node.value = (unsigned)&this;
36        push(global_queue, &node);
37        wait(node.sem);
38        this.sum += node.value;
39}
40
41void main(Producer & this) {
42        for(num_times) {
43                thrash();
44                wait(this);
45                thrash();
46                yield(random(10));
47        }
48}
49
50thread Consumer {
51        size_t sum;
52};
53void ?{}(Consumer & this) { this.sum = 0; }
54void main(Consumer & this) {
55        LOOP: for() {
56                waitfor( ^?{} : this) {
57                        break LOOP;
58                }
59                or else {}
60
61                some_node * node = pop(global_queue);
62                if(node) {
63                        this.sum += node->value;
64                        post(node->sem);
65                }
66                else yield();
67        }
68}
69
70int main() {
71        unsigned psum = 0;
72        unsigned csum = -32;
73        processor p[2];
74        sout | "Starting";
75        {
76                Consumer cons;
77                {
78                        Producer prods[13];
79                        for(i; 13) {
80                                psum += join(prods[i]).sum;
81                        }
82                }
83                csum = join(cons).sum;
84        }
85        sout | "Done!";
86        if(psum == csum) sout | "Match!";
87        else sout | "No Match!";
88}
Note: See TracBrowser for help on using the repository browser.