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(); }); |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 38 | mObjects.clear(); |
| 39 | mIsDeadObject = true; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void DeathRecipient::add(IProtectedCallback* killable) const { |
| 43 | CHECK(killable != nullptr); |
| 44 | std::lock_guard guard(mMutex); |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 45 | if (mIsDeadObject) { |
| 46 | killable->notifyAsDeadObject(); |
| 47 | } else { |
| 48 | mObjects.push_back(killable); |
| 49 | } |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void DeathRecipient::remove(IProtectedCallback* killable) const { |
| 53 | CHECK(killable != nullptr); |
| 54 | std::lock_guard guard(mMutex); |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 55 | const auto newEnd = std::remove(mObjects.begin(), mObjects.end(), killable); |
| 56 | mObjects.erase(newEnd, mObjects.end()); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | nn::GeneralResult<DeathHandler> DeathHandler::create(sp<hidl::base::V1_0::IBase> object) { |
| 60 | if (object == nullptr) { |
| 61 | return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT) |
| 62 | << "utils::DeathHandler::create must have non-null object"; |
| 63 | } |
| 64 | auto deathRecipient = sp<DeathRecipient>::make(); |
| 65 | |
| 66 | const auto ret = object->linkToDeath(deathRecipient, /*cookie=*/0); |
Michael Butler | cca3e20 | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 67 | const bool success = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 68 | if (!success) { |
| 69 | return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE) << "IBase::linkToDeath returned false"; |
| 70 | } |
| 71 | |
| 72 | return DeathHandler(std::move(object), std::move(deathRecipient)); |
| 73 | } |
| 74 | |
| 75 | DeathHandler::DeathHandler(sp<hidl::base::V1_0::IBase> object, sp<DeathRecipient> deathRecipient) |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 76 | : mObject(std::move(object)), mDeathRecipient(std::move(deathRecipient)) { |
| 77 | CHECK(mObject != nullptr); |
| 78 | CHECK(mDeathRecipient != nullptr); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | DeathHandler::~DeathHandler() { |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 82 | if (mObject != nullptr && mDeathRecipient != nullptr) { |
| 83 | const auto successful = mObject->unlinkToDeath(mDeathRecipient).isOk(); |
| 84 | if (!successful) { |
| 85 | LOG(ERROR) << "IBase::linkToDeath failed"; |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | [[nodiscard]] base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback( |
| 91 | IProtectedCallback* killable) const { |
| 92 | CHECK(killable != nullptr); |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 93 | mDeathRecipient->add(killable); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 94 | return base::make_scope_guard( |
Michael Butler | 76e491f | 2020-12-19 01:55:32 -0800 | [diff] [blame] | 95 | [deathRecipient = mDeathRecipient, killable] { deathRecipient->remove(killable); }); |
| 96 | } |
| 97 | |
| 98 | void DeathHandler::protectCallbackForLifetimeOfDeathHandler(IProtectedCallback* killable) const { |
| 99 | CHECK(killable != nullptr); |
| 100 | mDeathRecipient->add(killable); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace android::hardware::neuralnetworks::utils |