|
1 | 1 | // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
|
2 | 2 |
|
| 3 | +#include "test.h" |
| 4 | +#include <assert.h> |
3 | 5 | #include <sanitizer/tsan_interface.h>
|
4 | 6 | #include <stdio.h>
|
5 | 7 |
|
| 8 | +// We only enter a potentially blocking region on thread contention. To reliably |
| 9 | +// trigger this, we force the initialization function to block until another |
| 10 | +// thread has entered the potentially blocking region. |
| 11 | + |
| 12 | +static bool init_done = false; |
| 13 | + |
6 | 14 | namespace __tsan {
|
7 | 15 |
|
8 | 16 | #if (__APPLE__)
|
9 | 17 | __attribute__((weak))
|
10 | 18 | #endif
|
11 | 19 | void OnPotentiallyBlockingRegionBegin() {
|
12 |
| - printf("Enter __cxa_guard_acquire\n"); |
| 20 | + assert(!init_done); |
| 21 | + printf("Enter potentially blocking region\n"); |
| 22 | + // Signal the other thread to finish initialization. |
| 23 | + barrier_wait(&barrier); |
13 | 24 | }
|
14 | 25 |
|
15 | 26 | #if (__APPLE__)
|
16 | 27 | __attribute__((weak))
|
17 | 28 | #endif
|
18 |
| -void OnPotentiallyBlockingRegionEnd() { printf("Exit __cxa_guard_acquire\n"); } |
| 29 | +void OnPotentiallyBlockingRegionEnd() { |
| 30 | + printf("Exit potentially blocking region\n"); |
| 31 | +} |
19 | 32 |
|
20 | 33 | } // namespace __tsan
|
21 | 34 |
|
| 35 | +struct 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 | + |
| 45 | +const LazyInit &get_lazy_init() { |
| 46 | + static const LazyInit lazy_init; |
| 47 | + return lazy_init; |
| 48 | +} |
| 49 | + |
| 50 | +void *thread(void *arg) { |
| 51 | + get_lazy_init(); |
| 52 | + return nullptr; |
| 53 | +} |
| 54 | + |
| 55 | +struct LazyInit2 { |
| 56 | + LazyInit2() { printf("Enter constructor 2\n"); } |
| 57 | +}; |
| 58 | + |
| 59 | +const LazyInit2 &get_lazy_init2() { |
| 60 | + static const LazyInit2 lazy_init2; |
| 61 | + return lazy_init2; |
| 62 | +} |
| 63 | + |
22 | 64 | int main(int argc, char **argv) {
|
23 | 65 | // CHECK: Enter main
|
24 | 66 | printf("Enter main\n");
|
25 |
| - // CHECK-NEXT: Enter __cxa_guard_acquire |
26 |
| - // CHECK-NEXT: Exit __cxa_guard_acquire |
27 |
| - static int s = argc; |
28 |
| - (void)s; |
| 67 | + |
| 68 | + // If initialization is contended, the blocked thread should enter a |
| 69 | + // potentially blocking region. |
| 70 | + // |
| 71 | + // CHECK-NEXT: Enter constructor |
| 72 | + // CHECK-NEXT: Enter potentially blocking region |
| 73 | + // CHECK-NEXT: Exit constructor |
| 74 | + // CHECK-NEXT: Exit potentially blocking region |
| 75 | + barrier_init(&barrier, 2); |
| 76 | + pthread_t th1, th2; |
| 77 | + pthread_create(&th1, nullptr, thread, nullptr); |
| 78 | + pthread_create(&th2, nullptr, thread, nullptr); |
| 79 | + pthread_join(th1, nullptr); |
| 80 | + pthread_join(th2, nullptr); |
| 81 | + |
| 82 | + // Now that the value has been initialized, subsequent calls should not enter |
| 83 | + // a potentially blocking region. |
| 84 | + init_done = true; |
| 85 | + get_lazy_init(); |
| 86 | + |
| 87 | + // If uncontended, there is no potentially blocking region. |
| 88 | + // |
| 89 | + // CHECK-NEXT: Enter constructor 2 |
| 90 | + get_lazy_init2(); |
| 91 | + get_lazy_init2(); |
| 92 | + |
29 | 93 | // CHECK-NEXT: Exit main
|
30 | 94 | printf("Exit main\n");
|
31 | 95 | return 0;
|
|
0 commit comments