blob: 54a673caf54f15aed8f8ae4ee9664feaee7598ee [file] [log] [blame]
Lev Proleev900c28a2021-01-26 19:40:20 +00001/*
2 * Copyright (C) 2021 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/binder_auto_utils.h>
23#include <android/binder_interface_utils.h>
24#include <nnapi/Result.h>
Lev Proleev900c28a2021-01-26 19:40:20 +000025
26#include <algorithm>
27#include <functional>
28#include <memory>
29#include <mutex>
30#include <vector>
31
32#include "Utils.h"
33
34namespace aidl::android::hardware::neuralnetworks::utils {
35
36void DeathMonitor::serviceDied() {
37 std::lock_guard guard(mMutex);
38 std::for_each(mObjects.begin(), mObjects.end(),
Michael Butlere8645c32021-10-15 18:42:32 -070039 [](IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
Lev Proleev900c28a2021-01-26 19:40:20 +000040}
41
42void DeathMonitor::serviceDied(void* cookie) {
43 auto deathMonitor = static_cast<DeathMonitor*>(cookie);
44 deathMonitor->serviceDied();
45}
46
Michael Butlere8645c32021-10-15 18:42:32 -070047void DeathMonitor::add(IProtectedCallback* killable) const {
Lev Proleev900c28a2021-01-26 19:40:20 +000048 CHECK(killable != nullptr);
49 std::lock_guard guard(mMutex);
50 mObjects.push_back(killable);
51}
52
Michael Butlere8645c32021-10-15 18:42:32 -070053void DeathMonitor::remove(IProtectedCallback* killable) const {
Lev Proleev900c28a2021-01-26 19:40:20 +000054 CHECK(killable != nullptr);
55 std::lock_guard guard(mMutex);
56 const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable);
57 mObjects.erase(removedIter);
58}
59
60nn::GeneralResult<DeathHandler> DeathHandler::create(std::shared_ptr<ndk::ICInterface> object) {
61 if (object == nullptr) {
62 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
63 << "utils::DeathHandler::create must have non-null object";
64 }
65 auto deathMonitor = std::make_shared<DeathMonitor>();
66 auto deathRecipient = ndk::ScopedAIBinder_DeathRecipient(
67 AIBinder_DeathRecipient_new(DeathMonitor::serviceDied));
68
69 // If passed a local binder, AIBinder_linkToDeath will do nothing and return
70 // STATUS_INVALID_OPERATION. We ignore this case because we only use local binders in tests
71 // where this is not an error.
72 if (object->isRemote()) {
73 const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_linkToDeath(
74 object->asBinder().get(), deathRecipient.get(), deathMonitor.get()));
75 HANDLE_ASTATUS(ret) << "AIBinder_linkToDeath failed";
76 }
77
78 return DeathHandler(std::move(object), std::move(deathRecipient), std::move(deathMonitor));
79}
80
81DeathHandler::DeathHandler(std::shared_ptr<ndk::ICInterface> object,
82 ndk::ScopedAIBinder_DeathRecipient deathRecipient,
83 std::shared_ptr<DeathMonitor> deathMonitor)
84 : kObject(std::move(object)),
85 kDeathRecipient(std::move(deathRecipient)),
86 kDeathMonitor(std::move(deathMonitor)) {
87 CHECK(kObject != nullptr);
88 CHECK(kDeathRecipient.get() != nullptr);
89 CHECK(kDeathMonitor != nullptr);
90}
91
92DeathHandler::~DeathHandler() {
93 if (kObject != nullptr && kDeathRecipient.get() != nullptr && kDeathMonitor != nullptr) {
94 const auto ret = ndk::ScopedAStatus::fromStatus(AIBinder_unlinkToDeath(
95 kObject->asBinder().get(), kDeathRecipient.get(), kDeathMonitor.get()));
96 const auto maybeSuccess = handleTransportError(ret);
97 if (!maybeSuccess.ok()) {
98 LOG(ERROR) << maybeSuccess.error().message;
99 }
100 }
101}
102
103[[nodiscard]] ::android::base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
Michael Butlere8645c32021-10-15 18:42:32 -0700104 IProtectedCallback* killable) const {
Lev Proleev900c28a2021-01-26 19:40:20 +0000105 CHECK(killable != nullptr);
106 kDeathMonitor->add(killable);
107 return ::android::base::make_scope_guard(
108 [deathMonitor = kDeathMonitor, killable] { deathMonitor->remove(killable); });
109}
110
111} // namespace aidl::android::hardware::neuralnetworks::utils