blob: 37afcf0bd9a667718733559be1f9a90e8bc8e77e [file] [log] [blame]
Xusong Wangb5cb8f72018-10-31 08:43:12 -07001/*
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 Butler23d0e562019-07-16 16:52:06 -070017#define LOG_TAG "Callbacks"
18
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010019#include "1.0/Callbacks.h"
Michael Butler23d0e562019-07-16 16:52:06 -070020
Michael Butlercf22a572017-09-22 13:26:12 -070021#include <android-base/logging.h>
22
Michael Butler23d0e562019-07-16 16:52:06 -070023namespace android::hardware::neuralnetworks::V1_0::implementation {
Michael Butlercf22a572017-09-22 13:26:12 -070024
Michael Butler23d0e562019-07-16 16:52:06 -070025// PreparedModelCallback methods begin here
Michael Butlercf22a572017-09-22 13:26:12 -070026
27Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus,
Michael Butler23d0e562019-07-16 16:52:06 -070028 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 Wangb5cb8f72018-10-31 08:43:12 -070044 return Void();
45}
46
Michael Butler23d0e562019-07-16 16:52:06 -070047void PreparedModelCallback::wait() const {
48 std::unique_lock<std::mutex> lock(mMutex);
49 mCondition.wait(lock, [this] { return mNotified; });
50}
51
52ErrorStatus PreparedModelCallback::getStatus() const {
Michael Butlercf22a572017-09-22 13:26:12 -070053 wait();
54 return mErrorStatus;
55}
56
Michael Butler23d0e562019-07-16 16:52:06 -070057sp<IPreparedModel> PreparedModelCallback::getPreparedModel() const {
Michael Butlercf22a572017-09-22 13:26:12 -070058 wait();
59 return mPreparedModel;
60}
61
Michael Butler23d0e562019-07-16 16:52:06 -070062// ExecutionCallback methods begin here
Michael Butlercf22a572017-09-22 13:26:12 -070063
64Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) {
Michael Butler23d0e562019-07-16 16:52:06 -070065 {
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 Wangb5cb8f72018-10-31 08:43:12 -070078 return Void();
79}
80
Michael Butler23d0e562019-07-16 16:52:06 -070081void ExecutionCallback::wait() const {
82 std::unique_lock<std::mutex> lock(mMutex);
83 mCondition.wait(lock, [this] { return mNotified; });
84}
85
86ErrorStatus ExecutionCallback::getStatus() const {
Michael Butlercf22a572017-09-22 13:26:12 -070087 wait();
88 return mErrorStatus;
89}
90
Michael Butler23d0e562019-07-16 16:52:06 -070091} // namespace android::hardware::neuralnetworks::V1_0::implementation