brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 6050c24 Raw
104 lines · cpp
1// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s2 3#include "test.h"4#include <assert.h>5#include <sanitizer/tsan_interface.h>6#include <stdio.h>7 8// We only enter a potentially blocking region on thread contention. To reliably9// trigger this, we force the initialization function to block until another10// thread has entered the potentially blocking region.11 12static bool init_done = false;13 14namespace __tsan {15 16#if (__APPLE__)17__attribute__((weak))18#endif19void OnPotentiallyBlockingRegionBegin() {20  assert(!init_done);21  printf("Enter potentially blocking region\n");22  // Signal the other thread to finish initialization.23  barrier_wait(&barrier);24}25 26#if (__APPLE__)27__attribute__((weak))28#endif29void OnPotentiallyBlockingRegionEnd() {30  printf("Exit potentially blocking region\n");31}32 33} // namespace __tsan34 35struct LazyInit {36  LazyInit() {37    assert(!init_done);38    printf("Enter constructor\n");39    // Wait for the other thread to get to the blocking region.40    barrier_wait(&barrier);41    printf("Exit constructor\n");42  }43};44 45const LazyInit &get_lazy_init() {46  static const LazyInit lazy_init;47  return lazy_init;48}49 50void *thread(void *arg) {51  get_lazy_init();52  return nullptr;53}54 55struct LazyInit2 {56  LazyInit2() { printf("Enter constructor 2\n"); }57};58 59const LazyInit2 &get_lazy_init2() {60  static const LazyInit2 lazy_init2;61  return lazy_init2;62}63 64int main(int argc, char **argv) {65  // CHECK: Enter main66  printf("Enter main\n");67 68  // If initialization is contended, the blocked thread should enter a69  // potentially blocking region. Note that we use a DAG check because it is70  // possible for Thread 1 to acquire the guard, then Thread 2 fail to acquire71  // the guard then call `OnPotentiallyBlockingRegionBegin` and print "Enter72  // potentially blocking region\n", before Thread 1 manages to reach "Enter73  // constructor\n". This is exceptionally rare, but can be replicated by74  // inserting a `sleep(1)` between `LazyInit() {` and `printf("Enter75  // constructor\n");`. Due to the barrier it is not possible for the exit logs76  // to be inverted.77  //78  // CHECK-DAG: Enter constructor79  // CHECK-DAG: Enter potentially blocking region80  // CHECK-NEXT: Exit constructor81  // CHECK-NEXT: Exit potentially blocking region82  barrier_init(&barrier, 2);83  pthread_t th1, th2;84  pthread_create(&th1, nullptr, thread, nullptr);85  pthread_create(&th2, nullptr, thread, nullptr);86  pthread_join(th1, nullptr);87  pthread_join(th2, nullptr);88 89  // Now that the value has been initialized, subsequent calls should not enter90  // a potentially blocking region.91  init_done = true;92  get_lazy_init();93 94  // If uncontended, there is no potentially blocking region.95  //96  // CHECK-NEXT: Enter constructor 297  get_lazy_init2();98  get_lazy_init2();99 100  // CHECK-NEXT: Exit main101  printf("Exit main\n");102  return 0;103}104