/* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H #include #include #include #include #include #include #include #include #include // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface // lifetimes across processes and for protecting asynchronous calls across HIDL. namespace android::hardware::neuralnetworks::utils { class IProtectedCallback { public: /** * Marks this object as a dead object. */ virtual void notifyAsDeadObject() = 0; // Public virtual destructor to allow objects to be stored (and destroyed) as smart pointers. // E.g., std::unique_ptr. virtual ~IProtectedCallback() = default; protected: // Protect the non-destructor special member functions to prevent object slicing. IProtectedCallback() = default; IProtectedCallback(const IProtectedCallback&) = default; IProtectedCallback(IProtectedCallback&&) noexcept = default; IProtectedCallback& operator=(const IProtectedCallback&) = default; IProtectedCallback& operator=(IProtectedCallback&&) noexcept = default; }; // Thread safe class class DeathRecipient final : public hidl_death_recipient { public: void serviceDied(uint64_t cookie, const wp& who) override; // Precondition: `killable` must be non-null. void add(IProtectedCallback* killable) const; // Precondition: `killable` must be non-null. void remove(IProtectedCallback* killable) const; private: mutable std::mutex mMutex; mutable bool mIsDeadObject GUARDED_BY(mMutex) = false; mutable std::vector mObjects GUARDED_BY(mMutex); }; class DeathHandler final { public: static nn::GeneralResult create(sp object); DeathHandler(const DeathHandler&) = delete; DeathHandler(DeathHandler&&) noexcept = default; DeathHandler& operator=(const DeathHandler&) = delete; DeathHandler& operator=(DeathHandler&&) noexcept = delete; ~DeathHandler(); using Cleanup = std::function; using Hold = base::ScopeGuard; // Precondition: `killable` must be non-null. // `killable` must outlive the return value `Hold`. [[nodiscard]] Hold protectCallback(IProtectedCallback* killable) const; // Precondition: `killable` must be non-null. // `killable` must outlive the `DeathHandler`. void protectCallbackForLifetimeOfDeathHandler(IProtectedCallback* killable) const; private: DeathHandler(sp object, sp deathRecipient); sp mObject; sp mDeathRecipient; }; } // namespace android::hardware::neuralnetworks::utils #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H