brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.4 KiB · ca1eb3e Raw
59 lines · cpp
1// RUN: %clangxx_tsan %s -o %t2// RUN: %deflake %run %t 2>&1 | FileCheck %s3 4#include <thread>5 6#import "../test.h"7 8void *tag;9 10__attribute__((no_sanitize("thread")))11void ExternalWrite(void *addr) {12  __tsan_external_write(addr, __builtin_return_address(0), tag);13}14 15int main(int argc, char *argv[]) {16  barrier_init(&barrier, 2);17  tag = __tsan_external_register_tag("HelloWorld");18  fprintf(stderr, "Start.\n");19  // CHECK: Start.20   21  for (int i = 0; i < 4; i++) {22    void *opaque_object = malloc(16);23    std::thread t1([opaque_object] {24      ExternalWrite(opaque_object);25      barrier_wait(&barrier);26    });27    std::thread t2([opaque_object] {28      barrier_wait(&barrier);29      ExternalWrite(opaque_object);30    });31    // CHECK: WARNING: ThreadSanitizer: race on HelloWorld32    t1.join();33    t2.join();34  }35  36  fprintf(stderr, "First phase done.\n");37  // CHECK: First phase done.38 39  for (int i = 0; i < 4; i++) {40    void *opaque_object = malloc(16);41    std::thread t1([opaque_object] {42      ExternalWrite(opaque_object);43      barrier_wait(&barrier);44    });45    std::thread t2([opaque_object] {46      barrier_wait(&barrier);47      ExternalWrite(opaque_object);48    });49    // CHECK: WARNING: ThreadSanitizer: race on HelloWorld50    t1.join();51    t2.join();52  }53 54  fprintf(stderr, "Second phase done.\n");55  // CHECK: Second phase done.56}57 58// CHECK: ThreadSanitizer: reported 2 warnings59