Loading libs/binder/ProcessState.cpp +36 −24 Original line number Original line Diff line number Diff line Loading @@ -32,6 +32,7 @@ #include <errno.h> #include <errno.h> #include <fcntl.h> #include <fcntl.h> #include <mutex> #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #include <unistd.h> #include <unistd.h> Loading Loading @@ -73,38 +74,49 @@ protected: sp<ProcessState> ProcessState::self() sp<ProcessState> ProcessState::self() { { Mutex::Autolock _l(gProcessMutex); return init(kDefaultDriver, false /*requireDefault*/); if (gProcess != nullptr) { return gProcess; } gProcess = new ProcessState(kDefaultDriver); return gProcess; } } sp<ProcessState> ProcessState::initWithDriver(const char* driver) sp<ProcessState> ProcessState::initWithDriver(const char* driver) { { Mutex::Autolock _l(gProcessMutex); return init(driver, true /*requireDefault*/); if (gProcess != nullptr) { } // Allow for initWithDriver to be called repeatedly with the same // driver. sp<ProcessState> ProcessState::selfOrNull() if (!strcmp(gProcess->getDriverName().c_str(), driver)) { { return gProcess; return init(nullptr, false /*requireDefault*/); } } LOG_ALWAYS_FATAL("ProcessState was already initialized."); 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) { if (access(driver, R_OK) == -1) { ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); driver = "/dev/binder"; driver = "/dev/binder"; } } std::lock_guard<std::mutex> l(gProcessMutex); gProcess = new ProcessState(driver); gProcess = new ProcessState(driver); return gProcess; }); 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); } } sp<ProcessState> ProcessState::selfOrNull() { Mutex::Autolock _l(gProcessMutex); return gProcess; return gProcess; } } Loading libs/binder/Static.cpp +0 −5 Original line number Original line Diff line number Diff line Loading @@ -68,9 +68,4 @@ TextOutput& alog(*new LogTextOutput()); TextOutput& aout(*new FdTextOutput(STDOUT_FILENO)); TextOutput& aout(*new FdTextOutput(STDOUT_FILENO)); TextOutput& aerr(*new FdTextOutput(STDERR_FILENO)); TextOutput& aerr(*new FdTextOutput(STDERR_FILENO)); // ------------ ProcessState.cpp Mutex& gProcessMutex = *new Mutex; sp<ProcessState> gProcess; } // namespace android } // namespace android libs/binder/Static.h +0 −4 Original line number Original line Diff line number Diff line Loading @@ -27,8 +27,4 @@ namespace android { // For TextStream.cpp // For TextStream.cpp extern Vector<int32_t> gTextBuffers; extern Vector<int32_t> gTextBuffers; // For ProcessState.cpp extern Mutex& gProcessMutex; extern sp<ProcessState> gProcess; } // namespace android } // namespace android libs/binder/include/binder/ProcessState.h +4 −0 Original line number Original line Diff line number Diff line Loading @@ -42,6 +42,8 @@ public: * any call to ProcessState::self(). The default is /dev/vndbinder * any call to ProcessState::self(). The default is /dev/vndbinder * for processes built with the VNDK and /dev/binder for those * for processes built with the VNDK and /dev/binder for those * which are not. * which are not. * * If this is called with nullptr, the behavior is the same as selfOrNull. */ */ static sp<ProcessState> initWithDriver(const char *driver); static sp<ProcessState> initWithDriver(const char *driver); Loading Loading @@ -90,6 +92,8 @@ public: void setCallRestriction(CallRestriction restriction); void setCallRestriction(CallRestriction restriction); private: private: static sp<ProcessState> init(const char *defaultDriver, bool requireDefault); friend class IPCThreadState; friend class IPCThreadState; explicit ProcessState(const char* driver); explicit ProcessState(const char* driver); Loading Loading
libs/binder/ProcessState.cpp +36 −24 Original line number Original line Diff line number Diff line Loading @@ -32,6 +32,7 @@ #include <errno.h> #include <errno.h> #include <fcntl.h> #include <fcntl.h> #include <mutex> #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #include <unistd.h> #include <unistd.h> Loading Loading @@ -73,38 +74,49 @@ protected: sp<ProcessState> ProcessState::self() sp<ProcessState> ProcessState::self() { { Mutex::Autolock _l(gProcessMutex); return init(kDefaultDriver, false /*requireDefault*/); if (gProcess != nullptr) { return gProcess; } gProcess = new ProcessState(kDefaultDriver); return gProcess; } } sp<ProcessState> ProcessState::initWithDriver(const char* driver) sp<ProcessState> ProcessState::initWithDriver(const char* driver) { { Mutex::Autolock _l(gProcessMutex); return init(driver, true /*requireDefault*/); if (gProcess != nullptr) { } // Allow for initWithDriver to be called repeatedly with the same // driver. sp<ProcessState> ProcessState::selfOrNull() if (!strcmp(gProcess->getDriverName().c_str(), driver)) { { return gProcess; return init(nullptr, false /*requireDefault*/); } } LOG_ALWAYS_FATAL("ProcessState was already initialized."); 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) { if (access(driver, R_OK) == -1) { ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); driver = "/dev/binder"; driver = "/dev/binder"; } } std::lock_guard<std::mutex> l(gProcessMutex); gProcess = new ProcessState(driver); gProcess = new ProcessState(driver); return gProcess; }); 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); } } sp<ProcessState> ProcessState::selfOrNull() { Mutex::Autolock _l(gProcessMutex); return gProcess; return gProcess; } } Loading
libs/binder/Static.cpp +0 −5 Original line number Original line Diff line number Diff line Loading @@ -68,9 +68,4 @@ TextOutput& alog(*new LogTextOutput()); TextOutput& aout(*new FdTextOutput(STDOUT_FILENO)); TextOutput& aout(*new FdTextOutput(STDOUT_FILENO)); TextOutput& aerr(*new FdTextOutput(STDERR_FILENO)); TextOutput& aerr(*new FdTextOutput(STDERR_FILENO)); // ------------ ProcessState.cpp Mutex& gProcessMutex = *new Mutex; sp<ProcessState> gProcess; } // namespace android } // namespace android
libs/binder/Static.h +0 −4 Original line number Original line Diff line number Diff line Loading @@ -27,8 +27,4 @@ namespace android { // For TextStream.cpp // For TextStream.cpp extern Vector<int32_t> gTextBuffers; extern Vector<int32_t> gTextBuffers; // For ProcessState.cpp extern Mutex& gProcessMutex; extern sp<ProcessState> gProcess; } // namespace android } // namespace android
libs/binder/include/binder/ProcessState.h +4 −0 Original line number Original line Diff line number Diff line Loading @@ -42,6 +42,8 @@ public: * any call to ProcessState::self(). The default is /dev/vndbinder * any call to ProcessState::self(). The default is /dev/vndbinder * for processes built with the VNDK and /dev/binder for those * for processes built with the VNDK and /dev/binder for those * which are not. * which are not. * * If this is called with nullptr, the behavior is the same as selfOrNull. */ */ static sp<ProcessState> initWithDriver(const char *driver); static sp<ProcessState> initWithDriver(const char *driver); Loading Loading @@ -90,6 +92,8 @@ public: void setCallRestriction(CallRestriction restriction); void setCallRestriction(CallRestriction restriction); private: private: static sp<ProcessState> init(const char *defaultDriver, bool requireDefault); friend class IPCThreadState; friend class IPCThreadState; explicit ProcessState(const char* driver); explicit ProcessState(const char* driver); Loading