Steve Kondik | 55db053 | 2017-06-12 11:27:18 -0700 | [diff] [blame^] | 1 | #define LOG_TAG "VNC-EventQueue" |
| 2 | #include <utils/Log.h> |
| 3 | |
| 4 | #include "EventQueue.h" |
| 5 | |
| 6 | using namespace android; |
| 7 | |
| 8 | void EventQueue::enqueue(const Event& event) { |
| 9 | mQueue.push(event); |
| 10 | ALOGV("enqueue: mId=%d mData=%p qlen=%zu", event.mId, event.mData, mQueue.size()); |
| 11 | |
| 12 | Mutex::Autolock _l(mMutex); |
| 13 | mCondition.broadcast(); |
| 14 | } |
| 15 | |
| 16 | void EventQueue::await() { |
| 17 | Mutex::Autolock _l(mMutex); |
| 18 | |
| 19 | while (mRunning) { |
| 20 | ALOGV("begin wait"); |
| 21 | mCondition.wait(mMutex); |
| 22 | |
| 23 | ALOGV("queue active"); |
| 24 | while (!mQueue.empty()) { |
| 25 | Event event = mQueue.front(); |
| 26 | mQueue.pop(); |
| 27 | |
| 28 | mMutex.unlock(); |
| 29 | for (std::vector<EventListener *>::iterator it = mListeners.begin(); |
| 30 | it != mListeners.end(); ++it) { |
| 31 | ALOGV("call listener: %p", *it); |
| 32 | (*it)->onEvent(event); |
| 33 | } |
| 34 | mMutex.lock(); |
| 35 | |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void EventQueue::shutdown() { |
| 41 | Mutex::Autolock _l(mMutex); |
| 42 | flush(); |
| 43 | mRunning = false; |
| 44 | mCondition.broadcast(); |
| 45 | } |
| 46 | |
| 47 | void EventQueue::flush() { |
| 48 | Mutex::Autolock _l(mMutex); |
| 49 | mQueue = {}; |
| 50 | } |
| 51 | |
| 52 | void EventQueue::addListener(EventListener *listener) { |
| 53 | mListeners.push_back(listener); |
| 54 | ALOGV("addListener: %p", listener); |
| 55 | } |
| 56 | |
| 57 | void EventQueue::removeListener(EventListener *listener) { |
| 58 | mListeners.erase(std::remove(mListeners.begin(), mListeners.end(), listener), mListeners.end()); |
| 59 | } |