Keep track of DeathMonitor cookies am: 37c395758d
Original change: https://googleplex-android-review.googlesource.com/c/platform/hardware/interfaces/+/27163475 Change-Id: Ie58f7059cafbbe298c114fe86227249c0a65243f Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
30134f8b19
3 changed files with 64 additions and 9 deletions
|
@ -37,6 +37,8 @@ namespace aidl::android::hardware::neuralnetworks::utils {
|
|||
// Thread safe class
|
||||
class DeathMonitor final {
|
||||
public:
|
||||
explicit DeathMonitor(uintptr_t cookieKey) : kCookieKey(cookieKey) {}
|
||||
|
||||
static void serviceDied(void* cookie);
|
||||
void serviceDied();
|
||||
// Precondition: `killable` must be non-null.
|
||||
|
@ -44,9 +46,18 @@ class DeathMonitor final {
|
|||
// Precondition: `killable` must be non-null.
|
||||
void remove(hal::utils::IProtectedCallback* killable) const;
|
||||
|
||||
uintptr_t getCookieKey() const { return kCookieKey; }
|
||||
|
||||
~DeathMonitor();
|
||||
DeathMonitor(const DeathMonitor&) = delete;
|
||||
DeathMonitor(DeathMonitor&&) noexcept = delete;
|
||||
DeathMonitor& operator=(const DeathMonitor&) = delete;
|
||||
DeathMonitor& operator=(DeathMonitor&&) noexcept = delete;
|
||||
|
||||
private:
|
||||
mutable std::mutex mMutex;
|
||||
mutable std::vector<hal::utils::IProtectedCallback*> mObjects GUARDED_BY(mMutex);
|
||||
const uintptr_t kCookieKey;
|
||||
};
|
||||
|
||||
class DeathHandler final {
|
||||
|
|
|
@ -26,6 +26,7 @@
|
|||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
@ -34,6 +35,16 @@
|
|||
|
||||
namespace aidl::android::hardware::neuralnetworks::utils {
|
||||
|
||||
namespace {
|
||||
|
||||
// Only dereference the cookie if it's valid (if it's in this set)
|
||||
// Only used with ndk
|
||||
std::mutex sCookiesMutex;
|
||||
uintptr_t sCookieKeyCounter GUARDED_BY(sCookiesMutex) = 0;
|
||||
std::map<uintptr_t, std::weak_ptr<DeathMonitor>> sCookies GUARDED_BY(sCookiesMutex);
|
||||
|
||||
} // namespace
|
||||
|
||||
void DeathMonitor::serviceDied() {
|
||||
std::lock_guard guard(mMutex);
|
||||
std::for_each(mObjects.begin(), mObjects.end(),
|
||||
|
@ -41,8 +52,24 @@ void DeathMonitor::serviceDied() {
|
|||
}
|
||||
|
||||
void DeathMonitor::serviceDied(void* cookie) {
|
||||
auto deathMonitor = static_cast<DeathMonitor*>(cookie);
|
||||
deathMonitor->serviceDied();
|
||||
std::shared_ptr<DeathMonitor> monitor;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(sCookiesMutex);
|
||||
if (auto it = sCookies.find(reinterpret_cast<uintptr_t>(cookie)); it != sCookies.end()) {
|
||||
monitor = it->second.lock();
|
||||
sCookies.erase(it);
|
||||
} else {
|
||||
LOG(INFO)
|
||||
<< "Service died, but cookie is no longer valid so there is nothing to notify.";
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (monitor) {
|
||||
LOG(INFO) << "Notifying DeathMonitor from serviceDied.";
|
||||
monitor->serviceDied();
|
||||
} else {
|
||||
LOG(INFO) << "Tried to notify DeathMonitor from serviceDied but could not promote.";
|
||||
}
|
||||
}
|
||||
|
||||
void DeathMonitor::add(hal::utils::IProtectedCallback* killable) const {
|
||||
|
@ -58,12 +85,25 @@ void DeathMonitor::remove(hal::utils::IProtectedCallback* killable) const {
|
|||
mObjects.erase(removedIter);
|
||||
}
|
||||
|
||||
DeathMonitor::~DeathMonitor() {
|
||||
// lock must be taken so object is not used in OnBinderDied"
|
||||
std::lock_guard<std::mutex> guard(sCookiesMutex);
|
||||
sCookies.erase(kCookieKey);
|
||||
}
|
||||
|
||||
nn::GeneralResult<DeathHandler> DeathHandler::create(std::shared_ptr<ndk::ICInterface> object) {
|
||||
if (object == nullptr) {
|
||||
return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
|
||||
<< "utils::DeathHandler::create must have non-null object";
|
||||
}
|
||||
auto deathMonitor = std::make_shared<DeathMonitor>();
|
||||
|
||||
std::shared_ptr<DeathMonitor> deathMonitor;
|
||||
{
|
||||
std::lock_guard<std::mutex> guard(sCookiesMutex);
|
||||
deathMonitor = std::make_shared<DeathMonitor>(sCookieKeyCounter++);
|
||||
sCookies[deathMonitor->getCookieKey()] = deathMonitor;
|
||||
}
|
||||
|
||||
auto deathRecipient = ndk::ScopedAIBinder_DeathRecipient(
|
||||
AIBinder_DeathRecipient_new(DeathMonitor::serviceDied));
|
||||
|
||||
|
@ -71,8 +111,9 @@ nn::GeneralResult<DeathHandler> DeathHandler::create(std::shared_ptr<ndk::ICInte
|
|||
// STATUS_INVALID_OPERATION. We ignore this case because we only use local binders in tests
|
||||
// where this is not an error.
|
||||
if (object->isRemote()) {
|
||||
const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_linkToDeath(
|
||||
object->asBinder().get(), deathRecipient.get(), deathMonitor.get()));
|
||||
const auto ret = ndk::ScopedAStatus::fromStatus(
|
||||
AIBinder_linkToDeath(object->asBinder().get(), deathRecipient.get(),
|
||||
reinterpret_cast<void*>(deathMonitor->getCookieKey())));
|
||||
HANDLE_ASTATUS(ret) << "AIBinder_linkToDeath failed";
|
||||
}
|
||||
|
||||
|
@ -92,8 +133,9 @@ DeathHandler::DeathHandler(std::shared_ptr<ndk::ICInterface> object,
|
|||
|
||||
DeathHandler::~DeathHandler() {
|
||||
if (kObject != nullptr && kDeathRecipient.get() != nullptr && kDeathMonitor != nullptr) {
|
||||
const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_unlinkToDeath(
|
||||
kObject->asBinder().get(), kDeathRecipient.get(), kDeathMonitor.get()));
|
||||
const auto ret = ndk::ScopedAStatus::fromStatus(
|
||||
AIBinder_unlinkToDeath(kObject->asBinder().get(), kDeathRecipient.get(),
|
||||
reinterpret_cast<void*>(kDeathMonitor->getCookieKey())));
|
||||
const auto maybeSuccess = handleTransportError(ret);
|
||||
if (!maybeSuccess.ok()) {
|
||||
LOG(ERROR) << maybeSuccess.error().message;
|
||||
|
|
|
@ -669,7 +669,8 @@ TEST(DeviceTest, prepareModelAsyncCrash) {
|
|||
const auto mockDevice = createMockDevice();
|
||||
const auto device = Device::create(kName, mockDevice).value();
|
||||
const auto ret = [&device]() {
|
||||
DeathMonitor::serviceDied(device->getDeathMonitor());
|
||||
DeathMonitor::serviceDied(
|
||||
reinterpret_cast<void*>(device->getDeathMonitor()->getCookieKey()));
|
||||
return ndk::ScopedAStatus::ok();
|
||||
};
|
||||
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _, _))
|
||||
|
@ -792,7 +793,8 @@ TEST(DeviceTest, prepareModelFromCacheAsyncCrash) {
|
|||
const auto mockDevice = createMockDevice();
|
||||
const auto device = Device::create(kName, mockDevice).value();
|
||||
const auto ret = [&device]() {
|
||||
DeathMonitor::serviceDied(device->getDeathMonitor());
|
||||
DeathMonitor::serviceDied(
|
||||
reinterpret_cast<void*>(device->getDeathMonitor()->getCookieKey()));
|
||||
return ndk::ScopedAStatus::ok();
|
||||
};
|
||||
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _, _))
|
||||
|
|
Loading…
Reference in a new issue