1// RUN: %clangxx_msan -std=c++11 -O0 %s -o %t && %run %t
2// The main goal is getting the pthread name back and
3// FreeBSD based do not support this feature
4// UNSUPPORTED: android, target={{.*freebsd.*}}
5
6// Regression test for a deadlock in pthread_getattr_np
7
8#include <assert.h>
9#include <pthread.h>
10#include <string.h>
11#include <sanitizer/msan_interface.h>
12
13#include <stdio.h>
14
15// Stall child thread on this lock to make sure it doesn't finish
16// before the end of the pthread_getname_np() / pthread_setname_np() tests.
17static pthread_mutex_t lock;
18
19void *ThreadFn(void *) {
20 pthread_mutex_lock (mutex: &lock);
21 pthread_mutex_unlock (mutex: &lock);
22 return nullptr;
23}
24
25int main(void) {
26 pthread_t t;
27
28 pthread_mutex_init (mutex: &lock, NULL);
29 pthread_mutex_lock (mutex: &lock);
30
31 int res = pthread_create(newthread: &t, attr: 0, start_routine: ThreadFn, arg: 0);
32 assert(!res);
33
34 const char *kMyThreadName = "my-thread-name";
35#if defined(__NetBSD__)
36 res = pthread_setname_np(t, "%s", (void *)kMyThreadName);
37#else
38 res = pthread_setname_np(target_thread: t, name: kMyThreadName);
39#endif
40 assert(!res);
41
42 char buf[100];
43 res = pthread_getname_np(target_thread: t, buf: buf, buflen: sizeof(buf));
44 assert(!res);
45 assert(strcmp(buf, kMyThreadName) == 0);
46
47 pthread_mutex_unlock (mutex: &lock);
48
49 res = pthread_join(th: t, thread_return: 0);
50 assert(!res);
51 return 0;
52}
53

source code of compiler-rt/test/msan/pthread_getname_np.cpp