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, 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.
17 static pthread_mutex_t lock;
18 
19 void *ThreadFn(void *) {
20   pthread_mutex_lock (&lock);
21   pthread_mutex_unlock (&lock);
22   return nullptr;
23 }
24 
25 int main(void) {
26   pthread_t t;
27 
28   pthread_mutex_init (&lock, NULL);
29   pthread_mutex_lock (&lock);
30 
31   int res = pthread_create(&t, 0, ThreadFn, 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(t, kMyThreadName);
39 #endif
40   assert(!res);
41 
42   char buf[100];
43   res = pthread_getname_np(t, buf, sizeof(buf));
44   assert(!res);
45   assert(strcmp(buf, kMyThreadName) == 0);
46 
47   pthread_mutex_unlock (&lock);
48 
49   res = pthread_join(t, 0);
50   assert(!res);
51   return 0;
52 }
53