blob: 43fd04a89a79d3ca43ea1d53ae16d5d97acd5cb3 [file] [log] [blame]
Tomasz Wasilczyk48377552017-06-22 10:45:33 -07001/*
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 Wasilczykc1763a62017-07-25 10:01:17 -070017#include <broadcastradio-utils/WorkerThread.h>
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070018
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070019namespace android {
20
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070021using std::function;
22using std::lock_guard;
23using std::mutex;
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070024using std::unique_lock;
Weilin Xud7483322022-11-29 01:12:36 +000025using std::chrono::milliseconds;
26using std::chrono::steady_clock;
27using std::this_thread::sleep_for;
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070028
29bool operator<(const WorkerThread::Task& lhs, const WorkerThread::Task& rhs) {
30 return lhs.when > rhs.when;
31}
32
Keith Mokb1210922022-05-06 19:25:54 +000033WorkerThread::WorkerThread() : mIsTerminating(false) {
34 // putting mThread in constructor instead of initializer list
35 // to ensure all class members are init before mThread starts
36 mThread = std::thread(&WorkerThread::threadLoop, this);
37}
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070038
39WorkerThread::~WorkerThread() {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070040 {
41 lock_guard<mutex> lk(mMut);
42 mIsTerminating = true;
43 mCond.notify_one();
44 }
45 mThread.join();
46}
47
48void WorkerThread::schedule(function<void()> task, milliseconds delay) {
Weilin Xud7483322022-11-29 01:12:36 +000049 auto cancelTask = []() {};
50 schedule(std::move(task), cancelTask, delay);
51}
52
53void WorkerThread::schedule(function<void()> task, function<void()> cancelTask,
54 milliseconds delay) {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070055 auto when = steady_clock::now() + delay;
56
57 lock_guard<mutex> lk(mMut);
Weilin Xud7483322022-11-29 01:12:36 +000058 mTasks.push(Task({when, std::move(task), std::move(cancelTask)}));
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070059 mCond.notify_one();
60}
61
62void WorkerThread::cancelAll() {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070063 lock_guard<mutex> lk(mMut);
Weilin Xud7483322022-11-29 01:12:36 +000064 while (!mTasks.empty()) {
65 auto task = mTasks.top();
66 task.onCanceled();
67 mTasks.pop();
68 }
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070069}
70
71void WorkerThread::threadLoop() {
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070072 while (!mIsTerminating) {
73 unique_lock<mutex> lk(mMut);
74 if (mTasks.empty()) {
75 mCond.wait(lk);
76 continue;
77 }
78
79 auto task = mTasks.top();
80 if (task.when > steady_clock::now()) {
81 mCond.wait_until(lk, task.when);
82 continue;
83 }
84
85 mTasks.pop();
86 lk.unlock(); // what() might need to schedule another task
87 task.what();
88 }
89}
90
91} // namespace android