• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
59     static void serviceDied(void* cookie);
60     void serviceDied();
61     // Precondition: `killable` must be non-null.
62     void add(IProtectedCallback* killable) const;
63     // Precondition: `killable` must be non-null.
64     void remove(IProtectedCallback* killable) const;
65 
66   private:
67     mutable std::mutex mMutex;
68     mutable std::vector<IProtectedCallback*> mObjects GUARDED_BY(mMutex);
69 };
70 
71 class DeathHandler final {
72   public:
73     static nn::GeneralResult<DeathHandler> create(std::shared_ptr<ndk::ICInterface> 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     // Precondition: `killable` must be non-null.
83     [[nodiscard]] ::android::base::ScopeGuard<Cleanup> protectCallback(
84             IProtectedCallback* killable) const;
85 
getDeathMonitor()86     std::shared_ptr<DeathMonitor> getDeathMonitor() const { return kDeathMonitor; }
87 
88   private:
89     DeathHandler(std::shared_ptr<ndk::ICInterface> object,
90                  ndk::ScopedAIBinder_DeathRecipient deathRecipient,
91                  std::shared_ptr<DeathMonitor> deathMonitor);
92 
93     std::shared_ptr<ndk::ICInterface> kObject;
94     ndk::ScopedAIBinder_DeathRecipient kDeathRecipient;
95     std::shared_ptr<DeathMonitor> kDeathMonitor;
96 };
97 
98 }  // namespace aidl::android::hardware::neuralnetworks::utils
99 
100 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_PROTECT_CALLBACK_H
101