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 27 #include <functional> 28 #include <mutex> 29 #include <vector> 30 31 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface 32 // lifetimes across processes and for protecting asynchronous calls across AIDL. 33 34 namespace aidl::android::hardware::neuralnetworks::utils { 35 36 class IProtectedCallback { 37 public: 38 /** 39 * Marks this object as a dead object. 40 */ 41 virtual void notifyAsDeadObject() = 0; 42 43 // Public virtual destructor to allow objects to be stored (and destroyed) as smart pointers. 44 // E.g., std::unique_ptr<IProtectedCallback>. 45 virtual ~IProtectedCallback() = default; 46 47 protected: 48 // Protect the non-destructor special member functions to prevent object slicing. 49 IProtectedCallback() = default; 50 IProtectedCallback(const IProtectedCallback&) = default; 51 IProtectedCallback(IProtectedCallback&&) noexcept = default; 52 IProtectedCallback& operator=(const IProtectedCallback&) = default; 53 IProtectedCallback& operator=(IProtectedCallback&&) noexcept = default; 54 }; 55 56 // Thread safe class 57 class DeathMonitor final { 58 public: DeathMonitor(uintptr_t cookieKey)59 explicit DeathMonitor(uintptr_t cookieKey) : kCookieKey(cookieKey) {} 60 61 static void serviceDied(void* cookie); 62 void serviceDied(); 63 // Precondition: `killable` must be non-null. 64 void add(IProtectedCallback* killable) const; 65 // Precondition: `killable` must be non-null. 66 void remove(IProtectedCallback* killable) const; 67 getCookieKey()68 uintptr_t getCookieKey() const { return kCookieKey; } 69 70 ~DeathMonitor(); 71 DeathMonitor(const DeathMonitor&) = delete; 72 DeathMonitor(DeathMonitor&&) noexcept = delete; 73 DeathMonitor& operator=(const DeathMonitor&) = delete; 74 DeathMonitor& operator=(DeathMonitor&&) noexcept = delete; 75 76 private: 77 mutable std::mutex mMutex; 78 mutable std::vector<IProtectedCallback*> mObjects GUARDED_BY(mMutex); 79 const uintptr_t kCookieKey; 80 }; 81 82 class DeathHandler final { 83 public: 84 static nn::GeneralResult<DeathHandler> create(std::shared_ptr<ndk::ICInterface> object); 85 86 DeathHandler(const DeathHandler&) = delete; 87 DeathHandler(DeathHandler&&) noexcept = default; 88 DeathHandler& operator=(const DeathHandler&) = delete; 89 DeathHandler& operator=(DeathHandler&&) noexcept = delete; 90 ~DeathHandler(); 91 92 using Cleanup = std::function<void()>; 93 // Precondition: `killable` must be non-null. 94 [[nodiscard]] ::android::base::ScopeGuard<Cleanup> protectCallback( 95 IProtectedCallback* killable) const; 96 getDeathMonitor()97 std::shared_ptr<DeathMonitor> getDeathMonitor() const { return kDeathMonitor; } 98 99 private: 100 DeathHandler(std::shared_ptr<ndk::ICInterface> object, 101 ndk::ScopedAIBinder_DeathRecipient deathRecipient, 102 std::shared_ptr<DeathMonitor> deathMonitor); 103 104 std::shared_ptr<ndk::ICInterface> kObject; 105 ndk::ScopedAIBinder_DeathRecipient kDeathRecipient; 106 std::shared_ptr<DeathMonitor> kDeathMonitor; 107 }; 108 109 } // namespace aidl::android::hardware::neuralnetworks::utils 110 111 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H 112