Merge "Fix potential race condition on CTS TC pthread_gettid_np"

This commit is contained in:
Elliott Hughes 2015-11-11 21:21:33 +00:00 committed by Gerrit Code Review
commit ffe5c24c86

7
tests/pthread_test.cpp Normal file → Executable file
View file

@ -1303,8 +1303,11 @@ TEST(pthread, pthread_attr_getstack_18908062) {
} }
#if defined(__BIONIC__) #if defined(__BIONIC__)
static pthread_mutex_t gettid_mutex;
static void* pthread_gettid_np_helper(void* arg) { static void* pthread_gettid_np_helper(void* arg) {
pthread_mutex_lock(&gettid_mutex);
*reinterpret_cast<pid_t*>(arg) = gettid(); *reinterpret_cast<pid_t*>(arg) = gettid();
pthread_mutex_unlock(&gettid_mutex);
return NULL; return NULL;
} }
#endif #endif
@ -1315,11 +1318,15 @@ TEST(pthread, pthread_gettid_np) {
pid_t t_gettid_result; pid_t t_gettid_result;
pthread_t t; pthread_t t;
pthread_mutex_init(&gettid_mutex, NULL);
pthread_mutex_lock(&gettid_mutex);
pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result); pthread_create(&t, NULL, pthread_gettid_np_helper, &t_gettid_result);
pid_t t_pthread_gettid_np_result = pthread_gettid_np(t); pid_t t_pthread_gettid_np_result = pthread_gettid_np(t);
pthread_mutex_unlock(&gettid_mutex);
pthread_join(t, NULL); pthread_join(t, NULL);
pthread_mutex_destroy(&gettid_mutex);
ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result); ASSERT_EQ(t_gettid_result, t_pthread_gettid_np_result);
#else #else