diff options
author | 2021-01-19 17:40:23 +0000 | |
---|---|---|
committer | 2021-01-19 17:40:23 +0000 | |
commit | ff30b34cd24b23c8eb899ace1daf7a33db86a775 (patch) | |
tree | 13ffb7f1f5b51a05da5e4b3bd4588c05560b4cfc | |
parent | b86f892039f13f689ea8df485cc5ac908999afe2 (diff) | |
parent | c648a765dc0e3008db62a368df3ea7592d4c2452 (diff) |
Merge "libbinder - avoid pthread_cond_broadcast per call"
-rw-r--r-- | libs/binder/IPCThreadState.cpp | 9 | ||||
-rw-r--r-- | libs/binder/ProcessState.cpp | 1 | ||||
-rw-r--r-- | libs/binder/include/binder/ProcessState.h | 5 |
3 files changed, 13 insertions, 2 deletions
diff --git a/libs/binder/IPCThreadState.cpp b/libs/binder/IPCThreadState.cpp index 2eee2c40a3..b038feb878 100644 --- a/libs/binder/IPCThreadState.cpp +++ b/libs/binder/IPCThreadState.cpp @@ -489,12 +489,14 @@ void IPCThreadState::flushCommands() void IPCThreadState::blockUntilThreadAvailable() { pthread_mutex_lock(&mProcess->mThreadCountLock); + mProcess->mWaitingForThreads++; while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) { ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n", static_cast<unsigned long>(mProcess->mExecutingThreadsCount), static_cast<unsigned long>(mProcess->mMaxThreads)); pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock); } + mProcess->mWaitingForThreads--; pthread_mutex_unlock(&mProcess->mThreadCountLock); } @@ -534,7 +536,12 @@ status_t IPCThreadState::getAndExecuteCommand() } mProcess->mStarvationStartTimeMs = 0; } - pthread_cond_broadcast(&mProcess->mThreadCountDecrement); + + // Cond broadcast can be expensive, so don't send it every time a binder + // call is processed. b/168806193 + if (mProcess->mWaitingForThreads > 0) { + pthread_cond_broadcast(&mProcess->mThreadCountDecrement); + } pthread_mutex_unlock(&mProcess->mThreadCountLock); } diff --git a/libs/binder/ProcessState.cpp b/libs/binder/ProcessState.cpp index b5e4dfe4df..c38249ef7c 100644 --- a/libs/binder/ProcessState.cpp +++ b/libs/binder/ProcessState.cpp @@ -399,6 +399,7 @@ ProcessState::ProcessState(const char *driver) , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER) , mThreadCountDecrement(PTHREAD_COND_INITIALIZER) , mExecutingThreadsCount(0) + , mWaitingForThreads(0) , mMaxThreads(DEFAULT_MAX_BINDER_THREADS) , mStarvationStartTimeMs(0) , mThreadPoolStarted(false) diff --git a/libs/binder/include/binder/ProcessState.h b/libs/binder/include/binder/ProcessState.h index bab6469d12..2405ab6b9e 100644 --- a/libs/binder/include/binder/ProcessState.h +++ b/libs/binder/include/binder/ProcessState.h @@ -107,11 +107,14 @@ private: int mDriverFD; void* mVMStart; - // Protects thread count variable below. + // Protects thread count and wait variables below. pthread_mutex_t mThreadCountLock; + // Broadcast whenever mWaitingForThreads > 0 pthread_cond_t mThreadCountDecrement; // Number of binder threads current executing a command. size_t mExecutingThreadsCount; + // Number of threads calling IPCThreadState::blockUntilThreadAvailable() + size_t mWaitingForThreads; // Maximum number for binder threads allowed for this process. size_t mMaxThreads; // Time when thread pool was emptied |