1 | #include <atomic> |
---|
2 | #include <iostream> |
---|
3 | #include <locale> |
---|
4 | #include <thread> |
---|
5 | |
---|
6 | #include <x86intrin.h> |
---|
7 | |
---|
8 | struct __attribute__((aligned(128))) Global_t { |
---|
9 | volatile size_t value; |
---|
10 | } global; |
---|
11 | |
---|
12 | static const size_t iterations = 1'000'000'000; |
---|
13 | |
---|
14 | size_t read() { |
---|
15 | // size_t r = __atomic_load_n(&global.value, __ATOMIC_RELAXED); |
---|
16 | // _mm_stream_si64((long long int*)&global.value, r); |
---|
17 | // // _mm_clflush( (void*)&global.value ); |
---|
18 | // // __builtin_prefetch((void*)&global.value); |
---|
19 | // asm volatile( |
---|
20 | // "PREFETCHNTA %[target]" |
---|
21 | // : |
---|
22 | // : [target] "m" (global.value) |
---|
23 | // ); |
---|
24 | // return r; |
---|
25 | return __atomic_load_n(&global.value, __ATOMIC_SEQ_CST); |
---|
26 | |
---|
27 | // __m128i r = _mm_stream_load_si128((__m128i*)&global.value); |
---|
28 | // asm volatile( |
---|
29 | // "PREFETCHNTA %[target]" |
---|
30 | // : |
---|
31 | // : [target] "m" (global.value) |
---|
32 | // ); |
---|
33 | // return ((Global_t*)&r)->value; |
---|
34 | // size_t r; |
---|
35 | // asm volatile( |
---|
36 | // "MOVNTI %[target], %[r]\n\t" |
---|
37 | // : [r] "=r" (r) |
---|
38 | // : [target] "m" (global.value) |
---|
39 | // ); |
---|
40 | // return r; |
---|
41 | } |
---|
42 | |
---|
43 | void write(size_t v) { |
---|
44 | // __atomic_store_n(&global.value, v, __ATOMIC_SEQ_CST); |
---|
45 | // __atomic_store_n(&global.value, v, __ATOMIC_RELAXED); |
---|
46 | // asm volatile( |
---|
47 | // "MOVNTI %[v], %[target]\n\t" |
---|
48 | // : |
---|
49 | // : [target] "m" (global.value), [v] "r" (v) |
---|
50 | // ); |
---|
51 | _mm_stream_si64((long long int*)&global.value, v); |
---|
52 | } |
---|
53 | |
---|
54 | void reader(size_t * reads, size_t * diffs, size_t * m) { |
---|
55 | size_t last = read(); |
---|
56 | for(size_t i = 0; i < iterations; i++) { |
---|
57 | size_t val = read(); |
---|
58 | if(last != val) (*diffs)++; |
---|
59 | last = val; |
---|
60 | if(last > *m) *m = last; |
---|
61 | (*reads)++; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | std::atomic<bool> done = { false }; |
---|
66 | |
---|
67 | void writer() { |
---|
68 | size_t v = 0; |
---|
69 | while(!done) { |
---|
70 | v++; |
---|
71 | write(v); |
---|
72 | __atomic_thread_fence(__ATOMIC_SEQ_CST); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | int main() { |
---|
77 | std::cout.imbue(std::locale("")); |
---|
78 | size_t reads = 0; |
---|
79 | size_t diffs = 0; |
---|
80 | size_t max = 0; |
---|
81 | auto w = std::thread(writer); |
---|
82 | auto r = std::thread(reader, &reads, &diffs, &max); |
---|
83 | r.join(); |
---|
84 | done = true; |
---|
85 | w.join(); |
---|
86 | std::cout << reads << " " << diffs << " " << max << std::endl; |
---|
87 | } |
---|