Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #define LOG_TAG "WorkerThread" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Tomasz Wasilczyk | c1763a6 | 2017-07-25 10:01:17 -0700 | [diff] [blame] | 20 | #include <broadcastradio-utils/WorkerThread.h> |
Tomasz Wasilczyk | 4837755 | 2017-06-22 10:45:33 -0700 | [diff] [blame] | 21 | |
| 22 | #include <log/log.h> |
| 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | using std::chrono::milliseconds; |
| 27 | using std::chrono::steady_clock; |
| 28 | using std::function; |
| 29 | using std::lock_guard; |
| 30 | using std::mutex; |
| 31 | using std::priority_queue; |
| 32 | using std::this_thread::sleep_for; |
| 33 | using std::unique_lock; |
| 34 | |
| 35 | bool operator<(const WorkerThread::Task& lhs, const WorkerThread::Task& rhs) { |
| 36 | return lhs.when > rhs.when; |
| 37 | } |
| 38 | |
| 39 | WorkerThread::WorkerThread() : mIsTerminating(false), mThread(&WorkerThread::threadLoop, this) {} |
| 40 | |
| 41 | WorkerThread::~WorkerThread() { |
| 42 | ALOGV("%s", __func__); |
| 43 | { |
| 44 | lock_guard<mutex> lk(mMut); |
| 45 | mIsTerminating = true; |
| 46 | mCond.notify_one(); |
| 47 | } |
| 48 | mThread.join(); |
| 49 | } |
| 50 | |
| 51 | void WorkerThread::schedule(function<void()> task, milliseconds delay) { |
| 52 | ALOGV("%s", __func__); |
| 53 | |
| 54 | auto when = steady_clock::now() + delay; |
| 55 | |
| 56 | lock_guard<mutex> lk(mMut); |
| 57 | mTasks.push(Task({when, task})); |
| 58 | mCond.notify_one(); |
| 59 | } |
| 60 | |
| 61 | void WorkerThread::cancelAll() { |
| 62 | ALOGV("%s", __func__); |
| 63 | |
| 64 | lock_guard<mutex> lk(mMut); |
| 65 | priority_queue<Task>().swap(mTasks); // empty queue |
| 66 | } |
| 67 | |
| 68 | void WorkerThread::threadLoop() { |
| 69 | ALOGV("%s", __func__); |
| 70 | while (!mIsTerminating) { |
| 71 | unique_lock<mutex> lk(mMut); |
| 72 | if (mTasks.empty()) { |
| 73 | mCond.wait(lk); |
| 74 | continue; |
| 75 | } |
| 76 | |
| 77 | auto task = mTasks.top(); |
| 78 | if (task.when > steady_clock::now()) { |
| 79 | mCond.wait_until(lk, task.when); |
| 80 | continue; |
| 81 | } |
| 82 | |
| 83 | mTasks.pop(); |
| 84 | lk.unlock(); // what() might need to schedule another task |
| 85 | task.what(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } // namespace android |