c++: move atomic in from cpp-cheat

TODO: README improve, link to x86 LOCK docs
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-06-26 00:00:00 +00:00
parent e30f53e8a4
commit 88a1c914c9
3 changed files with 29 additions and 5 deletions

View File

@@ -1541,6 +1541,8 @@ https://github.com/cirosantilli/linux-kernel-module-cheat#gem5-debug-build
not my_path_properties['extra_objs_disable_baremetal_bootloader'] not my_path_properties['extra_objs_disable_baremetal_bootloader']
): ):
extra_objs.extend(extra_objs_baremetal_bootloader) extra_objs.extend(extra_objs_baremetal_bootloader)
if self.env['mode'] == 'userland':
cc_flags_after.extend(['-pthread', LF])
if self.need_rebuild([in_path] + extra_objs + extra_deps, out_path): if self.need_rebuild([in_path] + extra_objs + extra_deps, out_path):
cc_flags.extend(my_path_properties['cc_flags']) cc_flags.extend(my_path_properties['cc_flags'])
cc_flags_after.extend(my_path_properties['cc_flags_after']) cc_flags_after.extend(my_path_properties['cc_flags_after'])

View File

@@ -507,7 +507,6 @@ path_properties_tuples = (
'proc_events.c': {'requires_sudo': True}, 'proc_events.c': {'requires_sudo': True},
'sched_getaffinity.c': {'requires_syscall_getcpu': True}, 'sched_getaffinity.c': {'requires_syscall_getcpu': True},
'sched_getaffinity_threads.c': { 'sched_getaffinity_threads.c': {
'cc_flags_after': ['-pthread', LF],
'more_than_1s': True, 'more_than_1s': True,
'requires_syscall_getcpu': True, 'requires_syscall_getcpu': True,
}, },

View File

@@ -16,12 +16,31 @@
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
std::atomic_ulong my_atomic_ulong(0); std::atomic_ulong my_atomic_ulong(0);
unsigned long my_non_atomic_ulong = 0; unsigned long my_non_atomic_ulong = 0;
#if defined(__x86_64__)
unsigned long my_arch_atomic_ulong = 0;
unsigned long my_arch_non_atomic_ulong = 0;
#endif
size_t niters; size_t niters;
void threadMain() { void threadMain() {
for (size_t i = 0; i < niters; ++i) { for (size_t i = 0; i < niters; ++i) {
my_atomic_ulong++; my_atomic_ulong++;
my_non_atomic_ulong++; my_non_atomic_ulong++;
#if defined(__x86_64__)
__asm__ __volatile__ (
"incq %0;"
: "+m" (my_arch_non_atomic_ulong)
:
:
);
__asm__ __volatile__ (
"lock;"
"incq %0;"
: "+m" (my_arch_atomic_ulong)
:
:
);
#endif
} }
} }
#endif #endif
@@ -37,7 +56,7 @@ int main(int argc, char **argv) {
if (argc > 2) { if (argc > 2) {
niters = std::stoull(argv[2], NULL, 0); niters = std::stoull(argv[2], NULL, 0);
} else { } else {
niters = 1000; niters = 10000;
} }
std::vector<std::thread> threads(nthreads); std::vector<std::thread> threads(nthreads);
for (size_t i = 0; i < nthreads; ++i) for (size_t i = 0; i < nthreads; ++i)
@@ -45,8 +64,12 @@ int main(int argc, char **argv) {
for (size_t i = 0; i < nthreads; ++i) for (size_t i = 0; i < nthreads; ++i)
threads[i].join(); threads[i].join();
assert(my_atomic_ulong.load() == nthreads * niters); assert(my_atomic_ulong.load() == nthreads * niters);
// Same as above through `operator T`. // We can also use the atomics direclty through `operator T` conversion.
assert(my_atomic_ulong == nthreads * niters); assert(my_atomic_ulong == my_atomic_ulong.load());
std::cout << my_non_atomic_ulong << std::endl; std::cout << "my_non_atomic_ulong " << my_non_atomic_ulong << std::endl;
#if defined(__x86_64__)
assert(my_arch_atomic_ulong == nthreads * niters);
std::cout << "my_arch_non_atomic_ulong " << my_arch_non_atomic_ulong << std::endl;
#endif
#endif #endif
} }