Migrate pthread to std::thread
Bug: 341997808
Test: atest --host libutils_test
Change-Id: I4f435d5d85efcbcc351b7620811c172badc2276d
diff --git a/libutils/Looper.cpp b/libutils/Looper.cpp
index 7700c90..e11d197 100644
--- a/libutils/Looper.cpp
+++ b/libutils/Looper.cpp
@@ -70,8 +70,7 @@
// Maximum number of file descriptors for which to retrieve poll events each iteration.
static const int EPOLL_MAX_EVENTS = 16;
-static pthread_once_t gTLSOnce = PTHREAD_ONCE_INIT;
-static pthread_key_t gTLSKey = 0;
+thread_local static sp<Looper> gThreadLocalLooper;
Looper::Looper(bool allowNonCallbacks)
: mAllowNonCallbacks(allowNonCallbacks),
@@ -91,38 +90,12 @@
Looper::~Looper() {
}
-void Looper::initTLSKey() {
- int error = pthread_key_create(&gTLSKey, threadDestructor);
- LOG_ALWAYS_FATAL_IF(error != 0, "Could not allocate TLS key: %s", strerror(error));
-}
-
-void Looper::threadDestructor(void *st) {
- Looper* const self = static_cast<Looper*>(st);
- if (self != nullptr) {
- self->decStrong((void*)threadDestructor);
- }
-}
-
void Looper::setForThread(const sp<Looper>& looper) {
- sp<Looper> old = getForThread(); // also has side-effect of initializing TLS
-
- if (looper != nullptr) {
- looper->incStrong((void*)threadDestructor);
- }
-
- pthread_setspecific(gTLSKey, looper.get());
-
- if (old != nullptr) {
- old->decStrong((void*)threadDestructor);
- }
+ gThreadLocalLooper = looper;
}
sp<Looper> Looper::getForThread() {
- int result = pthread_once(& gTLSOnce, initTLSKey);
- LOG_ALWAYS_FATAL_IF(result != 0, "pthread_once failed");
-
- Looper* looper = (Looper*)pthread_getspecific(gTLSKey);
- return sp<Looper>::fromExisting(looper);
+ return gThreadLocalLooper;
}
sp<Looper> Looper::prepare(int opts) {
diff --git a/libutils/abi-dumps/arm64/source-based/libutils.so.lsdump b/libutils/abi-dumps/arm64/source-based/libutils.so.lsdump
index 82ddca8..caea1a9 100644
--- a/libutils/abi-dumps/arm64/source-based/libutils.so.lsdump
+++ b/libutils/abi-dumps/arm64/source-based/libutils.so.lsdump
@@ -510,9 +510,6 @@
"name" : "_ZN7android47LightRefBase_reportIncStrongRequireStrongFailedEPKv"
},
{
- "name" : "_ZN7android6Looper10initTLSKeyEv"
- },
- {
"name" : "_ZN7android6Looper11sendMessageERKNS_2spINS_14MessageHandlerEEERKNS_7MessageE"
},
{
@@ -528,9 +525,6 @@
"name" : "_ZN7android6Looper14removeMessagesERKNS_2spINS_14MessageHandlerEEEi"
},
{
- "name" : "_ZN7android6Looper16threadDestructorEPv"
- },
- {
"name" : "_ZN7android6Looper17sendMessageAtTimeElRKNS_2spINS_14MessageHandlerEEERKNS_7MessageE"
},
{
@@ -6910,13 +6904,6 @@
"source_file" : "system/core/libutils/include/utils/LightRefBase.h"
},
{
- "access" : "private",
- "function_name" : "android::Looper::initTLSKey",
- "linker_set_key" : "_ZN7android6Looper10initTLSKeyEv",
- "return_type" : "_ZTIv",
- "source_file" : "system/core/libutils/include/utils/Looper.h"
- },
- {
"function_name" : "android::Looper::sendMessage",
"linker_set_key" : "_ZN7android6Looper11sendMessageERKNS_2spINS_14MessageHandlerEEERKNS_7MessageE",
"parameters" :
@@ -6989,19 +6976,6 @@
"source_file" : "system/core/libutils/include/utils/Looper.h"
},
{
- "access" : "private",
- "function_name" : "android::Looper::threadDestructor",
- "linker_set_key" : "_ZN7android6Looper16threadDestructorEPv",
- "parameters" :
- [
- {
- "referenced_type" : "_ZTIPv"
- }
- ],
- "return_type" : "_ZTIv",
- "source_file" : "system/core/libutils/include/utils/Looper.h"
- },
- {
"function_name" : "android::Looper::sendMessageAtTime",
"linker_set_key" : "_ZN7android6Looper17sendMessageAtTimeElRKNS_2spINS_14MessageHandlerEEERKNS_7MessageE",
"parameters" :
diff --git a/libutils/abi-dumps/arm_arm64/source-based/libutils.so.lsdump b/libutils/abi-dumps/arm_arm64/source-based/libutils.so.lsdump
index dfc1ab5..b9c4c52 100644
--- a/libutils/abi-dumps/arm_arm64/source-based/libutils.so.lsdump
+++ b/libutils/abi-dumps/arm_arm64/source-based/libutils.so.lsdump
@@ -514,9 +514,6 @@
"name" : "_ZN7android47LightRefBase_reportIncStrongRequireStrongFailedEPKv"
},
{
- "name" : "_ZN7android6Looper10initTLSKeyEv"
- },
- {
"name" : "_ZN7android6Looper11sendMessageERKNS_2spINS_14MessageHandlerEEERKNS_7MessageE"
},
{
@@ -532,9 +529,6 @@
"name" : "_ZN7android6Looper14removeMessagesERKNS_2spINS_14MessageHandlerEEEi"
},
{
- "name" : "_ZN7android6Looper16threadDestructorEPv"
- },
- {
"name" : "_ZN7android6Looper17sendMessageAtTimeExRKNS_2spINS_14MessageHandlerEEERKNS_7MessageE"
},
{
@@ -6950,13 +6944,6 @@
"source_file" : "system/core/libutils/include/utils/LightRefBase.h"
},
{
- "access" : "private",
- "function_name" : "android::Looper::initTLSKey",
- "linker_set_key" : "_ZN7android6Looper10initTLSKeyEv",
- "return_type" : "_ZTIv",
- "source_file" : "system/core/libutils/include/utils/Looper.h"
- },
- {
"function_name" : "android::Looper::sendMessage",
"linker_set_key" : "_ZN7android6Looper11sendMessageERKNS_2spINS_14MessageHandlerEEERKNS_7MessageE",
"parameters" :
@@ -7029,19 +7016,6 @@
"source_file" : "system/core/libutils/include/utils/Looper.h"
},
{
- "access" : "private",
- "function_name" : "android::Looper::threadDestructor",
- "linker_set_key" : "_ZN7android6Looper16threadDestructorEPv",
- "parameters" :
- [
- {
- "referenced_type" : "_ZTIPv"
- }
- ],
- "return_type" : "_ZTIv",
- "source_file" : "system/core/libutils/include/utils/Looper.h"
- },
- {
"function_name" : "android::Looper::sendMessageAtTime",
"linker_set_key" : "_ZN7android6Looper17sendMessageAtTimeExRKNS_2spINS_14MessageHandlerEEERKNS_7MessageE",
"parameters" :
diff --git a/libutils/include/utils/Looper.h b/libutils/include/utils/Looper.h
index 41c5536..a02280b 100644
--- a/libutils/include/utils/Looper.h
+++ b/libutils/include/utils/Looper.h
@@ -499,8 +499,6 @@
void rebuildEpollLocked();
void scheduleEpollRebuildLocked();
- static void initTLSKey();
- static void threadDestructor(void *st);
static void initEpollEvent(struct epoll_event* eventItem);
};