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