Sahil Dhanju | c1ba5c4 | 2016-06-07 20:09:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "Event.h" |
| 18 | |
| 19 | using namespace android; |
| 20 | |
| 21 | Event::Event(Increment::IncrementCase type) : mIncrementType(type) {} |
| 22 | |
| 23 | void Event::readyToExecute() { |
| 24 | changeState(Event::EventState::Waiting); |
| 25 | waitUntil(Event::EventState::Signaled); |
| 26 | changeState(Event::EventState::Running); |
| 27 | } |
| 28 | |
| 29 | void Event::complete() { |
| 30 | waitUntil(Event::EventState::Waiting); |
| 31 | changeState(Event::EventState::Signaled); |
| 32 | waitUntil(Event::EventState::Running); |
| 33 | } |
| 34 | |
| 35 | void Event::waitUntil(Event::EventState state) { |
| 36 | std::unique_lock<std::mutex> lock(mLock); |
| 37 | mCond.wait(lock, [this, state] { return (mState == state); }); |
| 38 | } |
| 39 | |
| 40 | void Event::changeState(Event::EventState state) { |
| 41 | std::unique_lock<std::mutex> lock(mLock); |
| 42 | mState = state; |
| 43 | lock.unlock(); |
| 44 | |
| 45 | mCond.notify_one(); |
| 46 | } |
| 47 | |
| 48 | Increment::IncrementCase Event::getIncrementType() { |
| 49 | return mIncrementType; |
| 50 | } |