• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include <pthread.h>
20 
21 namespace {
22 
23 // Test that host pthread_exit runs destructors of local objects.
24 // This should happen at pthread_cleanup (before pthread_specific destructors).
25 
26 int g_count = 0;
27 
28 struct ScopedCount {
ScopedCount__anon7e976c730111::ScopedCount29   ScopedCount() { ++g_count; }
~ScopedCount__anon7e976c730111::ScopedCount30   ~ScopedCount() { --g_count; }
31 };
32 
RunPthreadExit()33 void RunPthreadExit() {
34   ASSERT_EQ(1, g_count);
35   pthread_exit(nullptr);
36   FAIL();
37 }
38 
ThreadFunc(void *)39 void* ThreadFunc(void* /* arg */) {
40   ScopedCount c;
41   RunPthreadExit();  // does not return
42   return nullptr;
43 }
44 
TEST(GuestThreadTest,PthreadExitRunsLocalDtors)45 TEST(GuestThreadTest, PthreadExitRunsLocalDtors) {
46   ASSERT_EQ(0, g_count);
47   pthread_t thread;
48   ASSERT_EQ(0, pthread_create(&thread, nullptr, ThreadFunc, nullptr));
49   ASSERT_EQ(0, pthread_join(thread, nullptr));
50   // TODO(b/27860783): it turned out that on bionic pthread_exit doesn't run
51   // destructors for local objects. If that gets fixed, change the code
52   // accordingly (see other TODOs for this bug).
53   // ASSERT_EQ(0, g_count);
54   ASSERT_EQ(1, g_count);
55 }
56 
57 }  // namespace
58