1 /*
2 * Copyright (C) 2021 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 #include "thread/WorkerThread.h"
18
19 namespace aidl::android::hardware::biometrics {
20
21 // It's important that mThread is initialized after everything else because it runs a member
22 // function that may use any member of this class.
WorkerThread(size_t maxQueueSize)23 WorkerThread::WorkerThread(size_t maxQueueSize)
24 : mMaxSize(maxQueueSize),
25 mIsDestructing(false),
26 mQueue(),
27 mQueueMutex(),
28 mQueueCond(),
29 mThread(&WorkerThread::threadFunc, this) {}
30
~WorkerThread()31 WorkerThread::~WorkerThread() {
32 // This is a signal for threadFunc to terminate as soon as possible, and a hint for schedule
33 // that it doesn't need to do any work.
34 {
35 std::unique_lock<std::mutex> lock(mQueueMutex);
36 mIsDestructing = true;
37 }
38 mQueueCond.notify_all();
39 mThread.join();
40 }
41
schedule(std::unique_ptr<Callable> task)42 bool WorkerThread::schedule(std::unique_ptr<Callable> task) {
43 if (mIsDestructing) {
44 return false;
45 }
46
47 std::unique_lock<std::mutex> lock(mQueueMutex);
48 if (mQueue.size() >= mMaxSize) {
49 return false;
50 }
51 mQueue.push_back(std::move(task));
52 lock.unlock();
53 mQueueCond.notify_one();
54 return true;
55 }
56
threadFunc()57 void WorkerThread::threadFunc() {
58 while (!mIsDestructing) {
59 std::unique_lock<std::mutex> lock(mQueueMutex);
60 mQueueCond.wait(lock, [this] { return !mQueue.empty() || mIsDestructing; });
61 if (mIsDestructing) {
62 return;
63 }
64 std::unique_ptr<Callable> task = std::move(mQueue.front());
65 mQueue.pop_front();
66 lock.unlock();
67 (*task)();
68 }
69 }
70
71 } // namespace aidl::android::hardware::biometrics
72