blob: bfcbb390e8645e63951b12992de08b107802db3a [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
17#define LOG_TAG "WorkerThread"
18//#define LOG_NDEBUG 0
19
Tomasz Wasilczykc1763a62017-07-25 10:01:17 -070020#include <broadcastradio-utils/WorkerThread.h>
Tomasz Wasilczyk48377552017-06-22 10:45:33 -070021
22#include <log/log.h>
23
24namespace android {
25
26using std::chrono::milliseconds;
27using std::chrono::steady_clock;
28using std::function;
29using std::lock_guard;
30using std::mutex;
31using std::priority_queue;
32using std::this_thread::sleep_for;
33using std::unique_lock;
34
35bool operator<(const WorkerThread::Task& lhs, const WorkerThread::Task& rhs) {
36 return lhs.when > rhs.when;
37}
38
39WorkerThread::WorkerThread() : mIsTerminating(false), mThread(&WorkerThread::threadLoop, this) {}
40
41WorkerThread::~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
51void 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
61void WorkerThread::cancelAll() {
62 ALOGV("%s", __func__);
63
64 lock_guard<mutex> lk(mMut);
65 priority_queue<Task>().swap(mTasks); // empty queue
66}
67
68void 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