summaryrefslogtreecommitdiff
path: root/neuralnetworks/utils/common/include/nnapi/hal/ProtectCallback.h
blob: 05110bc36492815d7008d6de6bbc78baeffc3a73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * 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 <android-base/scopeguard.h>
#include <android-base/thread_annotations.h>
#include <android/hidl/base/1.0/IBase.h>
#include <hidl/HidlSupport.h>
#include <nnapi/Result.h>
#include <nnapi/Types.h>

#include <functional>
#include <mutex>
#include <vector>

// 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<IProtectedCallback>.
    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<hidl::base::V1_0::IBase>& 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<IProtectedCallback*> mObjects GUARDED_BY(mMutex);
};

class DeathHandler final {
  public:
    static nn::GeneralResult<DeathHandler> create(sp<hidl::base::V1_0::IBase> object);

    DeathHandler(const DeathHandler&) = delete;
    DeathHandler(DeathHandler&&) noexcept = default;
    DeathHandler& operator=(const DeathHandler&) = delete;
    DeathHandler& operator=(DeathHandler&&) noexcept = delete;
    ~DeathHandler();

    using Cleanup = std::function<void()>;
    using Hold = base::ScopeGuard<Cleanup>;

    // 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<hidl::base::V1_0::IBase> object, sp<DeathRecipient> deathRecipient);

    sp<hidl::base::V1_0::IBase> mObject;
    sp<DeathRecipient> mDeathRecipient;
};

}  // namespace android::hardware::neuralnetworks::utils

#endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_PROTECT_CALLBACK_H