blob: f3ea92f7bbc9cdf233a3911cab70ce3230cc549f [file] [log] [blame]
Yifan Hong5e2318c2016-10-27 17:19:21 -07001/*
2 * Copyright (C) 2016 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 */
Yifan Hong0a351392017-03-20 17:17:52 -070016#ifndef ANDROID_HIDL_TASK_RUNNER_H
17#define ANDROID_HIDL_TASK_RUNNER_H
Yifan Hong5e2318c2016-10-27 17:19:21 -070018
Benjamin Lermanb8180e82023-03-03 17:51:49 +010019#include <functional>
Yifan Hong8184c122017-03-20 18:26:43 -070020#include <memory>
Yifan Hong5e2318c2016-10-27 17:19:21 -070021#include <thread>
22
23namespace android {
24namespace hardware {
Yifan Hong0a351392017-03-20 17:17:52 -070025namespace details {
Yifan Hong5e2318c2016-10-27 17:19:21 -070026
Steven Morelandc03f9432017-08-16 14:17:49 -070027using Task = std::function<void(void)>;
28
29template <typename T>
30struct SynchronizedQueue;
31
Yifan Hong5e2318c2016-10-27 17:19:21 -070032/*
33 * A background infinite loop that runs the Tasks push()'ed.
Yifan Hong8184c122017-03-20 18:26:43 -070034 * Equivalent to a simple single-threaded Looper.
Yifan Hong5e2318c2016-10-27 17:19:21 -070035 */
36class TaskRunner {
37public:
38
Yifan Hong6f667542017-03-20 19:04:05 -070039 /* Create an empty task runner. Nothing will be done until start() is called. */
Yifan Hong5e2318c2016-10-27 17:19:21 -070040 TaskRunner();
41
42 /*
Yifan Hong8184c122017-03-20 18:26:43 -070043 * Notify the background thread to terminate and return immediately.
44 * Tasks in the queue will continue to be done sequentially in background
45 * until all tasks are finished.
Yifan Hong5e2318c2016-10-27 17:19:21 -070046 */
47 ~TaskRunner();
48
49 /*
Yifan Hong6f667542017-03-20 19:04:05 -070050 * Sets the queue limit. Fails the push operation once the limit is reached.
Steven Morelandc03f9432017-08-16 14:17:49 -070051 * This function is named start for legacy reasons and to maintain ABI
52 * stability, but the underlying thread running tasks isn't started until
53 * the first task is pushed.
Yifan Hong6f667542017-03-20 19:04:05 -070054 */
55 void start(size_t limit);
56
57 /*
Yifan Hong5e2318c2016-10-27 17:19:21 -070058 * Add a task. Return true if successful, false if
Yifan Hong8184c122017-03-20 18:26:43 -070059 * the queue's size exceeds limit or t doesn't contain a callable target.
Yifan Hong5e2318c2016-10-27 17:19:21 -070060 */
Steven Moreland0540d282017-05-08 16:03:11 -070061 bool push(const Task &t);
Yifan Hong5e2318c2016-10-27 17:19:21 -070062
Yifan Hong5e2318c2016-10-27 17:19:21 -070063private:
Yifan Hong8184c122017-03-20 18:26:43 -070064 std::shared_ptr<SynchronizedQueue<Task>> mQueue;
Yifan Hong5e2318c2016-10-27 17:19:21 -070065};
66
Yifan Hong0a351392017-03-20 17:17:52 -070067} // namespace details
Yifan Hong5e2318c2016-10-27 17:19:21 -070068} // namespace hardware
69} // namespace android
70
Yifan Hong0a351392017-03-20 17:17:52 -070071#endif // ANDROID_HIDL_TASK_RUNNER_H