CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
104 lines
2.8 KiB
C++
104 lines
2.8 KiB
C++
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
|
|
|
|
#include "test.h"
|
|
#include <assert.h>
|
|
#include <sanitizer/tsan_interface.h>
|
|
#include <stdio.h>
|
|
|
|
// We only enter a potentially blocking region on thread contention. To reliably
|
|
// trigger this, we force the initialization function to block until another
|
|
// thread has entered the potentially blocking region.
|
|
|
|
static bool init_done = false;
|
|
|
|
namespace __tsan {
|
|
|
|
#if (__APPLE__)
|
|
__attribute__((weak))
|
|
#endif
|
|
void OnPotentiallyBlockingRegionBegin() {
|
|
assert(!init_done);
|
|
printf("Enter potentially blocking region\n");
|
|
// Signal the other thread to finish initialization.
|
|
barrier_wait(&barrier);
|
|
}
|
|
|
|
#if (__APPLE__)
|
|
__attribute__((weak))
|
|
#endif
|
|
void OnPotentiallyBlockingRegionEnd() {
|
|
printf("Exit potentially blocking region\n");
|
|
}
|
|
|
|
} // namespace __tsan
|
|
|
|
struct LazyInit {
|
|
LazyInit() {
|
|
assert(!init_done);
|
|
printf("Enter constructor\n");
|
|
// Wait for the other thread to get to the blocking region.
|
|
barrier_wait(&barrier);
|
|
printf("Exit constructor\n");
|
|
}
|
|
};
|
|
|
|
const LazyInit &get_lazy_init() {
|
|
static const LazyInit lazy_init;
|
|
return lazy_init;
|
|
}
|
|
|
|
void *thread(void *arg) {
|
|
get_lazy_init();
|
|
return nullptr;
|
|
}
|
|
|
|
struct LazyInit2 {
|
|
LazyInit2() { printf("Enter constructor 2\n"); }
|
|
};
|
|
|
|
const LazyInit2 &get_lazy_init2() {
|
|
static const LazyInit2 lazy_init2;
|
|
return lazy_init2;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
// CHECK: Enter main
|
|
printf("Enter main\n");
|
|
|
|
// If initialization is contended, the blocked thread should enter a
|
|
// potentially blocking region. Note that we use a DAG check because it is
|
|
// possible for Thread 1 to acquire the guard, then Thread 2 fail to acquire
|
|
// the guard then call `OnPotentiallyBlockingRegionBegin` and print "Enter
|
|
// potentially blocking region\n", before Thread 1 manages to reach "Enter
|
|
// constructor\n". This is exceptionally rare, but can be replicated by
|
|
// inserting a `sleep(1)` between `LazyInit() {` and `printf("Enter
|
|
// constructor\n");`. Due to the barrier it is not possible for the exit logs
|
|
// to be inverted.
|
|
//
|
|
// CHECK-DAG: Enter constructor
|
|
// CHECK-DAG: Enter potentially blocking region
|
|
// CHECK-NEXT: Exit constructor
|
|
// CHECK-NEXT: Exit potentially blocking region
|
|
barrier_init(&barrier, 2);
|
|
pthread_t th1, th2;
|
|
pthread_create(&th1, nullptr, thread, nullptr);
|
|
pthread_create(&th2, nullptr, thread, nullptr);
|
|
pthread_join(th1, nullptr);
|
|
pthread_join(th2, nullptr);
|
|
|
|
// Now that the value has been initialized, subsequent calls should not enter
|
|
// a potentially blocking region.
|
|
init_done = true;
|
|
get_lazy_init();
|
|
|
|
// If uncontended, there is no potentially blocking region.
|
|
//
|
|
// CHECK-NEXT: Enter constructor 2
|
|
get_lazy_init2();
|
|
get_lazy_init2();
|
|
|
|
// CHECK-NEXT: Exit main
|
|
printf("Exit main\n");
|
|
return 0;
|
|
}
|