diff options
author | Tomasz Wasilczyk <twasilczyk@google.com> | 2017-09-14 09:43:35 -0700 |
---|---|---|
committer | Tomasz Wasilczyk <twasilczyk@google.com> | 2017-09-21 13:25:17 -0700 |
commit | f679e8b6ece550ea84348c70b8e414d92fc4a5c6 (patch) | |
tree | bc768af1cc558b7f17a096b7839bf8f2a535feb8 /broadcastradio/common/utils/WorkerThread.cpp | |
parent | 2cf5bc8db511f2c71d5b69b75f9fd0f6ec097e1d (diff) |
Branch out Broadcast Radio 1.2 HAL.
Bug: 62945293
Change-Id: Ic84f9fe6c17040053257085801208ed527fd07ab
Fixes: 64115813
Test: instrumentation, VTS
Diffstat (limited to 'broadcastradio/common/utils/WorkerThread.cpp')
-rw-r--r-- | broadcastradio/common/utils/WorkerThread.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/broadcastradio/common/utils/WorkerThread.cpp b/broadcastradio/common/utils/WorkerThread.cpp new file mode 100644 index 0000000000..bfcbb390e8 --- /dev/null +++ b/broadcastradio/common/utils/WorkerThread.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define LOG_TAG "WorkerThread" +//#define LOG_NDEBUG 0 + +#include <broadcastradio-utils/WorkerThread.h> + +#include <log/log.h> + +namespace android { + +using std::chrono::milliseconds; +using std::chrono::steady_clock; +using std::function; +using std::lock_guard; +using std::mutex; +using std::priority_queue; +using std::this_thread::sleep_for; +using std::unique_lock; + +bool operator<(const WorkerThread::Task& lhs, const WorkerThread::Task& rhs) { + return lhs.when > rhs.when; +} + +WorkerThread::WorkerThread() : mIsTerminating(false), mThread(&WorkerThread::threadLoop, this) {} + +WorkerThread::~WorkerThread() { + ALOGV("%s", __func__); + { + lock_guard<mutex> lk(mMut); + mIsTerminating = true; + mCond.notify_one(); + } + mThread.join(); +} + +void WorkerThread::schedule(function<void()> task, milliseconds delay) { + ALOGV("%s", __func__); + + auto when = steady_clock::now() + delay; + + lock_guard<mutex> lk(mMut); + mTasks.push(Task({when, task})); + mCond.notify_one(); +} + +void WorkerThread::cancelAll() { + ALOGV("%s", __func__); + + lock_guard<mutex> lk(mMut); + priority_queue<Task>().swap(mTasks); // empty queue +} + +void WorkerThread::threadLoop() { + ALOGV("%s", __func__); + while (!mIsTerminating) { + unique_lock<mutex> lk(mMut); + if (mTasks.empty()) { + mCond.wait(lk); + continue; + } + + auto task = mTasks.top(); + if (task.when > steady_clock::now()) { + mCond.wait_until(lk, task.when); + continue; + } + + mTasks.pop(); + lk.unlock(); // what() might need to schedule another task + task.what(); + } +} + +} // namespace android |