53 lines · cpp
1// RUN: %clangxx_msan -std=c++11 -O0 %s -o %t && %run %t2// The main goal is getting the pthread name back and3// FreeBSD based do not support this feature4// UNSUPPORTED: android, target={{.*freebsd.*}}5 6// Regression test for a deadlock in pthread_getattr_np7 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 finish16// 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 (&lock);21 pthread_mutex_unlock (&lock);22 return nullptr;23}24 25int 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#else38 res = pthread_setname_np(t, kMyThreadName);39#endif40 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