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_RESILIENT_BURST_H 18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_RESILIENT_BURST_H 19 20 #include <android-base/thread_annotations.h> 21 #include <nnapi/IBurst.h> 22 #include <nnapi/Result.h> 23 #include <nnapi/Types.h> 24 25 #include <functional> 26 #include <memory> 27 #include <mutex> 28 #include <optional> 29 #include <utility> 30 31 namespace android::hardware::neuralnetworks::utils { 32 33 class ResilientBurst final : public nn::IBurst, 34 public std::enable_shared_from_this<ResilientBurst> { 35 struct PrivateConstructorTag {}; 36 37 public: 38 using Factory = std::function<nn::GeneralResult<nn::SharedBurst>()>; 39 40 static nn::GeneralResult<std::shared_ptr<const ResilientBurst>> create(Factory makeBurst); 41 42 ResilientBurst(PrivateConstructorTag tag, Factory makeBurst, nn::SharedBurst burst); 43 44 nn::SharedBurst getBurst() const; 45 nn::GeneralResult<nn::SharedBurst> recover(const nn::IBurst* failingBurst) const; 46 47 OptionalCacheHold cacheMemory(const nn::SharedMemory& memory) const override; 48 49 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> execute( 50 const nn::Request& request, nn::MeasureTiming measure, 51 const nn::OptionalTimePoint& deadline, 52 const nn::OptionalDuration& loopTimeoutDuration) const override; 53 54 nn::GeneralResult<nn::SharedExecution> createReusableExecution( 55 const nn::Request& request, nn::MeasureTiming measure, 56 const nn::OptionalDuration& loopTimeoutDuration) const override; 57 58 private: 59 bool isValidInternal() const EXCLUDES(mMutex); 60 nn::GeneralResult<nn::SharedExecution> createReusableExecutionInternal( 61 const nn::Request& request, nn::MeasureTiming measure, 62 const nn::OptionalDuration& loopTimeoutDuration) const; 63 64 const Factory kMakeBurst; 65 mutable std::mutex mMutex; 66 mutable nn::SharedBurst mBurst GUARDED_BY(mMutex); 67 }; 68 69 } // namespace android::hardware::neuralnetworks::utils 70 71 #endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_RESILIENT_BURST_H 72