llvm-project
44 строки · 1.0 Кб
1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"3
4// OS X doesn't have pthread_setname_np(tid, name).
5// UNSUPPORTED: darwin
6
7#if defined(__FreeBSD__)8#include <pthread_np.h>9#define tsan_pthread_setname_np pthread_set_name_np10#elif defined(__NetBSD__)11#define tsan_pthread_setname_np(a, b) pthread_setname_np((a), "%s", (void *)(b))12#else13#define tsan_pthread_setname_np pthread_setname_np14#endif15
16long long Global;17
18void *Thread1(void *x) {19barrier_wait(&barrier);20Global++;21return 0;22}
23
24void *Thread2(void *x) {25tsan_pthread_setname_np(pthread_self(), "foobar2");26Global--;27barrier_wait(&barrier);28return 0;29}
30
31int main() {32barrier_init(&barrier, 3);33pthread_t t[2];34pthread_create(&t[0], 0, Thread1, 0);35pthread_create(&t[1], 0, Thread2, 0);36tsan_pthread_setname_np(t[0], "foobar1");37barrier_wait(&barrier);38pthread_join(t[0], NULL);39pthread_join(t[1], NULL);40}
41
42// CHECK: WARNING: ThreadSanitizer: data race
43// CHECK: Thread T1 'foobar1'
44// CHECK: Thread T2 'foobar2'
45