1 /*
2 * Copyright (C) 2017 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 #include "PreparedModelCallback.h"
18
19 namespace android::nn {
20
notifyInternal(bool deadObject,ErrorStatus errorStatus,const sp<V1_0::IPreparedModel> & preparedModel)21 hardware::Return<void> PreparedModelCallback::notifyInternal(
22 bool deadObject, ErrorStatus errorStatus, const sp<V1_0::IPreparedModel>& preparedModel) {
23 {
24 std::lock_guard<std::mutex> hold(mMutex);
25
26 // quick-return if object has already been notified
27 if (mNotified) {
28 return hardware::Void();
29 }
30
31 // store results and mark as notified
32 mDeadObject = deadObject;
33 mErrorStatus = errorStatus;
34 mPreparedModel = preparedModel;
35 mNotified = true;
36 }
37
38 mCondition.notify_all();
39 return hardware::Void();
40 }
41
notify(V1_0::ErrorStatus errorStatus,const sp<V1_0::IPreparedModel> & preparedModel)42 hardware::Return<void> PreparedModelCallback::notify(
43 V1_0::ErrorStatus errorStatus, const sp<V1_0::IPreparedModel>& preparedModel) {
44 return notifyInternal(false, uncheckedConvert(errorStatus), preparedModel);
45 }
46
notify_1_2(V1_0::ErrorStatus errorStatus,const sp<V1_2::IPreparedModel> & preparedModel)47 hardware::Return<void> PreparedModelCallback::notify_1_2(
48 V1_0::ErrorStatus errorStatus, const sp<V1_2::IPreparedModel>& preparedModel) {
49 return notifyInternal(false, uncheckedConvert(errorStatus), preparedModel);
50 }
51
notify_1_3(V1_3::ErrorStatus errorStatus,const sp<V1_3::IPreparedModel> & preparedModel)52 hardware::Return<void> PreparedModelCallback::notify_1_3(
53 V1_3::ErrorStatus errorStatus, const sp<V1_3::IPreparedModel>& preparedModel) {
54 return notifyInternal(false, uncheckedConvert(errorStatus), preparedModel);
55 }
56
notifyAsDeadObject()57 void PreparedModelCallback::notifyAsDeadObject() {
58 notifyInternal(true, ErrorStatus::GENERAL_FAILURE, nullptr);
59 }
60
wait() const61 void PreparedModelCallback::wait() const {
62 std::unique_lock<std::mutex> lock(mMutex);
63 mCondition.wait(lock, [this] { return mNotified; });
64 }
65
getStatus() const66 ErrorStatus PreparedModelCallback::getStatus() const {
67 wait();
68 return mErrorStatus;
69 }
70
getPreparedModel() const71 sp<V1_0::IPreparedModel> PreparedModelCallback::getPreparedModel() const {
72 wait();
73 return mPreparedModel;
74 }
75
isDeadObject() const76 bool PreparedModelCallback::isDeadObject() const {
77 wait();
78 return mDeadObject;
79 }
80
81 } // namespace android::nn
82