1 /*
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 #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H
19 
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/Types.h>
26 
27 #include <functional>
28 #include <mutex>
29 #include <vector>
30 
31 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
32 // lifetimes across processes and for protecting asynchronous calls across HIDL.
33 
34 namespace 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 DeathRecipient final : public hidl_death_recipient {
58   public:
59     void serviceDied(uint64_t cookie, const wp<hidl::base::V1_0::IBase>& who) override;
60     // Precondition: `killable` must be non-null.
61     void add(IProtectedCallback* killable) const;
62     // Precondition: `killable` must be non-null.
63     void remove(IProtectedCallback* killable) const;
64 
65   private:
66     mutable std::mutex mMutex;
67     mutable bool mIsDeadObject GUARDED_BY(mMutex) = false;
68     mutable std::vector<IProtectedCallback*> mObjects GUARDED_BY(mMutex);
69 };
70 
71 class DeathHandler final {
72   public:
73     static nn::GeneralResult<DeathHandler> create(sp<hidl::base::V1_0::IBase> object);
74 
75     DeathHandler(const DeathHandler&) = delete;
76     DeathHandler(DeathHandler&&) noexcept = default;
77     DeathHandler& operator=(const DeathHandler&) = delete;
78     DeathHandler& operator=(DeathHandler&&) noexcept = delete;
79     ~DeathHandler();
80 
81     using Cleanup = std::function<void()>;
82     using Hold = base::ScopeGuard<Cleanup>;
83 
84     // Precondition: `killable` must be non-null.
85     // `killable` must outlive the return value `Hold`.
86     [[nodiscard]] Hold protectCallback(IProtectedCallback* killable) const;
87 
88     // Precondition: `killable` must be non-null.
89     // `killable` must outlive the `DeathHandler`.
90     void protectCallbackForLifetimeOfDeathHandler(IProtectedCallback* killable) const;
91 
92   private:
93     DeathHandler(sp<hidl::base::V1_0::IBase> object, sp<DeathRecipient> deathRecipient);
94 
95     sp<hidl::base::V1_0::IBase> mObject;
96     sp<DeathRecipient> mDeathRecipient;
97 };
98 
99 }  // namespace android::hardware::neuralnetworks::utils
100 
101 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H
102