File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ #include < cassert>
3
+ #include < atomic>
4
+ #include " hc.hpp"
5
+ #include " hc_am_internal.hpp"
6
+
7
+ int main () {
8
+
9
+ constexpr unsigned long long init_value = 0 ;
10
+ constexpr unsigned long long new_value = 1234 ;
11
+
12
+ char * mem = hc::internal::am_alloc_host_coherent (sizeof (std::atomic_ullong));
13
+ std::atomic_ullong* a = new (mem) std::atomic_ullong (init_value);
14
+
15
+ hc::array_view<unsigned long long ,1 > av_old (1 );
16
+ av_old[0 ] = 0xFFFFFFFFFFFFFFFF ;
17
+
18
+ hc::array_view<bool > av_success (1 );
19
+ av_success[0 ] = false ;
20
+
21
+ hc::array_view<unsigned long long ,1 > av_load_after_cas (1 );
22
+ av_load_after_cas[0 ] = 0xFFFFFFFFFFFFFFFF ;
23
+
24
+ hc::parallel_for_each (hc::extent<1 >(1 ), [=](hc::index <1 > i) [[hc]] {
25
+ auto old = a->load ();
26
+ av_old[0 ] = old;
27
+ auto success = a->compare_exchange_weak (old, new_value);
28
+ av_success[0 ] = success;
29
+ av_load_after_cas[0 ] = a->load ();
30
+ }).wait ();
31
+
32
+ assert (init_value == av_old[0 ]);
33
+ assert (av_success[0 ]);
34
+ assert (new_value == av_load_after_cas[0 ]);
35
+
36
+ hc::am_free (mem);
37
+ return 0 ;
38
+ }
You can’t perform that action at this time.
0 commit comments