Skip to content

Commit 88a1c91

Browse files
committed
c++: move atomic in from cpp-cheat
TODO: README improve, link to x86 LOCK docs
1 parent e30f53e commit 88a1c91

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,8 @@ def _build_one(
15411541
not my_path_properties['extra_objs_disable_baremetal_bootloader']
15421542
):
15431543
extra_objs.extend(extra_objs_baremetal_bootloader)
1544+
if self.env['mode'] == 'userland':
1545+
cc_flags_after.extend(['-pthread', LF])
15441546
if self.need_rebuild([in_path] + extra_objs + extra_deps, out_path):
15451547
cc_flags.extend(my_path_properties['cc_flags'])
15461548
cc_flags_after.extend(my_path_properties['cc_flags_after'])

path_properties.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,6 @@ def get(path):
507507
'proc_events.c': {'requires_sudo': True},
508508
'sched_getaffinity.c': {'requires_syscall_getcpu': True},
509509
'sched_getaffinity_threads.c': {
510-
'cc_flags_after': ['-pthread', LF],
511510
'more_than_1s': True,
512511
'requires_syscall_getcpu': True,
513512
},

userland/cpp/atomic.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,31 @@
1616
#if __cplusplus >= 201103L
1717
std::atomic_ulong my_atomic_ulong(0);
1818
unsigned long my_non_atomic_ulong = 0;
19+
#if defined(__x86_64__)
20+
unsigned long my_arch_atomic_ulong = 0;
21+
unsigned long my_arch_non_atomic_ulong = 0;
22+
#endif
1923
size_t niters;
2024

2125
void threadMain() {
2226
for (size_t i = 0; i < niters; ++i) {
2327
my_atomic_ulong++;
2428
my_non_atomic_ulong++;
29+
#if defined(__x86_64__)
30+
__asm__ __volatile__ (
31+
"incq %0;"
32+
: "+m" (my_arch_non_atomic_ulong)
33+
:
34+
:
35+
);
36+
__asm__ __volatile__ (
37+
"lock;"
38+
"incq %0;"
39+
: "+m" (my_arch_atomic_ulong)
40+
:
41+
:
42+
);
43+
#endif
2544
}
2645
}
2746
#endif
@@ -37,16 +56,20 @@ int main(int argc, char **argv) {
3756
if (argc > 2) {
3857
niters = std::stoull(argv[2], NULL, 0);
3958
} else {
40-
niters = 1000;
59+
niters = 10000;
4160
}
4261
std::vector<std::thread> threads(nthreads);
4362
for (size_t i = 0; i < nthreads; ++i)
4463
threads[i] = std::thread(threadMain);
4564
for (size_t i = 0; i < nthreads; ++i)
4665
threads[i].join();
4766
assert(my_atomic_ulong.load() == nthreads * niters);
48-
// Same as above through `operator T`.
49-
assert(my_atomic_ulong == nthreads * niters);
50-
std::cout << my_non_atomic_ulong << std::endl;
67+
// We can also use the atomics direclty through `operator T` conversion.
68+
assert(my_atomic_ulong == my_atomic_ulong.load());
69+
std::cout << "my_non_atomic_ulong " << my_non_atomic_ulong << std::endl;
70+
#if defined(__x86_64__)
71+
assert(my_arch_atomic_ulong == nthreads * niters);
72+
std::cout << "my_arch_non_atomic_ulong " << my_arch_non_atomic_ulong << std::endl;
73+
#endif
5174
#endif
5275
}

0 commit comments

Comments
 (0)