1 /* 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 #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H 18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H 19 20 #include <android-base/scopeguard.h> 21 #include <android-base/thread_annotations.h> 22 #include <android/binder_interface_utils.h> 23 #include <nnapi/Result.h> 24 #include <nnapi/Types.h> 25 #include <nnapi/hal/CommonUtils.h> 26 #include <nnapi/hal/ProtectCallback.h> 27 28 #include <functional> 29 #include <mutex> 30 #include <vector> 31 32 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface 33 // lifetimes across processes and for protecting asynchronous calls across AIDL. 34 35 namespace aidl::android::hardware::neuralnetworks::utils { 36 37 // Thread safe class 38 class DeathMonitor final { 39 public: 40 static void serviceDied(void* cookie); 41 void serviceDied(); 42 // Precondition: `killable` must be non-null. 43 void add(hal::utils::IProtectedCallback* killable) const; 44 // Precondition: `killable` must be non-null. 45 void remove(hal::utils::IProtectedCallback* killable) const; 46 47 private: 48 mutable std::mutex mMutex; 49 mutable std::vector<hal::utils::IProtectedCallback*> mObjects GUARDED_BY(mMutex); 50 }; 51 52 class DeathHandler final { 53 public: 54 static nn::GeneralResult<DeathHandler> create(std::shared_ptr<ndk::ICInterface> object); 55 56 DeathHandler(const DeathHandler&) = delete; 57 DeathHandler(DeathHandler&&) noexcept = default; 58 DeathHandler& operator=(const DeathHandler&) = delete; 59 DeathHandler& operator=(DeathHandler&&) noexcept = delete; 60 ~DeathHandler(); 61 62 using Cleanup = std::function<void()>; 63 // Precondition: `killable` must be non-null. 64 [[nodiscard]] ::android::base::ScopeGuard<Cleanup> protectCallback( 65 hal::utils::IProtectedCallback* killable) const; 66 getDeathMonitor()67 std::shared_ptr<DeathMonitor> getDeathMonitor() const { return kDeathMonitor; } 68 69 private: 70 DeathHandler(std::shared_ptr<ndk::ICInterface> object, 71 ndk::ScopedAIBinder_DeathRecipient deathRecipient, 72 std::shared_ptr<DeathMonitor> deathMonitor); 73 74 std::shared_ptr<ndk::ICInterface> kObject; 75 ndk::ScopedAIBinder_DeathRecipient kDeathRecipient; 76 std::shared_ptr<DeathMonitor> kDeathMonitor; 77 }; 78 79 } // namespace aidl::android::hardware::neuralnetworks::utils 80 81 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H 82