File: compare_exchange_acquire_fence.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,999,616 kB
  • sloc: cpp: 6,951,724; ansic: 1,486,157; asm: 913,598; python: 232,059; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,079; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,430; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (43 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// RUN: %clangxx_tsan -O1 %s %link_libcxx_tsan -o %t && %run %t 2>&1
// This is a correct program and tsan should not report a race.
//
// Verify that there is a happens-before relationship between a
// memory_order_release store that happens as part of a successful
// compare_exchange_strong(), and an atomic_thread_fence(memory_order_acquire)
// that happens after a relaxed load.

#include <atomic>
#include <sanitizer/tsan_interface.h>
#include <stdbool.h>
#include <stdio.h>
#include <thread>

std::atomic<bool> a;
unsigned int b;
constexpr int loops = 100000;

void Thread1() {
  for (int i = 0; i < loops; ++i) {
    while (a.load(std::memory_order_acquire)) {
    }
    b = i;
    bool expected = false;
    a.compare_exchange_strong(expected, true, std::memory_order_acq_rel);
  }
}

int main() {
  std::thread t(Thread1);
  unsigned int sum = 0;
  for (int i = 0; i < loops; ++i) {
    while (!a.load(std::memory_order_relaxed)) {
    }
    std::atomic_thread_fence(std::memory_order_acquire);
    __tsan_acquire(&a);
    sum += b;
    a.store(false, std::memory_order_release);
  }
  t.join();
  fprintf(stderr, "DONE: %u\n", sum);
  return 0;
}