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_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_EVENT_H 18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_EVENT_H 19 20 #include <android-base/logging.h> 21 #include <nnapi/Types.h> 22 23 #include <functional> 24 #include <memory> 25 #include <mutex> 26 #include <utility> 27 28 #include "ExecutionCallback.h" 29 30 namespace android::nn { 31 32 class IEvent { 33 public: 34 virtual ~IEvent() = default; 35 virtual ErrorStatus wait() const = 0; 36 virtual int getSyncFenceFd(bool shouldDup) const = 0; 37 }; 38 39 // The CallbackEvent wraps ExecutionCallback 40 class CallbackEvent : public IEvent { 41 public: CallbackEvent(std::shared_ptr<ExecutionCallback> callback)42 CallbackEvent(std::shared_ptr<ExecutionCallback> callback) 43 : kExecutionCallback(std::move(callback)) { 44 CHECK(kExecutionCallback != nullptr); 45 } 46 wait()47 ErrorStatus wait() const override { 48 kExecutionCallback->wait(); 49 return kExecutionCallback->getStatus(); 50 } 51 52 // Always return -1 as this is not backed by a sync fence. getSyncFenceFd(bool)53 int getSyncFenceFd(bool /*should_dup*/) const override { return -1; } 54 55 private: 56 const std::shared_ptr<ExecutionCallback> kExecutionCallback; 57 }; 58 59 // The SyncFenceEvent wraps sync fence and ExecuteFencedInfoCallback 60 class SyncFenceEvent : public IEvent { 61 using ExecutionFinishCallback = std::function<ErrorStatus(ErrorStatus)>; 62 63 public: SyncFenceEvent(int sync_fence_fd,const ExecuteFencedInfoCallback & callback,const ExecutionFinishCallback & finish)64 SyncFenceEvent(int sync_fence_fd, const ExecuteFencedInfoCallback& callback, 65 const ExecutionFinishCallback& finish) 66 : kFencedExecutionCallback(callback), kFinishCallback(finish) { 67 if (sync_fence_fd > 0) { 68 // Dup the provided file descriptor 69 mSyncFenceFd = dup(sync_fence_fd); 70 CHECK(mSyncFenceFd > 0); 71 } 72 } 73 74 // Close the fd the event owns. ~SyncFenceEvent()75 ~SyncFenceEvent() { close(mSyncFenceFd); } 76 77 // Use syncWait to wait for the sync fence until the status change. 78 // In case of syncWait error, query the dispatch callback for detailed error status. 79 // This method maps to the NDK ANeuralNetworksEvent_wait, which must be thread-safe. wait()80 ErrorStatus wait() const override { 81 std::lock_guard<std::mutex> lock(mMutex); 82 if (mFinished) return mError; 83 84 if (mSyncFenceFd > 0 && syncWait(mSyncFenceFd, -1) != FenceState::SIGNALED) { 85 mError = ErrorStatus::GENERAL_FAILURE; 86 // If there is a callback available, use the callback to get the error code. 87 if (kFencedExecutionCallback != nullptr) { 88 auto result = kFencedExecutionCallback(); 89 if (!result.has_value()) { 90 LOG(ERROR) << "Fenced execution callback failed: " << result.error().message; 91 mError = result.error().code; 92 CHECK_NE(mError, ErrorStatus::NONE); 93 } 94 } 95 } 96 if (kFinishCallback != nullptr) { 97 mError = kFinishCallback(mError); 98 } 99 mFinished = true; 100 return mError; 101 } 102 103 // Return the sync fence fd. 104 // If shouldDup is true, the caller must close the fd returned: 105 // - When being used internally within NNAPI runtime, set shouldDup to false. 106 // - When being used to return a fd to application code, set shouldDup to 107 // true. getSyncFenceFd(bool shouldDup)108 int getSyncFenceFd(bool shouldDup) const override { 109 if (shouldDup) { 110 return dup(mSyncFenceFd); 111 } 112 return mSyncFenceFd; 113 } 114 115 private: 116 // TODO(b/148423931): used android::base::unique_fd instead. 117 int mSyncFenceFd = -1; 118 const ExecuteFencedInfoCallback kFencedExecutionCallback; 119 const ExecutionFinishCallback kFinishCallback; 120 121 mutable std::mutex mMutex; 122 mutable bool mFinished GUARDED_BY(mMutex) = false; 123 mutable ErrorStatus mError GUARDED_BY(mMutex) = ErrorStatus::NONE; 124 }; 125 126 } // namespace android::nn 127 128 #endif // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_RUNTIME_EVENT_H 129