Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "ProtectCallback.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/scopeguard.h> |
| 21 | #include <android-base/thread_annotations.h> |
| 22 | #include <android/hidl/base/1.0/IBase.h> |
| 23 | #include <hidl/HidlSupport.h> |
| 24 | #include <nnapi/Result.h> |
| 25 | #include <nnapi/hal/HandleError.h> |
| 26 | |
| 27 | #include <algorithm> |
| 28 | #include <functional> |
| 29 | #include <mutex> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace android::hardware::neuralnetworks::utils { |
| 33 | |
| 34 | void DeathRecipient::serviceDied(uint64_t /*cookie*/, const wp<hidl::base::V1_0::IBase>& /*who*/) { |
| 35 | std::lock_guard guard(mMutex); |
| 36 | std::for_each(mObjects.begin(), mObjects.end(), |
| 37 | [](IProtectedCallback* killable) { killable->notifyAsDeadObject(); }); |
| 38 | } |
| 39 | |
| 40 | void DeathRecipient::add(IProtectedCallback* killable) const { |
| 41 | CHECK(killable != nullptr); |
| 42 | std::lock_guard guard(mMutex); |
| 43 | mObjects.push_back(killable); |
| 44 | } |
| 45 | |
| 46 | void DeathRecipient::remove(IProtectedCallback* killable) const { |
| 47 | CHECK(killable != nullptr); |
| 48 | std::lock_guard guard(mMutex); |
| 49 | const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable); |
| 50 | mObjects.erase(removedIter); |
| 51 | } |
| 52 | |
| 53 | nn::GeneralResult<DeathHandler> DeathHandler::create(sp<hidl::base::V1_0::IBase> object) { |
| 54 | if (object == nullptr) { |
| 55 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 56 | << "utils::DeathHandler::create must have non-null object"; |
| 57 | } |
| 58 | auto deathRecipient = sp<DeathRecipient>::make(); |
| 59 | |
| 60 | const auto ret = object->linkToDeath(deathRecipient, /*cookie=*/0); |
| 61 | const bool success = NN_TRY(handleTransportError(ret)); |
| 62 | if (!success) { |
| 63 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "IBase::linkToDeath returned false"; |
| 64 | } |
| 65 | |
| 66 | return DeathHandler(std::move(object), std::move(deathRecipient)); |
| 67 | } |
| 68 | |
| 69 | DeathHandler::DeathHandler(sp<hidl::base::V1_0::IBase> object, sp<DeathRecipient> deathRecipient) |
| 70 | : kObject(std::move(object)), kDeathRecipient(std::move(deathRecipient)) { |
| 71 | CHECK(kObject != nullptr); |
| 72 | CHECK(kDeathRecipient != nullptr); |
| 73 | } |
| 74 | |
| 75 | DeathHandler::~DeathHandler() { |
| 76 | if (kObject != nullptr && kDeathRecipient != nullptr) { |
| 77 | const auto ret = kObject->unlinkToDeath(kDeathRecipient); |
| 78 | const auto maybeSuccess = handleTransportError(ret); |
| 79 | if (!maybeSuccess.has_value()) { |
| 80 | LOG(ERROR) << maybeSuccess.error().message; |
| 81 | } else if (!maybeSuccess.value()) { |
| 82 | LOG(ERROR) << "IBase::linkToDeath returned false"; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | [[nodiscard]] base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback( |
| 88 | IProtectedCallback* killable) const { |
| 89 | CHECK(killable != nullptr); |
| 90 | kDeathRecipient->add(killable); |
| 91 | return base::make_scope_guard( |
| 92 | [deathRecipient = kDeathRecipient, killable] { deathRecipient->remove(killable); }); |
| 93 | } |
| 94 | |
| 95 | } // namespace android::hardware::neuralnetworks::utils |