blob: 18e1f3bf0b52bbd53f93fa0a28dc8fd335a6332a [file] [log] [blame]
Michael Butler4b276a72020-08-06 23:22:35 -07001/*
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
32namespace android::hardware::neuralnetworks::utils {
33
34void 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 Butler76e491f2020-12-19 01:55:32 -080038 mObjects.clear();
39 mIsDeadObject = true;
Michael Butler4b276a72020-08-06 23:22:35 -070040}
41
42void DeathRecipient::add(IProtectedCallback* killable) const {
43 CHECK(killable != nullptr);
44 std::lock_guard guard(mMutex);
Michael Butler76e491f2020-12-19 01:55:32 -080045 if (mIsDeadObject) {
46 killable->notifyAsDeadObject();
47 } else {
48 mObjects.push_back(killable);
49 }
Michael Butler4b276a72020-08-06 23:22:35 -070050}
51
52void DeathRecipient::remove(IProtectedCallback* killable) const {
53 CHECK(killable != nullptr);
54 std::lock_guard guard(mMutex);
Michael Butler76e491f2020-12-19 01:55:32 -080055 const auto newEnd = std::remove(mObjects.begin(), mObjects.end(), killable);
56 mObjects.erase(newEnd, mObjects.end());
Michael Butler4b276a72020-08-06 23:22:35 -070057}
58
59nn::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 Butlercca3e202020-11-22 20:25:34 -080067 const bool success = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070068 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
75DeathHandler::DeathHandler(sp<hidl::base::V1_0::IBase> object, sp<DeathRecipient> deathRecipient)
Michael Butler76e491f2020-12-19 01:55:32 -080076 : mObject(std::move(object)), mDeathRecipient(std::move(deathRecipient)) {
77 CHECK(mObject != nullptr);
78 CHECK(mDeathRecipient != nullptr);
Michael Butler4b276a72020-08-06 23:22:35 -070079}
80
81DeathHandler::~DeathHandler() {
Michael Butler76e491f2020-12-19 01:55:32 -080082 if (mObject != nullptr && mDeathRecipient != nullptr) {
83 const auto successful = mObject->unlinkToDeath(mDeathRecipient).isOk();
84 if (!successful) {
85 LOG(ERROR) << "IBase::linkToDeath failed";
Michael Butler4b276a72020-08-06 23:22:35 -070086 }
87 }
88}
89
90[[nodiscard]] base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
91 IProtectedCallback* killable) const {
92 CHECK(killable != nullptr);
Michael Butler76e491f2020-12-19 01:55:32 -080093 mDeathRecipient->add(killable);
Michael Butler4b276a72020-08-06 23:22:35 -070094 return base::make_scope_guard(
Michael Butler76e491f2020-12-19 01:55:32 -080095 [deathRecipient = mDeathRecipient, killable] { deathRecipient->remove(killable); });
96}
97
98void DeathHandler::protectCallbackForLifetimeOfDeathHandler(IProtectedCallback* killable) const {
99 CHECK(killable != nullptr);
100 mDeathRecipient->add(killable);
Michael Butler4b276a72020-08-06 23:22:35 -0700101}
102
103} // namespace android::hardware::neuralnetworks::utils