blob: ea5b7638cdc682ea36673666bb3743d2c857861d [file] [log] [blame]
Miao Wang1b27b7a2017-09-29 11:56:50 -07001/*
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
Michael Butler689d8922017-09-01 10:58:46 -070017#include "Event.h"
18#include <android-base/logging.h>
19
20namespace android {
21namespace hardware {
22namespace neuralnetworks {
23namespace V1_0 {
24namespace implementation {
25
26Event::Event() : mStatus(Status::WAITING) {}
27
28Event::~Event() {
David Gross402baa32017-09-11 16:10:31 -070029 // Note that we cannot call Event::join_thread from here: Event is
30 // intended to be reference counted, and it is possible that the
31 // reference count drops to zero in the bound thread, causing the
32 // bound thread to call this destructor. If a thread tries to join
33 // itself, it throws an exception, producing a message like the
34 // following:
35 //
36 // terminating with uncaught exception of type std::__1::system_error:
37 // thread::join failed: Resource deadlock would occur
Michael Butler689d8922017-09-01 10:58:46 -070038}
39
Michael Butler5f916fc2017-09-11 21:10:36 -070040Return<void> Event::notify(ErrorStatus status) {
Michael Butler689d8922017-09-01 10:58:46 -070041 {
42 std::lock_guard<std::mutex> lock(mMutex);
Michael Butler5f916fc2017-09-11 21:10:36 -070043 mStatus = status == ErrorStatus::NONE ? Status::SUCCESS : Status::ERROR;
Michael Butler689d8922017-09-01 10:58:46 -070044 if (mStatus == Status::SUCCESS && mCallback != nullptr) {
45 bool success = mCallback();
46 if (!success) {
47 LOG(ERROR) << "Event::notify -- callback failed";
48 }
49 }
50 }
51 mCondition.notify_all();
52 return Void();
53}
54
55Event::Status Event::poll() {
56 std::lock_guard<std::mutex> lock(mMutex);
57 return mStatus;
58}
59
60Event::Status Event::wait() {
61 std::unique_lock<std::mutex> lock(mMutex);
62 mCondition.wait(lock, [this]{return mStatus != Status::WAITING;});
David Gross402baa32017-09-11 16:10:31 -070063 join_thread_locked();
Michael Butler689d8922017-09-01 10:58:46 -070064 return mStatus;
65}
66
67bool Event::on_finish(std::function<bool(void)> callback) {
68 std::lock_guard<std::mutex> lock(mMutex);
69 if (mCallback != nullptr) {
70 LOG(ERROR) << "Event::on_finish -- a callback has already been bound to this event";
71 return false;
72 }
73 if (callback == nullptr) {
74 LOG(ERROR) << "Event::on_finish -- the new callback is invalid";
75 return false;
76 }
77 mCallback = std::move(callback);
78 return true;
79}
80
81bool Event::bind_thread(std::thread&& asyncThread) {
82 std::lock_guard<std::mutex> lock(mMutex);
83 if (mThread.joinable()) {
84 LOG(ERROR) << "Event::bind_thread -- a thread has already been bound to this event";
85 return false;
86 }
87 if (!asyncThread.joinable()) {
88 LOG(ERROR) << "Event::bind_thread -- the new thread is not joinable";
89 return false;
90 }
91 mThread = std::move(asyncThread);
92 return true;
93}
94
David Gross402baa32017-09-11 16:10:31 -070095void Event::join_thread() {
96 std::lock_guard<std::mutex> lock(mMutex);
97 join_thread_locked();
98}
99
100void Event::join_thread_locked() {
101 if (mThread.joinable()) {
102 mThread.join();
103 }
104}
105
Michael Butler689d8922017-09-01 10:58:46 -0700106} // namespace implementation
107} // namespace V1_0
108} // namespace neuralnetworks
109} // namespace hardware
110} // namespace android