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