diff --git a/libutils/Looper.cpp b/libutils/Looper.cpp index 7700c9030..e11d19731 100644 --- a/libutils/Looper.cpp +++ b/libutils/Looper.cpp @@ -70,8 +70,7 @@ int SimpleLooperCallback::handleEvent(int fd, int events, void* data) { // 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 gThreadLocalLooper; Looper::Looper(bool allowNonCallbacks) : mAllowNonCallbacks(allowNonCallbacks), @@ -91,38 +90,12 @@ Looper::Looper(bool allowNonCallbacks) 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(st); - if (self != nullptr) { - self->decStrong((void*)threadDestructor); - } -} - void Looper::setForThread(const sp& looper) { - sp 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::getForThread() { - int result = pthread_once(& gTLSOnce, initTLSKey); - LOG_ALWAYS_FATAL_IF(result != 0, "pthread_once failed"); - - Looper* looper = (Looper*)pthread_getspecific(gTLSKey); - return sp::fromExisting(looper); + return gThreadLocalLooper; } sp 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 82ddca8f5..caea1a96c 100644 --- a/libutils/abi-dumps/arm64/source-based/libutils.so.lsdump +++ b/libutils/abi-dumps/arm64/source-based/libutils.so.lsdump @@ -509,9 +509,6 @@ { "name" : "_ZN7android47LightRefBase_reportIncStrongRequireStrongFailedEPKv" }, - { - "name" : "_ZN7android6Looper10initTLSKeyEv" - }, { "name" : "_ZN7android6Looper11sendMessageERKNS_2spINS_14MessageHandlerEEERKNS_7MessageE" }, @@ -527,9 +524,6 @@ { "name" : "_ZN7android6Looper14removeMessagesERKNS_2spINS_14MessageHandlerEEEi" }, - { - "name" : "_ZN7android6Looper16threadDestructorEPv" - }, { "name" : "_ZN7android6Looper17sendMessageAtTimeElRKNS_2spINS_14MessageHandlerEEERKNS_7MessageE" }, @@ -6909,13 +6903,6 @@ "return_type" : "_ZTIv", "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", @@ -6988,19 +6975,6 @@ "return_type" : "_ZTIv", "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", 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 dfc1ab581..b9c4c521f 100644 --- a/libutils/abi-dumps/arm_arm64/source-based/libutils.so.lsdump +++ b/libutils/abi-dumps/arm_arm64/source-based/libutils.so.lsdump @@ -513,9 +513,6 @@ { "name" : "_ZN7android47LightRefBase_reportIncStrongRequireStrongFailedEPKv" }, - { - "name" : "_ZN7android6Looper10initTLSKeyEv" - }, { "name" : "_ZN7android6Looper11sendMessageERKNS_2spINS_14MessageHandlerEEERKNS_7MessageE" }, @@ -531,9 +528,6 @@ { "name" : "_ZN7android6Looper14removeMessagesERKNS_2spINS_14MessageHandlerEEEi" }, - { - "name" : "_ZN7android6Looper16threadDestructorEPv" - }, { "name" : "_ZN7android6Looper17sendMessageAtTimeExRKNS_2spINS_14MessageHandlerEEERKNS_7MessageE" }, @@ -6949,13 +6943,6 @@ "return_type" : "_ZTIv", "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", @@ -7028,19 +7015,6 @@ "return_type" : "_ZTIv", "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", diff --git a/libutils/include/utils/Looper.h b/libutils/include/utils/Looper.h index 41c5536f4..a02280b13 100644 --- a/libutils/include/utils/Looper.h +++ b/libutils/include/utils/Looper.h @@ -499,8 +499,6 @@ private: void rebuildEpollLocked(); void scheduleEpollRebuildLocked(); - static void initTLSKey(); - static void threadDestructor(void *st); static void initEpollEvent(struct epoll_event* eventItem); };