From ae8adff189902c9944975aa25de781e8fd4c40be Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Wed, 20 Sep 2023 15:31:04 +0300 Subject: [PATCH] Extract atomic-gcc-norace example from klib --- .../03-practical/46-atomic-gcc-norace.c | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/regression/03-practical/46-atomic-gcc-norace.c diff --git a/tests/regression/03-practical/46-atomic-gcc-norace.c b/tests/regression/03-practical/46-atomic-gcc-norace.c new file mode 100644 index 0000000000..b5dd0f68a0 --- /dev/null +++ b/tests/regression/03-practical/46-atomic-gcc-norace.c @@ -0,0 +1,34 @@ +// Race-free due to GCC atomic operation. +// Extracted from concrat/klib. +#include +#include +#include +extern int __VERIFIER_nondet_int(); + +int data; + +void *thread(void *arg) { + __sync_fetch_and_add(&data, 1); // NORACE + return NULL; +} + +int main() { + int threads_total = __VERIFIER_nondet_int(); + __goblint_assume(threads_total >= 0); + + pthread_t *tids = malloc(threads_total * sizeof(pthread_t)); + + // create threads + for (int i = 0; i < threads_total; i++) { + pthread_create(&tids[i], NULL, &thread, NULL); // may fail but doesn't matter + } + + // join threads + for (int i = 0; i < threads_total; i++) { + pthread_join(tids[i], NULL); + } + + free(tids); + + return 0; +}