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 #pragma once
18 
19 #include <mutex>
20 #include <optional>
21 #include <queue>
22 #include <thread>
23 
24 #include "Callable.h"
25 
26 namespace aidl::android::hardware::biometrics::fingerprint {
27 
28 // A class that encapsulates a worker thread and a task queue, and provides a convenient interface
29 // for a Session to schedule its tasks for asynchronous execution.
30 class WorkerThread final {
31   public:
32     // Internally creates a queue that cannot exceed maxQueueSize elements and a new thread that
33     // polls the queue for tasks until this instance is destructed.
34     explicit WorkerThread(size_t maxQueueSize);
35 
36     // Unblocks the internal queue and calls join on the internal thread allowing it to gracefully
37     // exit.
38     ~WorkerThread();
39 
40     // Disallow copying this class.
41     WorkerThread(const WorkerThread&) = delete;
42     WorkerThread& operator=(const WorkerThread&) = delete;
43 
44     // Also disable moving this class to simplify implementation.
45     WorkerThread(WorkerThread&&) = delete;
46     WorkerThread& operator=(WorkerThread&&) = delete;
47 
48     // If the internal queue is not full, pushes a task at the end of the queue and returns true.
49     // Otherwise, returns false. If the queue is busy, blocks until it becomes available.
50     // This method expects heap-allocated tasks because it's the simplest way to represent function
51     // objects of any type. Stack-allocated std::function could be used instead, but it cannot
52     // represent functions with move-only captures because std::function is inherently copyable.
53     // Not being able to pass move-only lambdas is a major limitation for the HAL implementation,
54     // so heap-allocated tasks that share a common interface (Callable) were chosen instead.
55     bool schedule(std::unique_ptr<Callable> task);
56 
57   private:
58     // The function that runs on the internal thread. Sequentially runs the available tasks from
59     // the queue. If the queue is empty, waits until a new task is added. If the worker is being
60     // destructed, finishes its current task and gracefully exits.
61     void threadFunc();
62 
63     // The maximum size that the queue is allowed to expand to.
64     size_t mMaxSize;
65 
66     // Whether the destructor was called. If true, tells threadFunc to exit as soon as possible, and
67     // tells schedule to avoid doing any work.
68     std::atomic<bool> mIsDestructing;
69 
70     // Queue that's guarded by mQueueMutex and mQueueCond.
71     std::deque<std::unique_ptr<Callable>> mQueue;
72     std::mutex mQueueMutex;
73     std::condition_variable mQueueCond;
74 
75     // The internal thread that works on the tasks from the queue.
76     std::thread mThread;
77 };
78 
79 }  // namespace aidl::android::hardware::biometrics::fingerprint
80