1 /* 2 * Copyright (C) 2021 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 "Callbacks.h" 20 21 #include <android-base/logging.h> 22 #include <android/binder_auto_utils.h> 23 #include <limits> 24 25 namespace aidl::android::hardware::neuralnetworks::implementation { 26 notify(ErrorStatus errorStatus,const std::shared_ptr<IPreparedModel> & preparedModel)27ndk::ScopedAStatus PreparedModelCallback::notify( 28 ErrorStatus errorStatus, const std::shared_ptr<IPreparedModel>& preparedModel) { 29 { 30 std::lock_guard<std::mutex> hold(mMutex); 31 // quick-return if object has already been notified 32 if (mNotified) { 33 return ndk::ScopedAStatus::ok(); 34 } 35 // store results and mark as notified 36 mErrorStatus = errorStatus; 37 mPreparedModel = preparedModel; 38 mNotified = true; 39 } 40 mCondition.notify_all(); 41 return ndk::ScopedAStatus::ok(); 42 } 43 wait() const44void PreparedModelCallback::wait() const { 45 std::unique_lock<std::mutex> lock(mMutex); 46 mCondition.wait(lock, [this] { return mNotified; }); 47 } 48 getStatus() const49ErrorStatus PreparedModelCallback::getStatus() const { 50 wait(); 51 return mErrorStatus; 52 } 53 getPreparedModel() const54std::shared_ptr<IPreparedModel> PreparedModelCallback::getPreparedModel() const { 55 wait(); 56 return mPreparedModel; 57 } 58 59 } // namespace aidl::android::hardware::neuralnetworks::implementation 60