Last change
on this file since 9d5eacb was
55b060d,
checked in by Peter A. Buhr <pabuhr@…>, 14 months ago
|
rename directories containers to collections
|
-
Property mode set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[cd26abf] | 1 | #include <fstream.hfa> |
---|
[55b060d] | 2 | #include <collections/lockfree.hfa> |
---|
[cd26abf] | 3 | #include <thread.hfa> |
---|
| 4 | |
---|
| 5 | const unsigned int num_times = 50000; |
---|
| 6 | |
---|
| 7 | struct some_node { |
---|
| 8 | some_node * volatile next; |
---|
| 9 | single_sem sem; |
---|
| 10 | unsigned value; |
---|
| 11 | }; |
---|
| 12 | |
---|
| 13 | void ?{}(some_node & this) { this.next = 0p; } |
---|
| 14 | |
---|
| 15 | static inline some_node * volatile & ?`next ( some_node * node ) { |
---|
| 16 | return node->next; |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | mpsc_queue(some_node) global_queue; |
---|
| 20 | |
---|
| 21 | void thrash() { |
---|
| 22 | unsigned t[100]; |
---|
| 23 | for(i; 100) { |
---|
| 24 | t[i] = 0xDEADBEEF; |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | thread Producer { |
---|
| 29 | size_t sum; |
---|
| 30 | }; |
---|
| 31 | void ?{}( Producer & this ) { this.sum = 0; } |
---|
| 32 | |
---|
| 33 | void 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 | |
---|
| 41 | void main(Producer & this) { |
---|
| 42 | for(num_times) { |
---|
| 43 | thrash(); |
---|
| 44 | wait(this); |
---|
| 45 | thrash(); |
---|
| 46 | yield(random(10)); |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | thread Consumer { |
---|
| 51 | size_t sum; |
---|
| 52 | }; |
---|
| 53 | void ?{}(Consumer & this) { this.sum = 0; } |
---|
| 54 | void 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 | |
---|
| 70 | int 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!"; |
---|
[55b060d] | 88 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.