2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "WorkerThread"
18 //#define LOG_NDEBUG 0
20 #include "WorkerThread.h"
26 using std::chrono::milliseconds;
27 using std::chrono::steady_clock;
29 using std::lock_guard;
31 using std::priority_queue;
32 using std::this_thread::sleep_for;
33 using std::unique_lock;
35 bool operator<(const WorkerThread::Task& lhs, const WorkerThread::Task& rhs) {
36 return lhs.when > rhs.when;
39 WorkerThread::WorkerThread() : mIsTerminating(false), mThread(&WorkerThread::threadLoop, this) {}
41 WorkerThread::~WorkerThread() {
42 ALOGV("%s", __func__);
44 lock_guard<mutex> lk(mMut);
45 mIsTerminating = true;
51 void WorkerThread::schedule(function<void()> task, milliseconds delay) {
52 ALOGV("%s", __func__);
54 auto when = steady_clock::now() + delay;
56 lock_guard<mutex> lk(mMut);
57 mTasks.push(Task({when, task}));
61 void WorkerThread::cancelAll() {
62 ALOGV("%s", __func__);
64 lock_guard<mutex> lk(mMut);
65 priority_queue<Task>().swap(mTasks); // empty queue
68 void WorkerThread::threadLoop() {
69 ALOGV("%s", __func__);
70 while (!mIsTerminating) {
71 unique_lock<mutex> lk(mMut);
77 auto task = mTasks.top();
78 if (task.when > steady_clock::now()) {
79 mCond.wait_until(lk, task.when);
84 lk.unlock(); // what() might need to schedule another task
89 } // namespace android