Relocate NN ProtectCallback to 1.0/utils
This change is part of a larger chain of changes to remove HIDL and AIDL
libraries from neuralnetworks_utils_hal_common.
Bug: N/A
Test: mma
Change-Id: Ib43f1cb683a09ae5c9116a6dea4d269c9c2c78b4
diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h
index 8651912..168264b 100644
--- a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h
+++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/Callbacks.h
@@ -32,8 +32,7 @@
namespace aidl::android::hardware::neuralnetworks::utils {
// An AIDL callback class to receive the results of IDevice::prepareModel* asynchronously.
-class PreparedModelCallback final : public BnPreparedModelCallback,
- public hal::utils::IProtectedCallback {
+class PreparedModelCallback final : public BnPreparedModelCallback, public IProtectedCallback {
public:
using Data = nn::GeneralResult<nn::SharedPreparedModel>;
diff --git a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h
index ab1108c..92ed1cd 100644
--- a/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h
+++ b/neuralnetworks/aidl/utils/include/nnapi/hal/aidl/ProtectCallback.h
@@ -23,7 +23,6 @@
#include <nnapi/Result.h>
#include <nnapi/Types.h>
#include <nnapi/hal/CommonUtils.h>
-#include <nnapi/hal/ProtectCallback.h>
#include <functional>
#include <mutex>
@@ -34,19 +33,39 @@
namespace aidl::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 DeathMonitor final {
public:
static void serviceDied(void* cookie);
void serviceDied();
// Precondition: `killable` must be non-null.
- void add(hal::utils::IProtectedCallback* killable) const;
+ void add(IProtectedCallback* killable) const;
// Precondition: `killable` must be non-null.
- void remove(hal::utils::IProtectedCallback* killable) const;
+ void remove(IProtectedCallback* killable) const;
private:
mutable std::mutex mMutex;
- mutable std::vector<hal::utils::IProtectedCallback*> mObjects GUARDED_BY(mMutex);
+ mutable std::vector<IProtectedCallback*> mObjects GUARDED_BY(mMutex);
};
class DeathHandler final {
@@ -62,7 +81,7 @@
using Cleanup = std::function<void()>;
// Precondition: `killable` must be non-null.
[[nodiscard]] ::android::base::ScopeGuard<Cleanup> protectCallback(
- hal::utils::IProtectedCallback* killable) const;
+ IProtectedCallback* killable) const;
std::shared_ptr<DeathMonitor> getDeathMonitor() const { return kDeathMonitor; }
diff --git a/neuralnetworks/aidl/utils/src/ProtectCallback.cpp b/neuralnetworks/aidl/utils/src/ProtectCallback.cpp
index 124641c..54a673c 100644
--- a/neuralnetworks/aidl/utils/src/ProtectCallback.cpp
+++ b/neuralnetworks/aidl/utils/src/ProtectCallback.cpp
@@ -22,7 +22,6 @@
#include <android/binder_auto_utils.h>
#include <android/binder_interface_utils.h>
#include <nnapi/Result.h>
-#include <nnapi/hal/ProtectCallback.h>
#include <algorithm>
#include <functional>
@@ -37,7 +36,7 @@
void DeathMonitor::serviceDied() {
std::lock_guard guard(mMutex);
std::for_each(mObjects.begin(), mObjects.end(),
- [](hal::utils::IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
+ [](IProtectedCallback* killable) { killable->notifyAsDeadObject(); });
}
void DeathMonitor::serviceDied(void* cookie) {
@@ -45,13 +44,13 @@
deathMonitor->serviceDied();
}
-void DeathMonitor::add(hal::utils::IProtectedCallback* killable) const {
+void DeathMonitor::add(IProtectedCallback* killable) const {
CHECK(killable != nullptr);
std::lock_guard guard(mMutex);
mObjects.push_back(killable);
}
-void DeathMonitor::remove(hal::utils::IProtectedCallback* killable) const {
+void DeathMonitor::remove(IProtectedCallback* killable) const {
CHECK(killable != nullptr);
std::lock_guard guard(mMutex);
const auto removedIter = std::remove(mObjects.begin(), mObjects.end(), killable);
@@ -102,7 +101,7 @@
}
[[nodiscard]] ::android::base::ScopeGuard<DeathHandler::Cleanup> DeathHandler::protectCallback(
- hal::utils::IProtectedCallback* killable) const {
+ IProtectedCallback* killable) const {
CHECK(killable != nullptr);
kDeathMonitor->add(killable);
return ::android::base::make_scope_guard(