Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Callbacks" |
| 18 | |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 19 | #include "1.0/Callbacks.h" |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 20 | |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 23 | namespace android::hardware::neuralnetworks::V1_0::implementation { |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 24 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 25 | // PreparedModelCallback methods begin here |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 26 | |
| 27 | Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus, |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 28 | const sp<IPreparedModel>& preparedModel) { |
| 29 | { |
| 30 | std::lock_guard<std::mutex> hold(mMutex); |
| 31 | |
| 32 | // quick-return if object has already been notified |
| 33 | if (mNotified) { |
| 34 | return Void(); |
| 35 | } |
| 36 | |
| 37 | // store results and mark as notified |
| 38 | mErrorStatus = errorStatus; |
| 39 | mPreparedModel = preparedModel; |
| 40 | mNotified = true; |
| 41 | } |
| 42 | |
| 43 | mCondition.notify_all(); |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 44 | return Void(); |
| 45 | } |
| 46 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 47 | void PreparedModelCallback::wait() const { |
| 48 | std::unique_lock<std::mutex> lock(mMutex); |
| 49 | mCondition.wait(lock, [this] { return mNotified; }); |
| 50 | } |
| 51 | |
| 52 | ErrorStatus PreparedModelCallback::getStatus() const { |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 53 | wait(); |
| 54 | return mErrorStatus; |
| 55 | } |
| 56 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 57 | sp<IPreparedModel> PreparedModelCallback::getPreparedModel() const { |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 58 | wait(); |
| 59 | return mPreparedModel; |
| 60 | } |
| 61 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 62 | // ExecutionCallback methods begin here |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 63 | |
| 64 | Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) { |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 65 | { |
| 66 | std::lock_guard<std::mutex> hold(mMutex); |
| 67 | |
| 68 | // quick-return if object has already been notified |
| 69 | if (mNotified) { |
| 70 | return Void(); |
| 71 | } |
| 72 | |
| 73 | mErrorStatus = errorStatus; |
| 74 | mNotified = true; |
| 75 | } |
| 76 | mCondition.notify_all(); |
| 77 | |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 78 | return Void(); |
| 79 | } |
| 80 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 81 | void ExecutionCallback::wait() const { |
| 82 | std::unique_lock<std::mutex> lock(mMutex); |
| 83 | mCondition.wait(lock, [this] { return mNotified; }); |
| 84 | } |
| 85 | |
| 86 | ErrorStatus ExecutionCallback::getStatus() const { |
Michael Butler | cf22a57 | 2017-09-22 13:26:12 -0700 | [diff] [blame] | 87 | wait(); |
| 88 | return mErrorStatus; |
| 89 | } |
| 90 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame] | 91 | } // namespace android::hardware::neuralnetworks::V1_0::implementation |