Michael Butler | 0e2ac1b | 2017-09-01 10:59:38 -0700 | [diff] [blame] | 1 | #include "Event.h" |
| 2 | #include <android-base/logging.h> |
| 3 | |
| 4 | namespace android { |
| 5 | namespace hardware { |
| 6 | namespace neuralnetworks { |
| 7 | namespace V1_0 { |
| 8 | namespace implementation { |
| 9 | |
| 10 | Event::Event() : mStatus(Status::WAITING) {} |
| 11 | |
| 12 | Event::~Event() { |
David Gross | 0380d7f | 2017-09-11 16:23:17 -0700 | [diff] [blame] | 13 | // Note that we cannot call Event::join_thread from here: Event is |
| 14 | // intended to be reference counted, and it is possible that the |
| 15 | // reference count drops to zero in the bound thread, causing the |
| 16 | // bound thread to call this destructor. If a thread tries to join |
| 17 | // itself, it throws an exception, producing a message like the |
| 18 | // following: |
| 19 | // |
| 20 | // terminating with uncaught exception of type std::__1::system_error: |
| 21 | // thread::join failed: Resource deadlock would occur |
Michael Butler | 0e2ac1b | 2017-09-01 10:59:38 -0700 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | Return<void> Event::notify(ReturnedStatus status) { |
| 25 | { |
| 26 | std::lock_guard<std::mutex> lock(mMutex); |
| 27 | mStatus = status == ReturnedStatus::SUCCESS ? Status::SUCCESS : Status::ERROR; |
| 28 | if (mStatus == Status::SUCCESS && mCallback != nullptr) { |
| 29 | bool success = mCallback(); |
| 30 | if (!success) { |
| 31 | LOG(ERROR) << "Event::notify -- callback failed"; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | mCondition.notify_all(); |
| 36 | return Void(); |
| 37 | } |
| 38 | |
| 39 | Event::Status Event::poll() { |
| 40 | std::lock_guard<std::mutex> lock(mMutex); |
| 41 | return mStatus; |
| 42 | } |
| 43 | |
| 44 | Event::Status Event::wait() { |
| 45 | std::unique_lock<std::mutex> lock(mMutex); |
| 46 | mCondition.wait(lock, [this]{return mStatus != Status::WAITING;}); |
David Gross | 0380d7f | 2017-09-11 16:23:17 -0700 | [diff] [blame] | 47 | join_thread_locked(); |
Michael Butler | 0e2ac1b | 2017-09-01 10:59:38 -0700 | [diff] [blame] | 48 | return mStatus; |
| 49 | } |
| 50 | |
| 51 | bool Event::on_finish(std::function<bool(void)> callback) { |
| 52 | std::lock_guard<std::mutex> lock(mMutex); |
| 53 | if (mCallback != nullptr) { |
| 54 | LOG(ERROR) << "Event::on_finish -- a callback has already been bound to this event"; |
| 55 | return false; |
| 56 | } |
| 57 | if (callback == nullptr) { |
| 58 | LOG(ERROR) << "Event::on_finish -- the new callback is invalid"; |
| 59 | return false; |
| 60 | } |
| 61 | mCallback = std::move(callback); |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | bool Event::bind_thread(std::thread&& asyncThread) { |
| 66 | std::lock_guard<std::mutex> lock(mMutex); |
| 67 | if (mThread.joinable()) { |
| 68 | LOG(ERROR) << "Event::bind_thread -- a thread has already been bound to this event"; |
| 69 | return false; |
| 70 | } |
| 71 | if (!asyncThread.joinable()) { |
| 72 | LOG(ERROR) << "Event::bind_thread -- the new thread is not joinable"; |
| 73 | return false; |
| 74 | } |
| 75 | mThread = std::move(asyncThread); |
| 76 | return true; |
| 77 | } |
| 78 | |
David Gross | 0380d7f | 2017-09-11 16:23:17 -0700 | [diff] [blame] | 79 | void Event::join_thread() { |
| 80 | std::lock_guard<std::mutex> lock(mMutex); |
| 81 | join_thread_locked(); |
| 82 | } |
| 83 | |
| 84 | void Event::join_thread_locked() { |
| 85 | if (mThread.joinable()) { |
| 86 | mThread.join(); |
| 87 | } |
| 88 | } |
| 89 | |
Michael Butler | 0e2ac1b | 2017-09-01 10:59:38 -0700 | [diff] [blame] | 90 | } // namespace implementation |
| 91 | } // namespace V1_0 |
| 92 | } // namespace neuralnetworks |
| 93 | } // namespace hardware |
| 94 | } // namespace android |