diff options
| author | 2020-07-22 21:12:50 +0000 | |
|---|---|---|
| committer | 2020-07-22 21:12:50 +0000 | |
| commit | 4d836a68c65e689d24f7956c35a263818efc0787 (patch) | |
| tree | e320e1b5229d13aa1a1c556480ef730f480b4c73 /libs/binder/ProcessState.cpp | |
| parent | eb2572cbe6bfebb0dbf5f8b2c4e63751cb314220 (diff) | |
| parent | 0ee6d8fcd8708cd878de2665d3197b0d7cd92e09 (diff) | |
Merge "libbinder: avoid global mutex every binder call" am: 2054ee3fee am: 0ee6d8fcd8
Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1367817
Change-Id: I28721567c4557e9facb58dc6e386ebafbecd918c
Diffstat (limited to 'libs/binder/ProcessState.cpp')
| -rw-r--r-- | libs/binder/ProcessState.cpp | 60 |
1 files changed, 36 insertions, 24 deletions
diff --git a/libs/binder/ProcessState.cpp b/libs/binder/ProcessState.cpp index f3861bb2b5..acc1e67eb8 100644 --- a/libs/binder/ProcessState.cpp +++ b/libs/binder/ProcessState.cpp @@ -32,6 +32,7 @@ #include <errno.h> #include <fcntl.h> +#include <mutex> #include <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -73,38 +74,49 @@ protected: sp<ProcessState> ProcessState::self() { - Mutex::Autolock _l(gProcessMutex); - if (gProcess != nullptr) { - return gProcess; - } - gProcess = new ProcessState(kDefaultDriver); - return gProcess; + return init(kDefaultDriver, false /*requireDefault*/); } sp<ProcessState> ProcessState::initWithDriver(const char* driver) { - Mutex::Autolock _l(gProcessMutex); - if (gProcess != nullptr) { - // Allow for initWithDriver to be called repeatedly with the same - // driver. - if (!strcmp(gProcess->getDriverName().c_str(), driver)) { - return gProcess; - } - LOG_ALWAYS_FATAL("ProcessState was already initialized."); - } - - if (access(driver, R_OK) == -1) { - ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); - driver = "/dev/binder"; - } - - gProcess = new ProcessState(driver); - return gProcess; + return init(driver, true /*requireDefault*/); } sp<ProcessState> ProcessState::selfOrNull() { - Mutex::Autolock _l(gProcessMutex); + return init(nullptr, false /*requireDefault*/); +} + +sp<ProcessState> ProcessState::init(const char *driver, bool requireDefault) +{ + [[clang::no_destroy]] static sp<ProcessState> gProcess; + [[clang::no_destroy]] static std::mutex gProcessMutex; + + if (driver == nullptr) { + std::lock_guard<std::mutex> l(gProcessMutex); + return gProcess; + } + + [[clang::no_destroy]] static std::once_flag gProcessOnce; + std::call_once(gProcessOnce, [&](){ + if (access(driver, R_OK) == -1) { + ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); + driver = "/dev/binder"; + } + + std::lock_guard<std::mutex> l(gProcessMutex); + gProcess = new ProcessState(driver); + }); + + if (requireDefault) { + // Detect if we are trying to initialize with a different driver, and + // consider that an error. ProcessState will only be initialized once above. + LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver, + "ProcessState was already initialized with %s," + " can't initialize with %s.", + gProcess->getDriverName().c_str(), driver); + } + return gProcess; } |