Xusong Wang | 68c3234 | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #define LOG_TAG "Callbacks" |
| 18 | |
| 19 | #include "1.3/Callbacks.h" |
| 20 | |
| 21 | #include <android-base/logging.h> |
| 22 | |
| 23 | #include <limits> |
| 24 | |
| 25 | namespace android::hardware::neuralnetworks::V1_3::implementation { |
| 26 | |
| 27 | using V1_0::ErrorStatus; |
| 28 | |
| 29 | // PreparedModelCallback methods begin here |
| 30 | |
| 31 | Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus, |
| 32 | const sp<V1_0::IPreparedModel>& preparedModel) { |
| 33 | { |
| 34 | std::lock_guard<std::mutex> hold(mMutex); |
| 35 | |
| 36 | // quick-return if object has already been notified |
| 37 | if (mNotified) { |
| 38 | return Void(); |
| 39 | } |
| 40 | |
| 41 | // store results and mark as notified |
| 42 | mErrorStatus = errorStatus; |
| 43 | mPreparedModel = preparedModel; |
| 44 | mNotified = true; |
| 45 | } |
| 46 | |
| 47 | mCondition.notify_all(); |
| 48 | return Void(); |
| 49 | } |
| 50 | |
| 51 | Return<void> PreparedModelCallback::notify_1_2(ErrorStatus errorStatus, |
| 52 | const sp<V1_2::IPreparedModel>& preparedModel) { |
| 53 | return notify(errorStatus, preparedModel); |
| 54 | } |
| 55 | |
| 56 | Return<void> PreparedModelCallback::notify_1_3(ErrorStatus errorStatus, |
Xusong Wang | 62a760c | 2019-10-25 12:07:17 -0700 | [diff] [blame^] | 57 | const sp<V1_3::IPreparedModel>& preparedModel) { |
Xusong Wang | 68c3234 | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 58 | return notify(errorStatus, preparedModel); |
| 59 | } |
| 60 | |
| 61 | void PreparedModelCallback::wait() const { |
| 62 | std::unique_lock<std::mutex> lock(mMutex); |
| 63 | mCondition.wait(lock, [this] { return mNotified; }); |
| 64 | } |
| 65 | |
| 66 | ErrorStatus PreparedModelCallback::getStatus() const { |
| 67 | wait(); |
| 68 | return mErrorStatus; |
| 69 | } |
| 70 | |
| 71 | sp<V1_0::IPreparedModel> PreparedModelCallback::getPreparedModel() const { |
| 72 | wait(); |
| 73 | return mPreparedModel; |
| 74 | } |
| 75 | |
| 76 | } // namespace android::hardware::neuralnetworks::V1_3::implementation |